Issue #3053 - updated list selection dialog to be more configurable and fixed subset action.

Change-Id: Ib65472679cf5c04f8c11b8fe3fe552058d9e6986

Former-commit-id: 765d2e80bc [formerly 4554f647e0f6de78f5ade64e1371fc8d9ba6a65c]
Former-commit-id: 331dc3397f
This commit is contained in:
Lee Venable 2014-04-22 15:40:43 -05:00
parent a93aa30709
commit 41cd3242f5
2 changed files with 235 additions and 44 deletions

View file

@ -22,6 +22,8 @@ package com.raytheon.uf.viz.datadelivery.actions;
import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException; 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.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI; 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. * Sep 04, 2013 2314 mpduff LoadSave dialog now non-blocking.
* Oct 11, 2013 2386 mpduff Refactor DD Front end. * Oct 11, 2013 2386 mpduff Refactor DD Front end.
* Apr 10, 2014 2864 mpduff Changed how saved subset files are stored. * 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.
* *
* </pre> * </pre>
* *
@ -97,13 +103,24 @@ public class SubsetAction extends AbstractHandler {
final Shell shell = PlatformUI.getWorkbench() final Shell shell = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell(); .getActiveWorkbenchWindow().getShell();
if (selectionDlg == null || selectionDlg.isDisposed()) { 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() { selectionDlg.setCloseCallback(new ICloseCallback() {
@Override @Override
public void dialogClosed(Object returnValue) { 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[]) { if (returnValue instanceof String[]) {
String[] selection = (String[]) returnValue; String[] selection = (String[]) returnValue;
if (selection.length == 1) {
if (selection.length == 0) {
return;
} else if (selection.length == 1) {
String[] parts = selection[0].split(":"); String[] parts = selection[0].split(":");
SubsetFileManager sfm = SubsetFileManager SubsetFileManager sfm = SubsetFileManager
@ -127,14 +144,21 @@ public class SubsetAction extends AbstractHandler {
dlg.bringToTop(); dlg.bringToTop();
} }
} else { } else {
throw new IllegalArgumentException( /*
"Expected 1 item, received " * This is just a safety check in case the
+ selection.length * dialog is configured incorrectly.
+ " items."); */
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 { } else {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Invalid return type from ListSelectionDlg"); "Invalid return type from ListSelectionDlg: "
+ returnValue.getClass());
} }
} }
}); });

View file

@ -20,16 +20,19 @@
package com.raytheon.viz.ui.dialogs; package com.raytheon.viz.ui.dialogs;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout; import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
/** /**
@ -42,6 +45,7 @@ import org.eclipse.swt.widgets.Shell;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* Apr 09, 2014 2864 mpduff Initial creation * Apr 09, 2014 2864 mpduff Initial creation
* Apr 22, 2014 3053 lvenable Updated to be more configurable.
* *
* </pre> * </pre>
* *
@ -59,40 +63,143 @@ public class ListSelectionDlg extends CaveSWTDialog {
/** The list widget */ /** The list widget */
private List selectList; 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. * Constructor.
* *
* @param parent * @param parent
* Parent shell * Parent shell.
* @param textChoices * @param textChoices
* Items for the list * Array of items to be put in the list.
* @param singleSelect * @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, public ListSelectionDlg(Shell parent, String[] textChoices,
boolean singleSelect) { boolean singleSelect, ReturnArray returnAs, String actionButtonText) {
super(parent, SWT.TITLE | SWT.RESIZE | SWT.APPLICATION_MODAL, this(parent, textChoices, singleSelect, returnAs, actionButtonText,
CAVE.DO_NOT_BLOCK); null, null);
this.textChoices = textChoices;
this.singleSelect = singleSelect;
if (singleSelect) {
setText("Select One");
} else {
setText("Select Items");
}
} }
/** /**
* Single select list. * Constructor.
* *
* @param parent * @param parent
* Parent shell * Parent shell.
* @param textChoices * @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) { public ListSelectionDlg(Shell parent, String[] textChoices,
this(parent, textChoices, true); 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 @Override
@ -117,6 +224,9 @@ public class ListSelectionDlg extends CaveSWTDialog {
mainComp.setLayout(new GridLayout(1, false)); mainComp.setLayout(new GridLayout(1, false));
mainComp.setLayoutData(gd); mainComp.setLayoutData(gd);
Label listLbl = new Label(mainComp, SWT.NONE);
listLbl.setText(listLblText);
int style = SWT.SINGLE; int style = SWT.SINGLE;
if (!this.singleSelect) { if (!this.singleSelect) {
style = SWT.MULTI; style = SWT.MULTI;
@ -127,18 +237,7 @@ public class ListSelectionDlg extends CaveSWTDialog {
gd = new GridData(SWT.FILL, SWT.FILL, true, true); gd = new GridData(SWT.FILL, SWT.FILL, true, true);
selectList = new List(mainComp, SWT.BORDER | style); selectList = new List(mainComp, SWT.BORDER | style);
selectList.setLayoutData(gd); selectList.setLayoutData(gd);
selectList.addMouseListener(new MouseListener() { selectList.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
// No op
}
@Override
public void mouseDown(MouseEvent e) {
// No op
}
@Override @Override
public void mouseDoubleClick(MouseEvent e) { public void mouseDoubleClick(MouseEvent e) {
action(); action();
@ -150,9 +249,12 @@ public class ListSelectionDlg extends CaveSWTDialog {
btnComp.setLayout(new GridLayout(2, false)); btnComp.setLayout(new GridLayout(2, false));
btnComp.setLayoutData(gd); btnComp.setLayoutData(gd);
int buttonWidth = 0;
Button selectBtn = new Button(btnComp, SWT.PUSH); Button selectBtn = new Button(btnComp, SWT.PUSH);
selectBtn.setLayoutData(new GridData(75, SWT.DEFAULT)); buttonWidth = calculateButtonWidth(selectBtn, actionButtonText);
selectBtn.setText("Select"); selectBtn.setLayoutData(new GridData(buttonWidth, SWT.DEFAULT));
selectBtn.setText(actionButtonText);
selectBtn.addSelectionListener(new SelectionAdapter() { selectBtn.addSelectionListener(new SelectionAdapter() {
@Override @Override
public void widgetSelected(SelectionEvent e) { public void widgetSelected(SelectionEvent e) {
@ -161,7 +263,7 @@ public class ListSelectionDlg extends CaveSWTDialog {
}); });
Button cancelBtn = new Button(btnComp, SWT.PUSH); 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.setText("Cancel");
cancelBtn.addSelectionListener(new SelectionAdapter() { cancelBtn.addSelectionListener(new SelectionAdapter() {
@Override @Override
@ -171,14 +273,79 @@ public class ListSelectionDlg extends CaveSWTDialog {
}); });
selectList.setItems(textChoices); 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. * Action handler.
*/ */
private void action() { private void action() {
int choice = displayConfirmationBox();
if (choice == SWT.CANCEL) {
return;
}
if (returnAsArray == ReturnArray.ARRAY_STRING_ITEMS) {
setReturnValue(selectList.getSelection()); setReturnValue(selectList.getSelection());
} else if (returnAsArray == ReturnArray.ARRAY_INDEXES) {
setReturnValue(selectList.getSelectionIndices());
} else {
setReturnValue(null);
}
close(); 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;
}
} }