1 22 23 package org.aspectj.debugger.gui; 24 25 import org.aspectj.debugger.base.*; 26 import org.aspectj.debugger.ide.*; 27 28 import java.awt.*; 29 import java.awt.event.*; 30 import java.util.*; 31 import java.util.List ; 32 import javax.swing.*; 33 import com.sun.jdi.*; 34 import com.sun.jdi.event.*; 35 36 37 public class AJToolBar 38 extends JToolBar 39 implements AJActionsConsts, 40 ThreadListener, 41 VMListener { 42 43 JComboBox comboBox = comboBox(); 44 JToolBar toolBar = new JToolBar(); 45 final static String ALL_THREADS = "All threads"; 46 final static int FONT_SIZE = 10; 47 final static int SC_FONT_SIZE = 8; 48 final static double WIDTH_FACTOR = .7; 49 final static double HEIGHT_FACTOR = .4; 50 private AJDebugger debugger; 51 52 public AJToolBar(GUIDebugger guid) { 53 54 this.debugger = guid.getDebugger(); 55 debugger.addThreadListener(this); 56 debugger.addVMListener(this); 57 58 String mode = Modes.getMode(); 59 if (mode.equals(Modes.GUI)) { 60 createGuiToolbar(guid); 61 } else { 67 createForteToolbar(guid); 68 } 69 } 70 71 private void createForteToolbar(GUIDebugger guid) { 72 badd(new ForteRunAction(guid)); 73 badd(new ContAction(guid)); 74 badd(new QuitAction(guid)); 76 badd(new SuspendAction(guid, threadGetter())); 77 badd(new ForteResumeAction(guid, threadGetter())); 78 badd(new StepAction(guid)); 79 badd(new StepIAction(guid)); 80 badd(new NextAction(guid)); 81 badd(new StepUpAction(guid)); 82 badd(new IDELineBreakpointAction(guid), "Add Breakpoint"); 85 badd(new WatchAction(guid)); 86 } 87 88 103 private void jbSeparator() { 104 addSeparator(); 105 } 106 107 private void createGuiToolbar(GUIDebugger guid) { 108 109 badd(new QuitAction(guid)); 111 badd(new UseAction(guid)); 112 badd(new DisconnectAction(guid)); 113 badd(new RunAction(guid)); 114 addSeparator(); 115 116 badd(new BreakpointAction(guid)); 118 badd(new CatchAction(guid)); 119 badd(new ClearAction(guid)); 120 badd(new ContAction(guid)); 121 badd(new NextAction(guid)); 122 badd(new StepAction(guid)); 123 badd(new StepUpAction(guid)); 124 badd(new StepIAction(guid)); 125 addSeparator(); 126 127 badd(new SuspendAction(guid, threadGetter())); 129 badd(new ResumeAction(guid, threadGetter())); 130 badd(new WhereAction(guid, threadGetter())); 131 comboBox = comboBox(); 132 add(comboBox); 133 addSeparator(); 134 135 badd(new DumpAction(guid)); 137 badd(new EvalAction(guid)); 138 badd(new FieldsAction(guid)); 139 badd(new LocalsAction(guid)); 140 badd(new PrintAction(guid)); 141 142 add(Box.createHorizontalGlue()); 143 } 144 145 public AJDebugger getDebugger() { 146 return debugger; 147 } 148 149 150 151 public void threadDeathEvent(ThreadDeathEvent e) { 152 removeThread(e.thread()); 153 } 154 155 public void threadStartEvent(ThreadStartEvent e) { 156 addThread(e.thread()); 157 } 158 159 160 161 public void vmDeathEvent(VMDeathEvent e) { 162 clearThreads(); 163 } 164 165 public void vmDisconnectEvent(VMDisconnectEvent e) { 166 clearThreads(); 167 } 168 169 public void vmStartEvent(VMStartEvent e) { 170 clearThreads(); 171 try { 172 Iterator iter = getDebugger().vm().allThreads().iterator(); 173 while (iter.hasNext()) { 174 addThread((ThreadReference) iter.next()); 175 } 176 } catch (Exception ee) {} 177 } 178 179 JComboBox comboBox() { 180 if (comboBox == null) { 181 comboBox = new JComboBox(); 182 } 183 comboBox.addItem(new ThreadPair(ALL_THREADS, -1)); 184 comboBox.setPreferredSize(new Dimension(100, 30)); 185 return comboBox; 186 } 187 188 ThreadGetter threadGetter() { 189 return new ThreadGetter() { 190 public String getThread() { 191 AJToolBar.ThreadPair pair = 192 (AJToolBar.ThreadPair) comboBox.getSelectedItem(); 193 if (pair.id != -1) { 194 return pair.id + ""; 195 } 196 return ""; 197 } 198 }; 199 } 200 201 private void addToT(JComponent c) { 202 toolBar.add(c); 203 } 204 205 private void addT(String title) { 206 toolBar.setToolTipText(title); 207 toolBar.setName(title); 208 add(toolBar); 209 toolBar = new JToolBar(); 210 } 211 212 private void badd(AbstractActionStruct a) { 213 badd(a, null); 214 } 215 216 217 private void badd(AbstractActionStruct a, String toolTipText) { 218 badd(a, toolTipText, null, null); 219 } 220 221 private void badd(AbstractActionStruct a, String toolTipText, final String toolTipText2, 222 final GUIDebugger guid) { 223 Icon icon = (Icon) a.getValue(ICON); 224 JButton b; 225 if (guid == null) { 226 b = new JButton(icon); 227 } else { 228 b = new JButton(icon) { 229 public String getToolTipText() { 230 return guid.getDebugger().isRunning() ? toolTipText2 : super.getToolTipText(); 231 } 232 }; 233 } 234 String name = (String ) a.getValue(LABEL); 235 JLabel label = new JLabel(name); 236 String shortCutStr = KeyEvent.getKeyModifiersText(a.mask()) + "-" + 237 KeyEvent.getKeyText(a.key()); 238 JLabel shortCutLabel = new JLabel(shortCutStr); 239 Font f = label.getFont(); 240 label.setFont(new Font(f.getName(), f.getStyle(), SC_FONT_SIZE)); 241 shortCutLabel.setFont(new Font(f.getName(), f.getStyle(), FONT_SIZE)); 242 b.addActionListener(a); 243 if (toolTipText == null) { 244 b.setToolTipText(name + " [" + shortCutStr + "]"); 245 } else { 246 b.setToolTipText(toolTipText); 247 } 248 forward(b); 249 JPanel p = new JPanel(); 250 p.setLayout(new BorderLayout()); 251 p.add(b, BorderLayout.CENTER); 252 forward(p); 253 changeDim(p); 254 add(p); 255 } 256 257 void forward(JComponent c) { 258 c.setNextFocusableComponent(ComponentRepository.getCommandLine()); 259 } 260 261 void changeDim(JComponent b) { 262 Dimension d = b.getPreferredSize(); 263 double w = d.getWidth(); 264 double h = d.getHeight(); 265 double wp = WIDTH_FACTOR * w; 266 double hp = HEIGHT_FACTOR * h; 267 Dimension newD = new Dimension((int) wp,(int) h); 268 b.setMaximumSize(newD); 269 b.setMinimumSize(newD); 270 b.setPreferredSize(newD); 271 } 272 273 private Action action(final String str, int icon) { 274 return new AbstractAction(str, AJIcons.getIcon(icon)) { 275 public void actionPerformed(ActionEvent e) { 276 execute(str); 277 } 278 }; 279 } 280 281 void execute(String str) { 282 CommandLine cl = ComponentRepository.getCommandLine(); 283 if (cl != null) { 284 cl.executeCommand(str); 285 } 286 } 287 288 289 void clearThreads() { 290 if (comboBox != null) { 291 comboBox.removeAllItems(); 292 } 293 comboBox = comboBox(); 294 } 295 296 void addThread(ThreadReference threadRef) { 297 addThread(threadRef.name(), threadRef.uniqueID()); 298 } 299 300 void addThread(String name, long id) { 301 if (comboBox == null) return; 302 ThreadPair newPair = new ThreadPair(name, id); 303 int i = 1; 304 int size = comboBox.getModel().getSize(); 305 long pid = -1; 306 ThreadPair pair = null; 307 while (i < size && 308 (pid = ((ThreadPair) comboBox.getModel().getElementAt(i)).id) < id) { 309 i++; 310 } 311 if (pid != id ) { 312 comboBox.insertItemAt(newPair, i); 313 } 314 } 315 316 void removeThread(ThreadReference threadRef) { 317 removeThread(threadRef.name(), threadRef.uniqueID()); 318 } 319 320 void removeThread(String name, long id) { 321 if (comboBox == null) return; 322 for (int i = 0; i < comboBox.getModel().getSize(); i++) { 323 ThreadPair pair = (ThreadPair) comboBox.getModel().getElementAt(i); 324 if (pair.name.equals(name) && pair.id == id) { 325 comboBox.removeItemAt(i); 326 } 327 } 328 } 329 330 static class ThreadPair { 331 String name; 332 long id; 333 ThreadPair(String name, long id) { 334 this.name = name; 335 this.id = id; 336 } 337 public String toString() { 338 return name + (id == -1 ? "" : " (" + id + ")"); 339 } 340 public boolean equals(Object o) { 341 if (!(o instanceof ThreadPair)) { 342 return super.equals(o); 343 } 344 return ( ((ThreadPair) o).name.equals(name) && 345 ((ThreadPair) o).id == id ); 346 } 347 } 348 349 public static String d() { return "Toolbar"; } 350 public String toString() { return d(); } 351 } 352 | Popular Tags |