-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathOSPLog.java
More file actions
1185 lines (1103 loc) · 36.7 KB
/
OSPLog.java
File metadata and controls
1185 lines (1103 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.controls;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.XMLFormatter;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import org.opensourcephysics.display.OSPRuntime;
import org.opensourcephysics.tools.FontSizer;
/**
* This is a viewable file-based message log for a java package.
* It displays log records in a text pane and saves them in a temp file.
*
* @author Douglas Brown
* @author Wolfgang Christian
* @version 1.0
*/
public class OSPLog extends JFrame {
/**
* A logger that can be used by any OSP program to log and show messages.
*/
private static OSPLog OSPLOG;
private static JFileChooser chooser;
protected static Style black, red, blue, green, magenta, gray;
protected static final Color DARK_GREEN = new Color(0, 128, 0), DARK_BLUE = new Color(0, 0, 128), DARK_RED = new Color(128, 0, 0);
public static final Level[] levels = new Level[] {Level.OFF, Level.SEVERE, Level.WARNING, Level.INFO, ConsoleLevel.ERR_CONSOLE, ConsoleLevel.OUT_CONSOLE, Level.CONFIG, Level.FINE, Level.FINER, Level.FINEST, Level.ALL};
private static Level defaultLevel = ConsoleLevel.OUT_CONSOLE;
public static final int OUT_OF_MEMORY_ERROR=1;
protected static boolean logConsole = true;
// instance fields
private Logger logger;
private Handler fileHandler;
private OSPLogHandler logHandler;
private JTextPane textPane;
private String logFileName;
private String tempFileName;
private JPanel logPanel;
private JPopupMenu popup;
private ButtonGroup popupGroup, menubarGroup;
private String pkgName;
private String bundleName;
private JMenuItem logToFileItem;
private boolean hasPermission = true;
private static LogRecord[] messageStorage = new LogRecord[128];
private static int messageIndex = 0;
static String eol = "\n"; //$NON-NLS-1$
static String logdir = "."; //$NON-NLS-1$
static String slash = "/"; //$NON-NLS-1$
static {
try { // system properties may not be readable in some environments
eol = System.getProperty("line.separator", eol); //$NON-NLS-1$
logdir = System.getProperty("user.home", logdir); //$NON-NLS-1$
slash = System.getProperty("file.separator", "/"); //$NON-NLS-1$//$NON-NLS-2$
} catch(Exception ex) {
/** empty block */
}
}
/**
* Gets the OSPLog that can be shared by multiple OSP packages.
*
* @return the shared OSPLog
*/
public static OSPLog getOSPLog() {
if(OSPLOG==null) {
if(!OSPRuntime.appletMode&&(OSPRuntime.applet==null)) { // cannot redirect applet streams
OSPLOG = new OSPLog("org.opensourcephysics", null); //$NON-NLS-1$
try {
System.setOut(new PrintStream(new LoggerOutputStream(ConsoleLevel.OUT_CONSOLE, System.out)));
System.setErr(new PrintStream(new LoggerOutputStream(ConsoleLevel.ERR_CONSOLE, System.err)));
} catch(SecurityException ex) {
/** empty block */
}
setLevel(defaultLevel);
}
}
return OSPLOG;
}
/**
* Sets the directory where the log file will be saved if logging is enabled.
* @param dir String
*/
public void setLogDir(String dir) {
logdir = dir;
}
/**
* Gets the directory where the log file will be saved if logging is enabled.
* @param dir String
*/
public String getLogDir() {
return logdir;
}
/**
* Determines if the shared log is visible.
*
* @return true if visible
*/
public static boolean isLogVisible() {
if((OSPRuntime.appletMode||(OSPRuntime.applet!=null))&&(MessageFrame.APPLET_MESSAGEFRAME!=null)) {
return MessageFrame.APPLET_MESSAGEFRAME.isVisible();
} else if(OSPLOG!=null) {
return OSPLOG.isVisible();
}
return false;
}
/**
* Sets the visibility of this log.
*
* @param true to set visible
*/
public void setVisible(boolean visible) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.showLog(visible);
} else {
super.setVisible(visible);
}
}
/**
* Determines if the log is visible.
*
* @return true if visible
*/
public boolean isVisible() {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
return org.opensourcephysics.controls.MessageFrame.isLogVisible();
}
return super.isVisible();
}
/**
* Shows the log when it is invoked from the event queue.
*/
public static JFrame showLog() {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
return org.opensourcephysics.controls.MessageFrame.showLog(true);
}
getOSPLog().setVisible(true);
Logger logger = OSPLOG.getLogger();
for(int i = 0, n = messageStorage.length; i<n; i++) {
LogRecord record = messageStorage[(i+messageIndex)%n];
if(record!=null) {
logger.log(record);
}
}
messageIndex = 0;
return getOSPLog();
}
/**
* Shows the log.
*/
public static void showLogInvokeLater() {
Runnable doLater = new Runnable() {
public void run() {
//showLogFromEventQueue();
}
};
java.awt.EventQueue.invokeLater(doLater);
}
/**
* Gets the logger level value.
* @return the current level value
*/
public static int getLevelValue() {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
return org.opensourcephysics.controls.MessageFrame.getLevelValue();
}
try {
Level level = getOSPLog().getLogger().getLevel();
if (level!=null) return level.intValue();
} catch(Exception ex) { // throws security exception if the caller does not have LoggingPermission("control").
}
return -1;
}
/**
* Sets the logger level.
*
* @param level the Level
*/
public static void setLevel(Level level) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.setLevel(level);
} else {
try {
getOSPLog().getLogger().setLevel(level);
} catch(Exception ex) { // throws security exception if the caller does not have LoggingPermission("control").
// keep the current level
}
// refresh the level menus if the menubar exists
if((getOSPLog()==null)||(getOSPLog().menubarGroup==null)) {
return;
}
for(int i = 0; i<2; i++) {
Enumeration<AbstractButton> e = getOSPLog().menubarGroup.getElements();
if(i==1) {
e = getOSPLog().popupGroup.getElements();
}
while(e.hasMoreElements()) {
JMenuItem item = (JMenuItem) e.nextElement();
if(getOSPLog().getLogger().getLevel().toString().equals(item.getActionCommand())) {
item.setSelected(true);
break;
}
}
}
}
}
/**
* Returns the Level with the specified name, or null if none.
*
* @param level the Level
*/
public static Level parseLevel(String level) {
for(int i = 0; i<levels.length; i++) {
if(levels[i].getName().equals(level)) {
return levels[i];
}
}
return null;
}
/**
* Logs a severe error message.
*
* @param msg the message
*/
public static void severe(String msg) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.severe(msg);
} else {
log(Level.SEVERE, msg);
}
}
/**
* Logs a warning message.
*
* @param msg the message
*/
public static void warning(String msg) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.warning(msg);
} else {
log(Level.WARNING, msg);
}
}
/**
* Logs an information message.
*
* @param msg the message
*/
public static void info(String msg) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.info(msg);
} else {
log(Level.INFO, msg);
}
}
/**
* Logs a configuration message.
*
* @param msg the message
*/
public static void config(String msg) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.config(msg);
} else {
log(Level.CONFIG, msg);
}
}
/**
* Logs a fine debugging message.
*
* @param msg the message
*/
public static void fine(String msg) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.fine(msg);
} else {
log(Level.FINE, msg);
}
}
/**
* Clears the Log.
*
* @param msg the message
*/
public static void clearLog() {
messageIndex = 0;
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.clear();
} else {
OSPLOG.clear();
}
}
/**
* Logs a finer debugging message.
*
* @param msg the message
*/
public static void finer(String msg) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.finer(msg);
} else {
log(Level.FINER, msg);
}
}
/**
* Logs a finest debugging message.
*
* @param msg the message
*/
public static void finest(String msg) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
org.opensourcephysics.controls.MessageFrame.finest(msg);
} else {
log(Level.FINEST, msg);
}
}
/**
* Sets whether console messages are logged.
*
* @param log true to log console messages
*/
public static void setConsoleMessagesLogged(boolean log) {
logConsole = log;
}
/**
* Gets whether console messages are logged.
*
* @return true if console messages are logged
*/
public static boolean isConsoleMessagesLogged() {
return logConsole;
}
/**
* Constructs an OSPLog for a specified package.
*
* @param pkg the package
*/
public OSPLog(Package pkg) {
this(pkg.getName(), null);
}
/**
* Constructs an OSPLog for a specified package and resource bundle.
*
* @param pkg the package
* @param resourceBundleName the name of the resource bundle
*/
public OSPLog(Package pkg, String resourceBundleName) {
this(pkg.getName(), resourceBundleName);
}
/**
* Constructs an OSPLog for a specified class.
*
* @param type the class
*/
public OSPLog(Class<?> type) {
this(type, null);
}
/**
* Constructs an OSPLog for a specified class and resource bundle.
*
* @param type the class
* @param resourceBundleName the name of the resource bundle
*/
public OSPLog(Class<?> type, String resourceBundleName) {
this(type.getPackage().getName(), resourceBundleName);
}
/**
* Gets the log panel so it can be displayed in a dialog or other container.
*
* @return a JPanel containing the log
*/
public JPanel getLogPanel() {
return logPanel;
}
/**
* Clears the log.
*/
public void clear() {
textPane.setText(null);
}
/**
* Saves the log to a text file specified by name.
*
* @param fileName the file name
* @return the name of the file
*/
public String saveLog(String fileName) {
if((fileName==null)||fileName.trim().equals("")) { //$NON-NLS-1$
return saveLogAs();
}
try {
BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
out.write(textPane.getText());
out.flush();
out.close();
return fileName;
} catch(IOException ex) {
return null;
}
}
/**
* Saves a log to a text file selected with a chooser.
*
* @return the name of the file
*/
public String saveLogAs() {
int result = getChooser().showSaveDialog(null);
if(result==JFileChooser.APPROVE_OPTION) {
File file = getChooser().getSelectedFile();
// check to see if file already exists
if(file.exists()) {
int selected = JOptionPane.showConfirmDialog(this, ControlsRes.getString("OSPLog.ReplaceExisting_dialog_message")+file.getName() //$NON-NLS-1$
+ControlsRes.getString("OSPLog.question_mark"), ControlsRes.getString("OSPLog.ReplaceFile_dialog_title"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.YES_NO_CANCEL_OPTION);
if(selected!=JOptionPane.YES_OPTION) {
return null;
}
}
String fileName = XML.getRelativePath(file.getAbsolutePath());
return saveLog(fileName);
}
return null;
}
/**
* Saves the xml-formatted log records to a file specified by name.
*
* @param fileName the file name
* @return the name of the file
*/
public String saveXML(String fileName) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
logger.log(Level.FINE, "Cannot save XML file when running as an applet."); //$NON-NLS-1$
return null; // cannot log to file in applet mode
}
if((fileName==null)||fileName.trim().equals("")) { //$NON-NLS-1$
return saveXMLAs();
}
// open temp file and get xml string
String xml = read(tempFileName);
// add closing tag to xml
Handler fileHandler = getFileHandler();
String tail = fileHandler.getFormatter().getTail(fileHandler);
xml = xml+tail;
// write the xml
try {
BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
out.write(xml);
out.flush();
out.close();
return fileName;
} catch(IOException ex) {
return null;
}
}
/**
* Saves the xml-formatted log records to a file selected with a chooser.
*
* @return the name of the file
*/
public String saveXMLAs() {
int result = getChooser().showSaveDialog(null);
if(result==JFileChooser.APPROVE_OPTION) {
File file = getChooser().getSelectedFile();
// check to see if file already exists
if(file.exists()) {
int selected = JOptionPane.showConfirmDialog(this, ControlsRes.getString("OSPLog.ReplaceExisting_dialog_message")+file.getName() //$NON-NLS-1$
+ControlsRes.getString("OSPLog.question_mark"), ControlsRes.getString("OSPLog.ReplaceFile_dialog_title"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.YES_NO_CANCEL_OPTION);
if(selected!=JOptionPane.YES_OPTION) {
return null;
}
}
logFileName = XML.getRelativePath(file.getAbsolutePath());
return saveXML(logFileName);
}
return null;
}
/**
* Opens a text file selected with a chooser and writes the contents to the log.
*
* @return the name of the file
*/
public String open() {
int result = getChooser().showOpenDialog(null);
if(result==JFileChooser.APPROVE_OPTION) {
File file = getChooser().getSelectedFile();
String fileName = XML.getRelativePath(file.getAbsolutePath());
return open(fileName);
}
return null;
}
/**
* Opens a text file specified by name and writes the contents to the log.
*
* @param fileName the file name
* @return the file name
*/
public String open(String fileName) {
textPane.setText(read(fileName));
return fileName;
}
/**
* Gets the logger.
*
* @return the logger
*/
public Logger getLogger() {
return logger;
}
/**
* Enables logging to a file.
*
* @param enable true to log to a file
*/
public void setLogToFile(boolean enable) {
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
logger.log(Level.FINE, "Cannot log to file when running as an applet."); //$NON-NLS-1$
return; // cannot log to file in applet mode
}
if(enable) {
logToFileItem.setSelected(true);
logger.addHandler(getFileHandler());
} else {
logToFileItem.setSelected(false);
logger.removeHandler(fileHandler);
}
}
/*
* //Uncomment this method to test the OSPLog.
* public static void main(String[] args) {
* JMenuBar menubar = getOSPLog().getJMenuBar();
* JMenu menu = new JMenu("Test");
* menubar.add(menu);
* String[] levels = new String[] {
* "SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST"
* };
* for(int i = 0;i<levels.length;i++) {
* JMenuItem item = new JMenuItem(levels[i]);
* menu.add(item, 0);
* item.setActionCommand(levels[i]);
* item.addActionListener(new ActionListener() {
* public void actionPerformed(ActionEvent e) {
* OSPLOG.getLogger().log(Level.parse(e.getActionCommand()), "Testing "+e.getActionCommand());
* }
* });
* }
* menu = new JMenu("Console");
* menubar.add(menu);
* JMenuItem item = new JMenuItem("Console Out");
* menu.add(item, 0);
* item.addActionListener(new ActionListener() {
* public void actionPerformed(ActionEvent e) {
* System.out.println("Out console message.");
* }
* });
* item = new JMenuItem("Console Err");
* menu.add(item, 0);
* item.addActionListener(new ActionListener() {
* public void actionPerformed(ActionEvent e) {
* System.err.println("Error console message.");
* }
* });
* item = new JMenuItem("Exception");
* menu.add(item, 0);
* item.addActionListener(new ActionListener() {
* public void actionPerformed(ActionEvent e) {
* double[] x = null;
* x[0] = 1;
* }
* });
* OSPLOG.setVisible(true);
* OSPLOG.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
* }
*/
/**
* Fires a property change event. Needed to expose protected method.
*/
protected void firePropertyChange(String propertyName,
Object oldValue, Object newValue) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
/**
* Creates the GUI.
*/
protected void createGUI() {
// create the panel, text pane and scroller
logPanel = new JPanel(new BorderLayout());
logPanel.setPreferredSize(new Dimension(480, 240));
setContentPane(logPanel);
textPane = new JTextPane() {
public void paintComponent(Graphics g) {
if(OSPRuntime.antiAliasText) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
super.paintComponent(g);
}
};
textPane.setEditable(false);
textPane.setAutoscrolls(true);
JScrollPane textScroller = new JScrollPane(textPane);
textScroller.setWheelScrollingEnabled(true);
logPanel.add(textScroller, BorderLayout.CENTER);
// create the colored styles
black = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
red = textPane.addStyle("red", black); //$NON-NLS-1$
StyleConstants.setForeground(red, DARK_RED);
blue = textPane.addStyle("blue", black); //$NON-NLS-1$
StyleConstants.setForeground(blue, DARK_BLUE);
green = textPane.addStyle("green", black); //$NON-NLS-1$
StyleConstants.setForeground(green, DARK_GREEN);
magenta = textPane.addStyle("magenta", black); //$NON-NLS-1$
StyleConstants.setForeground(magenta, Color.MAGENTA);
gray = textPane.addStyle("gray", black); //$NON-NLS-1$
StyleConstants.setForeground(gray, Color.GRAY);
// create the logger
createLogger();
// create the menus
createMenus();
pack();
textPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
try {
if(OSPRuntime.isPopupTrigger(e)) {
// show popup menu
if(popup!=null) {
FontSizer.setFonts(popup, FontSizer.getLevel());
popup.show(textPane, e.getX(), e.getY()+8);
}
}
} catch(Exception ex) {
System.err.println("Error in mouse action."); //$NON-NLS-1$
System.err.println(ex.toString());
ex.printStackTrace();
}
}
});
}
/**
* Creates and initializes the logger.
*
* @return the logger
*/
protected Logger createLogger() {
// get the package logger and reference the ResourceBundle it will use
if(bundleName!=null) {
try {
logger = Logger.getLogger(pkgName, bundleName);
} catch(Exception ex) {
logger = Logger.getLogger(pkgName);
}
} else {
logger = Logger.getLogger(pkgName);
}
try {
logger.setLevel(defaultLevel);
// add a log handler for this log
logHandler = new OSPLogHandler(textPane, this);
logHandler.setFormatter(new ConsoleFormatter());
logHandler.setLevel(Level.ALL);
// ignore parent handlers (specifically root console handler)
OSPRuntime.class.getClass(); // force the static methods to execute
logger.setUseParentHandlers(false);
logger.addHandler(logHandler);
} catch(SecurityException ex) {
hasPermission = false;
}
return logger;
}
/**
* Gets the file handler using lazy instantiation.
*
* @return the Handler
*/
protected synchronized Handler getFileHandler() {
if(fileHandler!=null) {
return fileHandler;
}
try {
// add a file handler with file name equal to short package name
int i = pkgName.lastIndexOf("."); //$NON-NLS-1$
if(i>-1) {
pkgName = pkgName.substring(i+1);
}
if(logdir.endsWith(slash)) {
tempFileName = logdir+pkgName+".log"; //$NON-NLS-1$
} else {
tempFileName = logdir+slash+pkgName+".log"; //$NON-NLS-1$
}
fileHandler = new FileHandler(tempFileName);
fileHandler.setFormatter(new XMLFormatter());
fileHandler.setLevel(Level.ALL);
logger.addHandler(fileHandler);
logger.log(Level.INFO, "Logging to file enabled. File = "+tempFileName); //$NON-NLS-1$
} catch(Exception ex) {
ex.printStackTrace();
}
return fileHandler;
}
/**
* Creates the popup menu.
*/
protected void createMenus() {
if(!hasPermission) {
return;
}
popup = new JPopupMenu();
JMenu menu = new JMenu(ControlsRes.getString("OSPLog.Level_menu")); //$NON-NLS-1$
popup.add(menu);
popupGroup = new ButtonGroup();
for(int i = 0; i<levels.length; i++) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(levels[i].getName());
menu.add(item, 0);
popupGroup.add(item);
if(logger.getLevel().toString().equals(levels[i])) {
item.setSelected(true);
}
item.setActionCommand(levels[i].getName());
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logger.setLevel(Level.parse(e.getActionCommand()));
Enumeration<AbstractButton> e2 = menubarGroup.getElements();
while(e2.hasMoreElements()) {
JMenuItem item = (JMenuItem) e2.nextElement();
if(logger.getLevel().toString().equals(item.getActionCommand())) {
item.setSelected(true);
break;
}
}
}
});
}
popup.addSeparator();
Action openAction = new AbstractAction(ControlsRes.getString("OSPLog.Open_popup")) { //$NON-NLS-1$
public void actionPerformed(ActionEvent e) {
open();
}
};
openAction.setEnabled(!OSPRuntime.appletMode&&(OSPRuntime.applet==null));
popup.add(openAction);
Action saveAsAction = new AbstractAction(ControlsRes.getString("OSPLog.SaveAs_popup")) { //$NON-NLS-1$
public void actionPerformed(ActionEvent e) {
saveLogAs();
}
};
saveAsAction.setEnabled(!OSPRuntime.appletMode&&(OSPRuntime.applet==null));
popup.add(saveAsAction);
popup.addSeparator();
Action clearAction = new AbstractAction(ControlsRes.getString("OSPLog.Clear_popup")) { //$NON-NLS-1$
public void actionPerformed(ActionEvent e) {
clear();
}
};
popup.add(clearAction);
// create menubar
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
menu = new JMenu(ControlsRes.getString("OSPLog.File_menu")); //$NON-NLS-1$
menubar.add(menu);
menu.add(openAction);
menu.add(saveAsAction);
menu = new JMenu(ControlsRes.getString("OSPLog.Edit_menu")); //$NON-NLS-1$
menubar.add(menu);
menu.add(clearAction);
menu = new JMenu(ControlsRes.getString("OSPLog.Level_menu")); //$NON-NLS-1$
menubar.add(menu);
menubarGroup = new ButtonGroup();
for(int i = 0; i<levels.length; i++) {
JRadioButtonMenuItem item = new JRadioButtonMenuItem(levels[i].getName());
menu.add(item, 0);
menubarGroup.add(item);
if(logger.getLevel().toString().equals(levels[i])) {
item.setSelected(true);
}
item.setActionCommand(levels[i].getName());
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logger.setLevel(Level.parse(e.getActionCommand()));
Enumeration<AbstractButton> e2 = popupGroup.getElements();
while(e2.hasMoreElements()) {
JMenuItem item = (JMenuItem) e2.nextElement();
if(logger.getLevel().toString().equals(item.getActionCommand())) {
item.setSelected(true);
break;
}
}
}
});
}
JMenu prefMenu = new JMenu(ControlsRes.getString("OSPLog.Options_menu")); //$NON-NLS-1$
menubar.add(prefMenu);
logToFileItem = new JCheckBoxMenuItem(ControlsRes.getString("OSPLog.LogToFile_check_box")); //$NON-NLS-1$
logToFileItem.setSelected(false);
logToFileItem.setEnabled(!OSPRuntime.appletMode&&(OSPRuntime.applet==null));
logToFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
setLogToFile(item.isSelected());
}
});
prefMenu.add(logToFileItem);
}
/**
* Gets a file chooser.
*
* @return the chooser
*/
protected static JFileChooser getChooser() {
if(chooser==null) {
chooser = new JFileChooser(new File(OSPRuntime.chooserDir));
}
FontSizer.setFonts(chooser, FontSizer.getLevel());
return chooser;
}
/**
* Reads a file.
*
* @param fileName the name of the file
* @return the file contents as a String
*/
protected String read(String fileName) {
File file = new File(fileName);
StringBuffer buffer = null;
try {
BufferedReader in = new BufferedReader(new FileReader(file));
buffer = new StringBuffer();
String line = in.readLine();
while(line!=null) {
buffer.append(line+XML.NEW_LINE);
line = in.readLine();
}
in.close();
} catch(IOException ex) {
logger.warning(ex.toString());
}
return buffer.toString();
}
private OSPLog(String name, String resourceBundleName) {
super(ControlsRes.getString("OSPLog.DefaultTitle")); //$NON-NLS-1$
this.setName("LogTool"); // identify this as a tool //$NON-NLS-1$
bundleName = resourceBundleName;
pkgName = name;
ConsoleLevel.class.getName(); // force ConsoleLevel to load static constants
createGUI();
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
}
private static void log(Level level, String msg) {
LogRecord record = new LogRecord(level, msg);
// get the stack trace
StackTraceElement stack[] = (new Throwable()).getStackTrace();
// find the first method not in class OSPLog
for(int i = 0; i<stack.length; i++) {
StackTraceElement el = stack[i];
String className = el.getClassName();
if(!className.equals("org.opensourcephysics.controls.OSPLog")) { //$NON-NLS-1$
// set the source class and method
record.setSourceClassName(className);
record.setSourceMethodName(el.getMethodName());
break;
}
}
if(OSPLOG!=null) {
OSPLOG.getLogger().log(record);
} else {
messageStorage[messageIndex] = record;
messageIndex++;
messageIndex = messageIndex%messageStorage.length;
}
}
}
/**
* A class that formats a record as if this were the console.
*/
class ConsoleFormatter extends SimpleFormatter {
/**
* Formats the record as if this were the console.
*
* @param record LogRecord
* @return String
*/
public String format(LogRecord record) {
String message = formatMessage(record);
if((record.getLevel().intValue()==ConsoleLevel.OUT_CONSOLE.intValue())||(record.getLevel().intValue()==ConsoleLevel.ERR_CONSOLE.intValue())) {
StringBuffer sb = new StringBuffer();
if((message.length()>0)&&(message.charAt(0)=='\t')) {
message = message.replaceFirst("\t", " "); //$NON-NLS-1$//$NON-NLS-2$
} else {