public Shell open(Display display) { shell = new Shell(display); shell.setLayout(new FillLayout()); shell.addShellListener( new ShellAdapter() { @Override public void shellClosed(ShellEvent e) { e.doit = closeAddressBook(); } }); createMenuBar(); searchDialog = new SearchDialog(shell); searchDialog.setSearchAreaNames(columnNames); searchDialog.setSearchAreaLabel(resAddressBook.getString("Column")); searchDialog.addFindListener( new FindListener() { public boolean find() { return findEntry(); } }); table = new Table(shell, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION); table.setHeaderVisible(true); table.setMenu(createPopUpMenu()); table.addSelectionListener( new SelectionAdapter() { @Override public void widgetDefaultSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length > 0) editEntry(items[0]); } }); for (int i = 0; i < columnNames.length; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText(columnNames[i]); column.setWidth(150); final int columnIndex = i; column.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { sort(columnIndex); } }); } newAddressBook(); shell.setSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300); shell.open(); return shell; }
private void removeMapping(Shell shell) { TableItem[] itemsIdMaps = fIdMapsTable.getSelection(); TableItem[] itemsMappings = fMappingsTable.getSelection(); if (itemsMappings.length > 0 && itemsIdMaps.length > 0) { Mapping mapping = (Mapping) itemsMappings[0].getData(); IdMap idmap = (IdMap) itemsIdMaps[0].getData(); HashMap idmapHS = (HashMap) fIdMaps.get(idmap.getName()); idmapHS.remove(mapping.getKey()); Vector mappings = idmap.getMappings(); mappings.remove(mapping); itemsMappings[0].dispose(); // Table is single selection } }
private void sort(int column) { if (table.getItemCount() <= 1) return; TableItem[] items = table.getItems(); String[][] data = new String[items.length][table.getColumnCount()]; for (int i = 0; i < items.length; i++) { for (int j = 0; j < table.getColumnCount(); j++) { data[i][j] = items[i].getText(j); } } Arrays.sort(data, new RowComparator(column)); if (lastSortColumn != column) { table.setSortColumn(table.getColumn(column)); table.setSortDirection(SWT.DOWN); for (int i = 0; i < data.length; i++) { items[i].setText(data[i]); } lastSortColumn = column; } else { // reverse order if the current column is selected again table.setSortDirection(SWT.UP); int j = data.length - 1; for (int i = 0; i < data.length; i++) { items[i].setText(data[j--]); } lastSortColumn = -1; } }
protected TableItem newOrderedTableItem(Mapping mapping, boolean selected) { TableItem[] items = fOrderedTable.getItems(); int i = 0; while (i < items.length && mapping.getElement().compareToIgnoreCase(items[i].getText(0)) > 0) i++; TableItem item = new TableItem(fOrderedTable, SWT.NULL, i); String[] values = new String[] {mapping.getElement(), mapping.getSignature()}; item.setText(values); item.setData(mapping); if (selected) fOrderedTable.setSelection(i); return item; }
private void removeOrdered(Shell shell) { TableItem[] itemsIdMaps = fIdMapsTable.getSelection(); TableItem[] itemsOrdered = fOrderedTable.getSelection(); if (itemsOrdered.length > 0 && itemsIdMaps.length > 0) { Mapping mapping = (Mapping) itemsOrdered[0].getData(); IdMap idmap = (IdMap) itemsIdMaps[0].getData(); ArrayList idmapAL = (ArrayList) fOrderedElements.get(idmap.getName()); idmapAL.remove(mapping.getKey()); if (idmapAL.size() <= 0) fOrderedElements.remove(idmap.getName()); ArrayList ordered = idmap.getOrdered(); ordered.remove(mapping); if (ordered.size() <= 0) idmap.setOrdered(null); itemsOrdered[0].dispose(); // Table is single selection } }
/** Updates the state (enabled, not enabled) of the buttons */ private void updateEnabledState() { TableItem[] itemsIdMaps = fIdMapsTable.getSelection(); if (itemsIdMaps.length > 0) { IdMap idmap = (IdMap) itemsIdMaps[0].getData(); if (idmap.isInternal()) { fRenameIdMapButton.setEnabled(false); fRemoveIdMapButton.setEnabled(false); fEditIdMapButton.setEnabled(true); fNewMappingsButton.setEnabled(false); fEditMappingsButton.setEnabled(false); fRemoveMappingsButton.setEnabled(false); fNewOrderedButton.setEnabled(false); fEditOrderedButton.setEnabled(false); fRemoveOrderedButton.setEnabled(false); } else { fRenameIdMapButton.setEnabled(true); fRemoveIdMapButton.setEnabled(true); fEditIdMapButton.setEnabled(false); fNewMappingsButton.setEnabled(true); fEditMappingsButton.setEnabled(true); fRemoveMappingsButton.setEnabled(true); fNewOrderedButton.setEnabled(true); fEditOrderedButton.setEnabled(true); fRemoveOrderedButton.setEnabled(true); } } }
/** Load the data of the selected parameter into the table */ private void list_selected() { String key; try { key = getCurrentKey(); } catch (Exception e1) { return; } MeasurementHistoryController hist = master.getHistoryController(); MeasurementHistory data; try { if (getCurrentTimeBase().equals("hours")) data = hist.getDataHours().get(key); else data = hist.getDataDays().get(key); } catch (Exception ex) { return; } Vector<MeasurementHistoryEntry> newData = new Vector<MeasurementHistoryEntry>(data.getValues().size()); newData.addAll(data.getValues()); Collections.sort(newData); Collections.reverse(newData); dataTable.removeAll(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); for (MeasurementHistoryEntry e : newData) { TableItem item = new TableItem(dataTable, SWT.NONE); item.setText(new String[] {sdf.format(e.getTimestamp()), "" + e.getValue()}); } }
public void buildWUIDTab(String wuid) { CTabItem tab2 = new CTabItem(folder, SWT.NONE); tab2.setText(wuid); Table table = new Table(folder, SWT.VIRTUAL | SWT.BORDER); table.setLinesVisible(true); table.setHeaderVisible(true); table.clearAll(); GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); data.heightHint = 200; table.setLayoutData(data); // openFile(fileName,table); tab2.setControl(table); }
private boolean save() { if (file == null) return saveAs(); Cursor waitCursor = new Cursor(shell.getDisplay(), SWT.CURSOR_WAIT); shell.setCursor(waitCursor); TableItem[] items = table.getItems(); String[] lines = new String[items.length]; for (int i = 0; i < items.length; i++) { String[] itemText = new String[table.getColumnCount()]; for (int j = 0; j < itemText.length; j++) { itemText[j] = items[i].getText(j); } lines[i] = encodeLine(itemText); } FileWriter fileWriter = null; try { fileWriter = new FileWriter(file.getAbsolutePath(), false); for (int i = 0; i < lines.length; i++) { fileWriter.write(lines[i]); } } catch (FileNotFoundException e) { displayError(resMessages.getString("File_not_found") + "\n" + file.getName()); return false; } catch (IOException e) { displayError(resMessages.getString("IO_error_write") + "\n" + file.getName()); return false; } finally { shell.setCursor(null); waitCursor.dispose(); if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { displayError(resMessages.getString("IO_error_close") + "\n" + file.getName()); return false; } } } shell.setText(resMessages.getString("Title_bar") + file.getName()); isModified = false; return true; }
private void sort(int column) { if (table.getItemCount() <= 1) return; TableItem[] items = table.getItems(); String[][] data = new String[items.length][table.getColumnCount()]; for (int i = 0; i < items.length; i++) { for (int j = 0; j < table.getColumnCount(); j++) { data[i][j] = items[i].getText(j); } } Arrays.sort(data, new RowComparator(column)); for (int i = 0; i < data.length; i++) { items[i].setText(data[i]); } }
private boolean findEntry() { Cursor waitCursor = shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT); shell.setCursor(waitCursor); boolean matchCase = searchDialog.getMatchCase(); boolean matchWord = searchDialog.getMatchWord(); String searchString = searchDialog.getSearchString(); int column = searchDialog.getSelectedSearchArea(); searchString = matchCase ? searchString : searchString.toLowerCase(); boolean found = false; if (searchDialog.getSearchDown()) { for (int i = table.getSelectionIndex() + 1; i < table.getItemCount(); i++) { if (found = findMatch(searchString, table.getItem(i), column, matchWord, matchCase)) { table.setSelection(i); break; } } } else { for (int i = table.getSelectionIndex() - 1; i > -1; i--) { if (found = findMatch(searchString, table.getItem(i), column, matchWord, matchCase)) { table.setSelection(i); break; } } } shell.setCursor(null); return found; }
private void editOrdered(Shell shell) { TableItem[] itemsIdMaps = fIdMapsTable.getSelection(); TableItem[] itemsOrdered = fOrderedTable.getSelection(); if (itemsOrdered.length > 0) { IdMap idmap = (IdMap) itemsIdMaps[0].getData(); ArrayList idmapAL = (ArrayList) fOrderedElements.get(idmap.getName()); Mapping mapping = (Mapping) itemsOrdered[0].getData(); String idmapALKey = mapping.getKey(); idmapAL.remove(idmapALKey); XMLCompareEditOrderedDialog dialog = new XMLCompareEditOrderedDialog(shell, mapping, null, true); if (dialog.open() == Window.OK) { idmapALKey = mapping.getKey(); idmapAL.add(idmapALKey); fOrderedTable.remove(fOrderedTable.indexOf(itemsOrdered[0])); newOrderedTableItem(mapping, true); } } }
private void editMapping(Shell shell) { TableItem[] itemsIdMaps = fIdMapsTable.getSelection(); TableItem[] itemsMappings = fMappingsTable.getSelection(); if (itemsMappings.length > 0) { IdMap idmap = (IdMap) itemsIdMaps[0].getData(); HashMap idmapHM = (HashMap) fIdMaps.get(idmap.getName()); Mapping mapping = (Mapping) itemsMappings[0].getData(); String idmapHMKey = mapping.getKey(); idmapHM.remove(idmapHMKey); XMLCompareEditMappingDialog dialog = new XMLCompareEditMappingDialog(shell, mapping, null, true); if (dialog.open() == Window.OK) { idmapHMKey = mapping.getKey(); idmapHM.put(idmapHMKey, mapping.getIdAttribute()); fMappingsTable.remove(fMappingsTable.indexOf(itemsMappings[0])); newMappingsTableItem(mapping, true); } } }
public void buildTab(String filename, String resType, CTabFolder subfolder) { System.out.println("buildTab"); CTabItem tab2 = new CTabItem(subfolder, SWT.NONE); tab2.setText(resType); Table table = new Table(subfolder, SWT.VIRTUAL | SWT.BORDER); table.setLinesVisible(true); table.setHeaderVisible(true); table.clearAll(); GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); data.heightHint = 200; table.setLayoutData(data); openFile(filename, table); tab2.setControl(table); System.out.println("BUILDSUBTAB--------" + subfolder.indexOf(tab2)); subfolder.setSelection(subfolder.indexOf(tab2)); }
protected TableItem newMappingsTableItem(Mapping mapping, boolean selected) { TableItem[] items = fMappingsTable.getItems(); int i = 0; while (i < items.length && mapping.getElement().compareToIgnoreCase(items[i].getText(0)) > 0) i++; TableItem item = new TableItem(fMappingsTable, SWT.NULL, i); String idtext = mapping.getIdAttribute(); String idtype; if (idtext.charAt(0) == XMLStructureCreator.ID_TYPE_BODY) { idtext = idtext.substring(1, idtext.length()); idtype = IDTYPE_CHILDBODY; } else idtype = IDTYPE_ATTRIBUTE; String[] values = new String[] {mapping.getElement(), mapping.getSignature(), idtext, idtype}; item.setText(values); item.setData(mapping); if (selected) fMappingsTable.setSelection(i); return item; }
protected void selectionChanged() { TableItem[] items = fIdMapsTable.getSelection(); if (items.length > 0) { // Refresh Mappings Table fMappingsTable.removeAll(); Vector Mappings = ((IdMap) items[0].getData()).getMappings(); for (Enumeration enumeration = Mappings.elements(); enumeration.hasMoreElements(); ) { newMappingsTableItem((Mapping) enumeration.nextElement(), false); } // Refresh Ordered Table fOrderedTable.removeAll(); ArrayList Ordered = ((IdMap) items[0].getData()).getOrdered(); if (Ordered != null) { for (Iterator iter_ordered = Ordered.iterator(); iter_ordered.hasNext(); ) { newOrderedTableItem((Mapping) iter_ordered.next(), false); } } } updateEnabledState(); }
private void renameIdMap(Shell shell) { TableItem[] itemsIdMaps = fIdMapsTable.getSelection(); if (itemsIdMaps.length > 0) { IdMap idmap = (IdMap) itemsIdMaps[0].getData(); String old_name = idmap.getName(); String old_extension = idmap.getExtension(); HashMap idmapHS = (HashMap) fIdMaps.get(old_name); XMLCompareAddIdMapDialog dialog = new XMLCompareAddIdMapDialog( shell, idmap, fIdMaps, fIdMapsInternal, fIdExtensionToName, true); if (dialog.open() == Window.OK) { fIdMaps.remove(old_name); fIdExtensionToName.remove(old_extension); fIdMaps.put(idmap.getName(), idmapHS); if (!idmap.getExtension().equals("")) // $NON-NLS-1$ fIdExtensionToName.put(idmap.getExtension(), idmap.getName()); fIdMapsTable.remove(fIdMapsTable.indexOf(itemsIdMaps[0])); newIdMapsTableItem(idmap, true); } } }
public static void main(String[] args) { final Display display = new Display(); final Image image = new Image(display, 16, 16); GC gc = new GC(image); gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); gc.fillRectangle(image.getBounds()); gc.dispose(); final Shell shell = new Shell(display); shell.setText("Lazy Table"); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.BORDER | SWT.MULTI); table.setSize(200, 200); Thread thread = new Thread() { @Override public void run() { for (int i = 0; i < 20000; i++) { if (table.isDisposed()) return; final int[] index = new int[] {i}; display.syncExec( () -> { if (table.isDisposed()) return; TableItem item = new TableItem(table, SWT.NONE); item.setText("Table Item " + index[0]); item.setImage(image); }); } } }; thread.start(); shell.setSize(200, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } image.dispose(); display.dispose(); }
private void editEntry(TableItem item) { DataEntryDialog dialog = new DataEntryDialog(shell); dialog.setLabels(columnNames); String[] values = new String[table.getColumnCount()]; for (int i = 0; i < values.length; i++) { values[i] = item.getText(i); } dialog.setValues(values); values = dialog.open(); if (values != null) { item.setText(values); isModified = true; } }
protected TableItem newIdMapsTableItem(IdMap idmap, boolean selected) { // find index where to insert table entry TableItem[] items = fIdMapsTable.getItems(); int i = 0; while (i < items.length && idmap.getName().compareToIgnoreCase(items[i].getText(0)) > 0) i++; TableItem item = new TableItem(fIdMapsTable, SWT.NULL, i); String[] values = new String[] { idmap.getName(), (idmap.isInternal()) ? XMLCompareMessages.XMLComparePreference_topTableColumn2internal : XMLCompareMessages.XMLComparePreference_topTableColumn2user, idmap.getExtension() }; item.setText(values); item.setData(idmap); if (selected) { fIdMapsTable.setSelection(i); fIdMapsTable.setFocus(); selectionChanged(); } return item; }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.BORDER); table.setHeaderVisible(true); final TableColumn column1 = new TableColumn(table, SWT.NONE); column1.setText("Column 1"); final TableColumn column2 = new TableColumn(table, SWT.NONE); column2.setText("Column 2"); TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] {"a", "3"}); item = new TableItem(table, SWT.NONE); item.setText(new String[] {"b", "2"}); item = new TableItem(table, SWT.NONE); item.setText(new String[] {"c", "1"}); column1.setWidth(100); column2.setWidth(100); Listener sortListener = e -> { TableItem[] items = table.getItems(); Collator collator = Collator.getInstance(Locale.getDefault()); TableColumn column = (TableColumn) e.widget; int index = column == column1 ? 0 : 1; for (int i = 1; i < items.length; i++) { String value1 = items[i].getText(index); for (int j = 0; j < i; j++) { String value2 = items[j].getText(index); if (collator.compare(value1, value2) < 0) { String[] values = {items[i].getText(0), items[i].getText(1)}; items[i].dispose(); TableItem item1 = new TableItem(table, SWT.NONE, j); item1.setText(values); items = table.getItems(); break; } } } table.setSortColumn(column); }; column1.addListener(SWT.Selection, sortListener); column2.addListener(SWT.Selection, sortListener); table.setSortColumn(column1); table.setSortDirection(SWT.UP); shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
private void removeIdMap(Shell shell) { TableItem[] itemsIdMap = fIdMapsTable.getSelection(); if (itemsIdMap.length > 0) { // fIdMaps.remove(itemsIdMap[0].getText()); String IdMapName = ((IdMap) itemsIdMap[0].getData()).getName(); fIdMaps.remove(IdMapName); fOrderedElements.remove(IdMapName); // All the corresponding ID Mappings must be removed as well TableItem[] itemsMappings = fMappingsTable.getItems(); for (int i = 0; i < itemsMappings.length; i++) { itemsMappings[i].dispose(); } // All the corresponding Ordered entries must be removed as well TableItem[] itemsOrdered = fOrderedTable.getItems(); for (int i = 0; i < itemsOrdered.length; i++) { itemsOrdered[i].dispose(); } // Remove extension if (!itemsIdMap[0].getText(2).equals("")) { // $NON-NLS-1$ fIdExtensionToName.remove(itemsIdMap[0].getText(2)); } itemsIdMap[0].dispose(); // Table is single selection } }
/** * 通过将检索结果结合中的记录分页加载的方法,实现控制检索结果记录的分页显示与快速加载 * * @param facttable 显示检索结果的GUI表格对象 * @param pagenum 显示在表格对象中的检索结果页码,从1开始计算 */ private void openCurrentTable(final int pagenum) { table.clearAll(); // 清空表格所有项目中的数据 // 数据记录在检索结果结合中的下标开始值 final int recordstart = (pagenum - 1) * pageSize; // 当前表格能显示的最后一条记录在检索结果集合中的索引号 int end = recordstart + pageSize; // 若表格可显示的记录大于检索结果记录的总数,就只显示到最后一条检索结果记录 final int recordend = Math.min(end, stationData.getRowCount()); // 显示的最后一条记录在检索结果集合中的索引号 // 当前页面显示的检索结果记录数 final int currentdispnum = recordend - recordstart; // 根据当前的页码,从检索结果集合中加载相应的数据到表的item域中 tableViewer.setInput(stationData.getRow(recordstart, recordstart + currentdispnum)); // 自动输入数据 tableViewer.refresh(); // 刷新表格false }
private void editIdMap(Shell shell) { TableItem[] items = fIdMapsTable.getSelection(); if (items.length > 0) { IdMap idmap = (IdMap) items[0].getData(); XMLCompareEditCopyIdMapDialog dialog = new XMLCompareEditCopyIdMapDialog(shell, idmap, fIdMaps, fIdMapsInternal); if (dialog.open() == Window.OK) { String new_idmapName = dialog.getResult(); if (!fIdMaps.containsKey(new_idmapName)) { // copy over id mappings Vector newMappings = new Vector(); IdMap newIdMap = new IdMap(new_idmapName, false, newMappings); HashMap newIdmapHM = new HashMap(); fIdMaps.put(newIdMap.getName(), newIdmapHM); Vector Mappings = idmap.getMappings(); for (Enumeration enumeration = Mappings.elements(); enumeration.hasMoreElements(); ) { Mapping mapping = (Mapping) enumeration.nextElement(); Mapping newMapping = new Mapping(mapping.getElement(), mapping.getSignature(), mapping.getIdAttribute()); newMappings.add(newMapping); newIdmapHM.put(newMapping.getKey(), newMapping.getIdAttribute()); } // copy over ordered entries ArrayList orderedAL = idmap.getOrdered(); if (orderedAL != null && orderedAL.size() > 0) { ArrayList newOrderedAL = new ArrayList(); newIdMap.setOrdered(newOrderedAL); ArrayList idmapOrdered = new ArrayList(); fOrderedElements.put(newIdMap.getName(), idmapOrdered); for (Iterator iter = orderedAL.iterator(); iter.hasNext(); ) { Mapping ordered = (Mapping) iter.next(); Mapping newOrdered = new Mapping(ordered.getElement(), ordered.getSignature()); newOrderedAL.add(newOrdered); idmapOrdered.add(newOrdered.getKey()); } } newIdMapsTableItem(newIdMap, true); selectionChanged(); } } } }
/** Converts an encoded <code>String</code> to a String array representing a table entry. */ private String[] decodeLine(String line) { if (line == null) return null; String[] parsedLine = new String[table.getColumnCount()]; for (int i = 0; i < parsedLine.length - 1; i++) { int index = line.indexOf(DELIMITER); if (index > -1) { parsedLine[i] = line.substring(0, index); line = line.substring(index + DELIMITER.length(), line.length()); } else { return null; } } if (line.indexOf(DELIMITER) != -1) return null; parsedLine[parsedLine.length - 1] = line; return parsedLine; }
private void addMapping(Shell shell) { TableItem[] items = fIdMapsTable.getSelection(); if (items.length > 0) { IdMap idmap = (IdMap) items[0].getData(); Mapping mapping = new Mapping(); HashMap idmapHM = (HashMap) fIdMaps.get(idmap.getName()); XMLCompareEditMappingDialog dialog = new XMLCompareEditMappingDialog(shell, mapping, idmapHM, false); if (dialog.open() == Window.OK) { String idmapHMKey = mapping.getKey(); if (idmapHM == null) idmapHM = new HashMap(); if (!idmapHM.containsKey(idmapHMKey)) { idmapHM.put(idmapHMKey, mapping.getIdAttribute()); newMappingsTableItem(mapping, true); Vector mappings = idmap.getMappings(); mappings.add(mapping); } } } }
private boolean closeAddressBook() { if (isModified) { // ask user if they want to save current address book MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO | SWT.CANCEL); box.setText(shell.getText()); box.setMessage(resAddressBook.getString("Close_save")); int choice = box.open(); if (choice == SWT.CANCEL) { return false; } else if (choice == SWT.YES) { if (!save()) return false; } } TableItem[] items = table.getItems(); for (int i = 0; i < items.length; i++) { items[i].dispose(); } return true; }
private void addOrdered(Shell shell) { TableItem[] items = fIdMapsTable.getSelection(); if (items.length > 0) { // Set orderedSet= fOrderedElements.keySet(); // for (Iterator iter= orderedSet.iterator(); iter.hasNext(); ) { // String IdMapName= (String) iter.next(); // ArrayList ordered= (ArrayList) fOrderedElements.get(IdMapName); // for (Iterator iter2= ordered.iterator(); iter2.hasNext(); ) { // System.out.println(IdMapName + ": " + iter2.next()); //$NON-NLS-1$ // } // } IdMap idmap = (IdMap) items[0].getData(); Mapping mapping = new Mapping(); ArrayList idmapAL = (ArrayList) fOrderedElements.get(idmap.getName()); if (idmapAL == null) idmapAL = new ArrayList(); XMLCompareEditOrderedDialog dialog = new XMLCompareEditOrderedDialog(shell, mapping, idmapAL, false); if (dialog.open() == Window.OK) { String idmapALKey = mapping.getKey(); if (!idmapAL.contains(idmapALKey)) { idmapAL.add(idmapALKey); newOrderedTableItem(mapping, true); ArrayList ordered = idmap.getOrdered(); if (ordered == null) { ordered = new ArrayList(); ordered.add(mapping); idmap.setOrdered(ordered); } else { ordered.add(mapping); } if (!fOrderedElements.containsKey(idmap.getName())) fOrderedElements.put(idmap.getName(), idmapAL); } } } }
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
private void openAddressBook() { FileDialog fileDialog = new FileDialog(shell, SWT.OPEN); fileDialog.setFilterExtensions(new String[] {"*.adr;", "*.*"}); fileDialog.setFilterNames( new String[] { resAddressBook.getString("Book_filter_name") + " (*.adr)", resAddressBook.getString("All_filter_name") + " (*.*)" }); String name = fileDialog.open(); if (name == null) return; File file = new File(name); if (!file.exists()) { displayError( resAddressBook.getString("File") + file.getName() + " " + resAddressBook.getString("Does_not_exist")); return; } Cursor waitCursor = shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT); shell.setCursor(waitCursor); FileReader fileReader = null; BufferedReader bufferedReader = null; String[] data = new String[0]; try { fileReader = new FileReader(file.getAbsolutePath()); bufferedReader = new BufferedReader(fileReader); String nextLine = bufferedReader.readLine(); while (nextLine != null) { String[] newData = new String[data.length + 1]; System.arraycopy(data, 0, newData, 0, data.length); newData[data.length] = nextLine; data = newData; nextLine = bufferedReader.readLine(); } } catch (FileNotFoundException e) { displayError(resAddressBook.getString("File_not_found") + "\n" + file.getName()); return; } catch (IOException e) { displayError(resAddressBook.getString("IO_error_read") + "\n" + file.getName()); return; } finally { shell.setCursor(null); if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { displayError(resAddressBook.getString("IO_error_close") + "\n" + file.getName()); return; } } } String[][] tableInfo = new String[data.length][table.getColumnCount()]; int writeIndex = 0; for (int i = 0; i < data.length; i++) { String[] line = decodeLine(data[i]); if (line != null) tableInfo[writeIndex++] = line; } if (writeIndex != data.length) { String[][] result = new String[writeIndex][table.getColumnCount()]; System.arraycopy(tableInfo, 0, result, 0, writeIndex); tableInfo = result; } Arrays.sort(tableInfo, new RowComparator(0)); for (int i = 0; i < tableInfo.length; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(tableInfo[i]); } shell.setText(resAddressBook.getString("Title_bar") + fileDialog.getFileName()); isModified = false; this.file = file; }