package ie.dcu.apps.ist.actions; import ie.dcu.apps.ist.views.SegmentationView; import ie.dcu.segment.SegmentationContext; import ie.dcu.segment.painters.SegmentationPainter; import ie.dcu.swt.*; import ie.dcu.util.FileUtils; import java.io.*; import java.util.logging.Level; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.*; /** * Export current view as image. * * @author Kevin McGuinness */ public class ExportViewAction extends AppAction { private static final String DEFAULT_SUFFIX = ".png"; private FileDialog dialog; private String directory; public ExportViewAction(ActionManager m) { super(m); } @Override public void run() { if (window.hasContext()) { File file = getFile(); if (file != null) { directory = file.getParent(); export(file); } } } private void export(File file) { Image im = paintContext(); try { SwtUtils.saveImage(im, file); status("View successfully exported as %s", file.getName()); } catch (IOException e) { handleError(file, e); } finally { im.dispose(); } } private void handleError(File file, IOException e) { // Log log(Level.WARNING, "Error exporting view as image", e); // Show error dialog error("Error exporting view as image %s: %s", file.getName(), e.getLocalizedMessage() ); // Set status message status(Error, "Error exporting view as image %s", file.getName()); } private File getFile() { createExportDialog(); String path = dialog.open(); if (path != null) { return checkFile(path); } return null; } private File checkFile(String path) { File file = new File(path); if (SwtUtils.getImageFormat(file) != -1) { return file; } // Unknown file extension, so assume jpg return new File(FileUtils.replaceExtension(path, DEFAULT_SUFFIX)); } private void createExportDialog() { if (dialog == null) { dialog = new FileDialog(window.getShell(), SWT.SAVE | SWT.SHEET); dialog.setText(property("ExportViewAction.dialog.text")); dialog.setFilterExtensions(getFilters()); dialog.setFilterNames(getFilterNames()); } dialog.setFileName(getFileName()); String dir = getDirectory(); if (dir != null) { // Unfortunately this is buggy on SWT/GTK and // the filter path isin't always set :'-( dialog.setFilterPath(dir); } } private String getFileName() { File file = window.getContext().getFile(); return FileUtils.replaceExtension(file.getName(), DEFAULT_SUFFIX); } private String getDirectory() { if (directory == null) { File folder = window.getContext().getFolder(); directory = (folder != null) ? directory = folder.getAbsolutePath() : null; } return directory; } private String[] getFilters() { return new String[] { property("ExportViewAction.dialog.filter.exts") }; } private String[] getFilterNames() { return new String[] { property("ExportViewAction.dialog.filter.text") }; } private Image paintContext() { SegmentationView view = window.getView(); SegmentationContext ctx = window.getContext(); SegmentationPainter painter = view.getPainter(); ObservableImage im = createCompatibleImage(ctx); painter.paint(ctx, im); return im.getImage(); } private ObservableImage createCompatibleImage(SegmentationContext ctx) { Image im = new Image(Display.getCurrent(), ctx.getBounds()); return new ObservableImage(im); } }