/** Returns the contents of the next text field. */ public String getNextString() { String theText; if (stringField == null) return ""; TextField tf = (TextField) (stringField.elementAt(sfIndex)); theText = tf.getText(); if (macro) { String label = (String) labels.get((Object) tf); theText = Macro.getValue(macroOptions, label, theText); if (theText != null && (theText.startsWith("&") || label.toLowerCase(Locale.US).startsWith(theText))) { // Is the value a macro variable? if (theText.startsWith("&")) theText = theText.substring(1); Interpreter interp = Interpreter.getInstance(); String s = interp != null ? interp.getVariableAsString(theText) : null; if (s != null) theText = s; } } if (recorderOn) { String s = theText; if (s != null && s.length() >= 3 && Character.isLetter(s.charAt(0)) && s.charAt(1) == ':' && s.charAt(2) == '\\') s = s.replaceAll("\\\\", "\\\\\\\\"); // replace "\" with "\\" in Windows file paths if (!smartRecording || !s.equals((String) defaultStrings.elementAt(sfIndex))) recordOption(tf, s); else if (Recorder.getCommandOptions() == null) Recorder.recordOption(" "); } sfIndex++; return theText; }
/** * Returns the contents of the next numeric field, or NaN if the field does not contain a number. */ public double getNextNumber() { if (numberField == null) return -1.0; TextField tf = (TextField) numberField.elementAt(nfIndex); String theText = tf.getText(); String label = null; if (macro) { label = (String) labels.get((Object) tf); theText = Macro.getValue(macroOptions, label, theText); // IJ.write("getNextNumber: "+label+" "+theText); } String originalText = (String) defaultText.elementAt(nfIndex); double defaultValue = ((Double) (defaultValues.elementAt(nfIndex))).doubleValue(); double value; boolean skipRecording = false; if (theText.equals(originalText)) { value = defaultValue; if (smartRecording) skipRecording = true; } else { Double d = getValue(theText); if (d != null) value = d.doubleValue(); else { // Is the value a macro variable? if (theText.startsWith("&")) theText = theText.substring(1); Interpreter interp = Interpreter.getInstance(); value = interp != null ? interp.getVariable2(theText) : Double.NaN; if (Double.isNaN(value)) { invalidNumber = true; errorMessage = "\"" + theText + "\" is an invalid number"; value = Double.NaN; if (macro) { IJ.error( "Macro Error", "Numeric value expected in run() function\n \n" + " Dialog box title: \"" + getTitle() + "\"\n" + " Key: \"" + label.toLowerCase(Locale.US) + "\"\n" + " Value or variable name: \"" + theText + "\""); } } } } if (recorderOn && !skipRecording) { recordOption(tf, trim(theText)); } nfIndex++; return value; }
/** * 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++; }
public void textValueChanged(TextEvent e) { notifyListeners(e); if (slider == null) return; Object source = e.getSource(); for (int i = 0; i < slider.size(); i++) { int index = sliderIndexes[i]; if (source == numberField.elementAt(index)) { TextField tf = (TextField) numberField.elementAt(index); double value = Tools.parseDouble(tf.getText()); if (!Double.isNaN(value)) { Scrollbar sb = (Scrollbar) slider.elementAt(i); sb.setValue((int) (value * sliderScales[i])); } // IJ.log(i+" "+tf.getText()); } } }
/** * 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); }