KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > BreakpointsPanel


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import java.util.Vector JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Enumeration JavaDoc;
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 JavaDoc;
45 import java.awt.event.*;
46 import java.awt.*;
47 import javax.swing.text.BadLocationException JavaDoc;
48 import javax.swing.text.Position JavaDoc;
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 /**
61  * Panel for displaying the breakpoints. This class is a swing view class and hence should only be accessed from the
62  * event-handling thread.
63  * @version $Id$
64  */

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   /** Constructs a new breakpoints panel.
73    * This is swing view class and hence should only be accessed from the event-handling thread.
74    * @param frame the MainFrame
75    */

76   public BreakpointsPanel(MainFrame frame) {
77     super(frame, "Breakpoints");
78     _model.getBreakpointManager().addListener(new RegionManagerListener<Breakpoint>() {
79       /** Called when a breakpoint is set in a document. Adds the breakpoint to the tree of breakpoints.
80        * Must be executed in event thread.
81        * @param bp the breakpoint
82        */

83       public void regionAdded(final Breakpoint bp, int index) {
84         assert EventQueue.isDispatchThread();
85         addRegion(bp);
86       }
87       
88       /**
89        * Called when a breakpoint is changed.
90        * Removes the breakpoint from the tree of breakpoints.
91        * @param bp the breakpoint
92        * @param index the index of the breakpoint
93        */

94       public void regionChanged(final Breakpoint bp, int index) {
95         // Only change GUI from event-dispatching thread
96
Runnable JavaDoc doCommand = new Runnable JavaDoc() {
97           public void run() {
98             String JavaDoc 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             // Find the document node for this region
109
Enumeration JavaDoc 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                 // Find the correct line start offset node for this breakpoint
115
Enumeration JavaDoc existingRegions = doc.children();
116                 while (existingRegions.hasMoreElements()) {
117                   DefaultMutableTreeNode existing = (DefaultMutableTreeNode)existingRegions.nextElement();
118                   @SuppressWarnings JavaDoc("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       /**
137        * Called when a breakpoint is removed from a document.
138        * Removes the breakpoint from the tree of breakpoints.
139        * @param bp the breakpoint
140        */

141       public void regionRemoved(final Breakpoint bp) {
142         removeRegion(bp);
143       }
144     });
145     _debugger = _model.getDebugger();
146   }
147   
148   /** Action performed when the Enter key is pressed. Should be overridden. */
149   protected void performDefaultAction() {
150     goToRegion();
151   }
152   
153   /** Creates the buttons for controlling the regions. Should be overridden. */
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   /** Update button state and text. */
196   protected void updateButtons() {
197     ArrayList JavaDoc<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   /** Makes the popup menu actions. Should be overridden if additional actions besides "Go to" and "Remove" are added. */
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   /** Go to region. */
226   protected void goToRegion() {
227     ArrayList JavaDoc<Breakpoint> bps = getSelectedRegions();
228     if (bps.size() == 1) {
229       _debugger.scrollToSource(bps.get(0));
230     }
231   }
232   
233   /** Toggle breakpoint's enable/disable flag. */
234   protected void enableDisableBreakpoint() {
235     final ArrayList JavaDoc<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 JavaDoc, Breakpoint>() {
240           public Object JavaDoc apply(Breakpoint bp) {
241             bp.setEnabled(newState);
242             return null;
243           }
244         });
245       }
246     }
247   }
248   
249   
250   /** Factory method to create user objects put in the tree.
251    * If subclasses extend RegionTreeUserObj, they need to override this method. */

252   protected RegionTreeUserObj<Breakpoint> makeRegionTreeUserObj(Breakpoint bp) {
253     return new BreakpointRegionTreeUserObj(bp);
254   }
255
256   /** Class that gets put into the tree. The toString() method determines what's displayed in the three. */
257   protected static class BreakpointRegionTreeUserObj extends RegionTreeUserObj<Breakpoint> {
258     public BreakpointRegionTreeUserObj (Breakpoint bp) { super(bp); }
259     public String JavaDoc toString() {
260       final StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
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 JavaDoc bpe) { /* ignore, just don't display line */ }
270       } finally { _region.getDocument().releaseReadLock(); }
271       return sb.toString();
272     }
273     public boolean equals(Object JavaDoc 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