Clover coverage report - DrJava Test Coverage (drjava-20110828-r5448)
Coverage timestamp: Sun Aug 28 2011 03:13:33 CDT
file stats: LOC: 277   Methods: 10
NCLOC: 151   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
ColoringView.java 27.8% 37.3% 40% 36.2%
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.model.definitions;
 38   
 39    import javax.swing.text.*;
 40    import java.awt.*;
 41    import javax.swing.event.DocumentEvent;
 42    import java.util.ArrayList;
 43   
 44    import edu.rice.cs.drjava.DrJava;
 45    import edu.rice.cs.drjava.model.*;
 46    import edu.rice.cs.drjava.model.repl.InteractionsDJDocument;
 47    import edu.rice.cs.drjava.config.OptionConstants;
 48    import edu.rice.cs.drjava.config.OptionEvent;
 49    import edu.rice.cs.drjava.config.OptionListener;
 50    import edu.rice.cs.drjava.model.definitions.reducedmodel.*;
 51    import edu.rice.cs.util.UnexpectedException;
 52    import edu.rice.cs.util.text.EditDocumentInterface;
 53   
 54    /** This view class renders text on the screen using the reduced model info. By extending WrappedPlainView, we only
 55    * have to override the parts we want to. Here we only override drawUnselectedText. We may want to override
 56    * drawSelectedText at some point. As of 2002/06/17, we now extend PlainView because WrappedPlainView was causing
 57    * bugs related to resizing the viewport of the definitions scroll pane.
 58    *
 59    * @version $Id: ColoringView.java 5175 2010-01-20 08:46:32Z mgricken $
 60    */
 61    public class ColoringView extends PlainView implements OptionConstants {
 62   
 63    public static Color COMMENTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_COMMENT_COLOR);
 64    public static Color DOUBLE_QUOTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_DOUBLE_QUOTED_COLOR);
 65    public static Color SINGLE_QUOTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_SINGLE_QUOTED_COLOR);
 66    public static Color NORMAL_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_NORMAL_COLOR);
 67    public static Color KEYWORD_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_KEYWORD_COLOR);
 68    public static Color NUMBER_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_NUMBER_COLOR);
 69    public static Color TYPE_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_TYPE_COLOR);
 70    public static Font MAIN_FONT = DrJava.getConfig().getSetting(FONT_MAIN);
 71   
 72    //Interactions only colors
 73    public static Color INTERACTIONS_SYSTEM_ERR_COLOR = DrJava.getConfig().getSetting(SYSTEM_ERR_COLOR);
 74    public static Color INTERACTIONS_SYSTEM_IN_COLOR = DrJava.getConfig().getSetting(SYSTEM_IN_COLOR);
 75    public static Color INTERACTIONS_SYSTEM_OUT_COLOR = DrJava.getConfig().getSetting(SYSTEM_OUT_COLOR);
 76    //Renamed as to avoid confusion with the one in option constants
 77    public static Color ERROR_COLOR = DrJava.getConfig().getSetting(INTERACTIONS_ERROR_COLOR);
 78    public static Color DEBUGGER_COLOR = DrJava.getConfig().getSetting(DEBUG_MESSAGE_COLOR);
 79   
 80    /** Constructs a new coloring view.
 81    * @param elem the element
 82    */
 83  597 public ColoringView(Element elem) {
 84  597 super(elem);
 85   
 86    // Listen for updates to configurable colors
 87  597 final ColorOptionListener col = new ColorOptionListener();
 88  597 final FontOptionListener fol = new FontOptionListener();
 89   
 90  597 Document doc = getDocument();
 91  597 if (doc instanceof AbstractDJDocument) {
 92    // delete the old color listeners, because they're hanging onto the wrong coloringview
 93    // add color listeners to highlight keywords etc
 94  146 DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_COMMENT_COLOR, col);
 95  146 DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_DOUBLE_QUOTED_COLOR, col);
 96  146 DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_SINGLE_QUOTED_COLOR, col);
 97  146 DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_NORMAL_COLOR, col);
 98  146 DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_KEYWORD_COLOR, col);
 99  146 DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_NUMBER_COLOR, col);
 100  146 DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_TYPE_COLOR, col);
 101  146 DrJava.getConfig().addOptionListener( OptionConstants.FONT_MAIN, fol);
 102   
 103  146 DrJava.getConfig().addOptionListener( OptionConstants.SYSTEM_ERR_COLOR, col);
 104  146 DrJava.getConfig().addOptionListener( OptionConstants.SYSTEM_IN_COLOR, col);
 105  146 DrJava.getConfig().addOptionListener( OptionConstants.SYSTEM_OUT_COLOR, col);
 106  146 DrJava.getConfig().addOptionListener( OptionConstants.INTERACTIONS_ERROR_COLOR, col);
 107  146 DrJava.getConfig().addOptionListener( OptionConstants.DEBUG_MESSAGE_COLOR, col);
 108   
 109    }
 110   
 111  597 if (doc instanceof DefinitionsDocument) {
 112    // remove the listeners when the document closes
 113  146 ((DefinitionsDocument)doc).addDocumentClosedListener(new DocumentClosedListener() {
 114  58 public void close() {
 115  58 DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_COMMENT_COLOR, col);
 116  58 DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_DOUBLE_QUOTED_COLOR, col);
 117  58 DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_SINGLE_QUOTED_COLOR, col);
 118  58 DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_NORMAL_COLOR, col);
 119  58 DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_KEYWORD_COLOR, col);
 120  58 DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_NUMBER_COLOR, col);
 121  58 DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_TYPE_COLOR, col);
 122  58 DrJava.getConfig().removeOptionListener( OptionConstants.FONT_MAIN, fol);
 123  58 DrJava.getConfig().removeOptionListener( OptionConstants.SYSTEM_ERR_COLOR, col);
 124  58 DrJava.getConfig().removeOptionListener( OptionConstants.SYSTEM_IN_COLOR, col);
 125  58 DrJava.getConfig().removeOptionListener( OptionConstants.SYSTEM_OUT_COLOR, col);
 126  58 DrJava.getConfig().removeOptionListener( OptionConstants.INTERACTIONS_ERROR_COLOR, col);
 127  58 DrJava.getConfig().removeOptionListener( OptionConstants.DEBUG_MESSAGE_COLOR, col);
 128    }
 129    });
 130    }
 131    }
 132   
 133    /** Renders the given range in the model as normal unselected text. Note that this text is all on one line.
 134    * The superclass deals with breaking lines and such. So all we have to do here is draw the text on [p0,p1) in the
 135    * model. We have to start drawing at (x,y), and the function returns the x coordinate when we're done.
 136    * @param g The graphics context
 137    * @param x The starting X coordinate
 138    * @param y The starting Y coordinate
 139    * @param start The beginning position in the model
 140    * @param end The ending position in the model
 141    * @return The x coordinate at the end of the range
 142    * @throws BadLocationException If the range is invalid
 143    */
 144  0 protected int drawUnselectedText(Graphics g, int x, int y, int start, int end) throws BadLocationException {
 145    // If there's nothing to show, don't do anything!
 146    // For some reason I don't understand we tend to get called sometimes to render a zero-length area.
 147  0 if (start == end) return x;
 148   
 149    // doc might be a PlainDocument (when AbstractDJPane is first constructed).
 150    // See comments for DefinitionsEditorKit.createNewDocument() for details.
 151  0 Document doc = getDocument();
 152  0 if (! (doc instanceof AbstractDJDocument)) return x; // return if there is no AbstracDJDocument
 153   
 154  0 final AbstractDJDocument _doc = (AbstractDJDocument) doc;
 155   
 156  0 ArrayList<HighlightStatus> stats = _doc.getHighlightStatus(start, end);
 157  0 if (stats.size() < 1) throw new UnexpectedException("GetHighlightStatus returned nothing!");
 158   
 159  0 for (HighlightStatus stat: stats) {
 160  0 int location = stat.getLocation();
 161  0 int length = stat.getLength();
 162   
 163    // If this highlight status extends past p1, end at p1
 164  0 if (location + length > end) length = end - stat.getLocation();
 165   
 166  0 if (! (_doc instanceof InteractionsDJDocument) || ! ((InteractionsDJDocument)_doc).setColoring((start + end)/2, g))
 167  0 setFormattingForState(g, stat.getState());
 168  0 Segment text = getLineBuffer();
 169  0 _doc.getText(location, length, text);
 170  0 x = Utilities.drawTabbedText(text, x, y, g, this, location); // updates x on each iteration
 171    }
 172  0 return x;
 173    }
 174   
 175    /** Draws the selected text image at the specified location.
 176    * @param g The text image
 177    * @param x The x coordinate for the drawn text
 178    * @param y The y coordinate for the drawn text
 179    * @param start The beginning position in the model
 180    * @param end The end position in the model
 181    * @return The location of the end of the image (range)
 182    * @throws BadLocationException
 183    */
 184  0 protected int drawSelectedText(Graphics g, int x, int y, int start, int end) throws BadLocationException {
 185   
 186    // DrJava.consoleErr().
 187    // println("drawSelected: " + p0 + "-" + p1 + " doclen=" + _doc.getLength() + " x=" + x + " y=" + y);
 188   
 189  0 EditDocumentInterface doc = (EditDocumentInterface) getDocument();
 190  0 if (doc instanceof InteractionsDJDocument) ((InteractionsDJDocument)doc).setBoldFonts(end, g);
 191   
 192  0 return super.drawSelectedText(g, x, y, start, end);
 193    }
 194   
 195    /** Given a particular state, assign it a color.
 196    * @param g Graphics object
 197    * @param state a given state
 198    */
 199  0 private void setFormattingForState(Graphics g, int state) {
 200  0 switch (state) {
 201  0 case HighlightStatus.NORMAL:
 202  0 g.setColor(NORMAL_COLOR);
 203  0 break;
 204  0 case HighlightStatus.COMMENTED:
 205  0 g.setColor(COMMENTED_COLOR);
 206  0 break;
 207  0 case HighlightStatus.SINGLE_QUOTED:
 208  0 g.setColor(SINGLE_QUOTED_COLOR);
 209  0 break;
 210  0 case HighlightStatus.DOUBLE_QUOTED:
 211  0 g.setColor(DOUBLE_QUOTED_COLOR);
 212  0 break;
 213  0 case HighlightStatus.KEYWORD:
 214  0 g.setColor(KEYWORD_COLOR);
 215  0 break;
 216  0 case HighlightStatus.NUMBER:
 217  0 g.setColor(NUMBER_COLOR);
 218  0 break;
 219  0 case HighlightStatus.TYPE:
 220  0 g.setColor(TYPE_COLOR);
 221  0 break;
 222  0 default:
 223  0 throw new RuntimeException("Can't get color for invalid state: " + state);
 224    }
 225  0 g.setFont(MAIN_FONT);
 226    }
 227   
 228   
 229    /** Repaints the container associated with this view, if such container exists. */
 230  99 private void repaintContainer() {
 231  99 Container c = getContainer();
 232  99 if (c != null) c.repaint();
 233    }
 234   
 235    /** Called when a change occurs.
 236    * @param changes document changes
 237    * @param a a Shape
 238    * @param f a ViewFactory
 239    */
 240  99 public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
 241  99 super.changedUpdate(changes, a, f);
 242    // Make sure we redraw since something changed in the formatting
 243  99 repaintContainer();
 244    }
 245   
 246    /** Called when an OptionListener perceives a change in any of the colors */
 247  0 public void updateColors() {
 248   
 249  0 COMMENTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_COMMENT_COLOR);
 250  0 DOUBLE_QUOTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_DOUBLE_QUOTED_COLOR);
 251  0 SINGLE_QUOTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_SINGLE_QUOTED_COLOR);
 252  0 NORMAL_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_NORMAL_COLOR);
 253  0 KEYWORD_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_KEYWORD_COLOR);
 254  0 NUMBER_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_NUMBER_COLOR);
 255  0 TYPE_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_TYPE_COLOR);
 256   
 257  0 INTERACTIONS_SYSTEM_ERR_COLOR = DrJava.getConfig().getSetting(SYSTEM_ERR_COLOR);
 258  0 INTERACTIONS_SYSTEM_IN_COLOR = DrJava.getConfig().getSetting(SYSTEM_IN_COLOR);
 259  0 INTERACTIONS_SYSTEM_OUT_COLOR = DrJava.getConfig().getSetting(SYSTEM_OUT_COLOR);
 260  0 ERROR_COLOR = DrJava.getConfig().getSetting(INTERACTIONS_ERROR_COLOR);
 261  0 DEBUGGER_COLOR = DrJava.getConfig().getSetting(DEBUG_MESSAGE_COLOR);
 262   
 263    // Avoid the ColoringView that does not have a container.
 264  0 repaintContainer();
 265    }
 266   
 267    /** The OptionListeners for DEFINITIONS COLORs */
 268    private class ColorOptionListener implements OptionListener<Color> {
 269  0 public void optionChanged(OptionEvent<Color> oce) { updateColors(); }
 270    }
 271   
 272    private static class FontOptionListener implements OptionListener<Font> {
 273  0 public void optionChanged(OptionEvent<Font> oce) {
 274  0 MAIN_FONT = DrJava.getConfig().getSetting(FONT_MAIN);
 275    }
 276    }
 277    }