/** * Adds a popup menu. * * @param label the label * @param items the menu items * @param defaultItem the menu item initially selected */ public void addChoice(String label, String[] items, String defaultItem) { String label2 = label; if (label2.indexOf('_') != -1) label2 = label2.replace('_', ' '); Label theLabel = makeLabel(label2); c.gridx = 0; c.gridy = y; c.anchor = GridBagConstraints.EAST; c.gridwidth = 1; if (choice == null) { choice = new Vector(4); c.insets = getInsets(5, 0, 5, 0); } else c.insets = getInsets(0, 0, 5, 0); grid.setConstraints(theLabel, c); add(theLabel); Choice thisChoice = new Choice(); thisChoice.addKeyListener(this); thisChoice.addItemListener(this); for (int i = 0; i < items.length; i++) thisChoice.addItem(items[i]); thisChoice.select(defaultItem); c.gridx = 1; c.gridy = y; c.anchor = GridBagConstraints.WEST; grid.setConstraints(thisChoice, c); add(thisChoice); choice.addElement(thisChoice); if (Recorder.record || macro) saveLabel(thisChoice, label); y++; }
/** Returns the index of the selected item in the next popup menu. */ public int getNextChoiceIndex() { if (choice == null) return -1; Choice thisChoice = (Choice) (choice.elementAt(choiceIndex)); int index = thisChoice.getSelectedIndex(); if (macro) { String label = (String) labels.get((Object) thisChoice); String oldItem = thisChoice.getSelectedItem(); int oldIndex = thisChoice.getSelectedIndex(); String item = Macro.getValue(macroOptions, label, oldItem); if (item != null && item.startsWith("&")) // value is macro variable item = getChoiceVariable(item); thisChoice.select(item); index = thisChoice.getSelectedIndex(); if (index == oldIndex && !item.equals(oldItem)) { // is value a macro variable? Interpreter interp = Interpreter.getInstance(); String s = interp != null ? interp.getStringVariable(item) : null; if (s == null) IJ.error(getTitle(), "\"" + item + "\" is not a valid choice for \"" + label + "\""); else item = s; } } if (recorderOn) { int defaultIndex = ((Integer) (defaultChoiceIndexes.elementAt(choiceIndex))).intValue(); if (!(smartRecording && index == defaultIndex)) { String item = thisChoice.getSelectedItem(); if (!(item.equals("*None*") && getTitle().equals("Merge Channels"))) recordOption(thisChoice, thisChoice.getSelectedItem()); } } choiceIndex++; return index; }
/** Returns the selected item in the next popup menu. */ public String getNextChoice() { if (choice == null) return ""; Choice thisChoice = (Choice) (choice.elementAt(choiceIndex)); String item = thisChoice.getSelectedItem(); if (macro) { String label = (String) labels.get((Object) thisChoice); item = Macro.getValue(macroOptions, label, item); if (item != null && item.startsWith("&")) // value is macro variable item = getChoiceVariable(item); } if (recorderOn) recordOption(thisChoice, item); choiceIndex++; return item; }