Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
/*
2
    AVRcamVIEW: A PC application to test out the functionallity of the
3
     AVRcam real-time image processing engine.
4
    Copyright (C) 2004    Brent A. Taylor
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
   For more information on the AVRcamVIEW, please contact:
21
 
22
   taylorba@comcast.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
 
27
package avr.swing;
28
 
29
import java.awt.*;
30
import java.awt.event.*;
31
import java.io.*;
32
import java.text.*;
33
import java.util.*;
34
import java.util.logging.*;
35
import java.util.zip.*;
36
import javax.swing.*;
37
import javax.swing.event.*;
38
import javax.swing.table.*;
39
 
40
import org.jdom.*;
41
import org.jdom.input.*;
42
import org.jdom.output.*;
43
 
44
import avr.lang.AVRSystem;
45
import avr.swing.filechooser.*;
46
import avr.swing.table.*;
47
 
48
public class JLogApplet extends JApplet {
49
 
50
   public static void main(String[] args) throws Exception {
51
 
52
      final JLogApplet log = new JLogApplet();
53
 
54
      // The JFileChooser takes about 2 seconds to fully load.
55
      // to keep the user from waiting, load the JFileChooser
56
      // in a background thread while the JLogFrame is loading.
57
      Thread loadFileChooser = new Thread(new Runnable() {
58
         public void run() {
59
            log.createJFileChooser();
60
         }
61
      });
62
 
63
      loadFileChooser.setName("Load File Chooser");
64
      loadFileChooser.start();
65
 
66
      JFrame frame = new JFrame("System Log");
67
 
68
 
69
      log.init();
70
 
71
      Dimension dim = log.getToolkit().getScreenSize();
72
      // set the log size to 3/4 the screen size and
73
      // center the log window to the middle of the screen.
74
      frame.setBounds(dim.width / 8, dim.height / 8,
75
                      dim.width * 3 / 4, dim.height * 3 / 4);
76
 
77
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
78
      frame.getContentPane().add(log);
79
      frame.setVisible(true);
80
 
81
   }
82
 
83
   private static final LogFileFilter XML_FILE_FILTER;
84
   private static final LogFileFilter TXT_FILE_FILTER;
85
   private static final DateFormat DATE_FORMAT;
86
 
87
   static {
88
 
89
      DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS");
90
      XML_FILE_FILTER = new LogFileFilter("XML Format (*.xml)", ".xml");
91
      TXT_FILE_FILTER = new LogFileFilter("Text Format (*.txt)", ".txt");
92
 
93
   }
94
 
95
   private LogTableModel model;
96
   private boolean isStandAlone;
97
   private JFileChooser fileChooser;
98
 
99
   public String getParameter(String key) {
100
      return isStandAlone ? System.getProperty(key) : super.getParameter(key);
101
   }
102
 
103
   public void init() {
104
      init(true);
105
   }
106
 
107
   public void init(boolean isStandAlone) {
108
      this.isStandAlone = isStandAlone;
109
 
110
      String selectedLevel = AVRSystem.PREFS.get("avr.log.level", Level.CONFIG.getLocalizedName());
111
      boolean onlyShowLevel = AVRSystem.PREFS.getBoolean("avr.log.onlyshowlevel.selected", false);
112
 
113
      JPanel logLevelP = new JPanel();
114
 
115
      JRadioButton severeRB = new JRadioButton(Level.SEVERE.getLocalizedName(), Level.SEVERE.getLocalizedName().equals(selectedLevel));
116
      JRadioButton warningRB = new JRadioButton(Level.WARNING.getLocalizedName(), Level.WARNING.getLocalizedName().equals(selectedLevel));
117
      JRadioButton infoRB = new JRadioButton(Level.INFO.getLocalizedName(), Level.INFO.getLocalizedName().equals(selectedLevel));
118
      JRadioButton configRB = new JRadioButton(Level.CONFIG.getLocalizedName(), Level.CONFIG.getLocalizedName().equals(selectedLevel));
119
      JRadioButton fineRB = new JRadioButton(Level.FINE.getLocalizedName(), Level.FINE.getLocalizedName().equals(selectedLevel));
120
      JRadioButton finerRB = new JRadioButton(Level.FINER.getLocalizedName(), Level.FINER.getLocalizedName().equals(selectedLevel));
121
      JRadioButton finestRB = new JRadioButton(Level.FINEST.getLocalizedName(), Level.FINEST.getLocalizedName().equals(selectedLevel));
122
 
123
      JCheckBox onlyShowLevelCB = new JCheckBox("Show Only Level", onlyShowLevel);
124
      onlyShowLevelCB.addActionListener(new OnlyShowLevelAction());
125
 
126
      ButtonGroup bg = new ButtonGroup();
127
      bg.add(severeRB);
128
      bg.add(warningRB);
129
      bg.add(infoRB);
130
      bg.add(configRB);
131
      bg.add(fineRB);
132
      bg.add(finerRB);
133
      bg.add(finestRB);
134
 
135
      LogAction logAction = new LogAction();
136
 
137
      severeRB.addActionListener(logAction);
138
      warningRB.addActionListener(logAction);
139
      infoRB.addActionListener(logAction);
140
      configRB.addActionListener(logAction);
141
      fineRB.addActionListener(logAction);
142
      finerRB.addActionListener(logAction);
143
      finestRB.addActionListener(logAction);
144
 
145
      logLevelP.add(new JLabel("Level: "));
146
      logLevelP.add(severeRB);
147
      logLevelP.add(warningRB);
148
      logLevelP.add(infoRB);
149
      logLevelP.add(configRB);
150
      logLevelP.add(fineRB);
151
      logLevelP.add(finerRB);
152
      logLevelP.add(finestRB);
153
      logLevelP.add(Box.createHorizontalStrut(20));
154
      logLevelP.add(onlyShowLevelCB);
155
 
156
      getContentPane().add(logLevelP, BorderLayout.NORTH);
157
 
158
      model = new LogTableModel(Level.parse(selectedLevel));
159
      model.setOnlyShowSelectedLevel(onlyShowLevel);
160
 
161
      JTable table = new JTable(model);
162
      table.setFont(new Font("Courier New", Font.PLAIN, 12));
163
 
164
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
165
      table.setDefaultRenderer(String.class, new LogTableCellRenderer());
166
      table.setRowHeight(table.getRowHeight() + 5);
167
      table.getTableHeader().setReorderingAllowed(false);
168
 
169
      model.addTableModelListener(new LogModelHandler(table));
170
 
171
      TableColumn column = table.getColumnModel().getColumn(0);
172
      column.setPreferredWidth(200);
173
      column.setMaxWidth(200);
174
 
175
      column = table.getColumnModel().getColumn(1);
176
      column.setPreferredWidth(75);
177
      column.setMaxWidth(75);
178
 
179
      column = table.getColumnModel().getColumn(2);
180
      column.setPreferredWidth(800);
181
 
182
      JScrollPane tableScroll = new JScrollPane(table);
183
      tableScroll.getViewport().setBackground(table.getBackground());
184
 
185
      setJMenuBar(createMenuBar());
186
 
187
      getContentPane().add(tableScroll, BorderLayout.CENTER);
188
 
189
   }
190
 
191
   // overwritten so this applet can be resized using javascript
192
   public void setSize(int width, int height) {
193
      Dimension size = getSize();
194
      if(size.width != width || size.height != height) {
195
         super.setSize(width, height);
196
         validate();
197
      }
198
   }
199
 
200
   public JFrame createFrame() {
201
      JFrame frame = new JFrame("AVRcamVIEW - System Log");
202
 
203
      frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
204
      frame.getContentPane().add(this);
205
 
206
      return frame;
207
   }
208
 
209
   public JDialog createDialog(Frame owner) {
210
 
211
      JDialog dialog = new JDialog(owner,
212
                                   "AVRcamVIEW - System Log",
213
                                   false);
214
 
215
      dialog.getContentPane().add(this);
216
 
217
      return dialog;
218
 
219
   }
220
 
221
   private JMenuBar createMenuBar() {
222
 
223
      JMenuBar menubar = new JMenuBar();
224
 
225
      JMenu fileM = new JMenu("File");
226
      fileM.setMnemonic('f');
227
 
228
      if(isStandAlone) {
229
         // only allow user to open a log file if the log is running stand-alone
230
         fileM.add(new ProxyAction(this, "open",
231
                                   "Open",
232
                                   'o'));
233
      } else {
234
         // only allow the user to clear the log if the log is NOT running stand-alone
235
         fileM.add(new ProxyAction(this, "clear",
236
                                   "Clear",
237
                                   'c'));
238
         fileM.addSeparator();
239
         fileM.add(new ProxyAction(this, "save",
240
                                   "Save",
241
                                   's'));
242
      }
243
 
244
      menubar.add(fileM);
245
 
246
      return menubar;
247
   }
248
 
249
   public LogTableModel getTableModel() {
250
      return model;
251
   }
252
 
253
//   public void open() {
254
//
255
//      if(fileChooser == null) {
256
//         createJFileChooser();
257
//      }
258
//
259
//      int option = fileChooser.showOpenDialog(getRootPane());
260
//      if(option == JFileChooser.APPROVE_OPTION) {
261
//
262
//         clear();
263
//
264
//         try {
265
//
266
//            getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
267
//            File logFile = fileChooser.getSelectedFile();
268
//
269
//            SAXBuilder builder = new SAXBuilder();
270
//            Document doc = builder.build(new GZIPInputStream(new FileInputStream(logFile)));
271
//
272
//            Element root = doc.getRootElement();
273
//
274
//            Iterator records = root.getChildren("record").iterator();
275
//            Element recordElement;
276
//            LogRecord record;
277
//
278
//            while(records.hasNext()) {
279
//
280
//               recordElement = (Element)records.next();
281
//
282
//               record = new LogRecord(Level.parse(recordElement.getChildText("level")), recordElement.getChildText("message"));
283
//               record.setMillis(Long.parseLong(recordElement.getChildText("millis")));
284
//               record.setSequenceNumber(Long.parseLong(recordElement.getChildText("sequence")));
285
//               record.setLoggerName(recordElement.getChildText("logger"));
286
//               record.setSourceClassName(recordElement.getChildText("class"));
287
//               record.setSourceMethodName(recordElement.getChildText("method"));
288
//               record.setThreadID(Integer.parseInt(recordElement.getChildText("thread")));
289
//
290
//               model.addRecord(record);
291
//
292
//            }
293
//
294
//
295
//         } catch(Exception e) {
296
//            e.printStackTrace(System.err);
297
//         }
298
//
299
//         getRootPane().setCursor(Cursor.getDefaultCursor());
300
//
301
//      }
302
//
303
//   }
304
//
305
   public void save() {
306
 
307
      if(model.getRowCount() == 0) {
308
         return;
309
      }
310
 
311
      if(fileChooser == null) {
312
         createJFileChooser();
313
      }
314
 
315
      int option = fileChooser.showSaveDialog(getRootPane());
316
      if(option == JFileChooser.APPROVE_OPTION) {
317
         try {
318
            File logFile = fileChooser.getSelectedFile();
319
 
320
            if(fileChooser.getFileFilter().equals(TXT_FILE_FILTER)) {
321
               if(!logFile.getName().endsWith(TXT_FILE_FILTER.getExtension())) {
322
                  logFile = new File(logFile.getAbsolutePath() + TXT_FILE_FILTER.getExtension());
323
               }
324
 
325
               saveTXT(logFile);
326
 
327
            } else if(fileChooser.getFileFilter().equals(XML_FILE_FILTER)) {
328
               if(!logFile.getName().endsWith(XML_FILE_FILTER.getExtension())) {
329
                  logFile = new File(logFile.getAbsolutePath() + XML_FILE_FILTER.getExtension());
330
               }
331
 
332
               saveXML(logFile);
333
 
334
            }
335
 
336
//            FileOutputStream outStream = new FileOutputStream(logFile);
337
//            ObjectOutputStream out = new ObjectOutputStream(outStream);
338
//
339
//            int rows = model.getRowCount();
340
//            out.writeInt(rows);
341
//
342
//            for(int i = 0; i < rows; i++) {
343
//               out.writeObject(model.getValueAt(i, 0));
344
//            }
345
//
346
//            out.close();
347
 
348
         } catch(Exception e) {
349
            e.printStackTrace(System.err);
350
         }
351
      }
352
 
353
   }
354
 
355
   private void saveTXT(File file) throws Exception {
356
 
357
      PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
358
 
359
      for(int i = 0; i < model.getRecordCount(); i++) {
360
         LogRecord record = model.getRecord(i);
361
         writer.print(DATE_FORMAT.format(new Date(record.getMillis())));
362
         writer.print('\t');
363
         writer.print(record.getLevel().toString());
364
         writer.print('\t');
365
         writer.println(record.getMessage());
366
      }
367
 
368
      writer.close();
369
 
370
   }
371
 
372
   private void saveXML(File file) throws Exception {
373
 
374
      Element root = new Element("Log");
375
 
376
      for(int i = 0; i < model.getRecordCount(); i++) {
377
         LogRecord record = model.getRecord(i);
378
 
379
         Element recordE = new Element("Record");
380
         recordE.addContent(new Element("TimeStamp").addContent(DATE_FORMAT.format(new Date(record.getMillis()))));
381
         recordE.addContent(new Element("Level").addContent(record.getLevel().toString()));
382
         recordE.addContent(new Element("Message").addContent(record.getMessage()));
383
 
384
         root.addContent(recordE);
385
      }
386
 
387
      FileOutputStream writer = new FileOutputStream(file);
388
 
389
      new XMLOutputter().output(new Document(root), writer);
390
 
391
      writer.close();
392
 
393
   }
394
 
395
   private void createJFileChooser() {
396
 
397
      getRootPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
398
      System.out.println("Loading File Chooser... " + Thread.currentThread().getName());
399
 
400
      if(fileChooser != null) {
401
         return;
402
      }
403
 
404
      fileChooser = new JFileChooser();
405
      fileChooser.removeChoosableFileFilter(fileChooser.getFileFilter());
406
      fileChooser.addChoosableFileFilter(TXT_FILE_FILTER);
407
      fileChooser.addChoosableFileFilter(XML_FILE_FILTER);
408
 
409
      fileChooser.setFileFilter(TXT_FILE_FILTER);
410
 
411
      System.out.println("File Chooser Loaded... " + Thread.currentThread().getName());
412
      getRootPane().setCursor(Cursor.getDefaultCursor());
413
 
414
   }
415
 
416
   public void clear() {
417
      model.clear();
418
   }
419
 
420
   private final class LogAction implements ActionListener {
421
 
422
      public void actionPerformed(ActionEvent ae) {
423
         String level = ae.getActionCommand();
424
         AVRSystem.PREFS.put("avr.log.level", level);
425
         model.setFilter(Level.parse(ae.getActionCommand()));
426
      }
427
 
428
   }
429
 
430
   private final class OnlyShowLevelAction implements ActionListener {
431
 
432
      public void actionPerformed(ActionEvent event) {
433
         boolean selected = ((JCheckBox)event.getSource()).isSelected();
434
         AVRSystem.PREFS.putBoolean("avr.log.onlyshowlevel.selected", selected);
435
         model.setOnlyShowSelectedLevel(selected);
436
      }
437
 
438
   }
439
 
440
   private final static class LogModelHandler implements TableModelListener {
441
 
442
      private JTable table;
443
 
444
      public LogModelHandler(JTable table) {
445
         this.table = table;
446
      }
447
 
448
      public void tableChanged(TableModelEvent tableModelEvent) {
449
         table.scrollRectToVisible(table.getCellRect(table.getRowCount() - 1, 0, true));
450
      }
451
 
452
   }
453
 
454
}