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: /** Indents the current line in the document to the indent level of the
044: * start of the previous line, preserving any text on the current line,
045: * and adds several lines of text at that indent level,
046: * and moves the cursor to a particular line and position.
047: * @version $Id: ActionStartPrevLinePlusMultilinePreserve.java 4255 2007-08-28 19:17:37Z mgricken $
048: */
049: class ActionStartPrevLinePlusMultilinePreserve extends IndentRuleAction {
050: private String[] _suffices;
051: private int _cursorLine, _cursorPos, _psrvLine, _psrvPos;
052:
053: /** Creates a multiline insert rule, properly preserving any text on current line.
054: * @param suffices the new lines to be added
055: * @param cursorLine the line on which to place the cursor
056: * @param cursorPos the character within the line string before which to place
057: * the cursor
058: * @param psrvLine the line in suffices on which to place the preserved text
059: * @param psrvPos the character within the line string in suffices before which
060: * to place the preserved text
061: * @throws IllegalArgumentException if the integer params are negative or
062: * outside the appropriate bounds
063: */
064: public ActionStartPrevLinePlusMultilinePreserve(String suffices[],
065: int cursorLine, int cursorPos, int psrvLine, int psrvPos) {
066: _suffices = suffices;
067: _cursorLine = cursorLine;
068: _cursorPos = cursorPos;
069: _psrvLine = psrvLine;
070: _psrvPos = psrvPos;
071: }
072:
073: /**
074: * Forwards the call to the enclosed ActionStartPrevLinePlusMultiline _a
075: * @param doc AbstractDJDocument containing the line to be indented.
076: * @param reason The reason that the indentation is taking place
077: * @return this is always false, since we are updating the cursor location
078: */
079: public boolean indentLine(AbstractDJDocument doc,
080: Indenter.IndentReason reason) {
081: try {
082: // copy it so any changes are not remembered
083: String[] suffices = new String[_suffices.length];
084: for (int i = 0; i < _suffices.length; i++)
085: suffices[i] = _suffices[i];
086:
087: // get the absolute boundaries of the text on this line
088: int here = doc.getCurrentLocation();
089: int lineStart = doc.getLineStartPos(here);
090: int lineEnd = doc.getLineEndPos(here);
091:
092: // cut the original text out of the current line
093: int lineLength = lineEnd - lineStart;
094: String preserved = doc.getText(lineStart, lineLength);
095: doc.remove(lineStart, lineLength);
096:
097: // Paste the cut text in the correct place in the suffices array
098: String prefix = suffices[_psrvLine].substring(0, _psrvPos);
099: String suffix = suffices[_psrvLine].substring(_psrvPos);
100: suffices[_psrvLine] = prefix + preserved + suffix;
101:
102: // forward the rest of the work to the other rule
103: ActionStartPrevLinePlusMultiline a;
104: //for(int i = 0; i < _suffices.length; i++)
105: // javax.swing.JOptionPane.showMessageDialog(null, "\""+suffices[i]+"\"", "suffices["+i+"]",javax.swing.JOptionPane.PLAIN_MESSAGE);;
106: a = new ActionStartPrevLinePlusMultiline(suffices,
107: _cursorLine, _cursorPos);
108: return a.indentLine(doc, reason);
109: } catch (BadLocationException e) {
110: // Shouldn't happen
111: throw new UnexpectedException(e);
112: }
113: }
114: }
|