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 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; public class SaveReminderDialog extends Dialog { // 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 = "Just close it (CAUTION: unsaved changes will be lost):"; private static final String CANCEL_TEXT = "Do NOT close this file:"; // Dialog buttons private Button saveButton; private Button noSaveButton; private Button cancelButton; public enum ResultChoice { SAVE, NOSAVE, CANCEL } public SaveReminderDialog(Shell shell, AppWindow window) { super(shell); setText(TITLE); this.window = window; } public ResultChoice open() { 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; } /* * overall layout */ private void createUI() { // outer composite content = new Composite(shell, 0); content.setLayout(LayoutFactory.createGridLayout(0, 0, 1, false)); // 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)); 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)); // 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); // Add listeners addListeners(); } 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 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 banner(Composite parent, String text) { // Create label 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 button(Composite parent, String key) { Button bt = new Button(parent, SWT.PUSH); bt.setLayoutData(layoutLower()); configure(bt, key); return bt; } 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 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 GridData layoutUpper() { return new GridData(SWT.LEFT, SWT.CENTER, false, false); } private GridData layoutLower() { return new GridData(SWT.FILL, SWT.CENTER, true, false); } }