5 import java.awt.event.*;
8 import java.awt.image.*;
13 import ij.plugin.filter.*;
36 public class ImageJ extends javax.swing.JApplet implements ActionListener,
37 MouseListener, KeyListener, ItemListener {
39 public static final String VERSION =
"1.32j";
40 public static Color backgroundColor =
new Color(200, 189, 203);
42 private static final String IJ_X=
"ij.x",IJ_Y=
"ij.y";
43 private static final String RESULTS_X=
"results.x",RESULTS_Y=
"results.y",
44 RESULTS_WIDTH=
"results.width",RESULTS_HEIGHT=
"results.height";
46 private Toolbar toolbar;
47 private JPanel statusBar;
48 private ImageCanvas imageCanvas;
49 private ProgressBar progressBar;
50 private JButton submitButton =
new JButton(
"Commit");
51 private JPanel mainPane;
52 private Label statusLine;
53 private boolean firstTime =
true;
54 private Vector classes =
new Vector();
55 private boolean exitWhenQuiting;
56 private boolean quitting;
57 private long keyPressedTime, actionPerformedTime;
59 private JScrollPane imageScrollPane;
62 private JTextField assetNameField;
65 private JComboBox fileTypeField;
68 private static final int NAME_FIELD_LENGTH = 20;
74 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
75 }
catch (Exception e) {}
79 mainPane =
new JPanel(
new BorderLayout());
82 toolbar =
new Toolbar();
83 toolbar.setBackground(backgroundColor);
84 toolbar.addKeyListener(
this);
85 JPanel jp =
new JPanel();
86 jp.setLayout(
new BorderLayout());
87 jp.add(toolbar, BorderLayout.CENTER);
88 mainPane.add(jp, BorderLayout.NORTH);
92 statusBar =
new JPanel();
93 statusBar.setLayout(
new BorderLayout());
94 statusBar.setForeground(Color.black);
95 statusBar.setBackground(backgroundColor);
96 statusLine =
new Label();
97 statusLine.addKeyListener(
this);
98 statusLine.addMouseListener(
this);
99 statusBar.add(
"Center", statusLine);
100 progressBar =
new ProgressBar(100, 18);
101 progressBar.addKeyListener(
this);
102 progressBar.addMouseListener(
this);
103 statusBar.add(
"East", progressBar);
105 statusBar.setSize(toolbar.getPreferredSize());
106 mainPane.add(statusBar, BorderLayout.SOUTH);
107 mainPane.addKeyListener(
this);
110 String err2 = m.addMenuBar();
111 m.installPopupMenu(
this);
114 addKeyListener(
this);
116 imageScrollPane =
new JScrollPane();
117 mainPane.add(imageScrollPane, BorderLayout.CENTER);
119 mainPane.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));
120 ((JComponent)(getContentPane())).setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,
new Color(172, 168, 153)));
122 getContentPane().add(mainPane, BorderLayout.CENTER);
125 JPanel bottomPanel =
new JPanel(
new BorderLayout());
126 bottomPanel.setBorder(BorderFactory.createEmptyBorder(4,2,2,2));
127 bottomPanel.setBackground(Color.WHITE);
129 JPanel submitPanel =
new JPanel();
130 String showSubmitParam = null;
131 try { showSubmitParam = getParameter(
"SHOW_SUBMIT"); }
catch (Exception e) {}
132 if (showSubmitParam != null) {
133 submitPanel.add(submitButton, BorderLayout.EAST);
134 submitPanel.setBorder(BorderFactory.createEmptyBorder());
135 submitPanel.setBackground(Color.WHITE);
138 JPanel namePanel =
new JPanel();
139 String[] typeOptions = {
".jpg",
".gif"};
140 assetNameField =
new JTextField(NAME_FIELD_LENGTH);
141 fileTypeField =
new JComboBox(typeOptions);
142 fileTypeField.setSize(100, assetNameField.getHeight());
143 JLabel nameLabel =
new JLabel(
"File Name: ");
144 nameLabel.setFont(nameLabel.getFont().deriveFont(Font.BOLD));
145 namePanel.add(nameLabel);
146 namePanel.add(assetNameField);
147 namePanel.add(fileTypeField);
148 namePanel.setBackground(Color.WHITE);
149 namePanel.setBorder(BorderFactory.createEmptyBorder());
151 bottomPanel.add(namePanel, BorderLayout.WEST);
152 if (showSubmitParam != null) bottomPanel.add(submitPanel, BorderLayout.EAST);
153 getContentPane().add(bottomPanel, BorderLayout.SOUTH);
155 setCursor(Cursor.getDefaultCursor());
169 }
catch (Exception e) {
170 JOptionPane.showMessageDialog(
ImageJ.this,
"Problem adding action listener for submit button",
"Error", JOptionPane.WARNING_MESSAGE);
173 assetNameField.addFocusListener(
new FocusListener() {
174 public void focusGained(FocusEvent ev) {
177 public void focusLost(FocusEvent ev) {}
179 }
catch (Exception e) {
180 JOptionPane.showMessageDialog(
ImageJ.this,
"Problem adding focus listener for name field",
"Error", JOptionPane.WARNING_MESSAGE);
183 String openURL = null;
184 if ((openURL = getParameter(
"OPEN_URL")) != null) {
186 setImagePlus(
new ImagePlus(openURL));
187 String filename = openURL.substring(openURL.lastIndexOf(
"/")+1);
188 int queryBegin = filename.indexOf(
"?");
189 if (queryBegin != -1) {
190 filename = filename.substring(0, queryBegin);
192 if (filename.toLowerCase().endsWith(
".gif")) {
193 fileTypeField.setSelectedItem(
".gif");
195 fileTypeField.setSelectedItem(
".jpg");
197 assetNameField.setText(filename.substring(0, filename.lastIndexOf(
".")));
198 }
catch (Exception e) {
205 public void showStatus(String s) {
206 statusLine.setText(s);
209 public ProgressBar getProgressBar() {
214 void doCommand(String name) {
215 new Executer(name, IJ.getInstance().getImagePlus());
218 public void runFilterPlugIn(Object theFilter, String cmd, String arg) {
219 IJ.runFilterPlugIn(theFilter, cmd, arg);
222 public Object runUserPlugIn(String commandName, String className, String arg,
boolean createNewLoader) {
223 return IJ.runUserPlugIn(commandName, className, arg, createNewLoader);
229 if (flags == 0)
return "";
230 if ((flags & Event.SHIFT_MASK) != 0) s +=
"Shift ";
231 if ((flags & Event.CTRL_MASK) != 0) s +=
"Control ";
232 if ((flags & Event.META_MASK) != 0) s +=
"Meta ";
233 if ((flags & Event.ALT_MASK) != 0) s +=
"Alt ";
240 if ((e.getSource() instanceof JMenuItem)) {
241 JMenuItem item = (JMenuItem)e.getSource();
242 String cmd = e.getActionCommand();
244 actionPerformedTime = System.currentTimeMillis();
245 long ellapsedTime = actionPerformedTime-keyPressedTime;
246 if (cmd!=null && ellapsedTime>=10L)
248 if (
IJ.debugMode)
IJ.
log(
"actionPerformed: "+ellapsedTime+
" "+e);
254 JMenuItem item = (JMenuItem)e.getSource();
255 JComponent parent = (JComponent)item.getParent();
256 String cmd = ((JCheckBoxMenuItem)(e.getItem())).getText().toString();
260 public void mousePressed(MouseEvent e) {
265 public void mouseReleased(MouseEvent e) {}
266 public void mouseExited(MouseEvent e) {}
267 public void mouseClicked(MouseEvent e) {}
268 public void mouseEntered(MouseEvent e) {}
270 public void keyPressed(KeyEvent e) {
271 int keyCode = e.getKeyCode();
272 IJ.setKeyDown(keyCode);
274 if (keyCode==e.VK_CONTROL || keyCode==e.VK_SHIFT)
276 char keyChar = e.getKeyChar();
277 int flags = e.getModifiers();
278 if (IJ.debugMode) IJ.log(
"keyCode=" + keyCode +
" (" + KeyEvent.getKeyText(keyCode)
279 +
") keyChar=\"" + keyChar +
"\" (" + (int)keyChar +
") "
280 + KeyEvent.getKeyModifiersText(flags));
281 boolean shift = (flags & e.SHIFT_MASK) != 0;
282 boolean control = (flags & e.CTRL_MASK) != 0;
283 boolean alt = (flags & e.ALT_MASK) != 0;
285 ImagePlus imp = IJ.getInstance().getImagePlus();
286 boolean isStack = (imp!=null) && (imp.getStackSize()>1);
288 if (control && (keyCode == e.VK_D)) {
294 if (imp!=null && !control && ((keyChar>=32 && keyChar<=255) || keyChar==
'\b' || keyChar==
'\n')) {
295 Roi roi = imp.getRoi();
296 if (roi instanceof TextRoi) {
297 if ((flags & e.META_MASK)!=0 && IJ.isMacOSX())
return;
300 case 'u':
case 'm': keyChar = IJ.micronSymbol;
break;
301 case 'A': keyChar = IJ.angstromSymbol;
break;
304 ((TextRoi)roi).addChar(keyChar);
309 Hashtable shortcuts = Menus.getShortcuts();
311 c = (String)shortcuts.get(
new Integer(keyCode+200));
313 c = (String)shortcuts.get(
new Integer(keyCode));
317 case KeyEvent.VK_BACK_SPACE: c=
"Clear"; hotkey=
true;
break;
318 case KeyEvent.VK_EQUALS:
case 0xbb: c=
"Start Animation [=]";
break;
319 case KeyEvent.VK_SLASH:
case 0xbf: c=
"Reslice [/]...";
break;
320 case KeyEvent.VK_COMMA:
case 0xbc: c=
"Previous Slice [<]";
break;
321 case KeyEvent.VK_PERIOD:
case 0xbe: c=
"Next Slice [>]";
break;
322 case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
324 if (imp!=null) roi = imp.getRoi();
325 if (roi==null)
return;
326 if ((flags & KeyEvent.ALT_MASK) != 0)
327 roi.nudgeCorner(keyCode);
333 if (c!=null && !c.equals(
"")) {
334 if (c.equals(
"Fill"))
338 keyPressedTime = System.currentTimeMillis();
343 public void keyReleased(KeyEvent e) {
344 IJ.setKeyUp(e.getKeyCode());
347 public void keyTyped(KeyEvent e) {}
349 public void register(Class c) {
350 if (!classes.contains(c))
351 classes.addElement(c);
354 public ImagePlus getImagePlus() {
355 if (imageCanvas != null)
356 return imageCanvas.getImagePlus();
361 public void setImagePlus(ImagePlus ip) {
362 mainPane.remove(imageScrollPane);
363 imageCanvas =
new ImageCanvas(ip);
364 ip.setCanvas(imageCanvas);
365 imageCanvas.addKeyListener(
this);
366 imageCanvas.setFocusable(
true);
367 imageScrollPane =
new JScrollPane(imageCanvas);
368 mainPane.add(imageScrollPane, BorderLayout.CENTER);
370 getContentPane().validate();
371 getContentPane().repaint();
375 public void clearImagePlus() {
376 getContentPane().remove(imageScrollPane);
378 getContentPane().validate();
381 public ImageCanvas getImageCanvas() {
388 String getAssetName()
390 return assetNameField.getText();
400 return (String)fileTypeField.getSelectedItem();
407 void updateFileTypes()
409 if (getImagePlus() == null)
return;
410 if (!FileSaver.okForGif(getImagePlus())) {
411 if (fileTypeField.getItemCount() > 1) {
412 fileTypeField.removeItem(
".gif");
415 if (fileTypeField.getItemCount() < 2) {
416 fileTypeField.addItem(
".gif");
421 public void setCurrentType(String type)
423 fileTypeField.setSelectedItem(type);
426 public Dimension getViewportSize()
428 return imageScrollPane.getViewport().getExtentSize();
431 public String doUpload()
433 ServerSubmitter ss =
new ServerSubmitter(
this);
434 ss.actionPerformed(null);
435 return ss.getTempFileName();
438 public String getFilename()
440 return getAssetName() + getFileType();
443 public void setFilename(String path)
445 String baseName =
new File(path).getName();
446 if (-1 != baseName.toLowerCase().indexOf(
".gif")) {
447 setCurrentType(
".gif");
449 setCurrentType(
".jpg");
451 if (-1 != baseName.lastIndexOf(
".")) {
452 baseName = baseName.substring(0, baseName.lastIndexOf(
"."));
454 assetNameField.setText(baseName);
457 public boolean isFocusable()