package ie.dcu.apps.ist.dialogs; import ie.dcu.apps.ist.export.imagemap.AreaShape; import ie.dcu.apps.ist.export.imagemap.RolloverEffect; import ie.dcu.swt.SwtUtils; import ie.dcu.swt.layout.LayoutFactory; import java.io.File; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.DirectoryDialog; 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.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class ExportDialog extends Dialog { public class Result { public final File folder; public final File exportFolder; public final String html; public final String zipFile; public final String image; //public final String link; //public final String description; public final AreaShape shape; public final RolloverEffect effect; public final boolean open; Result() { folder = new File(folderText.getText().trim()); //html = htmlText.getText().trim(); zipFile = zipFileName.getText().trim(); html = zipFile+".html"; image = "image.png"; // JP - URL specification disabled until it can be applied to individual segments //link = linkText.getText().trim(); //description = descriptionText.getText(); //shape = getShape(); exportFolder = new File(exportFolderText.getText().trim()); shape = AreaShape.valueOf("Polygon"); // JP - only one option for now effect = getEffect(); open = openButton.getSelection(); } }; // Result private Result result; // Top level components private Shell shell; private Composite content; private Composite widgets; private Composite buttons; // Widgets private Text folderText; private Text exportFolderText; private Button browseButton; private Button exportBrowseButton; private Text htmlText; private Text zipFileName; private Text imageText; private Text linkText; //private Button effectCheck; private Combo effectCombo; private Button[] shapeRadios; private Text descriptionText; private Button openButton; // Dialog buttons private Button cancelButton; private Button exportButton; // Effects private final String[] ROLLOVER_EFFECTS = { "Darken Background", "Outline Object", "Highlight Object" }; // Shapes private final String[] SHAPES = { "Polygon", "Rectangle", "Circle" }; // Default title private static final String TITLE = "Save Annotation"; public ExportDialog(Shell shell) { super(shell); setText(TITLE); } public ExportDialog(Shell shell, int style) { super(shell, style); setText(TITLE); } public Result 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; } private void createUI() { // Overall layout content = new Composite(shell, 0); content.setLayout(LayoutFactory.createGridLayout(0, 0, 1, false)); widgets = new Composite(content, 0); widgets.setLayout(LayoutFactory.createGridLayout(10, 5, 3, false)); widgets.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); hline(content); buttons = new Composite(content, 0); buttons.setLayout(LayoutFactory.createGridLayout(10, 5, 3, false)); buttons.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); // Form content label(widgets, "Select folder:"); folderText = text(widgets, ""); browseButton = button(widgets, "Browse..."); label(widgets, "Name your .zip file:"); zipFileName = text(widgets, ""); label(widgets, ".zip"); //spacer(widgets); /*label(widgets, "File name:"); imageText = text(widgets, "image"); spacer(widgets);*/ hline(widgets); /* TODO: enhance this later to work for multiple segments label(widgets, "Export shape:"); shapeRadios = radios(widgets, SHAPES); // TODO: Enable when available shapeRadios[2].setEnabled(false); spacer(widgets); label(widgets, "Object link:"); linkText = text(widgets, "http://"); spacer(widgets); label(widgets, "Object description:"); descriptionText = text(widgets, ""); spacer(widgets); */ // HTML image map options openButton = checkbox(widgets, "Create HTML image map and open in browser", false, 3); // TODO: cascade the following controls to disabled on the value of openButton label(widgets, "Select export folder:"); exportFolderText = text(widgets, ""); exportBrowseButton = button(widgets, "Browse..."); //effectCheck = checkbox(widgets, "Rollover effect:", true, 1); spacer(widgets); effectCombo = combo(widgets, ROLLOVER_EFFECTS); spacer(widgets); // defaults exportFolderText.setEnabled(false); exportBrowseButton.setEnabled(false); effectCombo.setEnabled(false); //hline(widgets); // Button bar expander(buttons); cancelButton = new Button(buttons, SWT.PUSH); cancelButton.setLayoutData(layout4()); cancelButton.setText("Cancel"); exportButton = new Button(buttons, SWT.PUSH); exportButton.setLayoutData(layout4()); exportButton.setText("Save"); // Set the default button shell.setDefaultButton(exportButton); // Add listeners addListeners(); } private void addListeners() { browseButton.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { String dir = getDirectory(); if (dir != null) { folderText.setText(dir); } } }); exportBrowseButton.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { String dir = getDirectory(); if (dir != null) { exportFolderText.setText(dir); } } }); cancelButton.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { result = null; shell.dispose(); } }); exportButton.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { if (validate()) { result = new Result(); shell.dispose(); } } }); openButton.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { exportFolderText.setEnabled(openButton.getSelection()); exportBrowseButton.setEnabled(openButton.getSelection()); effectCombo.setEnabled(openButton.getSelection()); } }); /* effectCheck.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { effectCombo.setEnabled(effectCheck.getSelection()); } }); */ } private boolean validate() { File folder = new File(folderText.getText()); if (!folder.isDirectory()) { validationError("The save folder must be specified"); folderText.setFocus(); return false; } String zipFile = zipFileName.getText().trim(); if (zipFile.equals("")) { validationError("The HTML file name cannot be empty"); zipFileName.setFocus(); return false; } /*String imageFile = imageText.getText().trim(); if (imageFile.equals("")) { validationError("The image file name cannot be empty"); imageText.setFocus(); return false; }*/ return true; } private void validationError(String message) { MessageBox box = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION); box.setMessage(message); box.setText("Invalid Input"); box.open(); } private AreaShape getShape() { for (Button bt : shapeRadios) { if (bt.getSelection()) { return AreaShape.valueOf(bt.getText()); } } return null; } private RolloverEffect getEffect() { //RolloverEffect effect = null; RolloverEffect effect = RolloverEffect.darkenBackground(); // default //if (effectCheck.getSelection()) { String text = effectCombo.getText(); if (text.equals("Outline Object")) { effect = RolloverEffect.outlineObject(); } else if (text.equals("Darken Background")) { effect = RolloverEffect.darkenBackground(); } else if (text.equals("Highlight Object")) { effect = RolloverEffect.brightenForeground(); } //} return effect; } private String getDirectory() { DirectoryDialog dialog = new DirectoryDialog(shell); return dialog.open(); } private Button[] radios(Composite parent, String ... items) { Composite c = new Composite(parent, SWT.NONE); c.setLayout(LayoutFactory.createGridLayout(5, 10, items.length, false)); c.setLayoutData(layout2()); Button[] bts = new Button[items.length]; for (int i = 0; i < bts.length; i++) { bts[i] = new Button(c, SWT.RADIO); bts[i].setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); bts[i].setText(items[i]); bts[i].setSelection(i == 0); } return bts; } 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 = 3; data.heightHint = 10; label.setLayoutData(data); return label; } private Label spacer(Composite parent) { Label label = new Label(parent, SWT.NONE); label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); return label; } private Label expander(Composite parent) { Label label = new Label(parent, SWT.NONE); label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); return label; } private Combo combo(Composite parent, String ... items) { Combo combo = new Combo(parent, SWT.READ_ONLY); combo.setItems(items); combo.setLayoutData(layout2()); if (items.length > 0) { combo.select(0); } return combo; } private Button checkbox(Composite parent, String text, boolean checked, int span) { Button bt = new Button(parent, SWT.CHECK); bt.setText(text); bt.setSelection(checked); GridData data = layout1(); data.horizontalSpan = span; bt.setLayoutData(data); return bt; } private Label label(Composite parent, String text) { Label label = new Label(parent, SWT.NONE); label.setText(text); label.setLayoutData(layout1()); return label; } private Text text(Composite parent, String value) { Text text = new Text(parent, SWT.BORDER | SWT.SINGLE); text.setText(value); text.setLayoutData(layout2()); return text; } private Button button(Composite parent, String text) { Button bt = new Button(parent, SWT.PUSH); bt.setText(text); bt.setLayoutData(layout3()); return bt; } private GridData layout1() { return new GridData(SWT.LEFT, SWT.CENTER, false, false); } private GridData layout2() { return new GridData(SWT.FILL, SWT.CENTER, true, false); } private GridData layout3() { return new GridData(SWT.RIGHT, SWT.CENTER, false, false); } private GridData layout4() { return new GridData(SWT.RIGHT, SWT.BOTTOM, false, false); } }