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 edu.rice.cs.drjava.model.AbstractDJDocument;
040: import edu.rice.cs.drjava.model.definitions.reducedmodel.IndentInfo;
041:
042: /** Aligns indentation of the current line to the character that opened the enclosing block or expression list.
043: * Optional additional whitespaces can be passed through the constructor.
044: * @version $Id: ActionBracePlus.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public class ActionBracePlus extends IndentRuleAction {
047: /** String holding the additional whitespaces to be inserted. */
048: private String _suffix;
049:
050: /** @param suffix The additional whitespaces to be inserted. */
051: public ActionBracePlus(String suffix) {
052: _suffix = suffix;
053: }
054:
055: /** Properly indents the line that the caret is currently on. Replaces all whitespace characters at the beginning of
056: * the line with the appropriate spacing or characters.<p>
057: * Preconditions: must be inside a brace.
058: * @param doc AbstractDJDocument containing the line to be indented.
059: * @return true if the caller should update the current location, false if the indenter has already done this.
060: */
061: public boolean indentLine(AbstractDJDocument doc,
062: Indenter.IndentReason reason) {
063: boolean supResult = super .indentLine(doc, reason);
064: int here = doc.getCurrentLocation();
065: int startLine = doc.getLineStartPos(here);
066: doc.setCurrentLocation(startLine);
067: IndentInfo ii = doc.getIndentInformation();
068:
069: // Check preconditions
070: if ((ii.braceType.equals("")) || (ii.distToBrace < 0)) {
071: // Can't find brace, so do nothing.
072: return supResult;
073: }
074:
075: // Find length to brace
076: int bracePos = startLine - ii.distToBrace;
077: int braceNewLine = 0;
078: if (ii.distToNewline >= 0) {
079: braceNewLine = startLine - ii.distToNewline;
080: }
081: int braceLen = bracePos - braceNewLine;
082:
083: // Create tab string
084: final StringBuilder tab = new StringBuilder(_suffix.length()
085: + braceLen);
086: for (int i = 0; i < braceLen; i++) {
087: tab.append(" ");
088: }
089: tab.append(_suffix);
090:
091: if (here > doc.getLength()) {
092: here = doc.getLength() - 1;
093: }
094: doc.setCurrentLocation(here);
095:
096: doc.setTab(tab.toString(), here);
097:
098: return supResult;
099: }
100: }
|