Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
TextWindow.java
1 package ij.text;
2 
3 import java.awt.*;
4 import java.io.*;
5 import java.awt.event.*;
6 import ij.*;
7 import ij.io.*;
8 import ij.gui.*;
9 import ij.plugin.filter.Analyzer;
10 import javax.swing.*;
11 
15 public class TextWindow extends JFrame implements ActionListener, FocusListener {
16 
17  private TextPanel textPanel;
18 
26  public TextWindow(String title, String data, int width, int height) {
27  this(title, "", data, width, height);
28  }
29 
38  public TextWindow(String title, String headings, String data, int width, int height) {
39  super(title);
40  enableEvents(AWTEvent.WINDOW_EVENT_MASK);
41  textPanel = new TextPanel(title);
42  textPanel.setTitle(title);
43  getContentPane().add(textPanel, BorderLayout.CENTER);
44  textPanel.setColumnHeadings(headings);
45  if (data!=null && !data.equals(""))
46  textPanel.append(data);
47  System.out.println("Data was "+data);
48  ImageJ ij = IJ.getInstance();
49  if (ij!=null) {
50  addKeyListener(ij);
51  }
52  addFocusListener(this);
53  addMenuBar();
54  setSize(width, height);
55  GUI.center(this);
56  show();
57  }
58 
66  public TextWindow(String path, int width, int height) {
67  super("");
68  enableEvents(AWTEvent.WINDOW_EVENT_MASK);
69  textPanel = new TextPanel();
70  getContentPane().add(textPanel, BorderLayout.CENTER);
71  if (openFile(path)) {
72  setSize(width, height);
73  show();
74  } else
75  dispose();
76  }
77 
78  void addMenuBar() {
79  JMenuBar mb = new JMenuBar();
80  JMenu m = new JMenu("File");
81  m.add(new JMenuItem("Save As..."/*, new MenuShortcut(KeyEvent.VK_S)*/));
82  m.addActionListener(this);
83  mb.add(m);
84  m = new JMenu("Edit");
85  m.add(new JMenuItem("Cut"/*, new MenuShortcut(KeyEvent.VK_X)*/));
86  m.add(new JMenuItem("Copy"/*, new MenuShortcut(KeyEvent.VK_C)*/));
87  m.add(new JMenuItem("Copy All"));
88  m.add(new JMenuItem("Clear"));
89  m.add(new JMenuItem("Select All"/*, new MenuShortcut(KeyEvent.VK_A)*/));
90  if (getTitle().equals("Results")) {
91  m.addSeparator();
92  m.add(new JMenuItem("Clear Results"));
93  m.add(new JMenuItem("Summarize"));
94  m.add(new JMenuItem("Set Measurements..."));
95  }
96  m.addActionListener(this);
97  mb.add(m);
98  setJMenuBar(mb);
99  }
100 
106  public void append(String text) {
107  textPanel.append(text);
108  }
109 
111  public void setFont(Font font) {
112  textPanel.setFont(font);
113  }
114 
115  boolean openFile(String path) {
116  OpenDialog od = new OpenDialog("Open Text File...", path);
117  String directory = od.getDirectory();
118  String name = od.getFileName();
119  if (name==null)
120  return false;
121  path = directory + name;
122 
123  IJ.showStatus("Opening: " + path);
124  try {
125  BufferedReader r = new BufferedReader(new FileReader(directory + name));
126  load(r);
127  r.close();
128  }
129  catch (Exception e) {
130  IJ.error(e.getMessage());
131  return true;
132  }
133  textPanel.setTitle(name);
134  setTitle(name);
135  IJ.showStatus("");
136  return true;
137  }
138 
141  return textPanel;
142  }
143 
145  public void load(BufferedReader in) throws IOException {
146  int count=0;
147  while (true) {
148  String s=in.readLine();
149  if (s==null) break;
150  textPanel.appendLine(s);
151  }
152  }
153 
154  public void actionPerformed(ActionEvent evt) {
155  String cmd = evt.getActionCommand();
156  textPanel.doCommand(cmd);
157  }
158 
159  public void processWindowEvent(WindowEvent e) {
160  super.processWindowEvent(e);
161  int id = e.getID();
162  if (id==WindowEvent.WINDOW_CLOSING)
163  close();
164  }
165 
166  public void close() {
167  if (getTitle().equals("Results")) {
168  if (!Analyzer.resetCounter())
169  return;
170  IJ.setTextPanel(null);
171  }
172  if (getTitle().equals("Log")) {
173  IJ.debugMode = false;
174  IJ.log("$Closed");
175  }
176  setVisible(false);
177  dispose();
178  textPanel.flush();
179  }
180 
181  public void focusGained(FocusEvent e) {
182  }
183 
184  public void focusLost(FocusEvent e) {}
185 
186 }