KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > editor > EditorConsole


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.editor;
33
34 import org.antlr.works.components.grammar.CEditorGrammar;
35 import org.antlr.works.swing.Toolbar;
36 import org.antlr.works.utils.Console;
37 import org.antlr.xjlib.foundation.XJUtils;
38
39 import javax.swing.*;
40 import java.awt.*;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.ActionListener JavaDoc;
43 import java.text.SimpleDateFormat JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Map JavaDoc;
47
48 public class EditorConsole extends EditorTab implements Console {
49
50     protected CEditorGrammar editor;
51
52     protected JPanel panel;
53     protected JTextArea textArea;
54
55     protected SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("HH:mm:ss");
56     protected Map JavaDoc<Thread JavaDoc,Integer JavaDoc> modeByThread = new HashMap JavaDoc<Thread JavaDoc, Integer JavaDoc>();
57
58     protected static EditorConsole current = null;
59
60     public static synchronized void setCurrent(EditorConsole console) {
61         current = console;
62     }
63
64     public static synchronized EditorConsole getCurrent() {
65         return current;
66     }
67
68     public EditorConsole(CEditorGrammar editor) {
69         this.editor = editor;
70
71         panel = new JPanel(new BorderLayout());
72         Toolbar box = Toolbar.createHorizontalToolbar();
73
74         JButton clear = new JButton("Clear All");
75         clear.addActionListener(new ActionListener JavaDoc() {
76             public void actionPerformed(ActionEvent JavaDoc e) {
77                 clear();
78             }
79         });
80         box.addElement(clear);
81         box.add(Box.createHorizontalGlue());
82
83         panel.add(createTextArea(), BorderLayout.CENTER);
84         panel.add(box, BorderLayout.SOUTH);
85     }
86
87     public void setMode(int mode) {
88         modeByThread.put(Thread.currentThread(), mode);
89     }
90
91     public int getMode() {
92         Integer JavaDoc mode = modeByThread.get(Thread.currentThread());
93         if(mode == null)
94             return Console.MODE_VERBOSE;
95         else
96             return mode;
97     }
98
99     public void makeCurrent() {
100         EditorConsole.setCurrent(this);
101     }
102
103     public Container getContainer() {
104         return panel;
105     }
106
107     public Container createTextArea() {
108         textArea = new JTextArea();
109         JScrollPane textAreaScrollPane = new JScrollPane(textArea);
110         textAreaScrollPane.setWheelScrollingEnabled(true);
111         return textAreaScrollPane;
112     }
113
114     public void clear() {
115         textArea.setText("");
116         editor.clearConsoleStatus();
117     }
118
119     public synchronized void println(String JavaDoc s) {
120         println(s, Console.LEVEL_NORMAL);
121     }
122
123     public synchronized void println(String JavaDoc s, int level) {
124         print(s+"\n", level);
125     }
126
127     public synchronized void print(Throwable JavaDoc e) {
128         println(XJUtils.stackTrace(e), Console.LEVEL_ERROR);
129     }
130
131     public synchronized void print(final String JavaDoc s, final int level) {
132         if(!SwingUtilities.isEventDispatchThread()) {
133             SwingUtilities.invokeLater(new Runnable JavaDoc() {
134                 public void run() {
135                     print(s, level);
136                 }
137             });
138         }
139         
140         String JavaDoc t = "["+dateFormat.format(new Date JavaDoc())+"] "+s;
141         textArea.setText(textArea.getText()+t);
142         textArea.setCaretPosition(textArea.getText().length());
143         System.out.println(s);
144
145         if(getMode() == Console.MODE_VERBOSE) {
146             editor.consolePrint(s, level);
147         }
148     }
149
150     public String JavaDoc getTabName() {
151         return "Console";
152     }
153
154     public Component getTabComponent() {
155         return getContainer();
156     }
157
158 }
159
Popular Tags