KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > indent > ActionStartPrevLinePlusMultilinePreserve


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project:
4  * http://sourceforge.net/projects/drjava/ or http://www.drjava.org/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2004 JavaPLT group at Rice University (javaplt@rice.edu)
9  * All rights reserved.
10  *
11  * Developed by: Java Programming Languages Team
12  * Rice University
13  * http://www.cs.rice.edu/~javaplt/
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal with the Software without restriction, including without
18  * limitation the rights to use, copy, modify, merge, publish, distribute,
19  * sublicense, and/or sell copies of the Software, and to permit persons to
20  * whom the Software is furnished to do so, subject to the following
21  * conditions:
22  *
23  * - Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimers.
25  * - Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimers in the
27  * documentation and/or other materials provided with the distribution.
28  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the
29  * names of its contributors may be used to endorse or promote products
30  * derived from this Software without specific prior written permission.
31  * - Products derived from this software may not be called "DrJava" nor
32  * use the term "DrJava" as part of their names without prior written
33  * permission from the JavaPLT group. For permission, write to
34  * javaplt@rice.edu.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39  * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42  * OTHER DEALINGS WITH THE SOFTWARE.
43  *
44 END_COPYRIGHT_BLOCK*/

45
46 package edu.rice.cs.drjava.model.definitions.indent;
47
48 import javax.swing.text.*;
49 import edu.rice.cs.util.UnexpectedException;
50 import edu.rice.cs.drjava.model.AbstractDJDocument;
51
52 /** Indents the current line in the document to the indent level of the
53   * start of the previous line, preserving any text on the current line,
54   * and adds several lines of text at that indent level,
55   * and moves the cursor to a particular line and position.
56   * @version $Id: ActionStartPrevLinePlusMultilinePreserve.java 3993 2006-09-02 20:23:38Z rcartwright $
57   */

58 class ActionStartPrevLinePlusMultilinePreserve extends IndentRuleAction {
59   private String JavaDoc[] _suffices;
60   private int _cursorLine, _cursorPos, _psrvLine, _psrvPos;
61
62   /** Creates a multiline insert rule, properly preserving any text on current line.
63     * @param suffices the new lines to be added
64     * @param cursorLine the line on which to place the cursor
65     * @param cursorPos the character within the line string before which to place
66     * the cursor
67     * @param psrvLine the line in suffices on which to place the preserved text
68     * @param psrvPos the character within the line string in suffices before which
69     * to place the preserved text
70     * @throws IllegalArgumentException if the integer params are negative or
71     * outside the appropriate bounds
72     */

73   public ActionStartPrevLinePlusMultilinePreserve(String JavaDoc suffices[],
74                                                   int cursorLine, int cursorPos,
75                                                   int psrvLine, int psrvPos) {
76     _suffices = suffices;
77     _cursorLine = cursorLine;
78     _cursorPos = cursorPos;
79     _psrvLine = psrvLine;
80     _psrvPos = psrvPos;
81   }
82
83   /**
84    * Forwards the call to the enclosed ActionStartPrevLinePlusMultiline _a
85    * @param doc AbstractDJDocument containing the line to be indented.
86    * @return this is always false, since we are updating the cursor location
87    */

88   public boolean indentLine(AbstractDJDocument doc, int reason) {
89     try {
90       // copy it so any changes are not remembered
91
String JavaDoc[] suffices = new String JavaDoc[_suffices.length];
92       for(int i = 0; i < _suffices.length; i++)
93         suffices[i] = _suffices[i];
94       
95       // get the absolute boundaries of the text on this line
96
int here = doc.getCurrentLocation();
97       int lineStart = doc.getLineStartPos(here);
98       int lineEnd = doc.getLineEndPos(here);
99
100       // cut the original text out of the current line
101
int lineLength = lineEnd-lineStart;
102       String JavaDoc preserved = doc.getText(lineStart, lineLength);
103       doc.remove(lineStart, lineLength);
104
105       // Paste the cut text in the correct place in the suffices array
106
String JavaDoc prefix = suffices[_psrvLine].substring(0,_psrvPos);
107       String JavaDoc suffix = suffices[_psrvLine].substring(_psrvPos);
108       suffices[_psrvLine] = prefix + preserved + suffix;
109
110       // forward the rest of the work to the other rule
111
ActionStartPrevLinePlusMultiline a;
112       //for(int i = 0; i < _suffices.length; i++)
113
// javax.swing.JOptionPane.showMessageDialog(null, "\""+suffices[i]+"\"", "suffices["+i+"]",javax.swing.JOptionPane.PLAIN_MESSAGE);;
114
a = new ActionStartPrevLinePlusMultiline(suffices, _cursorLine, _cursorPos);
115       return a.indentLine(doc, reason);
116     }
117     catch (BadLocationException e) {
118       // Shouldn't happen
119
throw new UnexpectedException(e);
120     }
121   }
122 }
123
Popular Tags