/** 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; }
public synchronized void adjustmentValueChanged(AdjustmentEvent e) { Object source = e.getSource(); for (int i = 0; i < slider.size(); i++) { if (source == slider.elementAt(i)) { Scrollbar sb = (Scrollbar) source; TextField tf = (TextField) numberField.elementAt(sliderIndexes[i]); int digits = sliderScales[i] == 1.0 ? 0 : 2; tf.setText("" + IJ.d2s(sb.getValue() / sliderScales[i], digits)); } } }
/** * 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; }
public void paint(Graphics g) { super.paint(g); if (firstPaint) { if (numberField != null && IJ.isMacOSX()) { // work around for bug on Intel Macs that caused 1st field to be un-editable TextField tf = (TextField) (numberField.elementAt(0)); tf.setEditable(false); tf.setEditable(true); } if (numberField == null && stringField == null) okay.requestFocus(); firstPaint = false; } }
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 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 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++; }
/** * 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); }
public void focusLost(FocusEvent e) { Component c = e.getComponent(); if (c instanceof TextField) ((TextField) c).select(0, 0); }
public void focusGained(FocusEvent e) { Component c = e.getComponent(); if (c instanceof TextField) ((TextField) c).selectAll(); }