From d67f7d3716101267d7d538fe5e260b8fe7330795 Mon Sep 17 00:00:00 2001 From: preecej Date: Thu, 4 Oct 2012 00:59:25 +0000 Subject: [PATCH] Tested the Close action. Drafted the Close dialog. Save action from Close action works, but only when hard-wired. Next: Return user choice from SaveReminder dialog to Close action. svn path=/; revision=387 --- .../config/application.mac.properties | 5 + .../resources/config/application.properties | 8 + .../ie/dcu/apps/ist/actions/CloseAction.java | 22 +- .../ie/dcu/apps/ist/actions/SaveAction.java | 53 ++++ .../ie/dcu/apps/ist/dialogs/PrefsDialog.java | 1 - .../apps/ist/dialogs/SaveReminderDialog.java | 261 ++++++++++++++++-- 6 files changed, 327 insertions(+), 23 deletions(-) diff --git a/Annotation/resources/config/application.mac.properties b/Annotation/resources/config/application.mac.properties index 5bea222..b4ddf8a 100644 --- a/Annotation/resources/config/application.mac.properties +++ b/Annotation/resources/config/application.mac.properties @@ -18,6 +18,11 @@ PrefsDialog.experiment-embedded.text=Show experiment panel embedded in main wind PrefsDialog.confirm-exit.text=Confirm exit when application is closed PrefsDialog.confirm-close.text=Remind to save current work when closing file +# Save Reminder Dialog +SaveReminderDialog.save.text=&Save +SaveReminderDialog.no-save.text=&Don't Save +SaveReminderDialog.cancel.text=&Cancel + # Experiment Panel ExperimentPanel.description.title=Task Description ExperimentPanel.timeout-message=The time allocated for this task is up! diff --git a/Annotation/resources/config/application.properties b/Annotation/resources/config/application.properties index ec7e641..a5cde02 100644 --- a/Annotation/resources/config/application.properties +++ b/Annotation/resources/config/application.properties @@ -20,6 +20,14 @@ PrefsDialog.experiment-embedded.text=Show experiment panel embedded in main wind PrefsDialog.confirm-exit.text=Confirm exit when application is closed PrefsDialog.confirm-close.text=Remind to save current work when closing file +# Save Reminder Dialog +SaveReminderDialog.save.text=&Save +SaveReminderDialog.save.icon=file:resources/icons/save.png +SaveReminderDialog.no-save.text=&Don't Save +SaveReminderDialog.no-save.icon=file:resources/icons/reset.png +SaveReminderDialog.cancel.text=&Cancel +SaveReminderDialog.cancel.icon=file:resources/icons/close.png + # Experiment Panel ExperimentPanel.description.title=Task Description ExperimentPanel.timeout-message=The time allocated for this task is up! diff --git a/Annotation/src/ie/dcu/apps/ist/actions/CloseAction.java b/Annotation/src/ie/dcu/apps/ist/actions/CloseAction.java index 433c88a..5b749d1 100644 --- a/Annotation/src/ie/dcu/apps/ist/actions/CloseAction.java +++ b/Annotation/src/ie/dcu/apps/ist/actions/CloseAction.java @@ -2,7 +2,12 @@ package ie.dcu.apps.ist.actions; import ie.dcu.apps.ist.*; import ie.dcu.apps.ist.dialogs.SaveReminderDialog; + +import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; +import org.eclipse.swt.widgets.MenuItem; /** * Close current image annotation file @@ -10,22 +15,29 @@ import org.eclipse.jface.dialogs.MessageDialog; * @author Justin Preece */ public class CloseAction extends AppAction { + public CloseAction(ActionManager m) { super(m); } @Override public void run() { + // TODO: add a call to check the confirm close app preference + // call Save Reminder dialog - SaveReminderDialog dialog = new SaveReminderDialog(window.getShell()); + SaveReminderDialog dialog = new SaveReminderDialog(window); SaveReminderDialog.ResultChoice result = dialog.open(); if (result != null) { // check return choice in result here and fire appropriate action: switch (result) { case SAVE: // save now and then close file ("Save now") - //TODO: trigger Save dialog to save the file - window.getView().resetView(); // clear current data and work + // trigger Save dialog to save the file + ActionManager actions = window.getActions(); + if (actions != null) { + SaveAction saveAction = actions.get(SaveAction.class); + saveAction.runSaveAndClose(); + } break; case NOSAVE: // do not save and then close file ("I've already saved my work") window.getView().resetView(); // clear current data and work @@ -37,7 +49,7 @@ public class CloseAction extends AppAction { } } } - + public static boolean confirmClose(AppWindow window) { boolean confirm = window.getPrefs().get( Boolean.class, AppPrefs.Keys.CONFIRM_CLOSE, true); @@ -50,4 +62,4 @@ public class CloseAction extends AppAction { return true; } -} +} \ No newline at end of file diff --git a/Annotation/src/ie/dcu/apps/ist/actions/SaveAction.java b/Annotation/src/ie/dcu/apps/ist/actions/SaveAction.java index a716699..47fa3a5 100644 --- a/Annotation/src/ie/dcu/apps/ist/actions/SaveAction.java +++ b/Annotation/src/ie/dcu/apps/ist/actions/SaveAction.java @@ -85,6 +85,59 @@ public class SaveAction extends AppAction { } } + 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); + + // 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); diff --git a/Annotation/src/ie/dcu/apps/ist/dialogs/PrefsDialog.java b/Annotation/src/ie/dcu/apps/ist/dialogs/PrefsDialog.java index e2d453a..7102e0a 100644 --- a/Annotation/src/ie/dcu/apps/ist/dialogs/PrefsDialog.java +++ b/Annotation/src/ie/dcu/apps/ist/dialogs/PrefsDialog.java @@ -178,7 +178,6 @@ public class PrefsDialog { return control; } - private Button createResetButton() { // Create button Button bt = createButton(buttonBar, RESET, SWT.PUSH); diff --git a/Annotation/src/ie/dcu/apps/ist/dialogs/SaveReminderDialog.java b/Annotation/src/ie/dcu/apps/ist/dialogs/SaveReminderDialog.java index 5f2e16f..ea2252b 100644 --- a/Annotation/src/ie/dcu/apps/ist/dialogs/SaveReminderDialog.java +++ b/Annotation/src/ie/dcu/apps/ist/dialogs/SaveReminderDialog.java @@ -1,31 +1,258 @@ package ie.dcu.apps.ist.dialogs; -import ie.dcu.apps.ist.dialogs.ExportDialog.Result; -import ie.dcu.apps.ist.export.imagemap.AreaShape; -import ie.dcu.apps.ist.export.imagemap.RolloverEffect; - -import java.io.File; - -import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Dialog; +import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; +import ie.dcu.apps.ist.*; +import ie.dcu.apps.ist.AppPrefs.Keys; +import ie.dcu.apps.ist.widgets.ColorSelector; +import ie.dcu.swt.SwtUtils; +import ie.dcu.swt.layout.LayoutFactory; + +import java.util.*; + +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class SaveReminderDialog { + private static final String QUESTION = "Do you wish to save your work before closing this file?"; + private static final String SAVE_TEXT = "Save now and then close this file."; + private static final String NOSAVE_TEXT = "Do NOT save this file; just close it."; + private static final String CANCEL_TEXT = "Do not close this file."; + + private final AppWindow window; + private final Shell shell; + private final Composite content; + private final Composite buttonBar; + private final Button saveButton; + private final Button noSaveButton; + private final Button cancelButton; + private final HashMap controls; + private ResultChoice choice; + + private transient GridData gd; + + public SaveReminderDialog(AppWindow window) { + this.window = window; + this.shell = createShell(); + this.content = createContent(); -public class SaveReminderDialog extends Dialog { + createControls(); - public SaveReminderDialog(Shell shell) { - super(shell); - setText(TITLE); + this.buttonBar = createButtonBar(); + this.saveButton = createSaveButton(); + this.noSaveButton = createNoSaveButton(); + this.cancelButton = createCancelButton(); + this.controls = new HashMap(); + + SwtUtils.center(window.getShell(), shell); } public enum ResultChoice { SAVE, NOSAVE, CANCEL } - // Default title - private static final String TITLE = "Save Reminder"; - public ResultChoice open() { - //TODO: build dialog UI here - return ResultChoice.SAVE; + shell.setVisible(true); + // TODO: can't return this early; look at ExportDialog for example; need to return choice via close + // or remove close and return here AFTER choice is made + return choice; + } + + public void close() { + shell.setVisible(false); + } + + private void createControls() { + createBanner(content, QUESTION); + // TODO: replace "button bar" with 2-col grid...labels on the left, buttons on the right in 3 rows + } + + private Shell createShell() { + Shell s = new Shell(window.getShell(), SWT.SHELL_TRIM | SWT.SHEET); + + // Add listener + s.addListener(SWT.Close, adapter); + + // Use a fill layout + s.setLayout(new FillLayout()); + + // Set size and title + s.setSize(420, 250); + s.setText(property("save-reminder", "Save Reminder")); + + return s; + } + + + private Composite createContent() { + + // Create content pane with grid layout + Composite c = new Composite(shell, 0); + c.setLayout(new GridLayout()); + return c; + } + + private Label createBanner(Composite comp, String key) { + // Create label + Label lb = new Label(comp, SWT.NONE); + lb.setText(property(key, key)); + lb.setFont(JFaceResources.getBannerFont()); + + // Layout label + gd = new GridData(SWT.LEFT, SWT.CENTER, true, false, 2, 1); + gd.verticalIndent = 5; + lb.setLayoutData(gd); + + return lb; + } + + private Composite createButtonBar() { + // Create bar + Composite bar = new Composite(content, SWT.NONE); + + // Layout bar + bar.setLayout(LayoutFactory.createGridLayout(0, 0, 3, true)); + bar.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false)); + + return bar; + } + + private Button createSaveButton() { + // Create button + Button bt = createButton(buttonBar, "save", SWT.PUSH); + + // Layout button + gd = new GridData(SWT.LEFT, SWT.BOTTOM, true, false); + gd.minimumWidth = 85; + bt.setLayoutData(gd); + + // Done + return bt; } -} + + private Button createNoSaveButton() { + // Create button + Button bt = createButton(buttonBar, "no-save", SWT.PUSH); + + // Layout button + gd = new GridData(SWT.CENTER, SWT.BOTTOM, true, false); + gd.minimumWidth = 85; + bt.setLayoutData(gd); + + // Done + return bt; + } + + private Button createCancelButton() { + // Create button + Button bt = createButton(buttonBar, "cancel", SWT.PUSH); + + // Layout button + gd = new GridData(SWT.RIGHT, SWT.BOTTOM, true, false); + gd.minimumWidth = 85; + bt.setLayoutData(gd); + + // Done + return bt; + } + + private Label createLabel(Composite content, String key) { + // Create label + Label lb = new Label(content, SWT.NONE); + lb.setText(property(key, "text", key)); + + // Layout label + gd = new GridData(SWT.LEFT, SWT.CENTER, true, false); + gd.horizontalIndent = 5; + lb.setLayoutData(gd); + + return lb; + } + + private Button createButton(Composite parent, String key, int style) { + Button bt = new Button(parent, style); + + // Configure button + configure(bt, key); + + // Add listener + bt.addListener(SWT.Selection, adapter); + + // Done + return bt; + } + + protected void handleEvent(Event event) { + switch (event.type) { + case SWT.Close: + handleClose(event); + break; + case SWT.Selection: + handleSelection(event); + break; + } + + } + + private void handleClose(Event event) { + // Prevent the shell from disposing on close + event.doit = false; + close(); + } + + private void handleSelection(Event event) { + if (event.widget == saveButton) { + choice = ResultChoice.SAVE; + close(); + } else if (event.widget == noSaveButton) { + this.choice = ResultChoice.NOSAVE; + close(); + } else if (event.widget == cancelButton) { + this.choice = ResultChoice.CANCEL; + close(); + } + } + + private void configure(Button bt, String key) { + // Read properties + String text = property(key, "text", ""); + String icon = property(key, "icon", null); + String ttip = property(key, "ttip", null); + + // Set values + bt.setText(text); + bt.setImage(image(icon)); + bt.setToolTipText(ttip); + bt.setData(key); + } + + private T control(Class clazz, String key) { + return clazz.cast(controls.get(key)); + } + + private String property(String key, String def) { + Properties p = window.getProperties(); + return p.getProperty(String.format("SaveReminderDialog.%s", key), def); + } + + private String property(String key, String type, String def) { + Properties p = window.getProperties(); + String prop = String.format("SaveReminderDialog.%s.%s", key, type); + return p.getProperty(prop, def); + } + + private Image image(String url) { + return (url != null) ? window.getIcon(url) : null; + } + + private final Listener adapter = new Listener() { + public void handleEvent(Event event) { + SaveReminderDialog.this.handleEvent(event); + } + }; +} \ No newline at end of file -- 2.34.1