|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ActionStartPrevLinePlusMultiline.java | 50% | 74.1% | 100% | 68.3% |
|
||||||||||||||
| 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.indent; | |
| 38 | ||
| 39 | import javax.swing.text.*; | |
| 40 | import edu.rice.cs.util.UnexpectedException; | |
| 41 | import edu.rice.cs.drjava.model.AbstractDJDocument; | |
| 42 | ||
| 43 | /** | |
| 44 | * Indents the current line in the document to the indent level of the | |
| 45 | * start of the previous line, adds several lines of text at that indent level, | |
| 46 | * and moves the cursor to a particular line and position. | |
| 47 | * @version $Id: ActionStartPrevLinePlusMultiline.java 5236 2010-04-27 01:43:36Z mgricken $ | |
| 48 | */ | |
| 49 | class ActionStartPrevLinePlusMultiline extends IndentRuleAction { | |
| 50 | private String[] _suffices; | |
| 51 | private int _line = 0; | |
| 52 | // private int _position = 0; | |
| 53 | private int _offset = 0; | |
| 54 | ||
| 55 | /** Creates a multiline insert rule. It should be noted that although the suffices | |
| 56 | * are referred to as "lines", this class simply appends the strings with a | |
| 57 | * number of spaces for padding. Any newline characters you intend to place | |
| 58 | * in the document must be explicitly placed within the input strings. | |
| 59 | * Typically, all but the last "line" will have a '\n' character at the end. | |
| 60 | * @param suffices the new lines to be added | |
| 61 | * @param line the line on which to place the cursor | |
| 62 | * @param position the character within the line string before which to place | |
| 63 | * the cursor | |
| 64 | * @throws IllegalArgumentException if the integer params are negative or | |
| 65 | * outside the appropriate bounds | |
| 66 | */ | |
| 67 | 10 | public ActionStartPrevLinePlusMultiline(String suffices[], |
| 68 | int line, int position) { | |
| 69 | 10 | _suffices = suffices; |
| 70 | ||
| 71 | // do bounds checking up-front | |
| 72 | 10 | if ((line >= 0) && (line < suffices.length)) { |
| 73 | 10 | _line = line; |
| 74 | } | |
| 75 | else { | |
| 76 | 0 | throw new IllegalArgumentException |
| 77 | ("The specified line was outside the bounds of the specified array."); | |
| 78 | } | |
| 79 | ||
| 80 | 10 | if ((position < 0) || (position > suffices[line].length())) { |
| 81 | 0 | throw new IllegalArgumentException |
| 82 | ("The specified position was not within the bounds of the specified line."); | |
| 83 | } | |
| 84 | ||
| 85 | // pre-compute the relative offset (without indents) of the new position | |
| 86 | 10 | for (int i = 0; i < line; i++) { |
| 87 | 0 | _offset += _suffices[i].length(); |
| 88 | } | |
| 89 | 10 | _offset += position; |
| 90 | } | |
| 91 | ||
| 92 | /** Indents the line according to the previous line, with the suffix lines added and the cursor moved to a specific | |
| 93 | * location. If on the first line, indent is set to 0. Only runs in event thread. | |
| 94 | * @param doc AbstractDJDocument containing the line to be indented. | |
| 95 | * @param reason The reason that the indentation is taking place | |
| 96 | * @return this is always false, since we are updating the cursor location | |
| 97 | */ | |
| 98 | 10 | public boolean indentLine(AbstractDJDocument doc, Indenter.IndentReason reason) { |
| 99 | 10 | super.indentLine(doc, reason); |
| 100 | 10 | try { |
| 101 | // Find start of line | |
| 102 | 10 | int here = doc.getCurrentLocation(); |
| 103 | 10 | int startLine = doc._getLineStartPos(here); |
| 104 | ||
| 105 | 10 | if (startLine > 0) { |
| 106 | // Find prefix of previous line | |
| 107 | 10 | int startPrevLine = doc._getLineStartPos(startLine - 1); |
| 108 | 10 | int firstChar = doc._getLineFirstCharPos(startPrevLine); |
| 109 | 10 | String prefix = doc.getText(startPrevLine, firstChar - startPrevLine); |
| 110 | ||
| 111 | // indent and add the suffices | |
| 112 | 10 | for (int i = 0; i < _suffices.length; i++) { |
| 113 | 20 | doc.setTab(prefix + _suffices[i], here); |
| 114 | 20 | here += prefix.length() + _suffices[i].length(); |
| 115 | } | |
| 116 | ||
| 117 | // move the cursor to the appropriate position | |
| 118 | 10 | int newPos = startLine + _offset + (prefix.length() * (_line + 1)); |
| 119 | 10 | doc.setCurrentLocation(newPos); |
| 120 | } | |
| 121 | else { | |
| 122 | // On first line | |
| 123 | 0 | for (int i = 0; i < _suffices.length; i++) { |
| 124 | 0 | doc.setTab(_suffices[i], here); |
| 125 | 0 | here += _suffices[i].length(); |
| 126 | } | |
| 127 | } | |
| 128 | 10 | return false; |
| 129 | } | |
| 130 | catch (BadLocationException e) { | |
| 131 | // Shouldn't happen | |
| 132 | 0 | throw new UnexpectedException(e); |
| 133 | } | |
| 134 | } | |
| 135 | } |
|
||||||||||