package ie.dcu.apps.ist.actions; import ie.dcu.apps.ist.dialogs.ExportDialog; import ie.dcu.apps.ist.export.imagemap.ExportException; import ie.dcu.apps.ist.export.imagemap.Exporter; import ie.dcu.segment.SegmentationContext; import ie.dcu.segment.SegmentationMask; import ie.dcu.swt.ImageConverter; import java.awt.image.BufferedImage; import java.io.*; import java.util.List; import java.util.logging.*; import org.eclipse.swt.*; import org.eclipse.swt.program.Program; import org.eclipse.swt.widgets.*; /** * Save segmentation context. * * @author Kevin McGuinness */ public class SaveAction extends AppAction { // Export image map functionality is changes as Save /** * The file extension given to a this object when it is saved on a disk */ public static final String IMGMAP_EXTENSION = ".zip"; public SaveAction(ActionManager m) { super(m); } @Override public void run() { SegmentationContext ctx = window.getContext(); List masks = ctx.getSegmentationMasks(); if (ctx.hasSegmentationMasks()) { // Get options from user ExportDialog dialog = new ExportDialog(window.getShell()); ExportDialog.Result result = dialog.open(); if (result != null) { // Grab image and mask BufferedImage image = ImageConverter.convert(ctx.getImageData()); // Setup exporter Exporter exporter = new Exporter(image, ctx); // save the annotated zip file String zipFile = result.zipFile; exporter.setZipFile(zipFile+IMGMAP_EXTENSION); exporter.setImageFile(result.image); exporter.setEffect(result.effect); exporter.setHtmlFile(zipFile+".html"); exporter.setExportShape(result.shape); //exporter.setObjectLink(result.link); //exporter.setObjectDescription(result.description); // Export try { exporter.export(result.folder,result.open,result.exportFolder,masks); } catch (IOException e) { handleError(e); return; } catch (ExportException e) { handleError(e); return; } // for opening the image after saving as ImageMap if (result.open) { File file = new File(result.exportFolder, result.html); Program program = Program.findProgram(".html"); if (program != null) { program.execute(file.getAbsolutePath()); } } } } } public void runSaveAndClose() { SegmentationContext ctx = window.getContext(); List masks = ctx.getSegmentationMasks(); if (ctx.hasSegmentationMasks()) { // Get options from user ExportDialog dialog = new ExportDialog(window.getShell()); ExportDialog.Result result = dialog.open(); if (result != null) { // Grab image and mask BufferedImage image = ImageConverter.convert(ctx.getImageData()); // Setup exporter Exporter exporter = new Exporter(image, ctx); // save the annotated zip file String zipFile = result.zipFile; exporter.setZipFile(zipFile+IMGMAP_EXTENSION); exporter.setImageFile(result.image); exporter.setEffect(result.effect); exporter.setHtmlFile(zipFile+".html"); exporter.setExportShape(result.shape); //exporter.setObjectLink(result.link); //exporter.setObjectDescription(result.description); // Export try { exporter.export(result.folder,result.open,result.exportFolder,masks); } catch (IOException e) { handleError(e); return; } catch (ExportException e) { handleError(e); return; } // for opening the image after saving as ImageMap if (result.open) { File file = new File(result.exportFolder, result.html); Program program = Program.findProgram(".html"); if (program != null) { program.execute(file.getAbsolutePath()); } } window.getView().resetView(); // clear current data and work } } } private void handleError(Exception e) { log(Level.WARNING, "Error exporting view as HTML image map", e); // Show error dialog error("Error exporting view as HTML image map: %s", e.getLocalizedMessage()); // Set status message status(Error, "Error exporting view as HTML image map"); } /*private FileDialog dialog; public SaveAction(ActionManager m) { super(m); } @Override public void run() { if (window.hasContext()) { File file; if (window.hasContextFile()) { file = window.getContextFile(); } else { 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()); }*/ }