diff --git a/cave/com.raytheon.uf.viz.datadelivery/src/com/raytheon/uf/viz/datadelivery/actions/SubsetAction.java b/cave/com.raytheon.uf.viz.datadelivery/src/com/raytheon/uf/viz/datadelivery/actions/SubsetAction.java index 3d31fd424f..9e825bc1e6 100644 --- a/cave/com.raytheon.uf.viz.datadelivery/src/com/raytheon/uf/viz/datadelivery/actions/SubsetAction.java +++ b/cave/com.raytheon.uf.viz.datadelivery/src/com/raytheon/uf/viz/datadelivery/actions/SubsetAction.java @@ -22,6 +22,8 @@ package com.raytheon.uf.viz.datadelivery.actions; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; @@ -61,6 +63,10 @@ import com.raytheon.viz.ui.dialogs.ListSelectionDlg; * Sep 04, 2013 2314 mpduff LoadSave dialog now non-blocking. * Oct 11, 2013 2386 mpduff Refactor DD Front end. * Apr 10, 2014 2864 mpduff Changed how saved subset files are stored. + * Apr 22, 2014 3053 lvenable Updated constructor args for ListSelectionDlg, put + * in a null check when the dialog is canceled, and removed + * throwing an exception that will be handled by a message box + * in the list selection dialog. * * * @@ -97,13 +103,24 @@ public class SubsetAction extends AbstractHandler { final Shell shell = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getShell(); if (selectionDlg == null || selectionDlg.isDisposed()) { - selectionDlg = new ListSelectionDlg(shell, choices); + selectionDlg = new ListSelectionDlg(shell, choices, true, + ListSelectionDlg.ReturnArray.ARRAY_STRING_ITEMS, + "Select"); selectionDlg.setCloseCallback(new ICloseCallback() { @Override public void dialogClosed(Object returnValue) { + // The the return value is null then return since + // the dialog was canceled. + if (returnValue == null) { + return; + } + if (returnValue instanceof String[]) { String[] selection = (String[]) returnValue; - if (selection.length == 1) { + + if (selection.length == 0) { + return; + } else if (selection.length == 1) { String[] parts = selection[0].split(":"); SubsetFileManager sfm = SubsetFileManager @@ -127,14 +144,21 @@ public class SubsetAction extends AbstractHandler { dlg.bringToTop(); } } else { - throw new IllegalArgumentException( - "Expected 1 item, received " - + selection.length - + " items."); + /* + * This is just a safety check in case the + * dialog is configured incorrectly. + */ + MessageBox mb = new MessageBox(shell, + SWT.ICON_WARNING | SWT.OK); + mb.setText("Multiple Items"); + mb.setMessage("Multiple items were selected. Only one item can be processed.\n" + + "You must relaunch the dialog and select one item to process."); + mb.open(); } } else { throw new IllegalArgumentException( - "Invalid return type from ListSelectionDlg"); + "Invalid return type from ListSelectionDlg: " + + returnValue.getClass()); } } }); diff --git a/cave/com.raytheon.viz.ui/src/com/raytheon/viz/ui/dialogs/ListSelectionDlg.java b/cave/com.raytheon.viz.ui/src/com/raytheon/viz/ui/dialogs/ListSelectionDlg.java index 681e93d183..7f8e167813 100644 --- a/cave/com.raytheon.viz.ui/src/com/raytheon/viz/ui/dialogs/ListSelectionDlg.java +++ b/cave/com.raytheon.viz.ui/src/com/raytheon/viz/ui/dialogs/ListSelectionDlg.java @@ -20,16 +20,19 @@ package com.raytheon.viz.ui.dialogs; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; -import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.GC; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Layout; import org.eclipse.swt.widgets.List; +import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; /** @@ -42,6 +45,7 @@ import org.eclipse.swt.widgets.Shell; * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- * Apr 09, 2014 2864 mpduff Initial creation + * Apr 22, 2014 3053 lvenable Updated to be more configurable. * * * @@ -59,40 +63,143 @@ public class ListSelectionDlg extends CaveSWTDialog { /** The list widget */ private List selectList; + /** Label above the list control. */ + private String listLblText; + + /** Default minimum list width. */ + private int defaultMinListWidth = 225; + + /** Default Minimum list height. */ + private int defaultMinListHeight = 275; + + /** Minimum list width. */ + private int minListWidth = defaultMinListWidth; + + /** Minimum list height. */ + private int minListHeight = defaultMinListHeight; + + /** Text to put in the action button. */ + private String actionButtonText; + + /** + * Enumeration to determine how the selection from the list will be + * returned. + */ + public enum ReturnArray { + ARRAY_STRING_ITEMS, ARRAY_INDEXES + }; + + /** Variable to determine how the data is returned. */ + private ReturnArray returnAsArray = ReturnArray.ARRAY_STRING_ITEMS; + /** * Constructor. * * @param parent - * Parent shell + * Parent shell. * @param textChoices - * Items for the list + * Array of items to be put in the list. * @param singleSelect - * true if only a single selection allowed + * True for single selection, false for multiple selection. + * @param returnAs + * Determine how the selection is returned. + * @param actionButtonText + * Text for the action button. */ public ListSelectionDlg(Shell parent, String[] textChoices, - boolean singleSelect) { - super(parent, SWT.TITLE | SWT.RESIZE | SWT.APPLICATION_MODAL, - CAVE.DO_NOT_BLOCK); - this.textChoices = textChoices; - this.singleSelect = singleSelect; - - if (singleSelect) { - setText("Select One"); - } else { - setText("Select Items"); - } + boolean singleSelect, ReturnArray returnAs, String actionButtonText) { + this(parent, textChoices, singleSelect, returnAs, actionButtonText, + null, null); } /** - * Single select list. + * Constructor. * * @param parent - * Parent shell + * Parent shell. * @param textChoices - * Items for the list + * Array of items to be put in the list. + * @param singleSelect + * True for single selection, false for multiple selection. + * @param returnAs + * Determine how the selection is returned. + * @param actionButtonText + * Text for the action button. + * @param title + * Dialog title. + * @param listMsg + * Text displayed in the label above the list control. */ - public ListSelectionDlg(Shell parent, String[] textChoices) { - this(parent, textChoices, true); + public ListSelectionDlg(Shell parent, String[] textChoices, + boolean singleSelect, ReturnArray returnAs, + String actionButtonText, String title, String listMsg) { + this(parent, textChoices, singleSelect, returnAs, actionButtonText, + title, listMsg, 225, 275); + } + + /** + * Constructor. + * + * @param parent + * Parent shell. + * @param textChoices + * Array of items to be put in the list. + * @param singleSelect + * True for single selection, false for multiple selection. + * @param returnAs + * Determine how the selection is returned. + * @param actionButtonText + * Text for the action button. + * @param title + * Dialog title. + * @param listMsg + * Text displayed in the label above the list control. + * @param minWidth + * Minimum list control width (minimum default value is 225). + * @param minHeight + * Minimum list control height (minimum default value is 275). + */ + public ListSelectionDlg(Shell parent, String[] textChoices, + boolean singleSelect, ReturnArray returnAs, + String actionButtonText, String title, String listMsg, + int minWidth, int minHeight) { + super(parent, SWT.TITLE | SWT.RESIZE | SWT.APPLICATION_MODAL, + CAVE.DO_NOT_BLOCK); + + this.textChoices = textChoices; + this.singleSelect = singleSelect; + this.returnAsArray = returnAs; + + // Set the minimum width & height for the list control. + this.minListWidth = (defaultMinListWidth < minWidth ? defaultMinListWidth + : minWidth); + this.minListHeight = (defaultMinListHeight < minHeight ? defaultMinListHeight + : minHeight); + + // Set the action button text. + if (actionButtonText == null) { + this.actionButtonText = "Select"; + } else { + this.actionButtonText = actionButtonText; + } + + // Set the dialog title. + if (title == null) { + setText("Selection"); + } else { + setText(title); + } + + // Set the text in the label above the list control. + if (listMsg == null) { + if (singleSelect) { + listLblText = "Select an item:"; + } else { + listLblText = "Select item(s):"; + } + } else { + listLblText = listMsg; + } } @Override @@ -117,6 +224,9 @@ public class ListSelectionDlg extends CaveSWTDialog { mainComp.setLayout(new GridLayout(1, false)); mainComp.setLayoutData(gd); + Label listLbl = new Label(mainComp, SWT.NONE); + listLbl.setText(listLblText); + int style = SWT.SINGLE; if (!this.singleSelect) { style = SWT.MULTI; @@ -127,18 +237,7 @@ public class ListSelectionDlg extends CaveSWTDialog { gd = new GridData(SWT.FILL, SWT.FILL, true, true); selectList = new List(mainComp, SWT.BORDER | style); selectList.setLayoutData(gd); - selectList.addMouseListener(new MouseListener() { - - @Override - public void mouseUp(MouseEvent e) { - // No op - } - - @Override - public void mouseDown(MouseEvent e) { - // No op - } - + selectList.addMouseListener(new MouseAdapter() { @Override public void mouseDoubleClick(MouseEvent e) { action(); @@ -150,9 +249,12 @@ public class ListSelectionDlg extends CaveSWTDialog { btnComp.setLayout(new GridLayout(2, false)); btnComp.setLayoutData(gd); + int buttonWidth = 0; + Button selectBtn = new Button(btnComp, SWT.PUSH); - selectBtn.setLayoutData(new GridData(75, SWT.DEFAULT)); - selectBtn.setText("Select"); + buttonWidth = calculateButtonWidth(selectBtn, actionButtonText); + selectBtn.setLayoutData(new GridData(buttonWidth, SWT.DEFAULT)); + selectBtn.setText(actionButtonText); selectBtn.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { @@ -161,7 +263,7 @@ public class ListSelectionDlg extends CaveSWTDialog { }); Button cancelBtn = new Button(btnComp, SWT.PUSH); - cancelBtn.setLayoutData(new GridData(75, SWT.DEFAULT)); + cancelBtn.setLayoutData(new GridData(buttonWidth, SWT.DEFAULT)); cancelBtn.setText("Cancel"); cancelBtn.addSelectionListener(new SelectionAdapter() { @Override @@ -171,14 +273,79 @@ public class ListSelectionDlg extends CaveSWTDialog { }); selectList.setItems(textChoices); - this.shell.setMinimumSize(225, 275); + this.shell.setMinimumSize(minListWidth, minListHeight); + } + + /** + * Calculate the button width based on the text to be displayed. + * + * @param btn + * Button control. + * @param text + * Text to be displayed. + * @return The calculated button width. + */ + private int calculateButtonWidth(Button btn, String text) { + int rv = 0; + int extentLength = 0; + int defaultButtonWidth = 75; + int textBufferWidth = 15; + + // Get the length of the text in pixels that will be displayed in the + // button. + GC gc = new GC(btn); + extentLength = gc.stringExtent(text).x; + rv = (defaultButtonWidth > extentLength) ? defaultButtonWidth + : extentLength; + gc.dispose(); + + /* + * Return the lenght of the text and the added buffer that accounts for + * the button edges. + */ + return rv + textBufferWidth; } /** * Action handler. */ private void action() { - setReturnValue(selectList.getSelection()); + + int choice = displayConfirmationBox(); + if (choice == SWT.CANCEL) { + return; + } + + if (returnAsArray == ReturnArray.ARRAY_STRING_ITEMS) { + setReturnValue(selectList.getSelection()); + } else if (returnAsArray == ReturnArray.ARRAY_INDEXES) { + setReturnValue(selectList.getSelectionIndices()); + } else { + setReturnValue(null); + } + close(); } + + /** + * Display a confirmation dialog to the user. + * + * @return SWT.OK or SWT.CANCEL + */ + private int displayConfirmationBox() { + String itemTxt = null; + + if (singleSelect) { + itemTxt = "No item is selected."; + } else { + itemTxt = "No items are selected."; + } + + MessageBox mb = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK + | SWT.CANCEL); + mb.setText("Confirmation"); + mb.setMessage(itemTxt + " Do you wish to continue?"); + int val = mb.open(); + return val; + } }