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, plus the given suffix, then backs up to a
046: * specific cursor position.
047: * @version $Id: ActionStartPrevLinePlusBackup.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: class ActionStartPrevLinePlusBackup extends IndentRuleAction {
050: private String _suffix;
051: private int _position = 0;
052:
053: /**
054: * Rule that repeats the indentation from the previous line, plus a string,
055: * then moves the cursor to a specified location.
056: * @param suffix The string to be added
057: * @param position the character within the suffix string before which to
058: * place the cursor
059: * @throws IllegalArgumentException if the position is negative or
060: * outside the bounds of the suffix string
061: */
062: public ActionStartPrevLinePlusBackup(String suffix, int position) {
063: _suffix = suffix;
064:
065: if ((position >= 0) && (position <= suffix.length())) {
066: _position = position;
067: } else {
068: throw new IllegalArgumentException(
069: "The specified position was not within the bounds of the suffix.");
070: }
071: }
072:
073: /**
074: * Indents the line according to the previous line, with the suffix string added,
075: * then backs up the cursor position a number of characters.
076: * If on the first line, indent is set to 0.
077: * @param doc AbstractDJDocument containing the line to be indented.
078: * @param The reason that the indentation is taking place
079: * @return this is always false, since we are updating the cursor location
080: */
081: public boolean indentLine(AbstractDJDocument doc,
082: Indenter.IndentReason reason) {
083: super .indentLine(doc, reason);
084: try {
085: // Find start of line
086: int here = doc.getCurrentLocation();
087: int startLine = doc.getLineStartPos(here);
088:
089: if (startLine > AbstractDJDocument.DOCSTART) {
090: // Find prefix of previous line
091: int startPrevLine = doc.getLineStartPos(startLine - 1);
092: int firstChar = doc.getLineFirstCharPos(startPrevLine);
093: String prefix = doc.getText(startPrevLine, firstChar
094: - startPrevLine);
095:
096: // indent and add the suffix
097: doc.setTab(prefix + _suffix, here);
098:
099: // move the cursor to the new position
100: doc.setCurrentLocation(startLine + prefix.length()
101: + _position);
102: } else {
103: // On first line
104: doc.setTab(_suffix, here);
105:
106: // move the cursor to the new position
107: doc.setCurrentLocation(here + _position);
108: }
109:
110: return false;
111: } catch (BadLocationException e) {
112: // Shouldn't happen
113: throw new UnexpectedException(e);
114: }
115: }
116: }
|