/** * Adds a radio button group. * * @param label group label (or null) * @param items radio button labels * @param rows number of rows * @param columns number of columns * @param defaultItem button initially selected */ public void addRadioButtonGroup( String label, String[] items, int rows, int columns, String defaultItem) { Panel panel = new Panel(); int n = items.length; panel.setLayout(new GridLayout(rows, columns, 0, 0)); CheckboxGroup cg = new CheckboxGroup(); for (int i = 0; i < n; i++) { Checkbox cb = new Checkbox(items[i], cg, items[i].equals(defaultItem)); cb.addItemListener(this); panel.add(cb); } if (radioButtonGroups == null) radioButtonGroups = new Vector(); radioButtonGroups.addElement(cg); Insets insets = getInsets(5, 10, 0, 0); if (label == null || label.equals("")) { label = "rbg" + radioButtonGroups.size(); insets.top += 5; } else { setInsets(10, insets.left, 0); addMessage(label); insets.top = 2; insets.left += 10; } c.gridx = 0; c.gridy = y; c.gridwidth = 2; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(insets.top, insets.left, 0, 0); grid.setConstraints(panel, c); add(panel); if (Recorder.record || macro) saveLabel(cg, label); y++; }
/** * Adds a group of checkboxs using a grid layout. * * @param rows the number of rows * @param columns the number of columns * @param labels the labels * @param defaultValues the initial states * @param headings the column headings Example: * http://imagej.nih.gov/ij/plugins/multi-column-dialog/index.html */ public void addCheckboxGroup( int rows, int columns, String[] labels, boolean[] defaultValues, String[] headings) { Panel panel = new Panel(); int nRows = headings != null ? rows + 1 : rows; panel.setLayout(new GridLayout(nRows, columns, 6, 0)); int startCBIndex = cbIndex; if (checkbox == null) checkbox = new Vector(12); if (headings != null) { Font font = new Font("SansSerif", Font.BOLD, 12); for (int i = 0; i < columns; i++) { if (i > headings.length - 1 || headings[i] == null) panel.add(new Label("")); else { Label label = new Label(headings[i]); label.setFont(font); panel.add(label); } } } int i1 = 0; int[] index = new int[labels.length]; for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { int i2 = col * rows + row; if (i2 >= labels.length) break; index[i1] = i2; String label = labels[i1]; if (label == null || label.length() == 0) { Label lbl = new Label(""); panel.add(lbl); i1++; continue; } if (label.indexOf('_') != -1) label = label.replace('_', ' '); Checkbox cb = new Checkbox(label); checkbox.addElement(cb); cb.setState(defaultValues[i1]); cb.addItemListener(this); if (Recorder.record || macro) saveLabel(cb, labels[i1]); if (IJ.isLinux()) { Panel panel2 = new Panel(); panel2.setLayout(new BorderLayout()); panel2.add("West", cb); panel.add(panel2); } else panel.add(cb); i1++; } } c.gridx = 0; c.gridy = y; c.gridwidth = 2; c.anchor = GridBagConstraints.WEST; c.insets = getInsets(10, 0, 0, 0); grid.setConstraints(panel, c); add(panel); y++; }
/** * Adds a Panel to the dialog with custom contraint and insets. The defaults are * GridBagConstraints.WEST (left justified) and "new Insets(5, 0, 0, 0)" (5 pixels of padding at * the top). */ public void addPanel(Panel panel, int constraints, Insets insets) { c.gridx = 0; c.gridy = y; c.gridwidth = 2; c.anchor = constraints; c.insets = insets; grid.setConstraints(panel, c); add(panel); y++; }
/** * 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); defaultChoiceIndexes = 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]); if (defaultItem != null) thisChoice.select(defaultItem); else thisChoice.select(0); c.gridx = 1; c.gridy = y; c.anchor = GridBagConstraints.WEST; grid.setConstraints(thisChoice, c); add(thisChoice); choice.addElement(thisChoice); int index = thisChoice.getSelectedIndex(); defaultChoiceIndexes.addElement(new Integer(index)); if (Recorder.record || macro) saveLabel(thisChoice, label); y++; }
/** * Adds a numeric field. The first word of the label must be unique or command recording will not * work. * * @param label the label * @param defaultValue value to be initially displayed * @param digits number of digits to right of decimal point * @param columns width of field in characters * @param units a string displayed to the right of the field */ public void addNumericField( String label, double defaultValue, int digits, int columns, String units) { 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 (firstNumericField) c.insets = getInsets(5, 0, 3, 0); else c.insets = getInsets(0, 0, 3, 0); grid.setConstraints(theLabel, c); add(theLabel); if (numberField == null) { numberField = new Vector(5); defaultValues = new Vector(5); defaultText = new Vector(5); } if (IJ.isWindows()) columns -= 2; if (columns < 1) columns = 1; String defaultString = IJ.d2s(defaultValue, digits); if (Double.isNaN(defaultValue)) defaultString = ""; TextField tf = new TextField(defaultString, columns); if (IJ.isLinux()) tf.setBackground(Color.white); tf.addActionListener(this); tf.addTextListener(this); tf.addFocusListener(this); tf.addKeyListener(this); numberField.addElement(tf); defaultValues.addElement(new Double(defaultValue)); defaultText.addElement(tf.getText()); c.gridx = 1; c.gridy = y; c.anchor = GridBagConstraints.WEST; tf.setEditable(true); // if (firstNumericField) tf.selectAll(); firstNumericField = false; if (units == null || units.equals("")) { grid.setConstraints(tf, c); add(tf); } else { Panel panel = new Panel(); panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); panel.add(tf); panel.add(new Label(" " + units)); grid.setConstraints(panel, c); add(panel); } if (Recorder.record || macro) saveLabel(tf, label); y++; }
/** * Adds one or two (side by side) text areas. * * @param text1 initial contents of the first text area * @param text2 initial contents of the second text area or null * @param rows the number of rows * @param columns the number of columns */ public void addTextAreas(String text1, String text2, int rows, int columns) { if (textArea1 != null) return; Panel panel = new Panel(); textArea1 = new TextArea(text1, rows, columns, TextArea.SCROLLBARS_NONE); if (IJ.isLinux()) textArea1.setBackground(Color.white); textArea1.addTextListener(this); panel.add(textArea1); if (text2 != null) { textArea2 = new TextArea(text2, rows, columns, TextArea.SCROLLBARS_NONE); if (IJ.isLinux()) textArea2.setBackground(Color.white); panel.add(textArea2); } c.gridx = 0; c.gridy = y; c.gridwidth = 2; c.anchor = GridBagConstraints.WEST; c.insets = getInsets(15, 20, 0, 0); grid.setConstraints(panel, c); add(panel); y++; }
/** * Adds a message consisting of one or more lines of text, which will be displayed using the * specified font and color. */ public void addMessage(String text, Font font, Color color) { theLabel = null; if (text.indexOf('\n') >= 0) theLabel = new MultiLineLabel(text); else theLabel = new Label(text); // theLabel.addKeyListener(this); c.gridx = 0; c.gridy = y; c.gridwidth = 2; c.anchor = GridBagConstraints.WEST; c.insets = getInsets(text.equals("") ? 0 : 10, 20, 0, 0); c.fill = GridBagConstraints.HORIZONTAL; grid.setConstraints(theLabel, c); if (font != null) theLabel.setFont(font); if (color != null) theLabel.setForeground(color); add(theLabel); c.fill = GridBagConstraints.NONE; y++; }
/** * Adds a checkbox; does not make it recordable if isPreview is true. With isPreview true, the * checkbox can be referred to as previewCheckbox from hereon. */ private void addCheckbox(String label, boolean defaultValue, boolean isPreview) { String label2 = label; if (label2.indexOf('_') != -1) label2 = label2.replace('_', ' '); if (checkbox == null) { checkbox = new Vector(4); c.insets = getInsets(15, 20, 0, 0); } else c.insets = getInsets(0, 20, 0, 0); c.gridx = 0; c.gridy = y; c.gridwidth = 2; c.anchor = GridBagConstraints.WEST; Checkbox cb = new Checkbox(label2); grid.setConstraints(cb, c); cb.setState(defaultValue); cb.addItemListener(this); cb.addKeyListener(this); add(cb); checkbox.addElement(cb); // ij.IJ.write("addCheckbox: "+ y+" "+cbIndex); if (!isPreview && (Recorder.record || macro)) // preview checkbox is not recordable saveLabel(cb, label); if (isPreview) previewCheckbox = cb; y++; }
/** * Adds a slider (scroll bar) to the dialog box. Floating point values will be used if * (maxValue-minValue)<=5.0 and either minValue or maxValue are non-integer. * * @param label the label * @param minValue the minimum value of the slider * @param maxValue the maximum value of the slider * @param defaultValue the initial value of the slider */ public void addSlider(String label, double minValue, double maxValue, double defaultValue) { if (defaultValue < minValue) defaultValue = minValue; if (defaultValue > maxValue) defaultValue = maxValue; int columns = 4; int digits = 0; double scale = 1.0; if ((maxValue - minValue) <= 5.0 && (minValue != (int) minValue || maxValue != (int) maxValue || defaultValue != (int) defaultValue)) { scale = 20.0; minValue *= scale; maxValue *= scale; defaultValue *= scale; digits = 2; } 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; c.insets = new Insets(0, 0, 3, 0); grid.setConstraints(theLabel, c); add(theLabel); if (slider == null) { slider = new Vector(5); sliderIndexes = new int[MAX_SLIDERS]; sliderScales = new double[MAX_SLIDERS]; } Scrollbar s = new Scrollbar( Scrollbar.HORIZONTAL, (int) defaultValue, 1, (int) minValue, (int) maxValue + 1); GUI.fix(s); slider.addElement(s); s.addAdjustmentListener(this); s.setUnitIncrement(1); if (numberField == null) { numberField = new Vector(5); defaultValues = new Vector(5); defaultText = new Vector(5); } if (IJ.isWindows()) columns -= 2; if (columns < 1) columns = 1; TextField tf = new TextField(IJ.d2s(defaultValue / scale, digits), columns); if (IJ.isLinux()) tf.setBackground(Color.white); tf.addActionListener(this); tf.addTextListener(this); tf.addFocusListener(this); tf.addKeyListener(this); numberField.addElement(tf); sliderIndexes[slider.size() - 1] = numberField.size() - 1; sliderScales[slider.size() - 1] = scale; defaultValues.addElement(new Double(defaultValue / scale)); defaultText.addElement(tf.getText()); tf.setEditable(true); firstSlider = false; Panel panel = new Panel(); GridBagLayout pgrid = new GridBagLayout(); GridBagConstraints pc = new GridBagConstraints(); panel.setLayout(pgrid); pc.gridx = 0; pc.gridy = 0; pc.gridwidth = 1; pc.ipadx = 85; pc.anchor = GridBagConstraints.WEST; pgrid.setConstraints(s, pc); panel.add(s); pc.ipadx = 0; // reset // text field pc.gridx = 1; pc.insets = new Insets(5, 5, 0, 0); pc.anchor = GridBagConstraints.EAST; pgrid.setConstraints(tf, pc); panel.add(tf); grid.setConstraints(panel, c); c.gridx = 1; c.gridy = y; c.gridwidth = 1; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(0, 0, 0, 0); grid.setConstraints(panel, c); add(panel); y++; if (Recorder.record || macro) saveLabel(tf, label); }
/** * Adds a text field. * * @param label the label * @param defaultText text initially displayed * @param columns width of the text field */ public void addStringField(String label, String defaultText, int columns) { 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; boolean custom = customInsets; if (stringField == null) { stringField = new Vector(4); defaultStrings = new Vector(4); c.insets = getInsets(5, 0, 5, 0); } else c.insets = getInsets(0, 0, 5, 0); grid.setConstraints(theLabel, c); add(theLabel); if (custom) { if (stringField.size() == 0) c.insets = getInsets(5, 0, 5, 0); else c.insets = getInsets(0, 0, 5, 0); } TextField tf = new TextField(defaultText, columns); if (IJ.isLinux()) tf.setBackground(Color.white); tf.setEchoChar(echoChar); echoChar = 0; tf.addActionListener(this); tf.addTextListener(this); tf.addFocusListener(this); tf.addKeyListener(this); c.gridx = 1; c.gridy = y; c.anchor = GridBagConstraints.WEST; grid.setConstraints(tf, c); tf.setEditable(true); add(tf); stringField.addElement(tf); defaultStrings.addElement(defaultText); if (Recorder.record || macro) saveLabel(tf, label); y++; }
/** Displays this dialog box. */ public void showDialog() { if (macro) { dispose(); recorderOn = Recorder.record && Recorder.recordInMacros; } else { if (pfr != null) // prepare preview (not in macro mode): tell the PlugInFilterRunner to listen pfr.setDialog(this); Panel buttons = new Panel(); buttons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0)); cancel = new Button(cancelLabel); cancel.addActionListener(this); cancel.addKeyListener(this); if (yesNoCancel) { okLabel = yesLabel; no = new Button(noLabel); no.addActionListener(this); no.addKeyListener(this); } okay = new Button(okLabel); okay.addActionListener(this); okay.addKeyListener(this); boolean addHelp = helpURL != null; if (addHelp) { help = new Button(helpLabel); help.addActionListener(this); help.addKeyListener(this); } if (IJ.isMacintosh()) { if (addHelp) buttons.add(help); if (yesNoCancel) buttons.add(no); if (!hideCancelButton) buttons.add(cancel); buttons.add(okay); } else { buttons.add(okay); if (yesNoCancel) buttons.add(no); ; if (!hideCancelButton) buttons.add(cancel); if (addHelp) buttons.add(help); } c.gridx = 0; c.gridy = y; c.anchor = GridBagConstraints.EAST; c.gridwidth = 2; c.insets = new Insets(15, 0, 0, 0); grid.setConstraints(buttons, c); add(buttons); if (IJ.isMacintosh()) setResizable(false); pack(); setup(); if (centerDialog) GUI.center(this); setVisible(true); recorderOn = Recorder.record; IJ.wait(50); // work around for Sun/WinNT bug } /* For plugins that read their input only via dialogItemChanged, call it at least once */ if (!wasCanceled && dialogListeners != null && dialogListeners.size() > 0) { resetCounters(); ((DialogListener) dialogListeners.elementAt(0)).dialogItemChanged(this, null); recorderOn = false; } resetCounters(); }