/** 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()}); } }
/** Export the selected values from the table to a csv file */ private void export() { FileDialog dialog = new FileDialog(shell, SWT.SAVE); String[] filterNames = new String[] {"CSV Files", "All Files (*)"}; String[] filterExtensions = new String[] {"*.csv;", "*"}; String filterPath = "/"; String platform = SWT.getPlatform(); if (platform.equals("win32") || platform.equals("wpf")) { filterNames = new String[] {"CSV Files", "All Files (*.*)"}; filterExtensions = new String[] {"*.csv", "*.*"}; filterPath = "c:\\"; } dialog.setFilterNames(filterNames); dialog.setFilterExtensions(filterExtensions); dialog.setFilterPath(filterPath); try { dialog.setFileName(this.getCurrentKey() + ".csv"); } catch (Exception e) { dialog.setFileName("export.csv"); } String fileName = dialog.open(); FileOutputStream fos; OutputStreamWriter out; try { fos = new FileOutputStream(fileName); out = new OutputStreamWriter(fos, "UTF-8"); } catch (Exception e) { MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR); messageBox.setMessage("Error creating export file " + fileName + " : " + e.getMessage()); messageBox.open(); return; } Vector<TableItem> sel = new Vector<TableItem>(this.dataTable.getSelection().length); sel.addAll(Arrays.asList(this.dataTable.getSelection())); Collections.reverse(sel); for (TableItem item : sel) { String date = item.getText(0); String value = item.getText(1); try { out.write(date + ";" + value + "\n"); } catch (IOException e) { continue; } } try { out.close(); fos.close(); } catch (IOException e) { MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR); messageBox.setMessage("Error writing export file " + fileName + " : " + e.getMessage()); messageBox.open(); e.printStackTrace(); } }
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; } }
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; } }
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 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; }
void _resize() { if (row == null) { setBounds(-200, -200, 0, 0); } else { int columnIndex = column == null ? 0 : table.indexOf(column); setBounds(row.getBounds(columnIndex)); } }
/** * 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); }
/** * Delete Values from the history * * @param all when true the whole history of the selected parameter is deleted */ private void delete(boolean all) { MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO | SWT.ICON_QUESTION); messageBox.setMessage("Are you shure you want to delete these entries?"); messageBox.setText("Delete History?"); if (messageBox.open() == SWT.NO) return; String key, timebase; try { key = getCurrentKey(); timebase = getCurrentTimeBase(); } catch (Exception e) { return; } MeasurementHistoryController hist = master.getHistoryController(); MeasurementHistory measurements; if (timebase.equals("hours")) measurements = hist.getDataHours().get(key); else measurements = hist.getDataDays().get(key); if (all) { measurements.removeAll(); } else { for (TableItem item : this.dataTable.getSelection()) { String date = item.getText(0); String value = item.getText(1); SimpleDateFormat bla = new SimpleDateFormat(DATE_FORMAT); Date tmpdate; try { tmpdate = bla.parse(date); } catch (ParseException e) { e.printStackTrace(); continue; } MeasurementHistoryEntry tmp = new MeasurementHistoryEntry(Double.parseDouble(value), tmpdate); measurements.removeEntry(tmp); } } this.list_selected(); }
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()); } }
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; } }
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); }
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 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; }
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() { 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 openFile(String fileName, Table table) { String outStr; try { CSVReader reader = new CSVReader(new FileReader(fileName), ',', '"'); String[] strLineArr; // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(fileName); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; table.clearAll(); table.removeAll(); table.redraw(); int length = 0; boolean first = true; // Read File Line By Line while ((strLineArr = reader.readNext()) != null) { // while ((strLine = br.readLine()) != null) { // Print the content on the console // outStr = strLine; TableItem item = null; if (first) { length = strLineArr.length; } else { item = new TableItem(table, SWT.NONE); } int thisLen = strLineArr.length; if (thisLen <= length) { // String[] line = new String[length]; for (int i = 0; i < thisLen; i++) { // line[i] = st.nextToken(); // item.setText (i, st.nextToken()); if (first) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText(strLineArr[i]); } else { // System.out.println("-- "+i+" -- " + strLineArr[i]); item.setText(i, strLineArr[i]); } } } first = false; } // System.out.println("Finisehd file"); for (int i = 0; i < length; i++) { table.getColumn(i).pack(); } // System.out.println("finished packing"); // Close the input stream in.close(); } catch (Exception e) { // Catch exception if any System.err.println("Error: " + e.getMessage()); e.printStackTrace(); } }