KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > gui > Util


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.gui;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.util.*;
28
29 import javax.swing.*;
30 import javax.swing.text.*;
31 import javax.swing.event.*;
32 import javax.swing.undo.*;
33
34 import com.sun.jdi.event.Event;
35
36 /**
37  * Used in debugging.
38  */

39 public class Util {
40
41     private static UtilFrame util = null;
42     private static boolean debug = false;
43
44     public static void init() {
45         if (util != null) return;
46         util = new UtilFrame();
47         util.pack();
48         util.setVisible(true);
49     }
50
51     public static Container panel() {
52         if (util != null) return util.getContentPane();
53         util = new UtilFrame();
54         return util.getContentPane();
55     }
56
57     public static void error(Object JavaDoc o) {
58         if (check()) util.error(o);
59     }
60
61     public static void debug(Object JavaDoc o) {
62         if (check()) util.debug(o);
63     }
64
65     public static void ex(Object JavaDoc t) {
66         if (check()) util.ex(t);
67     }
68
69     public static void aspect(Object JavaDoc o) {
70         if (check()) util.aspect(o);
71     }
72
73     public static void msg(Object JavaDoc o, Color c) {
74         if (check()) util.msg(o, c);
75     }
76
77     public static void msg(String JavaDoc s, Object JavaDoc o, Color c) {
78         if (check()) util.msg(s, o, c);
79     }
80
81     public static void setDebugging(boolean _debug) {
82         debug = _debug;
83     }
84
85     private static boolean check() {
86         if (!debug) {
87             return false;
88         }
89         if (util == null) {
90             init();
91         }
92         return true;
93     }
94 }
95
96 class UtilFrame extends JFrame implements ItemListener {
97
98     JTextPane textPane;
99     JCheckBox debugBox;
100     boolean debug = true;
101     DefaultStyledDocument dsd;
102     SimpleAttributeSet[] attrs = null;
103     int count = 0;
104
105     static final int MSG = -1;
106     static final int ERROR = 0;
107     static final int DEBUG = 1;
108     static final int ASPECT = 2;
109     static final int EXCEPTION = 3;
110
111     Color[] colors = {
112                                 Color.red,
113                                 Color.blue,
114                                 Color.green.darker().darker(),
115                                 Color.darkGray,
116                             };
117     int[] colorsConstants = {
118                                 ERROR,
119                                 DEBUG,
120                                 ASPECT,
121                                 EXCEPTION,
122                             };
123
124
125     public UtilFrame() {
126         super("Utilily Frame");
127
128         dsd = new DefaultStyledDocument();
129
130         textPane = new JTextPane(dsd);
131         JScrollPane scrollPane = new JScrollPane(textPane);
132         scrollPane.setPreferredSize(new Dimension(500, 500));
133
134         debugBox = new JCheckBox("Debug?");
135         debugBox.setSelected(true);
136         debugBox.addItemListener(this);
137
138         JPanel contentPane = new JPanel(new BorderLayout());
139         contentPane.add(scrollPane, BorderLayout.CENTER);
140         contentPane.add(debugBox, BorderLayout.NORTH);
141         setContentPane(contentPane);
142
143         initAttrs();
144     }
145
146     public void itemStateChanged(ItemEvent e) {
147         Object JavaDoc source = e.getItemSelectable();
148         if (source == debugBox) {
149             if (debug == true) {
150                 debug = false;
151             } else {
152                 debug = true;
153             }
154         }
155     }
156
157
158     protected void initAttrs() {
159         attrs = new SimpleAttributeSet[colors.length];
160         for (int i = 0; i < colors.length; i++) {
161             attrs[i] = getAttr(colors[i]);
162         }
163     }
164
165     protected SimpleAttributeSet getAttr(Color c) {
166         SimpleAttributeSet attr = new SimpleAttributeSet();
167         StyleConstants.setForeground(attr, c);
168         StyleConstants.setFontSize(attr, 10);
169         return attr;
170     }
171
172     public void error(Object JavaDoc o) {
173         msg("ERROR", o, colors[ERROR]);
174     }
175
176     public void debug(Object JavaDoc o) {
177         msg("DEBUG", o, colors[DEBUG]);
178     }
179
180     public void aspect(Object JavaDoc o) {
181         msg("ASPECT", o, colors[ASPECT]);
182     }
183
184     public void ex(Object JavaDoc o) {
185         if (o instanceof Exception JavaDoc) {
186             msg("EXCEPTION", ((Exception JavaDoc) o).getMessage(), colors[EXCEPTION]);
187         } else {
188             msg("EXCEPTION", o, colors[EXCEPTION]);
189         }
190     }
191
192     public void msg(Object JavaDoc o, Color c) {
193         msg("MSG", o, c);
194     }
195
196     public void msg(String JavaDoc m, Object JavaDoc o, Color c) {
197         String JavaDoc s = "NULL";
198         if (o != null) s = o.toString();
199         outln("<<" + m + ">> " + s, c);
200     }
201
202     private void outln(String JavaDoc s, Color c) {
203         outln(s, getAttr(c));
204     }
205
206     private void outln(String JavaDoc s, int i) {
207         outln(s, attrs[i]);
208     }
209
210     private void outln(String JavaDoc s, SimpleAttributeSet attr) {
211         if (!debug) {
212             return;
213         }
214         try {
215             dsd.insertString(dsd.getLength(), s + "\n", attr);
216             count += s.length() + 1;
217             textPane.setCaretPosition(count);
218         } catch (Exception JavaDoc e) {}
219     }
220 }
221
Popular Tags