Commits

Anonymous committed bd3ed6b

Enumerated variables now use dialog for text entry rather than a combo box

Comments (0)

Files changed (3)

src/main/java/org/nrg/upload/data/EnumeratedSessionVariable.java

 
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
+import java.util.Arrays;
 import java.util.Collection;
 
 import javax.swing.JComboBox;
+import javax.swing.JOptionPane;
 
 
 /**
  */
 public final class EnumeratedSessionVariable
 extends AbstractSessionVariable implements SessionVariable,ItemListener {
+	private static final String MAKE_SELECTION = "(Select)";
+	private static final String OTHER_ITEM = "Other...";
+	private static final Collection<?> UNSELECTABLES = Arrays.asList(MAKE_SELECTION, OTHER_ITEM);
 	private final JComboBox comboBox;
+	private Object lastDeselected = null;
 	
 	/**
 	 * @param name
 	 */
-	public EnumeratedSessionVariable(final String name, final Collection<?> items, boolean isEditable) {
+	public EnumeratedSessionVariable(final String name, final Collection<?> items,
+			boolean requireSelection, boolean allowOther) {
 		super(name);
-		comboBox = new JComboBox(items.toArray());
-		comboBox.setEditable(isEditable);
+		int size = items.size();
+		if (requireSelection) {
+			size++;
+		}
+		if (allowOther) {
+			size++;
+		}
+		final Object[] startItems = new Object[size];
+		int i = 0;
+		if (allowOther) {
+			startItems[i++] = MAKE_SELECTION;
+		}
+		for (final Object o : items) {
+			startItems[i++] = o;
+		}
+		startItems[i++] = OTHER_ITEM;
+		comboBox = new JComboBox(startItems);
+		comboBox.setEditable(false);
 		comboBox.addItemListener(this);
 	}
 
 	 * @see org.nrg.upload.data.SessionVariable#getValue()
 	 */
 	public String getValue() {
-		return comboBox.getSelectedItem().toString();
+		final Object selectedItem = comboBox.getSelectedItem();
+		return MAKE_SELECTION == selectedItem ? null : selectedItem.toString();
 	}
 
 	/* (non-Javadoc)
 	 * @see org.nrg.upload.data.SessionVariable#getValueMessage()
 	 */
-	public String getValueMessage() { return null; }
+	public String getValueMessage() {
+		final Object selectedItem = comboBox.getSelectedItem();
+		if (MAKE_SELECTION == selectedItem) {
+			return "Select a value for " + getName();
+		} else {
+			final String v = comboBox.getSelectedItem().toString();
+			if (null == v || "".equals(v)) {
+				return "Select a value for " + getName();
+			} else {
+				return null;
+			}
+		}
+	}
 
 	/*
 	 * (non-Javadoc)
 	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
 	 */
 	public void itemStateChanged(final ItemEvent e) {
-		if (ItemEvent.SELECTED == e.getStateChange()) {
-			fireHasChanged();
+		if (MAKE_SELECTION == comboBox.getItemAt(0)) {
+			comboBox.removeItem(MAKE_SELECTION);
+		}
+		if (ItemEvent.DESELECTED == e.getStateChange()) {
+			lastDeselected = e.getItem();
+			if (UNSELECTABLES.contains(lastDeselected)) {
+				lastDeselected = null;
+			}
+		} else if (ItemEvent.SELECTED == e.getStateChange()) {
+			if (OTHER_ITEM == e.getItem()) {
+				final String val = (String)JOptionPane.showInputDialog("Enter a value for " + getName() + ":");
+				if (null == val) {
+					if (null == lastDeselected) {
+						comboBox.setSelectedIndex(0);
+					} else {
+						comboBox.setSelectedItem(lastDeselected);
+					}
+				} else {
+					if (null == findItem(comboBox, val)) {
+						comboBox.removeItem(OTHER_ITEM);
+						comboBox.addItem(val);
+						comboBox.addItem(OTHER_ITEM);
+					}
+					comboBox.setSelectedItem(val);
+				}
+			} else {
+				fireHasChanged();
+			}
 		}
 	}
 	

src/main/java/org/nrg/upload/ui/AssignSessionVariablesPage.java

 import java.awt.GridBagLayout;
 import java.awt.Insets;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 		final SessionVariable tracer;
 		final Set<String> modalities = sess.getModalities();
 		if (modalities.contains("PET") || modalities.contains("PT")) {
-			tracer = new XnatWrappedVariable(new EnumeratedSessionVariable(TRACER,
-					Arrays.asList("PIB", "FDG"), true), TRACER_PATH);
+			final Collection<String> tracers = Arrays.asList("PIB", "FDG");
+			tracer = new XnatWrappedVariable(new EnumeratedSessionVariable(TRACER, tracers, true, true),
+					TRACER_PATH);
 			final SessionVariable bloodGlucose = new MutableXnatSessionVariable(BLOOD_GLUCOSE_PATH, BLOOD_GLUCOSE, "");
 			vs.add(bloodGlucose);
 			vs.add(new XnatWrappedVariable(new AssignedSessionVariable(BLOOD_GLUCOSE_UNITS, BLOOD_GLUCOSE_UNITS_VAL, true),

src/main/java/org/nrg/upload/ui/VariableAssignmentManager.java

 		}
 		
 		public void hasChanged(final SessionVariable v) {
-			assert this.v == v;
+			assert this.v.getName().equals(v.getName());
 			this.message.setText(null);
 			this.message.setToolTipText(null);
 			final Container parent = this.message.getParent();