private boolean saveAs() { FileDialog saveDialog = new FileDialog(shell, SWT.SAVE); saveDialog.setFilterExtensions(new String[] {"*.adr;", "*.*"}); saveDialog.setFilterNames(new String[] {"Address Books (*.adr)", "All Files "}); saveDialog.open(); String name = saveDialog.getFileName(); if (name.equals("")) return false; if (name.indexOf(".adr") != name.length() - 4) { name += ".adr"; } File file = new File(saveDialog.getFilterPath(), name); if (file.exists()) { MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO); box.setText(resAddressBook.getString("Save_as_title")); box.setMessage( resAddressBook.getString("File") + file.getName() + " " + resAddressBook.getString("Query_overwrite")); if (box.open() != SWT.YES) { return false; } } this.file = file; return save(); }
/** 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 openAddressBook() { FileDialog fileDialog = new FileDialog(shell, SWT.OPEN); fileDialog.setFilterExtensions(new String[] {"*.messages;", "*.*"}); fileDialog.setFilterNames( new String[] { resMessages.getString("Book_filter_name") + " (*.messages)", resMessages.getString("All_filter_name") + " (*.*)" }); String name = fileDialog.open(); openAddressBook(name); }
private void openFileMenuItemWidgetSelected(SelectionEvent evt) { FileDialog dialog = new FileDialog(getShell(), SWT.OPEN); String filename = dialog.open(); if (filename != null) { getShell().setText(filename); File file = new File(filename); XML.load(file); try { XML.Match(); setStatus(XML.Output); } catch (JDOMException e) { e.printStackTrace(); } } }
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; }
/** * Handle the create button selection event. * * @param event org.eclipse.swt.events.SelectionEvent */ void createButtonSelected(SelectionEvent event) { /* Compute the appropriate dialog style */ int style = getDefaultStyle(); if (okButton.getEnabled() && okButton.getSelection()) style |= SWT.OK; if (cancelButton.getEnabled() && cancelButton.getSelection()) style |= SWT.CANCEL; if (yesButton.getEnabled() && yesButton.getSelection()) style |= SWT.YES; if (noButton.getEnabled() && noButton.getSelection()) style |= SWT.NO; if (retryButton.getEnabled() && retryButton.getSelection()) style |= SWT.RETRY; if (abortButton.getEnabled() && abortButton.getSelection()) style |= SWT.ABORT; if (ignoreButton.getEnabled() && ignoreButton.getSelection()) style |= SWT.IGNORE; if (iconErrorButton.getEnabled() && iconErrorButton.getSelection()) style |= SWT.ICON_ERROR; if (iconInformationButton.getEnabled() && iconInformationButton.getSelection()) style |= SWT.ICON_INFORMATION; if (iconQuestionButton.getEnabled() && iconQuestionButton.getSelection()) style |= SWT.ICON_QUESTION; if (iconWarningButton.getEnabled() && iconWarningButton.getSelection()) style |= SWT.ICON_WARNING; if (iconWorkingButton.getEnabled() && iconWorkingButton.getSelection()) style |= SWT.ICON_WORKING; if (primaryModalButton.getEnabled() && primaryModalButton.getSelection()) style |= SWT.PRIMARY_MODAL; if (applicationModalButton.getEnabled() && applicationModalButton.getSelection()) style |= SWT.APPLICATION_MODAL; if (systemModalButton.getEnabled() && systemModalButton.getSelection()) style |= SWT.SYSTEM_MODAL; if (saveButton.getEnabled() && saveButton.getSelection()) style |= SWT.SAVE; if (openButton.getEnabled() && openButton.getSelection()) style |= SWT.OPEN; if (multiButton.getEnabled() && multiButton.getSelection()) style |= SWT.MULTI; /* Open the appropriate dialog type */ String name = dialogCombo.getText(); if (name.equals(ControlExample.getResourceString("ColorDialog"))) { ColorDialog dialog = new ColorDialog(shell, style); dialog.setRGB(new RGB(100, 100, 100)); dialog.setText(ControlExample.getResourceString("Title")); RGB result = dialog.open(); textWidget.append(ControlExample.getResourceString("ColorDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER); textWidget.append("getRGB() = " + dialog.getRGB() + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("DirectoryDialog"))) { DirectoryDialog dialog = new DirectoryDialog(shell, style); dialog.setMessage(ControlExample.getResourceString("Example_string")); dialog.setText(ControlExample.getResourceString("Title")); String result = dialog.open(); textWidget.append(ControlExample.getResourceString("DirectoryDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("FileDialog"))) { FileDialog dialog = new FileDialog(shell, style); dialog.setFileName(ControlExample.getResourceString("readme_txt")); dialog.setFilterNames(FilterNames); dialog.setFilterExtensions(FilterExtensions); dialog.setText(ControlExample.getResourceString("Title")); String result = dialog.open(); textWidget.append(ControlExample.getResourceString("FileDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER); textWidget.append("getFileNames() =" + Text.DELIMITER); if ((dialog.getStyle() & SWT.MULTI) != 0) { String[] files = dialog.getFileNames(); for (int i = 0; i < files.length; i++) { textWidget.append("\t" + files[i] + Text.DELIMITER); } } textWidget.append( "getFilterIndex() = " + dialog.getFilterIndex() + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("FontDialog"))) { FontDialog dialog = new FontDialog(shell, style); dialog.setText(ControlExample.getResourceString("Title")); FontData result = dialog.open(); textWidget.append(ControlExample.getResourceString("FontDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER); textWidget.append("getFontList() =" + Text.DELIMITER); FontData[] fonts = dialog.getFontList(); if (fonts != null) { for (int i = 0; i < fonts.length; i++) { textWidget.append("\t" + fonts[i] + Text.DELIMITER); } } textWidget.append("getRGB() = " + dialog.getRGB() + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("PrintDialog"))) { PrintDialog dialog = new PrintDialog(shell, style); dialog.setText(ControlExample.getResourceString("Title")); PrinterData result = dialog.open(); textWidget.append(ControlExample.getResourceString("PrintDialog") + Text.DELIMITER); textWidget.append( ControlExample.getResourceString("Result", new String[] {"" + result}) + Text.DELIMITER); textWidget.append("getScope() = " + dialog.getScope() + Text.DELIMITER); textWidget.append("getStartPage() = " + dialog.getStartPage() + Text.DELIMITER); textWidget.append("getEndPage() = " + dialog.getEndPage() + Text.DELIMITER); textWidget.append( "getPrintToFile() = " + dialog.getPrintToFile() + Text.DELIMITER + Text.DELIMITER); return; } if (name.equals(ControlExample.getResourceString("MessageBox"))) { MessageBox dialog = new MessageBox(shell, style); dialog.setMessage(ControlExample.getResourceString("Example_string")); dialog.setText(ControlExample.getResourceString("Title")); int result = dialog.open(); textWidget.append(ControlExample.getResourceString("MessageBox") + Text.DELIMITER); /* * The resulting integer depends on the original * dialog style. Decode the result and display it. */ switch (result) { case SWT.OK: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.OK"})); break; case SWT.YES: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.YES"})); break; case SWT.NO: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.NO"})); break; case SWT.CANCEL: textWidget.append( ControlExample.getResourceString("Result", new String[] {"SWT.CANCEL"})); break; case SWT.ABORT: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.ABORT"})); break; case SWT.RETRY: textWidget.append(ControlExample.getResourceString("Result", new String[] {"SWT.RETRY"})); break; case SWT.IGNORE: textWidget.append( ControlExample.getResourceString("Result", new String[] {"SWT.IGNORE"})); break; default: textWidget.append(ControlExample.getResourceString("Result", new String[] {"" + result})); break; } textWidget.append(Text.DELIMITER + Text.DELIMITER); } }
public static void main(String[] args) { final Display display = new Display(); final Clipboard clipboard = new Clipboard(display); final Shell shell = new Shell(display, SWT.SHELL_TRIM); shell.setLayout(new GridLayout()); shell.setText("Clipboard ImageTransfer"); final Button imageButton = new Button(shell, SWT.NONE); GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); gd.minimumHeight = 400; gd.minimumWidth = 600; imageButton.setLayoutData(gd); final Text imageText = new Text(shell, SWT.BORDER); imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); Composite buttons = new Composite(shell, SWT.NONE); buttons.setLayout(new GridLayout(4, true)); Button button = new Button(buttons, SWT.PUSH); button.setText("Open"); button.addListener( SWT.Selection, event -> { FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); image = new Image(display, string); imageButton.setImage(image); imageText.setText(string); } }); button = new Button(buttons, SWT.PUSH); button.setText("Copy"); button.addListener( SWT.Selection, event -> { Image image = imageButton.getImage(); if (image != null) { ImageTransfer imageTransfer = ImageTransfer.getInstance(); TextTransfer textTransfer = TextTransfer.getInstance(); clipboard.setContents( new Object[] {image.getImageData(), imageText.getText()}, new Transfer[] {imageTransfer, textTransfer}); } }); button = new Button(buttons, SWT.PUSH); button.setText("Paste"); button.addListener( SWT.Selection, event -> { ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance()); if (imageData != null) { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); image = new Image(display, imageData); imageButton.setImage(image); } else { imageButton.setText("No image"); imageButton.setImage(null); } String text = (String) clipboard.getContents(TextTransfer.getInstance()); if (text != null) { imageText.setText(text); } else { imageText.setText(""); } }); button = new Button(buttons, SWT.PUSH); button.setText("Clear"); button.addListener( SWT.Selection, event -> { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); imageButton.setImage(null); imageText.setText(""); }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }