private void createComp(Composite group) { GridLayout layout = new GridLayout(1, false); layout.verticalSpacing = 10; group.setLayout(layout); // TableViewer是通过Table来布局的 // 制作表格 MULTI可多选 H_SCROLL有水平 滚动条、V_SCROLL有垂直滚动条、BORDER有边框、FULL_SELECTION整行选择 table = new Table(group, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.VIRTUAL); // 注意此处的设置 TableLayout tableLayout = new TableLayout(); table.setLayout(tableLayout); // 指定Table单元格的宽度和高度 table.addListener( SWT.MeasureItem, new Listener() { // 向表格增加一个SWT.MeasureItem监听器,每当需要单元内容的大小的时候就会被调用。 public void handleEvent(Event event) { event.width = table.getGridLineWidth(); // 设置宽度 event.height = (int) Math.floor(event.gc.getFontMetrics().getHeight() * 1.5); // 设置高度为字体高度的1.5倍 } }); // 表格的视图 tableViewer = new TableViewer(table); // 标题和网格线可见 tableViewer.getTable().setLinesVisible(true); tableViewer.getTable().setHeaderVisible(true); // 设置填充 // GridData data = new GridData(SWT.LEFT,SWT.CENTER, true, false);//SWT.FILL, SWT.FILL, true, // false xbm GridData data = new GridData(GridData.FILL_BOTH); data.widthHint = 350; data.heightHint = 295; data.grabExcessHorizontalSpace = true; tableViewer.getTable().setLayoutData(data); // 表格的布局 // 创建表格列的标题 int width = 1; for (int i = 0; i < stationData.getColumnCount(); i++) { width = stationData.getColumnWidth(i); TableColumn column = new TableColumn(table, SWT.NONE); column.setWidth((int) (width * 8)); column.setText(stationData.getColumnHeads()[i]); // 设置表头 column.setAlignment(SWT.LEFT); // 对齐方式SWT.LEFT if (i == 0) // 站名 { // 列的选择事件 实现排序 column.addSelectionListener( new SelectionAdapter() { boolean sortType = true; // sortType记录上一次的排序方式,默认为升序 public void widgetSelected(SelectionEvent e) { sortType = !sortType; // 取反。下一次排序方式要和这一次的相反 tableViewer.setSorter(new StationSorter(sortType, stationData.columnHeads[0])); } }); } } /*tableLayout.addColumnData(new ColumnWeightData(8, 8, false));//设置列宽为8像素 TableColumn column_one = new TableColumn(table, SWT.NONE);//SWT.LEFT column_one.setText(stationData.COLUMN_HEADINGS[0]);//设置表头 column_one.setAlignment(SWT.LEFT);//对齐方式SWT.LEFT column.setWidth(10);//宽度 */ // 设置标题的提供者 tableViewer.setLabelProvider(new TableLabelProvider()); // 设置表格视图的内容提供者 tableViewer.setContentProvider(new TableContentProvider()); // 设置列的属性. tableViewer.setColumnProperties(stationData.columnHeads); // 定义每一列的别名 tableViewer.setColumnProperties(new String[] {"name", "down", "up", "map"}); // 设置每一列的单元格编辑组件CellEditor CellEditor[] celleditors = new CellEditor[5]; // 文本编辑框 celleditors[0] = null; celleditors[1] = new TextCellEditor(table); celleditors[2] = new TextCellEditor(table); // CheckboxCellEditor(table) 复选框 celleditors[3] = new ComboBoxCellEditor(table, StationData.MAPS, SWT.READ_ONLY); // 下拉框 Text text = (Text) celleditors[1].getControl(); // 设置第down列只能输入数值 text.addVerifyListener( new VerifyListener() { public void verifyText(VerifyEvent e) { // 输入控制键,输入中文,输入字符,输入数字 正整数验证 Pattern pattern = Pattern.compile("[0-9]\\d*"); // 正则表达式 Matcher matcher = pattern.matcher(e.text); if (matcher.matches()) // 处理数字 { /*if(Integer.parseInt(e.text) != 0)//确保输入的数字不是0 e.doit = true; else e.doit = false; */ e.doit = true; } else if (e.text.length() > 0) // 字符: 包含中文、空格 e.doit = false; else // 控制键 e.doit = true; } }); Text text1 = (Text) celleditors[2].getControl(); // 设置第up列只能输入数值 text1.addVerifyListener( new VerifyListener() { public void verifyText(VerifyEvent e) { String inStr = e.text; if (inStr.length() > 0) { e.doit = NumberUtils.isDigits(inStr); } } }); table.addMouseMoveListener( new MouseMoveListener() { public void mouseMove(MouseEvent e) { if (StationData.reloadFlag) { getStationInfo(); openCurrentTable(row); StationData.reloadFlag = false; // System.out.println("entry"); } } }); /*table.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent e) { getStationInfo(); openCurrentTable(row); } @Override public void focusLost(FocusEvent e) { } }); */ tableViewer.setCellEditors(celleditors); // 设置单元的更改器 tableViewer.setCellModifier(new TableCellModifier()); tableViewer.addFilter(new TableViewerFilter()); // 过滤器 // 构造工具条 Composite buttonComposite = new Composite(group, SWT.NONE); buttonComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); // 使工具条居中 Action actionModify = new Action("更新") { public void run() { // 取得用户所选择的第一行, 若没有选择则为null IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection(); // 获取选中的第一行数据 Station station = (Station) selection.getFirstElement(); if (station != null) { if (updateStationInfo( station.getStation_downnumber(), station.getStation_upnumber(), station.getStation_graph(), station.getStation_name())) { showMsg("成功更新!"); // 表格的刷新方法,界面会重新读取数据并显示 pushCommand(); tableViewer.refresh(); // false } else { showMsg("更新失败!"); // tableViewer.refresh();//false } } else { showMsg("请选取进行更新的行!"); } } }; Action actionDelete = new Action("删除") { public void run() { // 取得用户所选择的第一行, 若没有选择则为null IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection(); // 获取选中的第一个数据 Station station = (Station) selection.getFirstElement(); if (station != null) { // 先预先移动到下一行 Table table = tableViewer.getTable(); // int i = table.getSelectionIndex(); //取得当前所选行的序号,如没有则返回-1 // table.setSelection(i + 1); //当前选择行移下一行 // 确认删除 MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO | SWT.ICON_INFORMATION); messageBox.setText("提示信息"); messageBox.setMessage("确定要删除此记录吗?"); // SWT.YES 是 // SWT.NO 否 // SWT.CANCEL 取消 // SWT.RETRY 重试// SWT.ABORT 放弃// SWT.IGNORE // 忽略 if (messageBox.open() == SWT.YES) { if (deleteStationInfo(station.getStation_name())) // 从数据库中删除记录 { // showMsg("成功删除!"); ((List) tableViewer.getInput()).remove(station); // 数据模型的List容器中删除 stationData.remove(station.getStation_name()); openCurrentTable(row); tableViewer.remove(station); // 从表格界面上删除 pushCommand(); } else showMsg("删除失败!"); } } else { showMsg("请选取要删除的纪录!"); } } }; Action actionClear = new Action("清空") { // 清除所显示内容,点击保存后更新库 public void run() { if (clearStationInfo()) { showMsg("清空操作成功!"); stationData.removeAll(); openCurrentTable(row); pushCommand(); } else showMsg("清空操作失败!"); } }; Action actionHelp = new Action("帮助") { public void run() { String str = "更新:\n\r" + "先对某行内容进行修改,然后点击更新进行保存\n\r" + "清空:\n\r" + "从库中物理删除所有记录"; showMsg(str); } }; Action nextPage = new Action("下一页") { public void run() { row++; if (row > stationData.getTotalPageNum()) { row--; return; } openCurrentTable(row); } }; Action prevPage = new Action("上一页") { public void run() { row--; if (row < 1) { row++; return; } openCurrentTable(row); } }; Action refresh = new Action("刷新") { public void run() { getStationInfo(); openCurrentTable(row); } }; // 工具条 ToolBar toolBar = new ToolBar(buttonComposite, SWT.FLAT | SWT.RIGHT); // |SWT.BORDER // 工具条管理器 ToolBarManager manager = new ToolBarManager(toolBar); // manager.add(refresh); // manager.add(new Separator()); manager.add(nextPage); manager.add(prevPage); manager.add(new Separator()); manager.add(actionModify); manager.add(actionDelete); manager.add(actionClear); manager.add(new Separator()); manager.add(actionHelp); manager.update(true); // 选中某行时,改变行的颜色 table.addListener( SWT.EraseItem, new Listener() { public void handleEvent(Event event) { event.detail &= ~SWT.HOT; if ((event.detail & SWT.SELECTED) == 0) return; int clientWidth = table.getClientArea().width; GC gc = event.gc; Color oldForeground = gc.getForeground(); Color oldBackground = gc.getBackground(); gc.setForeground(red); // gc.setBackground(yellow); gc.fillGradientRectangle(0, event.y, clientWidth, event.height, false); gc.setForeground(oldForeground); gc.setBackground(oldBackground); event.detail &= ~SWT.SELECTED; } }); // 在tableviewer内部为数据记录和tableItem之间的映射创建一个hash表,这样可以加快tableItem的和记录间的查找速度 // 必须保证存在要显示的数据,否则程序出错。所以这里不能用下面的语句 // tableViewer.setUseHashlookup(true);//必须在setInput之前加入才有效 // 从服务器获取数据 getStationInfo(); // 通过setInput为table添加了一个list后,只要对这个list里的元素进行添加和删除, // table中的数据就会自动添加和删除,当然每次操作后需要调用refresh方法对tableviewer进行刷新。 // 打开界面所显示的内容 tableViewer.setInput(stationData.getData()); // 自动输入数据 即将数据显示在表格中 tableViewer.setItemCount(stationData.PAGE_SIZE); // 设置显示的Item数 } //// createComp
/** @see PreferencePage#createContents(Composite) */ protected Control createContents(Composite ancestor) { Composite parent = new Composite(ancestor, SWT.NULL); GridLayout layout = new GridLayout(); layout.numColumns = 2; layout.marginHeight = 0; layout.marginWidth = 0; parent.setLayout(layout); // layout the top table & its buttons Label label = new Label(parent, SWT.LEFT); label.setText(XMLCompareMessages.XMLComparePreference_topTableLabel); GridData data = new GridData(); data.horizontalAlignment = GridData.FILL; data.horizontalSpan = 2; label.setLayoutData(data); fIdMapsTable = new Table(parent, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); fIdMapsTable.setHeaderVisible(true); data = new GridData(GridData.FILL_BOTH); data.heightHint = fIdMapsTable.getItemHeight() * 4; fIdMapsTable.setLayoutData(data); fIdMapsTable.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { selectionChanged(); } }); String column2Text = XMLCompareMessages.XMLComparePreference_topTableColumn2; String column3Text = XMLCompareMessages.XMLComparePreference_topTableColumn3; ColumnLayoutData columnLayouts[] = { new ColumnWeightData(1), new ColumnPixelData(convertWidthInCharsToPixels(column2Text.length() + 2), true), new ColumnPixelData(convertWidthInCharsToPixels(column3Text.length() + 5), true) }; TableLayout tablelayout = new TableLayout(); fIdMapsTable.setLayout(tablelayout); for (int i = 0; i < 3; i++) tablelayout.addColumnData(columnLayouts[i]); TableColumn column = new TableColumn(fIdMapsTable, SWT.NONE); column.setText(XMLCompareMessages.XMLComparePreference_topTableColumn1); column = new TableColumn(fIdMapsTable, SWT.NONE); column.setText(column2Text); column = new TableColumn(fIdMapsTable, SWT.NONE); column.setText(column3Text); fillIdMapsTable(); Composite buttons = new Composite(parent, SWT.NULL); buttons.setLayout(new GridLayout()); data = new GridData(); data.verticalAlignment = GridData.FILL; data.horizontalAlignment = GridData.FILL; buttons.setLayoutData(data); fAddIdMapButton = new Button(buttons, SWT.PUSH); fAddIdMapButton.setText(XMLCompareMessages.XMLComparePreference_topAdd); fAddIdMapButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { addIdMap(fAddIdMapButton.getShell()); } }); data = new GridData(); data.horizontalAlignment = GridData.FILL; // data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT); int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); data.widthHint = Math.max(widthHint, fAddIdMapButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); fAddIdMapButton.setLayoutData(data); fRenameIdMapButton = new Button(buttons, SWT.PUSH); fRenameIdMapButton.setText(XMLCompareMessages.XMLComparePreference_topRename); fRenameIdMapButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { renameIdMap(fRenameIdMapButton.getShell()); } }); data = new GridData(); data.horizontalAlignment = GridData.FILL; // data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT); widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); data.widthHint = Math.max(widthHint, fAddIdMapButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); fRenameIdMapButton.setLayoutData(data); fRemoveIdMapButton = new Button(buttons, SWT.PUSH); fRemoveIdMapButton.setText(XMLCompareMessages.XMLComparePreference_topRemove); fRemoveIdMapButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { removeIdMap(fRemoveIdMapButton.getShell()); } }); data = new GridData(); data.horizontalAlignment = GridData.FILL; // data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT); widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); data.widthHint = Math.max(widthHint, fRemoveIdMapButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); fRemoveIdMapButton.setLayoutData(data); createSpacer(buttons); fEditIdMapButton = new Button(buttons, SWT.PUSH); fEditIdMapButton.setText(XMLCompareMessages.XMLComparePreference_topEdit); fEditIdMapButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { editIdMap(fEditIdMapButton.getShell()); } }); data = new GridData(); data.horizontalAlignment = GridData.FILL; // data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT); widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); data.widthHint = Math.max(widthHint, fEditIdMapButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); fEditIdMapButton.setLayoutData(data); // Spacer label = new Label(parent, SWT.LEFT); data = new GridData(); data.horizontalAlignment = GridData.FILL; data.horizontalSpan = 2; label.setLayoutData(data); // layout the middle table & its buttons label = new Label(parent, SWT.LEFT); label.setText(XMLCompareMessages.XMLComparePreference_middleTableLabel); data = new GridData(); data.horizontalAlignment = GridData.FILL; data.horizontalSpan = 2; label.setLayoutData(data); fMappingsTable = new Table(parent, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); fMappingsTable.setHeaderVisible(true); data = new GridData(GridData.FILL_BOTH); data.heightHint = fMappingsTable.getItemHeight() * 4; data.widthHint = convertWidthInCharsToPixels(70); fMappingsTable.setLayoutData(data); column3Text = XMLCompareMessages.XMLComparePreference_middleTableColumn3; String column4Text = XMLCompareMessages.XMLComparePreference_middleTableColumn4; columnLayouts = new ColumnLayoutData[] { new ColumnWeightData(10), new ColumnWeightData(18), new ColumnPixelData(convertWidthInCharsToPixels(column3Text.length() + 1), true), new ColumnPixelData(convertWidthInCharsToPixels(column4Text.length() + 3), true) }; tablelayout = new TableLayout(); fMappingsTable.setLayout(tablelayout); for (int i = 0; i < 4; i++) tablelayout.addColumnData(columnLayouts[i]); column = new TableColumn(fMappingsTable, SWT.NONE); column.setText(XMLCompareMessages.XMLComparePreference_middleTableColumn1); column = new TableColumn(fMappingsTable, SWT.NONE); column.setText(XMLCompareMessages.XMLComparePreference_middleTableColumn2); column = new TableColumn(fMappingsTable, SWT.NONE); column.setText(column3Text); column = new TableColumn(fMappingsTable, SWT.NONE); column.setText(column4Text); buttons = new Composite(parent, SWT.NULL); buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); layout = new GridLayout(); layout.marginHeight = 0; layout.marginWidth = 0; buttons.setLayout(layout); fNewMappingsButton = new Button(buttons, SWT.PUSH); fNewMappingsButton.setLayoutData(getButtonGridData(fNewMappingsButton)); fNewMappingsButton.setText(XMLCompareMessages.XMLComparePreference_middleNew); fNewMappingsButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { addMapping(fAddIdMapButton.getShell()); } }); fEditMappingsButton = new Button(buttons, SWT.PUSH); fEditMappingsButton.setLayoutData(getButtonGridData(fEditMappingsButton)); fEditMappingsButton.setText(XMLCompareMessages.XMLComparePreference_middleEdit); fEditMappingsButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { editMapping(fEditMappingsButton.getShell()); } }); fRemoveMappingsButton = new Button(buttons, SWT.PUSH); fRemoveMappingsButton.setLayoutData(getButtonGridData(fRemoveMappingsButton)); fRemoveMappingsButton.setText(XMLCompareMessages.XMLComparePreference_middleRemove); fRemoveMappingsButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { removeMapping(fRemoveMappingsButton.getShell()); } }); createSpacer(buttons); // layout the botton table & its buttons label = new Label(parent, SWT.LEFT); label.setText(XMLCompareMessages.XMLComparePreference_bottomTableLabel); data = new GridData(); data.horizontalAlignment = GridData.FILL; data.horizontalSpan = 2; label.setLayoutData(data); fOrderedTable = new Table(parent, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); fOrderedTable.setHeaderVisible(true); data = new GridData(GridData.FILL_BOTH); data.heightHint = fOrderedTable.getItemHeight() * 2; data.widthHint = convertWidthInCharsToPixels(70); fOrderedTable.setLayoutData(data); columnLayouts = new ColumnLayoutData[] {new ColumnWeightData(1), new ColumnWeightData(1)}; tablelayout = new TableLayout(); fOrderedTable.setLayout(tablelayout); for (int i = 0; i < 2; i++) tablelayout.addColumnData(columnLayouts[i]); column = new TableColumn(fOrderedTable, SWT.NONE); column.setText(XMLCompareMessages.XMLComparePreference_bottomTableColumn1); column = new TableColumn(fOrderedTable, SWT.NONE); column.setText(XMLCompareMessages.XMLComparePreference_bottomTableColumn2); buttons = new Composite(parent, SWT.NULL); buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); layout = new GridLayout(); layout.marginHeight = 0; layout.marginWidth = 0; buttons.setLayout(layout); fNewOrderedButton = new Button(buttons, SWT.PUSH); fNewOrderedButton.setLayoutData(getButtonGridData(fNewOrderedButton)); fNewOrderedButton.setText(XMLCompareMessages.XMLComparePreference_bottomNew); fNewOrderedButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { addOrdered(fNewOrderedButton.getShell()); } }); fEditOrderedButton = new Button(buttons, SWT.PUSH); fEditOrderedButton.setLayoutData(getButtonGridData(fEditOrderedButton)); fEditOrderedButton.setText(XMLCompareMessages.XMLComparePreference_bottomEdit); fEditOrderedButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { editOrdered(fEditOrderedButton.getShell()); } }); fRemoveOrderedButton = new Button(buttons, SWT.PUSH); fRemoveOrderedButton.setLayoutData(getButtonGridData(fRemoveOrderedButton)); fRemoveOrderedButton.setText(XMLCompareMessages.XMLComparePreference_bottomRemove); fRemoveOrderedButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { removeOrdered(fRemoveOrderedButton.getShell()); } }); createSpacer(buttons); fIdMapsTable.setSelection(0); fIdMapsTable.setFocus(); selectionChanged(); return parent; }