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 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 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 } }
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 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; }
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; }
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; }
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; }