3 import java.awt.event.*;
6 import ij.plugin.frame.Recorder;
7 import ij.plugin.ScreenGrabber;
34 TextListener, FocusListener, ItemListener, KeyListener, AdjustmentListener {
36 protected Vector numberField, stringField, checkbox, choice, slider;
37 protected TextArea textArea1, textArea2;
38 protected Vector defaultValues,defaultText;
39 protected Component theLabel;
40 private Button cancel, okay;
43 private int nfIndex, sfIndex, cbIndex, choiceIndex;
44 private GridBagLayout grid;
45 private GridBagConstraints c;
46 private boolean firstNumericField=
true;
47 private boolean firstSlider=
true;
49 private String errorMessage;
50 private boolean firstPaint =
true;
51 private Hashtable labels;
62 grid =
new GridBagLayout();
63 c =
new GridBagConstraints();
64 getContentPane().setLayout(grid);
104 public void addNumericField(String label,
double defaultValue,
int digits,
int columns, String units) {
105 String label2 = label;
106 if (label2.indexOf(
'_')!=-1)
107 label2 = label2.replace(
'_',
' ');
108 Label theLabel = makeLabel(label2);
109 c.gridx = 0; c.gridy = y;
110 c.anchor = GridBagConstraints.EAST;
112 if (firstNumericField)
113 c.insets =
new Insets(5, 0, 3, 0);
115 c.insets =
new Insets(0, 0, 3, 0);
116 grid.setConstraints(theLabel, c);
117 getContentPane().add(theLabel);
119 if (numberField==null) {
120 numberField =
new Vector(5);
121 defaultValues =
new Vector(5);
122 defaultText =
new Vector(5);
125 if (columns<1) columns = 1;
126 TextField tf =
new TextField(
IJ.
d2s(defaultValue, digits), columns);
127 tf.addActionListener(
this);
128 tf.addTextListener(
this);
129 tf.addFocusListener(
this);
130 tf.addKeyListener(
this);
131 numberField.addElement(tf);
132 defaultValues.addElement(
new Double(defaultValue));
133 defaultText.addElement(tf.getText());
134 c.gridx = 1; c.gridy = y;
135 c.anchor = GridBagConstraints.WEST;
136 tf.setEditable(
true);
137 if (firstNumericField) tf.selectAll();
138 firstNumericField =
false;
139 if (units==null||units.equals(
"")) {
140 grid.setConstraints(tf, c);
141 getContentPane().add(tf);
143 Panel panel =
new Panel();
144 panel.setLayout(
new FlowLayout(FlowLayout.LEFT, 0, 0));
146 panel.add(
new Label(
" "+units));
147 grid.setConstraints(panel, c);
148 getContentPane().add(panel);
153 private Label makeLabel(String label) {
156 return new Label(label);
159 private void saveLabel(Component component, String label) {
161 labels =
new Hashtable();
162 labels.put(component, label);
179 Label theLabel = makeLabel(label);
180 c.gridx = 0; c.gridy = y;
181 c.anchor = GridBagConstraints.EAST;
183 if (stringField==null) {
184 stringField =
new Vector(4);
185 c.insets =
new Insets(5, 0, 5, 0);
187 c.insets =
new Insets(0, 0, 5, 0);
188 grid.setConstraints(theLabel, c);
189 getContentPane().add(theLabel);
191 TextField tf =
new TextField(defaultText, columns);
192 tf.addActionListener(
this);
193 tf.addTextListener(
this);
194 tf.addFocusListener(
this);
195 tf.addKeyListener(
this);
196 c.gridx = 1; c.gridy = y;
197 c.anchor = GridBagConstraints.WEST;
198 grid.setConstraints(tf, c);
199 tf.setEditable(
true);
200 getContentPane().add(tf);
201 stringField.addElement(tf);
210 if (checkbox==null) {
211 checkbox =
new Vector(4);
212 c.insets =
new Insets(15, 20, 0, 0);
214 c.insets =
new Insets(0, 20, 0, 0);
215 c.gridx = 0; c.gridy = y;
217 c.anchor = GridBagConstraints.WEST;
218 Checkbox cb =
new Checkbox(label);
219 grid.setConstraints(cb, c);
220 cb.setState(defaultValue);
221 cb.addItemListener(
this);
222 cb.addKeyListener(
this);
223 getContentPane().add(cb);
224 checkbox.addElement(cb);
235 public void addCheckboxGroup(
int rows,
int columns, String[] labels,
boolean[] defaultValues) {
236 Panel panel =
new Panel();
237 panel.setLayout(
new GridLayout(rows,columns,10,0));
238 int startCBIndex = cbIndex;
240 int[] index =
new int[labels.length];
242 checkbox =
new Vector(12);
243 boolean addListeners = labels.length<=4;
244 for (
int row=0; row<rows; row++) {
245 for (
int col=0; col<columns; col++) {
246 int i2 = col*rows+row;
247 if (i2>=labels.length)
250 Checkbox cb =
new Checkbox(labels[i1]);
251 checkbox.addElement(cb);
252 cb.setState(defaultValues[i1]);
253 if (addListeners) cb.addItemListener(
this);
258 c.gridx = 0; c.gridy = y;
260 c.anchor = GridBagConstraints.WEST;
261 c.insets =
new Insets(10, 0, 0, 0);
262 grid.setConstraints(panel, c);
263 getContentPane().add(panel);
272 public void addChoice(String label, String[] items, String defaultItem) {
273 Label theLabel = makeLabel(label);
274 c.gridx = 0; c.gridy = y;
275 c.anchor = GridBagConstraints.EAST;
278 choice =
new Vector(4);
279 c.insets =
new Insets(5, 0, 5, 0);
281 c.insets =
new Insets(0, 0, 5, 0);
282 grid.setConstraints(theLabel, c);
283 getContentPane().add(theLabel);
284 Choice thisChoice =
new Choice();
285 thisChoice.addKeyListener(
this);
286 thisChoice.addItemListener(
this);
287 for (
int i=0; i<items.length; i++)
288 thisChoice.addItem(items[i]);
289 thisChoice.select(defaultItem);
290 c.gridx = 1; c.gridy = y;
291 c.anchor = GridBagConstraints.WEST;
292 grid.setConstraints(thisChoice, c);
293 getContentPane().add(thisChoice);
294 choice.addElement(thisChoice);
300 if (text.indexOf(
'\n')>=0)
303 theLabel =
new Label(text);
305 c.gridx = 0; c.gridy = y;
307 c.anchor = GridBagConstraints.WEST;
308 c.insets =
new Insets(text.equals(
"")?0:10, 20, 0, 0);
309 grid.setConstraints(theLabel, c);
310 getContentPane().add(theLabel);
320 public void addTextAreas(String text1, String text2,
int rows,
int columns) {
323 Panel panel =
new Panel();
324 textArea1 =
new TextArea(text1,rows,columns,TextArea.SCROLLBARS_NONE);
327 panel.add(textArea1);
329 textArea2 =
new TextArea(text2,rows,columns,TextArea.SCROLLBARS_NONE);
332 panel.add(textArea2);
334 c.gridx = 0; c.gridy = y;
336 c.anchor = GridBagConstraints.WEST;
337 c.insets =
new Insets(15, 20, 0, 0);
338 grid.setConstraints(panel, c);
339 getContentPane().add(panel);
343 public void addSlider(String label,
double minValue,
double maxValue,
double defaultValue) {
346 String label2 = label;
347 if (label2.indexOf(
'_')!=-1)
348 label2 = label2.replace(
'_',
' ');
349 Label theLabel = makeLabel(label2);
350 c.gridx = 0; c.gridy = y;
351 c.anchor = GridBagConstraints.EAST;
353 c.insets =
new Insets(0, 0, 3, 0);
354 grid.setConstraints(theLabel, c);
355 getContentPane().add(theLabel);
358 slider =
new Vector(5);
359 Scrollbar s =
new Scrollbar(Scrollbar.HORIZONTAL, (
int)defaultValue, 1, (
int)minValue, (
int)maxValue+1);
360 slider.addElement(s);
361 s.addAdjustmentListener(
this);
362 s.setUnitIncrement(1);
364 if (numberField==null) {
365 numberField =
new Vector(5);
366 defaultValues =
new Vector(5);
367 defaultText =
new Vector(5);
370 if (columns<1) columns = 1;
371 TextField tf =
new TextField(
IJ.
d2s(defaultValue, digits), columns);
372 tf.addActionListener(
this);
373 tf.addTextListener(
this);
374 tf.addFocusListener(
this);
375 tf.addKeyListener(
this);
376 numberField.addElement(tf);
377 defaultValues.addElement(
new Double(defaultValue));
378 defaultText.addElement(tf.getText());
379 tf.setEditable(
true);
380 if (firstNumericField && firstSlider) tf.selectAll();
383 Panel panel =
new Panel();
384 GridBagLayout pgrid =
new GridBagLayout();
385 GridBagConstraints pc =
new GridBagConstraints();
386 panel.setLayout(pgrid);
395 pc.gridx = 0; pc.gridy = 0;
398 pc.anchor = GridBagConstraints.WEST;
399 pgrid.setConstraints(s, pc);
404 pc.insets =
new Insets(5, 5, 0, 0);
405 pc.anchor = GridBagConstraints.EAST;
406 pgrid.setConstraints(tf, pc);
409 grid.setConstraints(panel, c);
410 c.gridx = 1; c.gridy = y;
412 c.anchor = GridBagConstraints.WEST;
413 c.insets =
new Insets(0, 0, 0, 0);
414 grid.setConstraints(panel, c);
415 getContentPane().add(panel);
421 addPanel(panel , GridBagConstraints.WEST,
new Insets(5, 0, 0, 0));
427 public void addPanel(Panel panel,
int contraints, Insets insets) {
428 c.gridx = 0; c.gridy = y;
430 c.anchor = contraints;
432 grid.setConstraints(panel, c);
433 getContentPane().add(panel);
444 if (numberField==null)
446 TextField tf = (TextField)numberField.elementAt(nfIndex);
447 String theText = tf.getText();
449 String originalText = (String)defaultText.elementAt(nfIndex);
450 double defaultValue = ((Double)(defaultValues.elementAt(nfIndex))).doubleValue();
452 if (theText.equals(originalText))
453 value = defaultValue;
455 Double d = getValue(theText);
457 value = d.doubleValue();
460 errorMessage =
"\""+theText+
"\" is an invalid number";
465 recordOption(tf, trim(theText));
470 private String trim(String value) {
471 if (value.endsWith(
".0"))
472 value = value.substring(0, value.length()-2);
473 if (value.endsWith(
".00"))
474 value = value.substring(0, value.length()-3);
478 private void recordOption(Component component, String value) {
479 String label = (String)labels.get((Object)component);
480 Recorder.recordOption(label, value);
483 private void recordCheckboxOption(Checkbox cb) {
484 String label = (String)labels.get((Object)cb);
487 Recorder.recordOption(label);
489 Recorder.recordOption(
" ");
493 protected Double getValue(String theText) {
495 try {d =
new Double(theText);}
496 catch (NumberFormatException e){
519 if (stringField==null)
521 TextField tf = (TextField)(stringField.elementAt(sfIndex));
522 theText = tf.getText();
524 recordOption(tf, theText);
533 Checkbox cb = (Checkbox)(checkbox.elementAt(cbIndex));
535 recordCheckboxOption(cb);
536 boolean state = cb.getState();
545 Choice thisChoice = (Choice)(choice.elementAt(choiceIndex));
546 String item = thisChoice.getSelectedItem();
548 recordOption(thisChoice, item);
557 Choice thisChoice = (Choice)(choice.elementAt(choiceIndex));
558 int index = thisChoice.getSelectedIndex();
560 recordOption(thisChoice, thisChoice.getSelectedItem());
568 if (textArea1!=null) {
569 textArea1.selectAll();
570 text = textArea1.getText();
573 Recorder.recordOption(
"text1", text.replace(
'\n',
' '));
574 }
else if (textArea2!=null) {
575 textArea2.selectAll();
576 text = textArea2.getText();
579 Recorder.recordOption(
"text2", text.replace(
'\n',
' '));
591 if (stringField!=null&&numberField==null) {
592 TextField tf = (TextField)(stringField.elementAt(0));
595 Panel buttons =
new Panel();
596 buttons.setLayout(
new FlowLayout(FlowLayout.CENTER, 5, 0));
597 cancel =
new Button(
"Cancel");
598 cancel.addActionListener(
this);
599 okay =
new Button(
" OK ");
600 okay.addActionListener(
this);
608 c.gridx = 0; c.gridy = y;
609 c.anchor = GridBagConstraints.EAST;
611 c.insets =
new Insets(15, 0, 0, 0);
612 grid.setConstraints(buttons, c);
613 getContentPane().add(buttons);
660 protected void setup() {
663 public void actionPerformed(ActionEvent e) {
664 Object source = e.getSource();
665 if (source==okay || source==cancel) {
676 public void textValueChanged(TextEvent e) {
677 if (slider==null || slider.size()!=numberField.size())
679 Object source = e.getSource();
680 for (
int i=0; i<numberField.size(); i++) {
681 if (source==numberField.elementAt(i)) {
682 TextField tf = (TextField)numberField.elementAt(i);
683 double value =
Tools.parseDouble(tf.getText());
684 if (!Double.isNaN(value)) {
685 Scrollbar sb = (Scrollbar)slider.elementAt(i);
686 sb.setValue((
int)value);
693 public void itemStateChanged(ItemEvent e) {
696 public void focusGained(FocusEvent e) {
697 Component c = e.getComponent();
698 if (c instanceof TextField)
699 ((TextField)c).selectAll();
702 public void focusLost(FocusEvent e) {
703 Component c = e.getComponent();
704 if (c instanceof TextField)
705 ((TextField)c).select(0,0);
708 public void keyPressed(KeyEvent e) {
709 int keyCode = e.getKeyCode();
710 IJ.setKeyDown(keyCode);
711 if (keyCode==KeyEvent.VK_ENTER && textArea1==null)
715 public void keyReleased(KeyEvent e) {
716 int keyCode = e.getKeyCode();
717 IJ.setKeyUp(keyCode);
718 int flags = e.getModifiers();
719 boolean control = (flags & KeyEvent.CTRL_MASK) != 0;
720 boolean meta = (flags & KeyEvent.META_MASK) != 0;
721 boolean shift = (flags & e.SHIFT_MASK) != 0;
722 if (keyCode==KeyEvent.VK_G && shift && (control||meta) && IJ.isJava2())
723 new ScreenGrabber().run(
"");
726 public void keyTyped(KeyEvent e) {}
728 public Insets getInsets() {
729 Insets i= super.getInsets();
730 return new Insets(i.top+10, i.left+10, i.bottom+10, i.right+10);
733 public synchronized void adjustmentValueChanged(AdjustmentEvent e) {
734 Object source = e.getSource();
735 for (
int i=0; i<slider.size(); i++) {
736 if (source==slider.elementAt(i)) {
737 Scrollbar sb = (Scrollbar)source;
738 TextField tf = (TextField)numberField.elementAt(i);
739 tf.setText(
""+sb.getValue());
744 public void paint(Graphics g) {
746 if (firstPaint && numberField!=null) {
747 TextField tf = (TextField)(numberField.elementAt(0));