1 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 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 o) { 58 if (check()) util.error(o); 59 } 60 61 public static void debug(Object o) { 62 if (check()) util.debug(o); 63 } 64 65 public static void ex(Object t) { 66 if (check()) util.ex(t); 67 } 68 69 public static void aspect(Object o) { 70 if (check()) util.aspect(o); 71 } 72 73 public static void msg(Object o, Color c) { 74 if (check()) util.msg(o, c); 75 } 76 77 public static void msg(String s, Object 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 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 o) { 173 msg("ERROR", o, colors[ERROR]); 174 } 175 176 public void debug(Object o) { 177 msg("DEBUG", o, colors[DEBUG]); 178 } 179 180 public void aspect(Object o) { 181 msg("ASPECT", o, colors[ASPECT]); 182 } 183 184 public void ex(Object o) { 185 if (o instanceof Exception ) { 186 msg("EXCEPTION", ((Exception ) o).getMessage(), colors[EXCEPTION]); 187 } else { 188 msg("EXCEPTION", o, colors[EXCEPTION]); 189 } 190 } 191 192 public void msg(Object o, Color c) { 193 msg("MSG", o, c); 194 } 195 196 public void msg(String m, Object o, Color c) { 197 String s = "NULL"; 198 if (o != null) s = o.toString(); 199 outln("<<" + m + ">> " + s, c); 200 } 201 202 private void outln(String s, Color c) { 203 outln(s, getAttr(c)); 204 } 205 206 private void outln(String s, int i) { 207 outln(s, attrs[i]); 208 } 209 210 private void outln(String 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 e) {} 219 } 220 } 221 | Popular Tags |