Esempio n. 1
0
 /** 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;
 }
Esempio n. 2
0
 /**
  * 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++;
 }
Esempio n. 3
0
 /**
  * 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++;
 }
Esempio n. 4
0
 /** 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;
 }
Esempio n. 5
0
 /**
  * Notify any DialogListeners of changes having occurred If a listener returns false, do not call
  * further listeners and disable the OK button and preview Checkbox (if it exists). For
  * PlugInFilters, this ensures that the PlugInFilterRunner, which listens as the last one, is not
  * called if the PlugInFilter has detected invalid parameters. Thus, unnecessary calling the
  * run(ip) method of the PlugInFilter for preview is avoided in that case.
  */
 private void notifyListeners(AWTEvent e) {
   if (dialogListeners == null) return;
   boolean everythingOk = true;
   for (int i = 0; everythingOk && i < dialogListeners.size(); i++)
     try {
       resetCounters();
       if (!((DialogListener) dialogListeners.elementAt(i)).dialogItemChanged(this, e))
         everythingOk = false;
     } // disable further listeners if false (invalid parameters) returned
     catch (Exception err) { // for exceptions, don't cover the input by a window but
       IJ.beep(); // show them at in the "Log"
       IJ.log(
           "ERROR: "
               + err
               + "\nin DialogListener of "
               + dialogListeners.elementAt(i)
               + "\nat "
               + (err.getStackTrace()[0])
               + "\nfrom "
               + (err.getStackTrace()[1])); // requires Java 1.4
     }
   boolean workaroundOSXbug = IJ.isMacOSX() && okay != null && !okay.isEnabled() && everythingOk;
   if (previewCheckbox != null) previewCheckbox.setEnabled(everythingOk);
   if (okay != null) okay.setEnabled(everythingOk);
   if (workaroundOSXbug) repaint(); // OSX 10.4 bug delays update of enabled until the next input
 }
Esempio n. 6
0
 void accessTextFields() {
   if (stringField != null) {
     for (int i = 0; i < stringField.size(); i++)
       ((TextField) (stringField.elementAt(i))).getText();
   }
   if (numberField != null) {
     for (int i = 0; i < numberField.size(); i++)
       ((TextField) (numberField.elementAt(i))).getText();
   }
 }
Esempio n. 7
0
 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));
     }
   }
 }
Esempio n. 8
0
 /**
  * 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;
 }
Esempio n. 9
0
 /**
  * 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++;
 }
Esempio n. 10
0
 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());
     }
   }
 }
Esempio n. 11
0
 /**
  * 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++;
 }
Esempio n. 12
0
 String[] openUrlAsList(String address) {
   Vector v = new Vector();
   try {
     URL url = new URL(address);
     InputStream in = url.openStream();
     BufferedReader br = new BufferedReader(new InputStreamReader(in));
     String line;
     while (true) {
       line = br.readLine();
       if (line == null) break;
       if (!line.equals("")) v.addElement(line);
     }
     br.close();
   } catch (Exception e) {
   }
   String[] lines = new String[v.size()];
   v.copyInto((String[]) lines);
   return lines;
 }
Esempio n. 13
0
 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;
   }
 }
Esempio n. 14
0
 /** Returns the state of the next checkbox. */
 public boolean getNextBoolean() {
   if (checkbox == null) return false;
   Checkbox cb = (Checkbox) (checkbox.elementAt(cbIndex));
   if (recorderOn) recordCheckboxOption(cb);
   boolean state = cb.getState();
   if (macro) {
     String label = (String) labels.get((Object) cb);
     String key = Macro.trimKey(label);
     state = isMatch(macroOptions, key + " ");
   }
   cbIndex++;
   return state;
 }
Esempio n. 15
0
 /**
  * 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++;
 }
Esempio n. 16
0
 /** 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;
 }
Esempio n. 17
0
 /** Returns the selected item in the next radio button group. */
 public String getNextRadioButton() {
   if (radioButtonGroups == null) return null;
   CheckboxGroup cg = (CheckboxGroup) (radioButtonGroups.elementAt(radioButtonIndex));
   radioButtonIndex++;
   Checkbox checkbox = cg.getSelectedCheckbox();
   String item = "null";
   if (checkbox != null) item = checkbox.getLabel();
   if (macro) {
     String label = (String) labels.get((Object) cg);
     item = Macro.getValue(macroOptions, label, item);
   }
   if (recorderOn) recordOption(cg, item);
   return item;
 }
Esempio n. 18
0
 /**
  * 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++;
 }
Esempio n. 19
0
 /** @deprecated replaced by ImagePlus.getOverlay() */
 public Vector getDisplayList() {
   if (overlay == null) return null;
   Vector displayList = new Vector();
   for (int i = 0; i < overlay.size(); i++) displayList.add(overlay.get(i));
   return displayList;
 }
Esempio n. 20
0
  boolean showDialog(ImageProcessor ip) {
    String macroOptions = Macro.getOptions();
    if (macroOptions != null) {
      if (macroOptions.indexOf(" interpolate") != -1)
        macroOptions.replaceAll(" interpolate", " interpolation=Bilinear");
      else if (macroOptions.indexOf(" interpolation=") == -1)
        macroOptions = macroOptions + " interpolation=None";
      Macro.setOptions(macroOptions);
    }
    int bitDepth = imp.getBitDepth();
    int stackSize = imp.getStackSize();
    boolean isStack = stackSize > 1;
    oldDepth = stackSize;
    if (isStack) {
      xstr = "1.0";
      ystr = "1.0";
      zstr = "1.0";
    }
    r = ip.getRoi();
    int width = newWidth;
    if (width == 0) width = r.width;
    int height = (int) ((double) width * r.height / r.width);
    xscale = Tools.parseDouble(xstr, 0.0);
    yscale = Tools.parseDouble(ystr, 0.0);
    zscale = 1.0;
    if (xscale != 0.0 && yscale != 0.0) {
      width = (int) (r.width * xscale);
      height = (int) (r.height * yscale);
    } else {
      xstr = "-";
      ystr = "-";
    }
    GenericDialog gd = new GenericDialog("Scale");
    gd.addStringField("X Scale:", xstr);
    gd.addStringField("Y Scale:", ystr);
    if (isStack) gd.addStringField("Z Scale:", zstr);
    gd.setInsets(5, 0, 5);
    gd.addStringField("Width (pixels):", "" + width);
    gd.addStringField("Height (pixels):", "" + height);
    if (isStack) {
      String label = "Depth (images):";
      if (imp.isHyperStack()) {
        int slices = imp.getNSlices();
        int frames = imp.getNFrames();
        if (slices == 1 && frames > 1) {
          label = "Depth (frames):";
          oldDepth = frames;
        } else {
          label = "Depth (slices):";
          oldDepth = slices;
        }
      }
      gd.addStringField(label, "" + oldDepth);
    }
    fields = gd.getStringFields();
    for (int i = 0; i < fields.size(); i++) {
      ((TextField) fields.elementAt(i)).addTextListener(this);
      ((TextField) fields.elementAt(i)).addFocusListener(this);
    }
    xField = (TextField) fields.elementAt(0);
    yField = (TextField) fields.elementAt(1);
    if (isStack) {
      zField = (TextField) fields.elementAt(2);
      widthField = (TextField) fields.elementAt(3);
      heightField = (TextField) fields.elementAt(4);
      depthField = (TextField) fields.elementAt(5);
    } else {
      widthField = (TextField) fields.elementAt(2);
      heightField = (TextField) fields.elementAt(3);
    }
    fieldWithFocus = xField;
    gd.addChoice("Interpolation:", methods, methods[interpolationMethod]);
    if (bitDepth == 8 || bitDepth == 24)
      gd.addCheckbox("Fill with background color", fillWithBackground);
    gd.addCheckbox("Average when downsizing", averageWhenDownsizing);
    boolean hyperstack = imp.isHyperStack() || imp.isComposite();
    if (isStack && !hyperstack) gd.addCheckbox("Process entire stack", processStack);
    gd.addCheckbox("Create new window", newWindow);
    title = WindowManager.getUniqueName(imp.getTitle());
    gd.setInsets(10, 0, 0);
    gd.addStringField("Title:", title, 12);
    gd.showDialog();
    if (gd.wasCanceled()) return false;
    xstr = gd.getNextString();
    ystr = gd.getNextString();
    xscale = Tools.parseDouble(xstr, 0.0);
    yscale = Tools.parseDouble(ystr, 0.0);
    if (isStack) {
      zstr = gd.getNextString();
      zscale = Tools.parseDouble(ystr, 0.0);
    }
    String wstr = gd.getNextString();
    newWidth = (int) Tools.parseDouble(wstr, 0);
    newHeight = (int) Tools.parseDouble(gd.getNextString(), 0);
    if (newHeight != 0 && (wstr.equals("-") || wstr.equals("0")))
      newWidth = (int) (newHeight * (double) r.width / r.height);
    if (newWidth == 0 || newHeight == 0) {
      IJ.error("Scaler", "Width or height is 0");
      return false;
    }
    if (xscale > 0.0 && yscale > 0.0) {
      newWidth = (int) (r.width * xscale);
      newHeight = (int) (r.height * yscale);
    }
    if (isStack) newDepth = (int) Tools.parseDouble(gd.getNextString(), 0);
    interpolationMethod = gd.getNextChoiceIndex();
    if (bitDepth == 8 || bitDepth == 24) fillWithBackground = gd.getNextBoolean();
    averageWhenDownsizing = gd.getNextBoolean();
    if (isStack && !hyperstack) processStack = gd.getNextBoolean();
    if (hyperstack) processStack = true;
    newWindow = gd.getNextBoolean();
    if (xscale == 0.0) {
      xscale = (double) newWidth / r.width;
      yscale = (double) newHeight / r.height;
    }
    title = gd.getNextString();

    if (fillWithBackground) {
      Color bgc = Toolbar.getBackgroundColor();
      if (bitDepth == 8) bgValue = ip.getBestIndex(bgc);
      else if (bitDepth == 24) bgValue = bgc.getRGB();
    } else bgValue = 0.0;
    return true;
  }
Esempio n. 21
0
  boolean showDialog(ImageProcessor ip) {
    int bitDepth = imp.getBitDepth();
    boolean isStack = imp.getStackSize() > 1;
    r = ip.getRoi();
    int width = newWidth;
    if (width == 0) width = r.width;
    int height = (int) ((double) width * r.height / r.width);
    xscale = Tools.parseDouble(xstr, 0.0);
    yscale = Tools.parseDouble(ystr, 0.0);
    if (xscale != 0.0 && yscale != 0.0) {
      width = (int) (r.width * xscale);
      height = (int) (r.height * yscale);
    } else {
      xstr = "-";
      ystr = "-";
    }
    GenericDialog gd = new GenericDialog("Scale");
    gd.addStringField("X Scale (0.05-25):", xstr);
    gd.addStringField("Y Scale (0.05-25):", ystr);
    gd.setInsets(5, 0, 5);
    gd.addStringField("Width (pixels):", "" + width);
    gd.addStringField("Height (pixels):", "" + height);
    fields = gd.getStringFields();
    for (int i = 0; i < 3; i++) {
      ((TextField) fields.elementAt(i)).addTextListener(this);
      ((TextField) fields.elementAt(i)).addFocusListener(this);
    }
    xField = (TextField) fields.elementAt(0);
    yField = (TextField) fields.elementAt(1);
    widthField = (TextField) fields.elementAt(2);
    heightField = (TextField) fields.elementAt(3);
    fieldWithFocus = xField;
    gd.addCheckbox("Interpolate", interpolate);
    if (bitDepth == 8 || bitDepth == 24)
      gd.addCheckbox("Fill with Background Color", fillWithBackground);
    if (isStack) gd.addCheckbox("Process Entire Stack", processStack);
    gd.addCheckbox("Create New Window", newWindow);
    title = WindowManager.getUniqueName(imp.getTitle());
    gd.setInsets(10, 0, 0);
    gd.addStringField("Title:", title, 12);
    gd.showDialog();
    if (gd.wasCanceled()) return false;
    xstr = gd.getNextString();
    ystr = gd.getNextString();
    xscale = Tools.parseDouble(xstr, 0.0);
    yscale = Tools.parseDouble(ystr, 0.0);
    String wstr = gd.getNextString();
    newWidth = (int) Tools.parseDouble(wstr, 0);
    newHeight = (int) Tools.parseDouble(gd.getNextString(), 0);
    if (newHeight != 0 && (wstr.equals("-") || wstr.equals("0")))
      newWidth = (int) (newHeight * (double) r.width / r.height);
    if (newWidth == 0 || newHeight == 0) {
      IJ.error("Invalid width or height entered");
      return false;
    }
    if (xscale > 25.0) xscale = 25.0;
    if (yscale > 25.0) yscale = 25.0;
    if (xscale > 0.0 && yscale > 0.0) {
      newWidth = (int) (r.width * xscale);
      newHeight = (int) (r.height * yscale);
    }
    interpolate = gd.getNextBoolean();
    if (bitDepth == 8 || bitDepth == 24) fillWithBackground = gd.getNextBoolean();
    if (isStack) processStack = gd.getNextBoolean();
    newWindow = gd.getNextBoolean();
    if (!newWindow && xscale == 0.0) {
      xscale = (double) newWidth / r.width;
      yscale = (double) newHeight / r.height;
    }
    title = gd.getNextString();

    if (fillWithBackground) {
      Color bgc = Toolbar.getBackgroundColor();
      if (bitDepth == 8) bgValue = ip.getBestIndex(bgc);
      else if (bitDepth == 24) bgValue = bgc.getRGB();
    } else {
      if (bitDepth == 8) bgValue = ip.isInvertedLut() ? 0.0 : 255.0; // white
      else if (bitDepth == 24) bgValue = 0xffffffff; // white
    }
    return true;
  }
Esempio n. 22
0
 /**
  * Add an Object implementing the DialogListener interface. This object will be notified by its
  * dialogItemChanged method of input to the dialog. The first DialogListener will be also called
  * after the user has typed 'OK' or if the dialog has been invoked by a macro; it should read all
  * input fields of the dialog. For other listeners, the OK button will not cause a call to
  * dialogItemChanged; the CANCEL button will never cause such a call.
  *
  * @param dl the Object that wants to listen.
  */
 public void addDialogListener(DialogListener dl) {
   if (dialogListeners == null) dialogListeners = new Vector();
   dialogListeners.addElement(dl);
   if (IJ.debugMode) IJ.log("GenericDialog: Listener added: " + dl);
 }
Esempio n. 23
0
 /** Adds an image to the dialog. */
 public void addImage(ImagePlus image) {
   ImagePanel imagePanel = new ImagePanel(image);
   addPanel(imagePanel);
   if (imagePanels == null) imagePanels = new Vector();
   imagePanels.add(imagePanel);
 }
Esempio n. 24
0
  /**
   * 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);
  }
Esempio n. 25
0
 /** 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();
 }
Esempio n. 26
0
 public void setDefaultString(int index, String str) {
   if (defaultStrings != null && index >= 0 && index < defaultStrings.size())
     defaultStrings.set(index, str);
 }
Esempio n. 27
0
  public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
    if (IJ.isMacOSX()) IJ.wait(50);
    Calibration cal = imp.getCalibration();
    width = gd.getNextNumber();
    height = gd.getNextNumber();
    xRoi = gd.getNextNumber();
    yRoi = gd.getNextNumber();
    if (stackSize > 1) iSlice = (int) gd.getNextNumber();
    oval = gd.getNextBoolean();
    square = gd.getNextBoolean();
    centered = gd.getNextBoolean();
    if (cal.scaled()) scaledUnits = gd.getNextBoolean();
    if (gd.invalidNumber() || width <= 0 || height <= 0) return false;
    //
    Vector numFields = gd.getNumericFields();
    Vector checkboxes = gd.getCheckboxes();
    boolean newWidth = false, newHeight = false, newXY = false;
    if (e != null && e.getSource() == checkboxes.get(SQUARE) && square) {
      width = 0.5 * (width + height); // make square: same width&height
      height = width;
      newWidth = true;
      newHeight = true;
    }
    if (e != null && e.getSource() == checkboxes.get(CENTERED)) {
      double shiftBy = centered ? 0.5 : -0.5; // 'centered' changed:
      xRoi += shiftBy * width; // shift x, y to keep roi the same
      yRoi += shiftBy * height;
      newXY = true;
    }
    if (square && width != height && e != null) { // in 'square' mode, synchronize width&height
      if (e.getSource() == numFields.get(WIDTH)) {
        height = width;
        newHeight = true;
      } else if (e.getSource() == numFields.get(HEIGHT)) {
        width = height;
        newWidth = true;
      }
    }
    if (e != null && cal.scaled() && e.getSource() == checkboxes.get(SCALED_UNITS)) {
      double xFactor = scaledUnits ? cal.pixelWidth : 1. / cal.pixelWidth;
      double yFactor = scaledUnits ? cal.pixelHeight : 1. / cal.pixelHeight;
      width *= xFactor; // transform everything to keep roi the same
      height *= yFactor;
      xRoi *= xFactor;
      yRoi *= yFactor;
      newWidth = true;
      newHeight = true;
      newXY = true;
    }
    int digits = (scaledUnits || (int) width != width) ? 2 : 0;
    if (newWidth) ((TextField) (numFields.get(WIDTH))).setText(IJ.d2s(width, digits));
    if (newHeight) ((TextField) (numFields.get(HEIGHT))).setText(IJ.d2s(height, digits));
    digits = (scaledUnits || (int) xRoi != xRoi || (int) yRoi != yRoi) ? 2 : 0;
    if (newXY) {
      ((TextField) (numFields.get(X_ROI))).setText(IJ.d2s(xRoi, digits));
      ((TextField) (numFields.get(Y_ROI))).setText(IJ.d2s(yRoi, digits));
    }

    if (stackSize > 1 && iSlice > 0 && iSlice <= stackSize) imp.setSlice(iSlice);
    if (!newWidth && !newHeight && !newXY) // don't draw if an update will come immediately
    drawRoi();
    return true;
  }
Esempio n. 28
0
 public void repaint() {
   super.repaint();
   if (imagePanels != null) {
     for (int i = 0; i < imagePanels.size(); i++) ((ImagePanel) imagePanels.get(i)).repaint();
   }
 }