From 5a08963e95046cea7dfcf7e9b857ba9c502bba65 Mon Sep 17 00:00:00 2001 From: preecej Date: Thu, 4 Oct 2012 19:38:32 +0000 Subject: [PATCH] Close and Save Reminder f(x) implemented. svn path=/; revision=388 --- .../ie/dcu/apps/ist/actions/CloseAction.java | 13 +- .../apps/ist/dialogs/SaveReminderDialog.java | 338 +++++++----------- 2 files changed, 143 insertions(+), 208 deletions(-) diff --git a/Annotation/src/ie/dcu/apps/ist/actions/CloseAction.java b/Annotation/src/ie/dcu/apps/ist/actions/CloseAction.java index 5b749d1..920e1b8 100644 --- a/Annotation/src/ie/dcu/apps/ist/actions/CloseAction.java +++ b/Annotation/src/ie/dcu/apps/ist/actions/CloseAction.java @@ -2,12 +2,7 @@ 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 @@ -25,13 +20,13 @@ public class CloseAction extends AppAction { // TODO: add a call to check the confirm close app preference // call Save Reminder dialog - SaveReminderDialog dialog = new SaveReminderDialog(window); + SaveReminderDialog dialog = new SaveReminderDialog(window.getShell(),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") + case SAVE: // save now and then close file // trigger Save dialog to save the file ActionManager actions = window.getActions(); if (actions != null) { @@ -39,11 +34,11 @@ public class CloseAction extends AppAction { saveAction.runSaveAndClose(); } break; - case NOSAVE: // do not save and then close file ("I've already saved my work") + case NOSAVE: // do not save and then close file window.getView().resetView(); // clear current data and work status("Closed"); break; - case CANCEL: // cancel the close ("Do not close this file") + case CANCEL: // cancel the close status("Close cancelled."); break; } diff --git a/Annotation/src/ie/dcu/apps/ist/dialogs/SaveReminderDialog.java b/Annotation/src/ie/dcu/apps/ist/dialogs/SaveReminderDialog.java index ea2252b..e7e55ae 100644 --- a/Annotation/src/ie/dcu/apps/ist/dialogs/SaveReminderDialog.java +++ b/Annotation/src/ie/dcu/apps/ist/dialogs/SaveReminderDialog.java @@ -1,221 +1,167 @@ package ie.dcu.apps.ist.dialogs; +import java.util.Properties; + +import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Dialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Listener; 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 org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.layout.*; +import org.eclipse.jface.resource.JFaceResources; +import ie.dcu.apps.ist.AppWindow; 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 extends Dialog { -public class SaveReminderDialog { + // Result + private ResultChoice result; + + // Top level components + private AppWindow window; + private Shell shell; + private Composite content; + private Composite upper; + private Composite lower; + + // Text + private static final String TITLE = "Save Reminder"; 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 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:"; + + // Dialog buttons + private Button saveButton; + private Button noSaveButton; + private Button cancelButton; - private transient GridData gd; - - public SaveReminderDialog(AppWindow window) { - this.window = window; - this.shell = createShell(); - this.content = createContent(); - - createControls(); - - 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 } + + public SaveReminderDialog(Shell shell, AppWindow window) { + super(shell); + setText(TITLE); + this.window = window; + } public ResultChoice open() { - 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; + Shell parent = getParent(); + shell = new Shell(parent, SWT.DIALOG_TRIM | + SWT.APPLICATION_MODAL | SWT.SHEET); + shell.setText(getText()); + shell.setLayout(new FillLayout()); + + createUI(); + + shell.pack(); + SwtUtils.center(parent, shell); + shell.open(); + Display display = parent.getDisplay(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + return result; } - public void close() { - shell.setVisible(false); - } + /* + * overall layout + */ + private void createUI() { + // outer composite + content = new Composite(shell, 0); + content.setLayout(LayoutFactory.createGridLayout(0, 0, 1, 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 - } + // upper composite + upper = new Composite(content, 0); + upper.setLayout(LayoutFactory.createGridLayout(10, 5, 1, false)); + upper.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); - 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); + hline(content); + + // lower composite + lower = new Composite(content, 0); + lower.setLayout(LayoutFactory.createGridLayout(10, 5, 2, false)); + lower.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); - // Layout button - gd = new GridData(SWT.LEFT, SWT.BOTTOM, true, false); - gd.minimumWidth = 85; - bt.setLayoutData(gd); + // add inner content + banner(upper, QUESTION); + + label(lower, SAVE_TEXT); + saveButton = button(lower,"save"); + + label(lower, NOSAVE_TEXT); + noSaveButton = button(lower,"no-save"); + + label(lower, CANCEL_TEXT); + cancelButton = button(lower,"cancel"); + + // Set the default button + shell.setDefaultButton(saveButton); - // Done - return bt; + // Add listeners + addListeners(); } - - 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 void addListeners() { + saveButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + result = ResultChoice.SAVE; + shell.dispose(); + } + }); + + noSaveButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + result = ResultChoice.NOSAVE; + shell.dispose(); + } + }); + + cancelButton.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + result = ResultChoice.CANCEL; + shell.dispose(); + } + }); } - 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 hline(Composite parent) { + Label label = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL); + GridData data = new GridData(SWT.FILL, SWT.CENTER, false, false); + data.horizontalSpan = 2; + data.heightHint = 10; + label.setLayoutData(data); + return label; } - private Label createLabel(Composite content, String key) { + private Label banner(Composite parent, String text) { // 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); - + Label lb = new Label(parent, SWT.NONE); + lb.setText(text); + lb.setFont(JFaceResources.getBannerFont()); return lb; } + + private Label label(Composite parent, String text) { + Label label = new Label(parent, SWT.NONE); + label.setText(text); + label.setLayoutData(layoutUpper()); + return label; + } - private Button createButton(Composite parent, String key, int style) { - Button bt = new Button(parent, style); - - // Configure button + private Button button(Composite parent, String key) { + Button bt = new Button(parent, SWT.PUSH); + bt.setLayoutData(layoutLower()); 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) { @@ -230,29 +176,23 @@ public class SaveReminderDialog { 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); - } - }; + private GridData layoutUpper() { + return new GridData(SWT.LEFT, SWT.CENTER, false, false); + } + + private GridData layoutLower() { + return new GridData(SWT.FILL, SWT.CENTER, true, false); + } + } \ No newline at end of file -- 2.34.1