Clover coverage report - DrJava Test Coverage (drjava-20110828-r5448)
Coverage timestamp: Sun Aug 28 2011 03:13:33 CDT
file stats: LOC: 226   Methods: 21
NCLOC: 130   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
BreakpointsPanel.java 7.1% 37.3% 19% 28.7%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.ui;
 38   
 39    import java.util.ArrayList;
 40   
 41    import javax.swing.*;
 42    import javax.swing.tree.*;
 43    import javax.swing.text.BadLocationException;
 44    import java.awt.event.*;
 45    import java.awt.*;
 46   
 47    import edu.rice.cs.plt.lambda.Lambda;
 48    import edu.rice.cs.drjava.model.RegionManager;
 49    import edu.rice.cs.drjava.model.RegionManagerListener;
 50    import edu.rice.cs.drjava.model.debug.*;
 51   
 52    /** Panel for displaying the breakpoints. This class is a swing view class and hence should only be accessed from the
 53    * event-handling thread.
 54    * @version $Id: BreakpointsPanel.java 5236 2010-04-27 01:43:36Z mgricken $
 55    */
 56    public class BreakpointsPanel extends RegionsTreePanel<Breakpoint> {
 57    protected JButton _goToButton;
 58    protected JButton _enableDisableButton;
 59    protected JButton _removeButton;
 60    protected JButton _removeAllButton;
 61    protected final Debugger _debugger;
 62   
 63    /** Constructs a new breakpoints panel.
 64    * This is swing view class and hence should only be accessed from the event-handling thread.
 65    * @param frame the MainFrame
 66    */
 67  38 public BreakpointsPanel(MainFrame frame, RegionManager<Breakpoint> breakpointManager) {
 68  38 super(frame, "Breakpoints", breakpointManager);
 69    // TODO: consolidate the following listener with the MainFrame Breakpoint listener
 70  38 _regionManager.addListener(new RegionManagerListener<Breakpoint>() {
 71    /** Called when a breakpoint is set in a document. Adds the breakpoint to the tree of breakpoints.
 72    * Must be executed in event thread.
 73    * @param bp the breakpoint
 74    */
 75  0 public void regionAdded(final Breakpoint bp) {
 76  0 assert EventQueue.isDispatchThread();
 77  0 addRegion(bp);
 78    }
 79   
 80    /** Called when a breakpoint is changed.
 81    * Removes the breakpoint from the tree of breakpoints.
 82    * @param bp the breakpoint
 83    */
 84  0 public void regionChanged(final Breakpoint bp) {
 85  0 assert EventQueue.isDispatchThread();
 86   
 87  0 DefaultMutableTreeNode regNode = _regionToTreeNode.get(bp);
 88  0 ((DefaultTreeModel)_regTree.getModel()).nodeChanged(regNode);
 89    }
 90   
 91    /** Called when a breakpoint is removed from a document.
 92    * Removes the breakpoint from the tree of breakpoints.
 93    * @param bp the breakpoint
 94    */
 95  0 public void regionRemoved(final Breakpoint bp) { removeRegion(bp); }
 96    });
 97  38 _debugger = _model.getDebugger();
 98    }
 99   
 100    /** Action performed when the Enter key is pressed. Should be overridden. */
 101  0 protected void performDefaultAction() { goToRegion(); }
 102   
 103    /** Creates the buttons for controlling the regions. Should be overridden. */
 104  38 protected JComponent[] makeButtons() {
 105  38 Action goToAction = new AbstractAction("Go to") {
 106  0 public void actionPerformed(ActionEvent ae) {
 107  0 goToRegion();
 108    }
 109    };
 110  38 _goToButton = new JButton(goToAction);
 111   
 112  38 Action enableDisableAction = new AbstractAction("Disable") {
 113  0 public void actionPerformed(ActionEvent ae) {
 114  0 enableDisableBreakpoint();
 115    }
 116    };
 117  38 _enableDisableButton = new JButton(enableDisableAction);
 118   
 119  38 Action removeAction = new AbstractAction("Remove") {
 120  0 public void actionPerformed(ActionEvent ae) {
 121    // startChanging();
 122  0 for (Breakpoint bp: getSelectedRegions()) _regionManager.removeRegion(bp);
 123    // finishChanging();
 124    }
 125    };
 126  38 _removeButton = new JButton(removeAction);
 127   
 128  38 Action removeAllAction = new AbstractAction("Remove All") {
 129  0 public void actionPerformed(ActionEvent ae) {
 130    // startChanging();
 131  0 _regionManager.clearRegions();
 132    // finishChanging();
 133    }
 134    };
 135  38 _removeAllButton = new JButton(removeAllAction);
 136   
 137  38 JComponent[] buts = new JComponent[] {
 138    _enableDisableButton,
 139    _goToButton,
 140    _removeButton,
 141    _removeAllButton
 142    };
 143   
 144  38 return buts;
 145    }
 146   
 147    /** Update button state and text. */
 148  38 protected void _updateButtons() {
 149  38 ArrayList<Breakpoint> regs = getSelectedRegions();
 150  38 _goToButton.setEnabled(regs.size() == 1);
 151  38 _removeButton.setEnabled(regs.size() > 0);
 152  38 _removeAllButton.setEnabled(_rootNode != null && _rootNode.getDepth() > 0);
 153  38 _enableDisableButton.setEnabled(regs.size() > 0);
 154  38 if (regs.size() > 0) {
 155  0 if (regs.get(0).isEnabled()) _enableDisableButton.setText("Disable");
 156  0 else _enableDisableButton.setText("Enable");
 157    }
 158  38 _removeAllButton.setEnabled(_rootNode != null && _rootNode.getDepth() > 0);
 159    }
 160   
 161    /** Overloaded for BreakpointsPanel, do not close the panel if the tree becomes empty. */
 162  0 protected void closeIfEmpty() {
 163    // do not close the panel if the tree becomes empty
 164    }
 165   
 166    /** Makes the popup menu actions. Should be overridden if additional actions besides "Go to" and "Remove" are added. */
 167  38 protected AbstractAction[] makePopupMenuActions() {
 168  38 AbstractAction[] acts = new AbstractAction[] {
 169    new AbstractAction("Go to") {
 170  0 public void actionPerformed(ActionEvent e) { goToRegion(); }
 171    },
 172   
 173    new AbstractAction("Remove") {
 174  0 public void actionPerformed(ActionEvent e) {
 175  0 for (Breakpoint bp: getSelectedRegions()) _regionManager .removeRegion(bp);
 176    }
 177    }
 178    };
 179  38 return acts;
 180    }
 181   
 182    /** Go to region. */
 183  0 protected void goToRegion() {
 184  0 ArrayList<Breakpoint> bps = getSelectedRegions();
 185  0 if (bps.size() == 1) _debugger.scrollToSource(bps.get(0));
 186    }
 187   
 188    /** Toggle breakpoint's enable/disable flag. */
 189  0 protected void enableDisableBreakpoint() {
 190  0 final ArrayList<Breakpoint> bps = getSelectedRegions();
 191  0 if (bps.size() > 0) {
 192  0 final boolean newState = !bps.get(0).isEnabled();
 193  0 for (Breakpoint bp: bps) {
 194  0 _regionManager.changeRegion(bp, new Lambda<Breakpoint,Object>() {
 195  0 public Object value(Breakpoint bp) {
 196  0 bp.setEnabled(newState);
 197  0 return null;
 198    }
 199    });
 200    }
 201    }
 202    }
 203   
 204   
 205    /** Factory method to create user objects put in the tree.
 206    * If subclasses extend RegionTreeUserObj, they need to override this method. */
 207  0 protected RegionTreeUserObj<Breakpoint> makeRegionTreeUserObj(Breakpoint bp) {
 208  0 return new BreakpointRegionTreeUserObj(bp);
 209    }
 210   
 211    /** Class that gets put into the tree. The toString() method determines what's displayed in the three. */
 212    protected static class BreakpointRegionTreeUserObj extends RegionTreeUserObj<Breakpoint> {
 213  0 public BreakpointRegionTreeUserObj (Breakpoint bp) { super(bp); }
 214  0 public String toString() {
 215  0 final StringBuilder sb = new StringBuilder();
 216  0 sb.append(lineNumber());
 217  0 try {
 218  0 if (!_region.isEnabled()) { sb.append(" (disabled)"); }
 219  0 sb.append(": ");
 220  0 int length = Math.min(120, _region.getEndOffset()-_region.getStartOffset());
 221  0 sb.append(_region.getDocument().getText(_region.getStartOffset(), length).trim());
 222    } catch(BadLocationException bpe) { /* ignore, just don't display line */ }
 223  0 return sb.toString();
 224    }
 225    }
 226    }