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
* @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
}
}
}
-
+
public static boolean confirmClose(AppWindow window) {
boolean confirm = window.getPrefs().get(
Boolean.class, AppPrefs.Keys.CONFIRM_CLOSE, true);
return true;
}
-}
+}
\ No newline at end of file
}
}
+ public void runSaveAndClose() {
+
+ SegmentationContext ctx = window.getContext();
+ List<SegmentationMask> 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);
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<String, Object> 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<String, Object>();
+
+ 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> T control(Class<T> 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