1 33 34 package edu.rice.cs.drjava.ui; 35 36 import java.util.Vector ; 37 import java.util.ArrayList ; 38 import java.util.Enumeration ; 39 40 import javax.swing.*; 41 import javax.swing.event.*; 42 import javax.swing.tree.*; 43 import javax.swing.table.*; 44 import javax.swing.text.BadLocationException ; 45 import java.awt.event.*; 46 import java.awt.*; 47 import javax.swing.text.BadLocationException ; 48 import javax.swing.text.Position ; 49 50 import edu.rice.cs.util.Lambda; 51 import edu.rice.cs.drjava.model.RegionManagerListener; 52 import edu.rice.cs.drjava.model.DocumentRegion; 53 import edu.rice.cs.drjava.model.debug.*; 54 import edu.rice.cs.drjava.model.OpenDefinitionsDocument; 55 import edu.rice.cs.drjava.config.*; 56 import edu.rice.cs.drjava.model.definitions.ClassNameNotFoundException; 57 import edu.rice.cs.util.swing.Utilities; 58 import edu.rice.cs.util.UnexpectedException; 59 60 65 public class BreakpointsPanel extends RegionsTreePanel<Breakpoint> { 66 protected JButton _goToButton; 67 protected JButton _enableDisableButton; 68 protected JButton _removeButton; 69 protected JButton _removeAllButton; 70 protected final Debugger _debugger; 71 72 76 public BreakpointsPanel(MainFrame frame) { 77 super(frame, "Breakpoints"); 78 _model.getBreakpointManager().addListener(new RegionManagerListener<Breakpoint>() { 79 83 public void regionAdded(final Breakpoint bp, int index) { 84 assert EventQueue.isDispatchThread(); 85 addRegion(bp); 86 } 87 88 94 public void regionChanged(final Breakpoint bp, int index) { 95 Runnable doCommand = new Runnable () { 97 public void run() { 98 String name = ""; 99 try { 100 name = bp.getDocument().getQualifiedClassName(); 101 } 102 catch (ClassNameNotFoundException cnnfe) { 103 name = bp.getDocument().toString(); 104 } 105 106 DefaultMutableTreeNode regDocNode = new DefaultMutableTreeNode(name); 107 108 Enumeration documents = _regionRootNode.children(); 110 boolean found = false; 111 while ((!found) && (documents.hasMoreElements())) { 112 DefaultMutableTreeNode doc = (DefaultMutableTreeNode)documents.nextElement(); 113 if (doc.getUserObject().equals(regDocNode.getUserObject())) { 114 Enumeration existingRegions = doc.children(); 116 while (existingRegions.hasMoreElements()) { 117 DefaultMutableTreeNode existing = (DefaultMutableTreeNode)existingRegions.nextElement(); 118 @SuppressWarnings ("unchecked") RegionTreeUserObj<Breakpoint> uo = 119 (RegionTreeUserObj<Breakpoint>)existing.getUserObject(); 120 if (uo.region().getStartOffset()==bp.getStartOffset()) { 121 Breakpoint r = uo.region(); 122 r.setEnabled(bp.isEnabled()); 123 ((DefaultTreeModel)_regTree.getModel()).nodeChanged(existing); 124 found = true; 125 break; 126 } 127 } 128 } 129 } 130 updateButtons(); 131 } 132 }; 133 Utilities.invokeLater(doCommand); 134 } 135 136 141 public void regionRemoved(final Breakpoint bp) { 142 removeRegion(bp); 143 } 144 }); 145 _debugger = _model.getDebugger(); 146 } 147 148 149 protected void performDefaultAction() { 150 goToRegion(); 151 } 152 153 154 protected JComponent[] makeButtons() { 155 Action goToAction = new AbstractAction("Go to") { 156 public void actionPerformed(ActionEvent ae) { 157 goToRegion(); 158 } 159 }; 160 _goToButton = new JButton(goToAction); 161 162 Action enableDisableAction = new AbstractAction("Disable") { 163 public void actionPerformed(ActionEvent ae) { 164 enableDisableBreakpoint(); 165 } 166 }; 167 _enableDisableButton = new JButton(enableDisableAction); 168 169 Action removeAction = new AbstractAction("Remove") { 170 public void actionPerformed(ActionEvent ae) { 171 for (Breakpoint bp: getSelectedRegions()) { 172 _model.getBreakpointManager().removeRegion(bp); 173 } 174 } 175 }; 176 _removeButton = new JButton(removeAction); 177 178 Action removeAllAction = new AbstractAction("Remove All") { 179 public void actionPerformed(ActionEvent ae) { 180 _model.getBreakpointManager().clearRegions(); 181 } 182 }; 183 _removeAllButton = new JButton(removeAllAction); 184 185 JComponent[] buts = new JComponent[] { 186 _enableDisableButton, 187 _goToButton, 188 _removeButton, 189 _removeAllButton 190 }; 191 192 return buts; 193 } 194 195 196 protected void updateButtons() { 197 ArrayList <Breakpoint> regs = getSelectedRegions(); 198 _goToButton.setEnabled(regs.size() == 1); 199 _removeButton.setEnabled(regs.size() > 0); 200 _removeAllButton.setEnabled(_regionRootNode != null && _regionRootNode.getDepth() > 0); 201 _enableDisableButton.setEnabled(regs.size()>0); 202 if (regs.size() > 0) { 203 if (regs.get(0).isEnabled()) _enableDisableButton.setText("Disable"); 204 else _enableDisableButton.setText("Enable"); 205 } 206 _removeAllButton.setEnabled(_regionRootNode != null && _regionRootNode.getDepth() > 0); 207 } 208 209 210 protected AbstractAction[] makePopupMenuActions() { 211 AbstractAction[] acts = new AbstractAction[] { 212 new AbstractAction("Go to") { 213 public void actionPerformed(ActionEvent e) { goToRegion(); } 214 }, 215 216 new AbstractAction("Remove") { 217 public void actionPerformed(ActionEvent e) { 218 for (Breakpoint bp: getSelectedRegions()) _model.getBreakpointManager().removeRegion(bp); 219 } 220 } 221 }; 222 return acts; 223 } 224 225 226 protected void goToRegion() { 227 ArrayList <Breakpoint> bps = getSelectedRegions(); 228 if (bps.size() == 1) { 229 _debugger.scrollToSource(bps.get(0)); 230 } 231 } 232 233 234 protected void enableDisableBreakpoint() { 235 final ArrayList <Breakpoint> bps = getSelectedRegions(); 236 if (bps.size()>0) { 237 final boolean newState = !bps.get(0).isEnabled(); 238 for (Breakpoint bp: bps) { 239 _model.getBreakpointManager().changeRegion(bp, new Lambda<Object , Breakpoint>() { 240 public Object apply(Breakpoint bp) { 241 bp.setEnabled(newState); 242 return null; 243 } 244 }); 245 } 246 } 247 } 248 249 250 252 protected RegionTreeUserObj<Breakpoint> makeRegionTreeUserObj(Breakpoint bp) { 253 return new BreakpointRegionTreeUserObj(bp); 254 } 255 256 257 protected static class BreakpointRegionTreeUserObj extends RegionTreeUserObj<Breakpoint> { 258 public BreakpointRegionTreeUserObj (Breakpoint bp) { super(bp); } 259 public String toString() { 260 final StringBuilder sb = new StringBuilder (); 261 _region.getDocument().acquireReadLock(); 262 try { 263 sb.append(lineNumber()); 264 try { 265 if (!_region.isEnabled()) { sb.append(" (disabled)"); } 266 sb.append(": "); 267 int length = Math.min(120, _region.getEndOffset()-_region.getStartOffset()); 268 sb.append(_region.getDocument().getText(_region.getStartOffset(), length).trim()); 269 } catch(BadLocationException bpe) { } 270 } finally { _region.getDocument().releaseReadLock(); } 271 return sb.toString(); 272 } 273 public boolean equals(Object other) { 274 BreakpointRegionTreeUserObj o = (BreakpointRegionTreeUserObj)other; 275 return (o.region().getDocument().equals(region().getDocument())) && 276 (o.region().getStartOffset()==region().getStartOffset()) && 277 (o.region().getEndOffset()==region().getEndOffset()) && 278 (o.region().isEnabled()==region().isEnabled()); 279 } 280 } 281 } 282 | Popular Tags |