package ie.dcu.apps.ist.actions; import java.io.*; import java.util.logging.*; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; /** * Save segmentation context as. * * @author Kevin McGuinness */ public class SaveAsAction extends AppAction { private FileDialog dialog; public SaveAsAction(ActionManager m) { super(m); } @Override public void run() { if (window.hasContext()) { File file = getFile(); if (file != null) { save(file); } } } private void createSaveDialog() { if (dialog == null) { dialog = new FileDialog(window.getShell(), SWT.SAVE | SWT.SHEET); dialog.setText(property("SaveAction.dialog.text")); dialog.setFilterExtensions(getFilters()); dialog.setFilterNames(getFilterNames()); String dir = getDirectory(); if (dir != null) { dialog.setFilterPath(dir); } } dialog.setFileName(getFileName()); } private String getDirectory() { File folder = window.getContext().getFolder(); if (folder != null) { return folder.getAbsolutePath(); } return null; } private String getFileName() { return window.getContext().getDefaultFilename(); } private String[] getFilters() { return new String[] { property("SaveAction.dialog.filter.exts") }; } private String[] getFilterNames() { return new String[] { property("SaveAction.dialog.filter.text") }; } private File getFile() { createSaveDialog(); String path = dialog.open(); if (path != null) { return new File(path); } return null; } private void save(File file) { try { window.getContext().save(file); window.updateWindowTitle(); status("Saved context %s successfully", file.getName()); } catch (IOException e) { handleError(file, e); } } private void handleError(File file, IOException e) { // Log log(Level.SEVERE, "Error saving file", e); // Show error dialog error("Error saving segmentation context %s: %s", file.getName(), e.getLocalizedMessage() ); // Set status message status(Error, "Error saving segmentation context %s", file.getName()); } }