Clover coverage report - DrJava Test Coverage (drjava-20110828-r5448)
Coverage timestamp: Sun Aug 28 2011 03:13:33 CDT
file stats: LOC: 304   Methods: 30
NCLOC: 181   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractConsoleController.java 14.3% 60.6% 36.7% 48.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.awt.EventQueue;
 40    import java.awt.event.ActionEvent;
 41    import java.awt.Color;
 42    import java.awt.Font;
 43   
 44    import javax.swing.*;
 45    import javax.swing.text.*;
 46   
 47    import java.util.Vector;
 48   
 49    import edu.rice.cs.util.text.ConsoleDocument;
 50   
 51    import edu.rice.cs.drjava.DrJava;
 52    import edu.rice.cs.drjava.config.OptionConstants;
 53    import edu.rice.cs.drjava.config.OptionListener;
 54    import edu.rice.cs.drjava.config.OptionEvent;
 55    import edu.rice.cs.drjava.model.repl.*;
 56    import edu.rice.cs.drjava.platform.PlatformFactory;
 57    import edu.rice.cs.drjava.model.ClipboardHistoryModel;
 58   
 59    /** Abstract class that hooks a Swing console/interactions document with its Swing pane.
 60    * TODO: move interactions specific functionality to InteractionsController by creating ConsoleDJDocument class
 61    * @version $Id: AbstractConsoleController.java 5360 2010-08-13 22:36:42Z mgricken $
 62    */
 63    public abstract class AbstractConsoleController /* implements Serializable */ {
 64   
 65    /** Adapter for the Swing document used by the model.*/
 66    protected final InteractionsDJDocument _interactionsDJDocument;
 67   
 68    /** Pane from the view. */
 69    protected final InteractionsPane _pane;
 70   
 71    /** Style to use for default text. */
 72    protected final SimpleAttributeSet _defaultStyle;
 73   
 74    /** Style to use for System.out. */
 75    protected final SimpleAttributeSet _systemOutStyle;
 76   
 77    /** Style to use for System.err. */
 78    protected final SimpleAttributeSet _systemErrStyle;
 79   
 80    /** Action to change focus to previous pane. Package private for testing purposes. */
 81    volatile Action switchToPrevPaneAction;
 82   
 83    /** Action to change focus to next pane. */
 84    volatile Action switchToNextPaneAction;
 85   
 86    /** Initializes the Swing console document and Swing interactions pane. Subclasses *must* call _init() at the end
 87    * of their constructors.
 88    */
 89  203 protected AbstractConsoleController(InteractionsDJDocument doc, InteractionsPane pane) {
 90  203 _interactionsDJDocument = doc;
 91  203 _pane = pane;
 92  203 _defaultStyle = new SimpleAttributeSet();
 93  203 _systemOutStyle = new SimpleAttributeSet();
 94  203 _systemErrStyle = new SimpleAttributeSet();
 95    }
 96   
 97    /** Gets the console document for this console.*/
 98    public abstract ConsoleDocument getConsoleDoc();
 99   
 100    /** Initialization method. *Must* be called in constructor by all subclasses. */
 101  203 protected void _init() {
 102  203 _addDocumentStyles();
 103  203 _setupModel();
 104  203 _setupView();
 105    }
 106   
 107    /** Adds AttributeSets as named styles to the Swing console document. */
 108  203 protected void _addDocumentStyles() {
 109    // Default
 110  203 _interactionsDJDocument.setDocStyle(ConsoleDocument.DEFAULT_STYLE, _defaultStyle);
 111   
 112    // System.out
 113  203 _systemOutStyle.addAttributes(_defaultStyle);
 114  203 _systemOutStyle.addAttribute(StyleConstants.Foreground,
 115    DrJava.getConfig().getSetting(OptionConstants.SYSTEM_OUT_COLOR));
 116  203 _interactionsDJDocument.setDocStyle(ConsoleDocument.SYSTEM_OUT_STYLE, _systemOutStyle);
 117  203 DrJava.getConfig().addOptionListener(OptionConstants.SYSTEM_OUT_COLOR,
 118    new OptionListener<Color>() {
 119  0 public void optionChanged(OptionEvent<Color> oe) {
 120  0 _systemOutStyle.addAttribute(StyleConstants.Foreground, oe.value);
 121    }
 122    });
 123   
 124    // System.err
 125  203 _systemErrStyle.addAttributes(_defaultStyle);
 126  203 _systemErrStyle.addAttribute(StyleConstants.Foreground,
 127    DrJava.getConfig().getSetting(OptionConstants.SYSTEM_ERR_COLOR));
 128  203 _interactionsDJDocument.setDocStyle(ConsoleDocument.SYSTEM_ERR_STYLE, _systemErrStyle);
 129  203 DrJava.getConfig().addOptionListener(OptionConstants.SYSTEM_ERR_COLOR,
 130    new OptionListener<Color>() {
 131  0 public void optionChanged(OptionEvent<Color> oe) {
 132  0 _systemErrStyle.addAttribute(StyleConstants.Foreground, oe.value);
 133    }
 134    });
 135    }
 136   
 137    /** Sets up the model.*/
 138    protected abstract void _setupModel();
 139   
 140    /** Sets up the view. */
 141  203 protected void _setupView() {
 142  203 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_BEGIN_LINE), gotoPromptPosAction);
 143  203 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_BEGIN_LINE_SELECT), selectToPromptPosAction);
 144  203 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_END_LINE), gotoEndAction);
 145  203 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_END_LINE_SELECT), selectToEndAction);
 146   
 147  203 DrJava.getConfig().addOptionListener(OptionConstants.KEY_BEGIN_LINE, new OptionListener<Vector<KeyStroke>>() {
 148  0 public void optionChanged(OptionEvent<Vector<KeyStroke>> oe) {
 149  0 _pane.addActionForKeyStroke(oe.value, gotoPromptPosAction);
 150    }
 151    });
 152  203 DrJava.getConfig().addOptionListener(OptionConstants.KEY_BEGIN_LINE_SELECT, new OptionListener<Vector<KeyStroke>>() {
 153  0 public void optionChanged(OptionEvent<Vector<KeyStroke>> oe) {
 154  0 _pane.addActionForKeyStroke(oe.value, selectToPromptPosAction);
 155    }
 156    });
 157  203 DrJava.getConfig().addOptionListener(OptionConstants.KEY_END_LINE, new OptionListener<Vector<KeyStroke>>() {
 158  0 public void optionChanged(OptionEvent<Vector<KeyStroke>> oe) {
 159  0 _pane.addActionForKeyStroke(oe.value, gotoEndAction);
 160    }
 161    });
 162  203 DrJava.getConfig().addOptionListener(OptionConstants.KEY_END_LINE_SELECT, new OptionListener<Vector<KeyStroke>>() {
 163  0 public void optionChanged(OptionEvent<Vector<KeyStroke>> oe) {
 164  0 _pane.addActionForKeyStroke(oe.value, selectToEndAction);
 165    }
 166    });
 167   
 168  203 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_CUT), cutAction);
 169  203 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_COPY), copyAction);
 170  203 DrJava.getConfig().addOptionListener(OptionConstants.KEY_CUT, new OptionListener<Vector<KeyStroke>>() {
 171  0 public void optionChanged(OptionEvent<Vector<KeyStroke>> oe) {
 172  0 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_CUT), cutAction);
 173    }
 174    });
 175  203 DrJava.getConfig().addOptionListener(OptionConstants.KEY_COPY, new OptionListener<Vector<KeyStroke>>() {
 176  0 public void optionChanged(OptionEvent<Vector<KeyStroke>> oe) {
 177  0 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_COPY), copyAction);
 178    }
 179    });
 180    }
 181   
 182    /** Clears and resets the view (other than features derived from the model. */
 183  0 public void resetView() {
 184    // _pane.resetPrompts(); // NOT USED
 185    // System.err.println("Prompts.reset" + "Prompts for pane " + _pane.hashCode() + " is " + _pane.getPromptList());
 186    }
 187   
 188    /** Default cut action. */
 189    Action cutAction = new DefaultEditorKit.CutAction() {
 190  0 public void actionPerformed(ActionEvent e) {
 191   
 192  0 if (_pane.getSelectedText() != null) {
 193  0 super.actionPerformed(e);
 194  0 String s = edu.rice.cs.util.swing.Utilities.getClipboardSelection(_pane);
 195  0 if (s != null && s.length() != 0) { ClipboardHistoryModel.singleton().put(s); }
 196    }
 197    }
 198    };
 199   
 200    /** Default copy action. */
 201    Action copyAction = new DefaultEditorKit.CopyAction() {
 202  0 public void actionPerformed(ActionEvent e) {
 203  0 if (_pane.getSelectedText() != null) {
 204  0 super.actionPerformed(e);
 205  0 String s = edu.rice.cs.util.swing.Utilities.getClipboardSelection(_pane);
 206  0 if (s != null && s.length() != 0) { ClipboardHistoryModel.singleton().put(s); }
 207    }
 208    }
 209    };
 210   
 211    /** Accessor method for the InteractionsDJDocument. */
 212  0 public InteractionsDJDocument getDocumentAdapter() { return _interactionsDJDocument; }
 213   
 214    /** Accessor method for the InteractionsPane. */
 215  76 public InteractionsPane getPane() { return _pane; }
 216   
 217    /** Determines if the associated console pane is currently computing.
 218    * @return true iff the console is busy
 219    */
 220  4 protected boolean _busy() { return ! getConsoleDoc().hasPrompt(); }
 221   
 222    /** Inserts a new line at the caret position. */
 223    AbstractAction newLineAction = new AbstractAction() {
 224  1 public void actionPerformed(ActionEvent e) {
 225  1 ConsoleDocument doc = getConsoleDoc();
 226  1 doc.insertNewline(_pane.getCaretPosition());
 227    }
 228    };
 229   
 230    /** Removes all text after the prompt. */
 231    AbstractAction clearCurrentAction = new AbstractAction() {
 232  0 public void actionPerformed(ActionEvent e) { getConsoleDoc().clearCurrentInput(); }
 233    };
 234   
 235    /** Goes to the end of the current input line. */
 236    AbstractAction gotoEndAction = new AbstractAction() {
 237  0 public void actionPerformed(ActionEvent e) { moveToEnd(); }
 238    };
 239   
 240    /** Selects to the end of the current input line. */
 241    AbstractAction selectToEndAction = new AbstractAction() {
 242  0 public void actionPerformed(ActionEvent e) {
 243  0 ConsoleDocument doc = getConsoleDoc();
 244  0 _pane.moveCaretPosition(doc.getLength());
 245    }
 246    };
 247   
 248    /** Moves the caret to the prompt. */
 249    AbstractAction gotoPromptPosAction = new AbstractAction() {
 250  0 public void actionPerformed(ActionEvent e) { moveToPrompt(); }
 251    };
 252   
 253    /** Selects to the current prompt. */
 254    AbstractAction selectToPromptPosAction = new AbstractAction() {
 255  0 public void actionPerformed(ActionEvent e) {
 256  0 assert EventQueue.isDispatchThread();
 257  0 ConsoleDocument doc = getConsoleDoc();
 258    // Selects the text between the old pos and the prompt
 259  0 _pane.moveCaretPosition(doc.getPromptPos());
 260    }
 261    };
 262   
 263    /** Moves the pane's caret to the end of the document. Only affects reduced_model not the document model. */
 264  14 void moveToEnd() {
 265  14 assert EventQueue.isDispatchThread();
 266  14 int len = getConsoleDoc().getLength();
 267  14 _pane.setCaretPosition(len);
 268    // setCachedCaretPos(len);
 269    }
 270   
 271    /** Moves the pane's caret to the document's prompt. Only affects reduced_model not the document model. */
 272  5 void moveToPrompt() {
 273  5 assert EventQueue.isDispatchThread();
 274  5 int pos = getConsoleDoc().getPromptPos();
 275  5 _pane.setCaretPosition(pos);
 276    }
 277   
 278  38 public void setPrevPaneAction(Action a) {
 279  38 switchToPrevPaneAction = a;
 280   
 281    // We do this here since switchToPrevPaneAction is set after the constructor is called.
 282  38 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_PREVIOUS_PANE),
 283    switchToPrevPaneAction);
 284  38 DrJava.getConfig().addOptionListener(OptionConstants.KEY_PREVIOUS_PANE, new OptionListener<Vector<KeyStroke>>() {
 285  0 public void optionChanged(OptionEvent<Vector<KeyStroke>> oe) {
 286  0 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_PREVIOUS_PANE), switchToPrevPaneAction);
 287    }
 288    });
 289    }
 290   
 291  38 public void setNextPaneAction(Action a) {
 292  38 switchToNextPaneAction = a;
 293   
 294    // We do this here since switchToNextPaneAction is set after the
 295    // constructor is called.
 296  38 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_NEXT_PANE),
 297    switchToNextPaneAction);
 298  38 DrJava.getConfig().addOptionListener(OptionConstants.KEY_NEXT_PANE, new OptionListener<Vector<KeyStroke>>() {
 299  0 public void optionChanged(OptionEvent<Vector<KeyStroke>> oe) {
 300  0 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_NEXT_PANE), switchToNextPaneAction);
 301    }
 302    });
 303    }
 304    }