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; } }
/** * Sets the font that the receiver will use to paint textual information for the specified cell in * this item to the font specified by the argument, or to the default font for that kind of * control if the argument is null. * * @param index the column index * @param font the new font (or null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed * </ul> * * @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> * * @since 3.0 */ public void setFont(int index, Font font) { checkWidget(); if (font != null && font.isDisposed()) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } int count = Math.max(1, parent.getColumnCount()); if (0 > index || index > count - 1) return; if (cellFont == null) { if (font == null) return; cellFont = new Font[count]; } Font oldFont = cellFont[index]; if (oldFont == font) return; cellFont[index] = font; if (oldFont != null && oldFont.equals(font)) return; int modelIndex = parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns[index].modelIndex; int /*long*/ fontHandle = font != null ? font.handle : 0; OS.gtk_list_store_set(parent.modelHandle, handle, modelIndex + Table.CELL_FONT, fontHandle, -1); /* * Bug in GTK. When using fixed-height-mode, * row changes do not cause the row to be repainted. The fix is to * invalidate the row when it is cleared. */ if ((parent.style & SWT.VIRTUAL) != 0) { if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2) && OS.GTK_VERSION < OS.VERSION(2, 6, 3)) { redraw(); } } cached = true; if (font != null) { boolean customDraw = (parent.columnCount == 0) ? parent.firstCustomDraw : parent.columns[index].customDraw; if (!customDraw) { if ((parent.style & SWT.VIRTUAL) == 0) { int /*long*/ parentHandle = parent.handle; int /*long*/ column = 0; if (parent.columnCount > 0) { column = parent.columns[index].handle; } else { column = OS.gtk_tree_view_get_column(parentHandle, index); } if (column == 0) return; int /*long*/ textRenderer = parent.getTextRenderer(column); int /*long*/ imageRenderer = parent.getPixbufRenderer(column); OS.gtk_tree_view_column_set_cell_data_func( column, textRenderer, display.cellDataProc, parentHandle, 0); OS.gtk_tree_view_column_set_cell_data_func( column, imageRenderer, display.cellDataProc, parentHandle, 0); } if (parent.columnCount == 0) { parent.firstCustomDraw = true; } else { parent.columns[index].customDraw = true; } } } }
/** * Returns a rectangle describing the receiver's size and location relative to its parent at a * column in the table. * * @param index the index that specifies the column * @return the receiver's bounding column rectangle * @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 Rectangle getBounds(final int index) { checkWidget(); if (!parent.checkData(this, parent.indexOf(this))) { error(SWT.ERROR_WIDGET_DISPOSED); } Rectangle result; int columnCount = parent.getColumnCount(); if (columnCount > 0 && (index < 0 || index >= columnCount)) { result = new Rectangle(0, 0, 0, 0); } else { Rectangle textBounds = getTextBounds(index); int left = getLeft(index); int itemIndex = parent.indexOf(this); int top = getTop(itemIndex); int width = 0; if (index == 0 && columnCount == 0) { Rectangle imageBounds = getImageBounds(index); int spacing = getSpacing(index); int paddingWidth = parent.getCellPadding().width; width = imageBounds.width + spacing + textBounds.width + paddingWidth; } else if (index >= 0 && index < columnCount) { width = parent.getColumn(index).getWidth() - getCheckWidth(index); } int height = getHeight(index); result = new Rectangle(left, top, width, height); } return result; }
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; } } } }
/** * Positions the TableCursor over the cell at the given row and column in the parent table. * * @param row the index 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(int row, int column) { checkWidget(); int columnCount = table.getColumnCount(); int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1; if (row < 0 || row >= table.getItemCount() || column < 0 || column > maxColumnIndex) SWT.error(SWT.ERROR_INVALID_ARGUMENT); setRowColumn(row, column, 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); }
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]); } }
/** * Sets the receiver's image at a column. * * @param index the column index * @param image the new image * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed * </ul> * * @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 setImage(int index, Image image) { checkWidget(); if (image != null && image.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } if (image != null && image.type == SWT.ICON) { if (image.equals(_getImage(index))) return; } int count = Math.max(1, parent.getColumnCount()); if (0 > index || index > count - 1) return; int /*long*/ pixbuf = 0; if (image != null) { ImageList imageList = parent.imageList; if (imageList == null) imageList = parent.imageList = new ImageList(); int imageIndex = imageList.indexOf(image); if (imageIndex == -1) imageIndex = imageList.add(image); pixbuf = imageList.getPixbuf(imageIndex); } int modelIndex = parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns[index].modelIndex; OS.gtk_list_store_set(parent.modelHandle, handle, modelIndex + Table.CELL_PIXBUF, pixbuf, -1); /* * Bug in GTK. When using fixed-height-mode, * row changes do not cause the row to be repainted. The fix is to * invalidate the row when it is cleared. */ if ((parent.style & SWT.VIRTUAL) != 0) { if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2) && OS.GTK_VERSION < OS.VERSION(2, 6, 3)) { redraw(); } } /* * Bug in GTK. When in fixed height mode, GTK does not recalculate the cell renderer width * when the image is changed in the model. The fix is to force it to recalculate the width if * more space is required. */ if ((parent.style & SWT.VIRTUAL) != 0 && parent.currentItem == null) { if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2)) { if (image != null) { int /*long*/ parentHandle = parent.handle; int /*long*/ column = OS.gtk_tree_view_get_column(parentHandle, index); int[] w = new int[1]; int /*long*/ pixbufRenderer = parent.getPixbufRenderer(column); OS.gtk_tree_view_column_cell_get_position(column, pixbufRenderer, null, w); if (w[0] < image.getBounds().width) { /* * There is no direct way to clear the cell renderer width so we * are relying on the fact that it is done as part of modifying * the style. */ int /*long*/ style = OS.gtk_widget_get_modifier_style(parentHandle); parent.modifyStyle(parentHandle, style); } } } } cached = true; }
/** * Sets the receiver's text at a column * * @param index the column index * @param text the new text * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the text is null * </ul> * * @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 setText(final int index, final String text) { checkWidget(); if (text == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } int count = Math.max(1, parent.getColumnCount()); if (index >= 0 && index < count) { ensureData(index, count); if (!text.equals(data[index].text)) { data[index].text = text; markCached(); if (parent.getColumnCount() == 0) { parent.updateScrollBars(); } parent.redraw(); } } }
final void removeData(final int index) { if (data != null && parent.getColumnCount() > 1) { Data[] newData = new Data[data.length - 1]; System.arraycopy(data, 0, newData, 0, index); int offSet = data.length - index - 1; System.arraycopy(data, index + 1, newData, index, offSet); data = newData; } }
private int getHeight(final int index) { int result = 0; int columnCount = parent.getColumnCount(); boolean singleColumn = index == 0 && columnCount == 0; boolean columnInRange = index >= 0 && index < columnCount; if (singleColumn || columnInRange) { result = parent.getItemHeight(); } return result; }
/** * Sets the receiver's image at a column. * * @param index the column index * @param image the new image * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed * </ul> * * @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 setImage(final int index, final Image image) { checkWidget(); if (image != null && image.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } int count = Math.max(1, parent.getColumnCount()); if (index >= 0 && index < count) { ensureData(index, count); if (!equals(data[index].image, image)) { parent.updateColumnImageCount(index, data[index].image, image); data[index].image = image; parent.updateItemImageSize(image); markCached(); if (parent.getColumnCount() == 0) { parent.updateScrollBars(); } parent.redraw(); } } }
private int getLeft(final int index) { int result = 0; int columnCount = parent.getColumnCount(); if (index == 0 && columnCount == 0) { result = getCheckWidth(index) - parent.leftOffset; } else if (index >= 0 && index < columnCount) { // TODO [rh] consider applying the leftOffset already in Column#getLeft() int columnLeft = parent.getColumn(index).getLeft(); result = getCheckWidth(index) + columnLeft - parent.leftOffset; } return result; }
Image _getImage(int index) { int count = Math.max(1, parent.getColumnCount()); if (0 > index || index > count - 1) return null; int /*long*/[] ptr = new int /*long*/[1]; int modelIndex = parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns[index].modelIndex; OS.gtk_tree_model_get(parent.modelHandle, handle, modelIndex + Table.CELL_PIXBUF, ptr, -1); if (ptr[0] == 0) return null; ImageList imageList = parent.imageList; int imageIndex = imageList.indexOf(ptr[0]); if (imageIndex == -1) return null; return imageList.get(imageIndex); }
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; } }
String _getText(int index) { int count = Math.max(1, parent.getColumnCount()); if (0 > index || index > count - 1) return ""; int /*long*/[] ptr = new int /*long*/[1]; int modelIndex = parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns[index].modelIndex; OS.gtk_tree_model_get(parent.modelHandle, handle, modelIndex + Table.CELL_TEXT, ptr, -1); if (ptr[0] == 0) return ""; int length = OS.strlen(ptr[0]); byte[] buffer = new byte[length]; OS.memmove(buffer, ptr[0], length); OS.g_free(ptr[0]); return new String(Converter.mbcsToWcs(null, buffer)); }
/** * Sets the font that the receiver will use to paint textual information for this item to the font * specified by the argument, or to the default font for that kind of control if the argument is * null. * * @param font the new font (or null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed * </ul> * * @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 setFont(final Font font) { checkWidget(); if (font != null && font.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } if (!equals(this.font, font)) { this.font = font; markCached(); if (parent.getColumnCount() == 0) { parent.updateScrollBars(); } parent.redraw(); } }
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; }
/** * Returns a rectangle describing the size and location relative to its parent of the text at a * column in the table. An empty rectangle is returned if index exceeds the index of the table's * last column. * * @param index the index that specifies the column * @return the receiver's bounding text rectangle * @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 Rectangle getTextBounds(final int index) { checkWidget(); int itemIndex = parent.indexOf(this); if (!parent.checkData(this, itemIndex)) { error(SWT.ERROR_WIDGET_DISPOSED); } int left = 0; int top = 0; int width = 0; Rectangle cellPadding = parent.getCellPadding(); if (index == 0 && parent.getColumnCount() == 0) { int imageWidth = 0; int spacing = 0; if (parent.hasColumnImages(0)) { imageWidth = parent.getItemImageSize().x; spacing = getSpacing(0); } left = getLeft(0) + cellPadding.x + imageWidth + spacing; top = getTop(itemIndex); Font font = getFont(); width = Graphics.stringExtent(font, getText(0)).x; } else if (itemIndex != -1 && index < parent.getColumnCount()) { int imageWidth = 0; if (parent.hasColumnImages(index)) { imageWidth = parent.getItemImageSize().x; } int spacing = getSpacing(index); left = getLeft(index) + cellPadding.x + imageWidth + spacing; top = getTop(itemIndex); width = getColumnWidth(index) - cellPadding.width - imageWidth - spacing; if (width < 0) { width = 0; } } int height = getHeight(index); return new Rectangle(left, top, width, height); }
/** * Sets the foreground color at the given column index in the receiver to the color specified by * the argument, or to the default system color for the item if the argument is null. * * @param index the column index * @param color the new color (or null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed * </ul> * * @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 setForeground(final int index, final Color color) { checkWidget(); if (color != null && color.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } int count = Math.max(1, parent.getColumnCount()); if (index >= 0 && index < count) { ensureData(index, count); if (!equals(data[index].foreground, color)) { data[index].foreground = color; markCached(); parent.redraw(); } } }
/** * Sets the font that the receiver will use to paint textual information for the specified cell in * this item to the font specified by the argument, or to the default font for that kind of * control if the argument is null. * * @param index the column index * @param font the new font (or null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed * </ul> * * @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 setFont(final int index, final Font font) { checkWidget(); if (font != null && font.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } int count = Math.max(1, parent.getColumnCount()); if (index >= 0 && index < count) { ensureData(index, count); if (!equals(font, data[index].font)) { data[index].font = font; markCached(); parent.redraw(); } } }
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; }
/** 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; }
/** * Sets the receiver's text at a column * * @param index the column index * @param string the new text * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the text is null * </ul> * * @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 setText(int index, String string) { checkWidget(); if (string == null) error(SWT.ERROR_NULL_ARGUMENT); if (_getText(index).equals(string)) return; int count = Math.max(1, parent.getColumnCount()); if (0 > index || index > count - 1) return; byte[] buffer = Converter.wcsToMbcs(null, string, true); int modelIndex = parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns[index].modelIndex; OS.gtk_list_store_set(parent.modelHandle, handle, modelIndex + Table.CELL_TEXT, buffer, -1); /* * Bug in GTK. When using fixed-height-mode, * row changes do not cause the row to be repainted. The fix is to * invalidate the row when it is cleared. */ if ((parent.style & SWT.VIRTUAL) != 0) { if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2) && OS.GTK_VERSION < OS.VERSION(2, 6, 3)) { redraw(); } } cached = true; }
public static void maxTableColumnsWidth(Table table) { table.setRedraw(false); try { int columnCount = table.getColumnCount(); if (columnCount > 0) { int totalWidth = 0; final TableColumn[] columns = table.getColumns(); for (TableColumn tc : columns) { tc.pack(); totalWidth += tc.getWidth(); } final Rectangle clientArea = table.getClientArea(); if (totalWidth < clientArea.width) { int extraSpace = clientArea.width - totalWidth; extraSpace /= columnCount; for (TableColumn tc : columns) { tc.setWidth(tc.getWidth() + extraSpace); } } } } finally { table.setRedraw(true); } }
void setRowColumn(int row, int column, boolean notify) { TableItem item = row == -1 ? null : table.getItem(row); TableColumn col = column == -1 || table.getColumnCount() == 0 ? null : table.getColumn(column); setRowColumn(item, col, notify); }
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; }
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; } } }
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); } }
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; }