001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions.indent;
038:
039: import javax.swing.text.*;
040: import edu.rice.cs.util.UnexpectedException;
041: import edu.rice.cs.drjava.model.AbstractDJDocument;
042:
043: /**
044: * Indents the current line in the document to the indent level of the
045: * start of the previous line, adds several lines of text at that indent level,
046: * and moves the cursor to a particular line and position.
047: * @version $Id: ActionStartPrevLinePlusMultiline.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: class ActionStartPrevLinePlusMultiline extends IndentRuleAction {
050: private String[] _suffices;
051: private int _line = 0;
052: // private int _position = 0;
053: private int _offset = 0;
054:
055: /**
056: * Creates a multiline insert rule. It should be noted that although the suffices
057: * are referred to as "lines", this class simply appends the strings with a
058: * number of spaces for padding. Any newline characters you intend to place
059: * in the document must be explicitly placed within the input strings.
060: * Typically, all but the last "line" will have a '\n' character at the end.
061: * @param suffices the new lines to be added
062: * @param line the line on which to place the cursor
063: * @param position the character within the line string before which to place
064: * the cursor
065: * @throws IllegalArgumentException if the integer params are negative or
066: * outside the appropriate bounds
067: */
068: public ActionStartPrevLinePlusMultiline(String suffices[],
069: int line, int position) {
070: _suffices = suffices;
071:
072: // do bounds checking up-front
073: if ((line >= 0) && (line < suffices.length)) {
074: _line = line;
075: } else {
076: throw new IllegalArgumentException(
077: "The specified line was outside the bounds of the specified array.");
078: }
079:
080: if ((position < 0) || (position > suffices[line].length())) {
081: throw new IllegalArgumentException(
082: "The specified position was not within the bounds of the specified line.");
083: }
084: // else {
085: // _position = position;
086: // }
087:
088: // pre-compute the relative offset (without indents) of the new position
089: for (int i = 0; i < line; i++) {
090: _offset += _suffices[i].length();
091: }
092: _offset += position;
093: }
094:
095: /**
096: * Indents the line according to the previous line, with the suffix lines added
097: * and the cursor moved to a specific location.
098: * If on the first line, indent is set to 0.
099: * @param doc AbstractDJDocument containing the line to be indented.
100: * @param reason The reason that the indentation is taking place
101: * @return this is always false, since we are updating the cursor location
102: */
103: public boolean indentLine(AbstractDJDocument doc,
104: Indenter.IndentReason reason) {
105: super .indentLine(doc, reason);
106: try {
107: // Find start of line
108: int here = doc.getCurrentLocation();
109: int startLine = doc.getLineStartPos(here);
110:
111: if (startLine > AbstractDJDocument.DOCSTART) {
112: // Find prefix of previous line
113: int startPrevLine = doc.getLineStartPos(startLine - 1);
114: int firstChar = doc.getLineFirstCharPos(startPrevLine);
115: String prefix = doc.getText(startPrevLine, firstChar
116: - startPrevLine);
117:
118: // indent and add the suffices
119: for (int i = 0; i < _suffices.length; i++) {
120: doc.setTab(prefix + _suffices[i], here);
121: here += prefix.length() + _suffices[i].length();
122: }
123:
124: // move the cursor to the appropriate position
125: int newPos = startLine + _offset
126: + (prefix.length() * (_line + 1));
127: doc.setCurrentLocation(newPos);
128: } else {
129: // On first line
130: for (int i = 0; i < _suffices.length; i++) {
131: doc.setTab(_suffices[i], here);
132: here += _suffices[i].length();
133: }
134: }
135: return false;
136: } catch (BadLocationException e) {
137: // Shouldn't happen
138: throw new UnexpectedException(e);
139: }
140: }
141: }
|