Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ImageJ.java
1 package ij;
2 
3 import java.awt.*;
4 import java.util.*;
5 import java.awt.event.*;
6 import java.io.*;
7 import java.net.URL;
8 import java.awt.image.*;
9 import ij.gui.*;
10 import ij.process.*;
11 import ij.io.*;
12 import ij.plugin.*;
13 import ij.plugin.filter.*;
14 import ij.text.*;
15 import javax.swing.*;
16 import java.applet.*;
17 import java.awt.*;
18 
36 public class ImageJ extends javax.swing.JApplet implements ActionListener,
37  MouseListener, KeyListener, ItemListener {
38 
39  public static final String VERSION = "1.32j";
40  public static Color backgroundColor = new Color(200, 189, 203); //224,226,235
41 
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";
45 
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;
58  boolean hotkey;
59  private JScrollPane imageScrollPane;
60 
61  /* Text field to enter the name for the asset */
62  private JTextField assetNameField;
63 
64  /* Dropdown box to select the filetype */
65  private JComboBox fileTypeField;
66 
67  /* Length of asset name field */
68  private static final int NAME_FIELD_LENGTH = 20;
69 
71  public ImageJ() {
72  super();
73  try {
74  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
75  } catch (Exception e) {}
76 
77  String err1 = Prefs.load(this, null);
78 
79  mainPane = new JPanel(new BorderLayout());
80 
81  // Tool bar
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);
89 
90 
91  // Status bar
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);
104 
105  statusBar.setSize(toolbar.getPreferredSize());
106  mainPane.add(statusBar, BorderLayout.SOUTH);
107  mainPane.addKeyListener(this);
108 
109  Menus m = new Menus(this, null);
110  String err2 = m.addMenuBar();
111  m.installPopupMenu(this);
112 
113  IJ.init(this, null);
114  addKeyListener(this);
115 
116  imageScrollPane = new JScrollPane();
117  mainPane.add(imageScrollPane, BorderLayout.CENTER);
118 
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)));
121 
122  getContentPane().add(mainPane, BorderLayout.CENTER);
123 
124  // Submit button and name field
125  JPanel bottomPanel = new JPanel(new BorderLayout());
126  bottomPanel.setBorder(BorderFactory.createEmptyBorder(4,2,2,2));
127  bottomPanel.setBackground(Color.WHITE);
128 
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);
136  }
137 
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());
150 
151  bottomPanel.add(namePanel, BorderLayout.WEST);
152  if (showSubmitParam != null) bottomPanel.add(submitPanel, BorderLayout.EAST);
153  getContentPane().add(bottomPanel, BorderLayout.SOUTH);
154 
155  setCursor(Cursor.getDefaultCursor()); // work-around for JDK 1.1.8 bug
156  setVisible(true);
157  if (err1!=null)
158  IJ.error(err1);
159  if (err2!=null)
160  IJ.error(err2);
161  if (IJ.isJava2()) {
162  IJ.runPlugIn("ij.plugin.DragAndDrop", "");
163  }
164  }
165 
166  public void init() {
167  try {
168  submitButton.addActionListener(new ServerSubmitter(this));
169  } catch (Exception e) {
170  JOptionPane.showMessageDialog(ImageJ.this, "Problem adding action listener for submit button", "Error", JOptionPane.WARNING_MESSAGE);
171  }
172  try {
173  assetNameField.addFocusListener(new FocusListener() {
174  public void focusGained(FocusEvent ev) {
175  updateFileTypes();
176  }
177  public void focusLost(FocusEvent ev) {}
178  });
179  } catch (Exception e) {
180  JOptionPane.showMessageDialog(ImageJ.this, "Problem adding focus listener for name field", "Error", JOptionPane.WARNING_MESSAGE);
181  }
182 
183  String openURL = null;
184  if ((openURL = getParameter("OPEN_URL")) != null) {
185  try {
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);
191  }
192  if (filename.toLowerCase().endsWith(".gif")) {
193  fileTypeField.setSelectedItem(".gif");
194  } else {
195  fileTypeField.setSelectedItem(".jpg");
196  }
197  assetNameField.setText(filename.substring(0, filename.lastIndexOf(".")));
198  } catch (Exception e) {
199 
200  }
201  }
202  }
203 
204 
205  public void showStatus(String s) {
206  statusLine.setText(s);
207  }
208 
209  public ProgressBar getProgressBar() {
210  return progressBar;
211  }
212 
214  void doCommand(String name) {
215  new Executer(name, IJ.getInstance().getImagePlus());
216  }
217 
218  public void runFilterPlugIn(Object theFilter, String cmd, String arg) {
219  IJ.runFilterPlugIn(theFilter, cmd, arg);
220  }
221 
222  public Object runUserPlugIn(String commandName, String className, String arg, boolean createNewLoader) {
223  return IJ.runUserPlugIn(commandName, className, arg, createNewLoader);
224  }
225 
227  public static String modifiers(int flags) { //?? needs to be moved
228  String s = " [ ";
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 ";
234  s += "] ";
235  return s;
236  }
237 
239  public void actionPerformed(ActionEvent e) {
240  if ((e.getSource() instanceof JMenuItem)) {
241  JMenuItem item = (JMenuItem)e.getSource();
242  String cmd = e.getActionCommand();
243  hotkey = false;
244  actionPerformedTime = System.currentTimeMillis();
245  long ellapsedTime = actionPerformedTime-keyPressedTime;
246  if (cmd!=null && ellapsedTime>=10L)
247  doCommand(cmd);
248  if (IJ.debugMode) IJ.log("actionPerformed: "+ellapsedTime+" "+e);
249  }
250  }
251 
253  public void itemStateChanged(ItemEvent e) {
254  JMenuItem item = (JMenuItem)e.getSource();
255  JComponent parent = (JComponent)item.getParent();
256  String cmd = ((JCheckBoxMenuItem)(e.getItem())).getText().toString();
257  doCommand(cmd);
258  }
259 
260  public void mousePressed(MouseEvent e) {
261  Undo.reset();
262  IJ.showStatus("Memory: "+IJ.freeMemory());
263  }
264 
265  public void mouseReleased(MouseEvent e) {}
266  public void mouseExited(MouseEvent e) {}
267  public void mouseClicked(MouseEvent e) {}
268  public void mouseEntered(MouseEvent e) {}
269 
270  public void keyPressed(KeyEvent e) {
271  int keyCode = e.getKeyCode();
272  IJ.setKeyDown(keyCode);
273  hotkey = false;
274  if (keyCode==e.VK_CONTROL || keyCode==e.VK_SHIFT)
275  return;
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;
284  String c = "";
285  ImagePlus imp = IJ.getInstance().getImagePlus();
286  boolean isStack = (imp!=null) && (imp.getStackSize()>1);
287 
288  if (control && (keyCode == e.VK_D)) {
289  // hard coding the control-d shortcut until we fix the menu shortcuts
290  IJ.run("Draw");
291  imp.killRoi();
292  }
293 
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;
298  if (alt)
299  switch (keyChar) {
300  case 'u': case 'm': keyChar = IJ.micronSymbol; break;
301  case 'A': keyChar = IJ.angstromSymbol; break;
302  default:
303  }
304  ((TextRoi)roi).addChar(keyChar);
305  return;
306  }
307  }
308 
309  Hashtable shortcuts = Menus.getShortcuts();
310  if (shift)
311  c = (String)shortcuts.get(new Integer(keyCode+200));
312  else
313  c = (String)shortcuts.get(new Integer(keyCode));
314 
315  if (c==null)
316  switch(keyCode) {
317  case KeyEvent.VK_BACK_SPACE: c="Clear"; hotkey=true; break; // delete
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: // arrow keys
323  Roi roi = null;
324  if (imp!=null) roi = imp.getRoi();
325  if (roi==null) return;
326  if ((flags & KeyEvent.ALT_MASK) != 0)
327  roi.nudgeCorner(keyCode);
328  else
329  roi.nudge(keyCode);
330  return;
331  default: break;
332  }
333  if (c!=null && !c.equals("")) {
334  if (c.equals("Fill"))
335  hotkey = true;
336  else {
337  doCommand(c);
338  keyPressedTime = System.currentTimeMillis();
339  }
340  }
341  }
342 
343  public void keyReleased(KeyEvent e) {
344  IJ.setKeyUp(e.getKeyCode());
345  }
346 
347  public void keyTyped(KeyEvent e) {}
348 
349  public void register(Class c) {
350  if (!classes.contains(c))
351  classes.addElement(c);
352  }
353 
354  public ImagePlus getImagePlus() {
355  if (imageCanvas != null)
356  return imageCanvas.getImagePlus();
357  else
358  return null;
359  }
360 
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);
369  updateFileTypes();
370  getContentPane().validate();
371  getContentPane().repaint();
372  requestFocus();
373  }
374 
375  public void clearImagePlus() {
376  getContentPane().remove(imageScrollPane);
377  imageCanvas = null;
378  getContentPane().validate();
379  }
380 
381  public ImageCanvas getImageCanvas() {
382  return imageCanvas;
383  }
384 
388  String getAssetName()
389  {
390  return assetNameField.getText();
391 
392  }//end getAssetName();
393 
394 
398  String getFileType()
399  {
400  return (String)fileTypeField.getSelectedItem();
401 
402  }//end getFileType()
403 
407  void updateFileTypes()
408  {
409  if (getImagePlus() == null) return;
410  if (!FileSaver.okForGif(getImagePlus())) {
411  if (fileTypeField.getItemCount() > 1) {
412  fileTypeField.removeItem(".gif");
413  }
414  } else {
415  if (fileTypeField.getItemCount() < 2) {
416  fileTypeField.addItem(".gif");
417  }
418  }
419  }//end updateFileTypes()
420 
421  public void setCurrentType(String type)
422  {
423  fileTypeField.setSelectedItem(type);
424  }
425 
426  public Dimension getViewportSize()
427  {
428  return imageScrollPane.getViewport().getExtentSize();
429  }
430 
431  public String doUpload()
432  {
433  ServerSubmitter ss = new ServerSubmitter(this);
434  ss.actionPerformed(null);
435  return ss.getTempFileName();
436  }
437 
438  public String getFilename()
439  {
440  return getAssetName() + getFileType();
441  }
442 
443  public void setFilename(String path)
444  {
445  String baseName = new File(path).getName();
446  if (-1 != baseName.toLowerCase().indexOf(".gif")) {
447  setCurrentType(".gif");
448  } else {
449  setCurrentType(".jpg");
450  }
451  if (-1 != baseName.lastIndexOf(".")) {
452  baseName = baseName.substring(0, baseName.lastIndexOf("."));
453  }
454  assetNameField.setText(baseName);
455  }
456 
457  public boolean isFocusable()
458  {
459  return true;
460  }
461 
462 } //class ImageJ