Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 320   Methods: 17
NCLOC: 216   Classes: 6
 
 Source file Conditionals Statements Methods TOTAL
DefinitionsEditorKit.java 20.5% 25.7% 58.8% 26.4%
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 edu.rice.cs.drjava.ui.AbstractDJPane;
 40    import edu.rice.cs.util.UnexpectedException;
 41   
 42    import javax.swing.text.*;
 43    import javax.swing.Action;
 44    import java.awt.event.ActionEvent;
 45    import edu.rice.cs.drjava.model.GlobalEventNotifier;
 46   
 47    /** The editor kit class for editing Java source files. It functions as the controller in an MVC hierarchy. It also
 48    * implements a factory for new documents and a factory for Views (the things that render the document). May only be
 49    * used as the EditorKit for panes extending AbstractDJPane. In fact, only used as the EditorKit for DefintionsPanes.
 50    * Stored as a field of DefinitionsPane.
 51    * @version $Id: DefinitionsEditorKit.java 5439 2011-08-11 17:13:04Z rcartwright $
 52    */
 53    public class DefinitionsEditorKit extends StyledEditorKit {
 54   
 55    public static final String DELIMITERS = "!@%^&*()-=+[]{};:'\",.<>/?";
 56   
 57    private GlobalEventNotifier _notifier;
 58    private Action[] _actions;
 59   
 60    /** Creates a new editor kit with the given listeners.
 61    * @param notifier Keeps track of the listeners to the model
 62    */
 63  607 public DefinitionsEditorKit(GlobalEventNotifier notifier) {
 64  607 _notifier = notifier;
 65  607 Action[] supActions = super.getActions();
 66  607 _actions = new Action[supActions.length];
 67  607 for(int i = 0; i < _actions.length; ++i) {
 68  44311 Action a = supActions[i];
 69  44311 Object name = a.getValue("Name");
 70  44311 if (name.equals(beginWordAction))
 71  607 _actions[i] = new BeginWordAction(beginWordAction, false);
 72  43704 else if (name.equals(endWordAction))
 73  607 _actions[i] = new EndWordAction(endWordAction, false);
 74  43097 else if (name.equals(nextWordAction))
 75  607 _actions[i] = new NextWordAction(nextWordAction, false);
 76  42490 else if (name.equals(previousWordAction))
 77  607 _actions[i] = new PreviousWordAction(previousWordAction, false);
 78  41883 else if (name.equals(selectionNextWordAction))
 79  607 _actions[i] = new NextWordAction(selectionNextWordAction, true);
 80  41276 else if (name.equals(selectionPreviousWordAction))
 81  607 _actions[i] = new PreviousWordAction(selectionPreviousWordAction, true);
 82  40669 else if (name.equals(selectWordAction))
 83  607 _actions[i] = new SelectWordAction();
 84  40062 else _actions[i] = a;
 85    }
 86    }
 87   
 88  93 public Action[] getActions() { return _actions; }
 89   
 90    private static ViewFactory _factory = new ViewFactory() {
 91  597 public View create(Element elem) {
 92    // The following line is for performance analysis only!
 93    // return new WrappedPlainView(elem, true);
 94  597 return new ColoringView(elem);
 95    }
 96    };
 97   
 98    /** Creates a new DefinitionsDocument. Formerly named createDefaultDocument() because the view (DefinitionsPane)
 99    * would create a DefinitionsDocument by default when it was constructed. However, this default document was
 100    * immediately discarded because a DefinitionsDocument for the constructed DefinitionsPane already existed.
 101    * Unfortunately, JEditorPane does not have a constructor that takes a Document as input. We conceivably could
 102    * design this EditorKit to return the pre-existing document when the JEditorPane requests a new one, but the
 103    * EditorKit is specified by a static field of DefinitionsPane so there is no clean way to install the proper
 104    * EditorKit before the JEditorPane constructor asks for the Document.
 105    *
 106    * As an easier alternative, we just let the DefaultEditorKit return a PlainDocument (much lighter weight),
 107    * which is thrown away when the true DefinitionsDocument is assigned
 108    *
 109    * Improvements to this approach are welcome... :)
 110    */
 111  0 public DefinitionsDocument createNewDocument() { return _createDefaultTypedDocument(); }
 112   
 113    /** Creates a new DefinitionsDocument.
 114    * @return a new DefinitionsDocument.
 115    */
 116  0 private DefinitionsDocument _createDefaultTypedDocument() { return new DefinitionsDocument(_notifier); }
 117   
 118    /** Get the MIME content type of the document
 119    * @return "text/java"
 120    */
 121  187 public String getContentType() { return "text/java"; }
 122   
 123    /** We want to use our ColoringView to render text, so here we return a factory that creates ColoringViews. */
 124  780 public final ViewFactory getViewFactory() { return _factory; }
 125   
 126    /** Brings the cursor to the beginning of the current word separated by whitespace or a delimiting character. */
 127    static class BeginWordAction extends TextAction {
 128    private boolean _select;
 129   
 130  1214 BeginWordAction(String nm, boolean b) {
 131  1214 super(nm);
 132  1214 _select = b;
 133    }
 134   
 135  0 public void actionPerformed(ActionEvent e) {
 136  0 AbstractDJPane target = (AbstractDJPane) getTextComponent(e);
 137  0 if (target != null) {
 138  0 try {
 139  0 int offs = target.getCaretPosition();
 140  0 final String text = target.getDocument().getText(0, offs);
 141  0 while(offs > 0) {
 142  0 char chPrev = text.charAt(offs - 1);
 143  0 if ((DELIMITERS.indexOf(chPrev) >= 0) || (Character.isWhitespace(chPrev))) {
 144  0 break;
 145    }
 146  0 --offs;
 147  0 if (offs == 0) break; // otherwise offs-1 below generates an index out of bounds
 148  0 char ch = text.charAt(offs);
 149  0 chPrev = text.charAt(offs - 1);
 150  0 if ((DELIMITERS.indexOf(ch) >= 0) || (DELIMITERS.indexOf(chPrev) >= 0)
 151    || Character.isWhitespace(ch) || Character.isWhitespace(chPrev)) {
 152  0 break;
 153    }
 154    }
 155  0 if (_select) {
 156  0 target.moveCaretPosition(offs);
 157    } else {
 158  0 target.setCaretPosition(offs);
 159    }
 160    }
 161  0 catch(BadLocationException ble) { throw new UnexpectedException(ble); }
 162    }
 163    }
 164    }
 165   
 166    /** Sets the cursor at the end of the current word separated by whitespace or a delimiting character. */
 167    static class EndWordAction extends TextAction {
 168    private boolean _select;
 169   
 170  1214 EndWordAction(String nm, boolean b) {
 171  1214 super(nm);
 172  1214 _select = b;
 173    }
 174   
 175  0 public void actionPerformed(ActionEvent e) {
 176  0 AbstractDJPane target = (AbstractDJPane) getTextComponent(e);
 177  0 if (target != null) {
 178  0 try {
 179  0 int offs = target.getCaretPosition();
 180  0 final int iOffs = offs;
 181  0 final String text = target.getDocument().getText(iOffs,target.getDocument().getLength()-iOffs);
 182  0 while((offs-iOffs)<text.length()-1) {
 183  0 ++offs;
 184  0 char ch = text.charAt(offs-iOffs);
 185  0 if ((DELIMITERS.indexOf(ch) >= 0) || Character.isWhitespace(ch)) {
 186  0 break;
 187    }
 188    }
 189  0 if (_select) {
 190  0 target.moveCaretPosition(offs);
 191    } else {
 192  0 target.setCaretPosition(offs);
 193    }
 194    }
 195  0 catch(BadLocationException ble) { throw new UnexpectedException(ble); }
 196    }
 197    }
 198    }
 199   
 200    /** Moves the cursor to the beginning of the previous word. If the cursor is currently inside of a word, moves it to
 201    * the beginning of that word. Otherwise, moves the cursor to the beginning of the previous word.
 202    * Also stops at delimiting characters and at the end of a line
 203    */
 204    static class PreviousWordAction extends TextAction {
 205    private boolean _select;
 206   
 207  1214 PreviousWordAction(String nm, boolean b) {
 208  1214 super(nm);
 209  1214 _select = b;
 210    }
 211   
 212  0 public void actionPerformed(ActionEvent e) {
 213  0 AbstractDJPane target = (AbstractDJPane) getTextComponent(e);
 214  0 if (target != null) {
 215  0 try {
 216  0 int offs = target.getCaretPosition();
 217  0 final String text = target.getDocument().getText(0,offs);
 218  0 while(offs > 0) {
 219  0 --offs;
 220  0 if (offs == 0) break;
 221  0 char ch = text.charAt(offs);
 222  0 char chPrev = text.charAt(offs - 1);
 223  0 if (Character.isWhitespace(ch) && Character.isWhitespace(chPrev) && ch!='\n') continue;
 224  0 else if (DELIMITERS.indexOf(ch) >= 0 || DELIMITERS.indexOf(chPrev) >= 0 ||
 225    (offs >= 2 && Character.isWhitespace(chPrev) && !Character.isWhitespace(text.charAt(offs-2)) && ch!='\n'))
 226  0 break;
 227  0 else if (Character.isWhitespace(chPrev) && !Character.isWhitespace(ch)) break;
 228  0 else if (!Character.isWhitespace(chPrev) && ch == '\n') break;
 229  0 else if (Character.isWhitespace(chPrev) && ch=='\n') { // compensate for space at the end of a line
 230  0 while(Character.isWhitespace(chPrev) && (offs > 0)) {
 231  0 --offs;
 232  0 ch = text.charAt(offs);
 233  0 chPrev = text.charAt(offs - 1);
 234    }
 235  0 break;
 236    }
 237    }
 238  0 if (_select) target.moveCaretPosition(offs);
 239  0 else target.setCaretPosition(offs);
 240    }
 241  0 catch(BadLocationException ble) { throw new UnexpectedException(ble); }
 242    }
 243    }
 244    }
 245   
 246    /** Moves the cursor from the current word to the beginning of the next word, stopping at delimiting characters and
 247    * at the end of a line
 248    */
 249    static class NextWordAction extends TextAction {
 250    private boolean _select;
 251   
 252  1214 NextWordAction(String nm, boolean b) {
 253  1214 super(nm);
 254  1214 _select = b;
 255    }
 256   
 257  0 public void actionPerformed(ActionEvent e) {
 258  0 AbstractDJPane target = (AbstractDJPane) getTextComponent(e);
 259  0 if (target != null) {
 260  0 try {
 261  0 int offs = target.getCaretPosition();
 262  0 final int iOffs = offs;
 263  0 final String text = target.getDocument().getText(iOffs,target.getDocument().getLength() - iOffs);
 264  0 final int len = text.length();
 265  0 while((offs-iOffs) < len) {
 266  0 ++offs;
 267  0 if (offs-iOffs == len)
 268  0 break;
 269  0 char ch = text.charAt(offs-iOffs);
 270  0 char chPrev = text.charAt(offs-iOffs - 1);
 271  0 if ((DELIMITERS.indexOf(ch) >= 0) ||
 272    (DELIMITERS.indexOf(chPrev) >= 0) ||
 273    Character.isWhitespace(chPrev) ||
 274    ch == '\n') {
 275  0 while((offs-iOffs<len) && Character.isWhitespace(ch) && ch != '\n') {
 276  0 if (DELIMITERS.indexOf(chPrev) >= 0)
 277  0 break;
 278  0 ++offs;
 279  0 ch = text.charAt(offs-iOffs);
 280    }
 281  0 if (ch == '\n' && Character.isWhitespace(text.charAt(offs - iOffs - 1))) continue;
 282  0 else break;
 283    }
 284    //used to fix incorrect behavior when a space is at the end of a line
 285  0 if (!Character.isWhitespace(chPrev) && Character.isWhitespace(ch)) {
 286  0 int offs0 = offs;
 287  0 while((offs-iOffs)<(len-1) && ch!='\n' && Character.isWhitespace(ch)) {
 288  0 ++offs;
 289  0 ch = text.charAt(offs-iOffs);
 290  0 chPrev = text.charAt(offs-iOffs - 1);
 291    }
 292  0 offs = offs0;
 293  0 if (ch=='\n') break;
 294  0 else continue;
 295    }
 296    }
 297  0 if (_select) target.moveCaretPosition(offs);
 298  0 else target.setCaretPosition(offs);
 299    }
 300  0 catch(BadLocationException ble) { throw new UnexpectedException(ble); }
 301    }
 302    }
 303    }
 304   
 305    /** Defines the action for word selection as in when double-clicking a word. */
 306    static class SelectWordAction extends TextAction {
 307    private Action start;
 308    private Action end;
 309   
 310  607 public SelectWordAction() {
 311  607 super(selectWordAction);
 312  607 start = new BeginWordAction("pigdog", false);
 313  607 end = new EndWordAction("pigdog", true);
 314    }
 315  0 public void actionPerformed(ActionEvent e) {
 316  0 start.actionPerformed(e);
 317  0 end.actionPerformed(e);
 318    }
 319    }
 320    }