private void newEntry() { DataEntryDialog dialog = new DataEntryDialog(shell); dialog.setLabels(columnNames); String[] data = dialog.open(); if (data != null) { TableItem item = new TableItem(table, SWT.NONE); item.setText(data); isModified = true; } }
public static int getColumnAtPos(TableItem item, int x, int y) { int columnCount = item.getParent().getColumnCount(); for (int i = 0; i < columnCount; i++) { Rectangle rect = item.getBounds(i); if (rect.contains(x, y)) { return i; } } return -1; }
public static void sortTable(Table table, Comparator<TableItem> comparator) { int columnCount = table.getColumnCount(); String[] values = new String[columnCount]; TableItem[] items = table.getItems(); for (int i = 1; i < items.length; i++) { for (int j = 0; j < i; j++) { TableItem item = items[i]; if (comparator.compare(item, items[j]) < 0) { for (int k = 0; k < columnCount; k++) { values[k] = item.getText(k); } Object data = item.getData(); boolean checked = item.getChecked(); item.dispose(); item = new TableItem(table, SWT.NONE, j); item.setText(values); item.setData(data); item.setChecked(checked); items = table.getItems(); break; } } } }
void tableMouseDown(Event event) { if (isDisposed() || !isVisible()) return; Point pt = new Point(event.x, event.y); int lineWidth = table.getLinesVisible() ? table.getGridLineWidth() : 0; TableItem item = table.getItem(pt); if ((table.getStyle() & SWT.FULL_SELECTION) != 0) { if (item == null) return; } else { int start = item != null ? table.indexOf(item) : table.getTopIndex(); int end = table.getItemCount(); Rectangle clientRect = table.getClientArea(); for (int i = start; i < end; i++) { TableItem nextItem = table.getItem(i); Rectangle rect = nextItem.getBounds(0); if (pt.y >= rect.y && pt.y < rect.y + rect.height + lineWidth) { item = nextItem; break; } if (rect.y > clientRect.y + clientRect.height) return; } if (item == null) return; } TableColumn newColumn = null; int columnCount = table.getColumnCount(); if (columnCount == 0) { if ((table.getStyle() & SWT.FULL_SELECTION) == 0) { Rectangle rect = item.getBounds(0); rect.width += lineWidth; rect.height += lineWidth; if (!rect.contains(pt)) return; } } else { for (int i = 0; i < columnCount; i++) { Rectangle rect = item.getBounds(i); rect.width += lineWidth; rect.height += lineWidth; if (rect.contains(pt)) { newColumn = table.getColumn(i); break; } } if (newColumn == null) { if ((table.getStyle() & SWT.FULL_SELECTION) == 0) return; newColumn = table.getColumn(0); } } setRowColumn(item, newColumn, true); setFocus(); return; }
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; } }
private boolean findMatch( String searchString, TableItem item, int column, boolean matchWord, boolean matchCase) { String tableText = matchCase ? item.getText(column) : item.getText(column).toLowerCase(); if (matchWord) { if (tableText != null && tableText.equals(searchString)) { return true; } } else { if (tableText != null && tableText.indexOf(searchString) != -1) { return true; } } return false; }
/** * Positions the TableCursor over the cell at the given row and column in the parent table. * * @param row the TableItem of the row for the cell to select * @param column the index of column for the cell to select * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setSelection(TableItem row, int column) { checkWidget(); int columnCount = table.getColumnCount(); int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1; if (row == null || row.isDisposed() || column < 0 || column > maxColumnIndex) SWT.error(SWT.ERROR_INVALID_ARGUMENT); setRowColumn(table.indexOf(row), column, false); }
void _resize() { if (row == null) { setBounds(-200, -200, 0, 0); } else { int columnIndex = column == null ? 0 : table.indexOf(column); setBounds(row.getBounds(columnIndex)); } }
void unhookRowColumnListeners() { if (column != null) { column.removeListener(SWT.Dispose, disposeColumnListener); column.removeListener(SWT.Move, resizeListener); column.removeListener(SWT.Resize, resizeListener); column = null; } if (row != null) { row.removeListener(SWT.Dispose, disposeItemListener); row = null; } }
private void launchEditor(TableItem item) { String theCommand = resUser.getString("Custom_editor"); theCommand = theCommand.replaceAll("%file%", item.getText(3)); theCommand = theCommand.replaceAll("%line%", item.getText(1)); theCommand = theCommand.replace('/', File.separatorChar); theCommand = theCommand.replaceAll("%slash%", "/"); try { Runtime.getRuntime().exec(theCommand); } catch (IOException e) { displayError("Error launching editor: " + e.getMessage()); } }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); final Table table = new Table(shell, SWT.BORDER | SWT.MULTI); Rectangle clientArea = shell.getClientArea(); table.setBounds(clientArea.x, clientArea.y, 200, 200); for (int i = 0; i < 128; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText("Item " + i); } Menu menu = new Menu(shell, SWT.POP_UP); table.setMenu(menu); MenuItem item = new MenuItem(menu, SWT.PUSH); item.setText("Delete Selection"); item.addListener(SWT.Selection, event -> table.remove(table.getSelectionIndices())); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
void setRowColumn(TableItem row, TableColumn column, boolean notify) { if (this.row == row && this.column == column) { return; } if (this.row != null && this.row != row) { this.row.removeListener(SWT.Dispose, disposeItemListener); this.row = null; } if (this.column != null && this.column != column) { this.column.removeListener(SWT.Dispose, disposeColumnListener); this.column.removeListener(SWT.Move, resizeListener); this.column.removeListener(SWT.Resize, resizeListener); this.column = null; } if (row != null) { if (this.row != row) { this.row = row; row.addListener(SWT.Dispose, disposeItemListener); table.showItem(row); } if (this.column != column && column != null) { this.column = column; column.addListener(SWT.Dispose, disposeColumnListener); column.addListener(SWT.Move, resizeListener); column.addListener(SWT.Resize, resizeListener); table.showColumn(column); } int columnIndex = column == null ? 0 : table.indexOf(column); setBounds(row.getBounds(columnIndex)); redraw(); if (notify) { notifyListeners(SWT.Selection, new Event()); } } getAccessible().setFocus(ACC.CHILDID_SELF); }
void paint(Event event) { if (row == null) return; int columnIndex = column == null ? 0 : table.indexOf(column); GC gc = event.gc; gc.setBackground(getBackground()); gc.setForeground(getForeground()); gc.fillRectangle(event.x, event.y, event.width, event.height); int x = 0; Point size = getSize(); Image image = row.getImage(columnIndex); if (image != null) { Rectangle imageSize = image.getBounds(); int imageY = (size.y - imageSize.height) / 2; gc.drawImage(image, x, imageY); x += imageSize.width; } String text = row.getText(columnIndex); if (text.length() > 0) { Rectangle bounds = row.getBounds(columnIndex); Point extent = gc.stringExtent(text); // Temporary code - need a better way to determine table trim String platform = SWT.getPlatform(); if ("win32".equals(platform)) { // $NON-NLS-1$ if (table.getColumnCount() == 0 || columnIndex == 0) { x += 2; } else { int alignmnent = column.getAlignment(); switch (alignmnent) { case SWT.LEFT: x += 6; break; case SWT.RIGHT: x = bounds.width - extent.x - 6; break; case SWT.CENTER: x += (bounds.width - x - extent.x) / 2; break; } } } else { if (table.getColumnCount() == 0) { x += 5; } else { int alignmnent = column.getAlignment(); switch (alignmnent) { case SWT.LEFT: x += 5; break; case SWT.RIGHT: x = bounds.width - extent.x - 2; break; case SWT.CENTER: x += (bounds.width - x - extent.x) / 2 + 2; break; } } } int textY = (size.y - extent.y) / 2; gc.drawString(text, x, textY); } if (isFocusControl()) { Display display = getDisplay(); gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.drawFocus(0, 0, size.x, size.y); } }
void keyDown(Event event) { if (row == null) return; switch (event.character) { case SWT.CR: notifyListeners(SWT.DefaultSelection, new Event()); return; } int rowIndex = table.indexOf(row); int columnIndex = column == null ? 0 : table.indexOf(column); switch (event.keyCode) { case SWT.ARROW_UP: setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true); break; case SWT.ARROW_DOWN: setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true); break; case SWT.ARROW_LEFT: case SWT.ARROW_RIGHT: { int columnCount = table.getColumnCount(); if (columnCount == 0) break; int[] order = table.getColumnOrder(); int index = 0; while (index < order.length) { if (order[index] == columnIndex) break; index++; } if (index == order.length) index = 0; int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) != 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT; if (event.keyCode == leadKey) { setRowColumn(rowIndex, order[Math.max(0, index - 1)], true); } else { setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true); } break; } case SWT.HOME: setRowColumn(0, columnIndex, true); break; case SWT.END: { int i = table.getItemCount() - 1; setRowColumn(i, columnIndex, true); break; } case SWT.PAGE_UP: { int index = table.getTopIndex(); if (index == rowIndex) { Rectangle rect = table.getClientArea(); TableItem item = table.getItem(index); Rectangle itemRect = item.getBounds(0); rect.height -= itemRect.y; int height = table.getItemHeight(); int page = Math.max(1, rect.height / height); index = Math.max(0, index - page + 1); } setRowColumn(index, columnIndex, true); break; } case SWT.PAGE_DOWN: { int index = table.getTopIndex(); Rectangle rect = table.getClientArea(); TableItem item = table.getItem(index); Rectangle itemRect = item.getBounds(0); rect.height -= itemRect.y; int height = table.getItemHeight(); int page = Math.max(1, rect.height / height); int end = table.getItemCount() - 1; index = Math.min(end, index + page - 1); if (index == rowIndex) { index = Math.min(end, index + page - 1); } setRowColumn(index, columnIndex, true); break; } } }
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; }
private void openAddressBook(String name) { if (name == null) return; File file = new File(name); if (!file.exists()) { displayError( resMessages.getString("File") + file.getName() + " " + resMessages.getString("Does_not_exist")); return; } Cursor waitCursor = new Cursor(shell.getDisplay(), 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(resMessages.getString("File_not_found") + "\n" + file.getName()); return; } catch (IOException e) { displayError(resMessages.getString("IO_error_read") + "\n" + file.getName()); return; } finally { shell.setCursor(null); waitCursor.dispose(); if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { displayError(resMessages.getString("IO_error_close") + "\n" + file.getName()); return; } } } String[][] tableInfo = new String[data.length][table.getColumnCount()]; for (int i = 0; i < data.length; i++) { tableInfo[i] = decodeLine(data[i]); } 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(resMessages.getString("Title_bar") + file.getName()); isModified = false; this.file = file; }