KickJava   Java API By Example, From Geeks To Geeks.

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


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

33
34 package edu.rice.cs.drjava.model.definitions.indent;
35
36 import edu.rice.cs.drjava.model.AbstractDJDocument;
37 import edu.rice.cs.util.UnexpectedException;
38
39 import javax.swing.text.BadLocationException JavaDoc;
40
41 /**
42  * Indents the current line in the document to the indent level of the
43  * start of the statement previous to the one the cursor is currently on,
44  * plus the given suffix string.
45  *
46  * @version $Id: ActionStartPrevStmtPlus.java 3901 2006-06-30 05:28:11Z rcartwright $
47  */

48 public class ActionStartPrevStmtPlus extends IndentRuleAction {
49   private String JavaDoc _suffix;
50   private boolean _useColon;
51
52   /**
53    * Constructs a new rule with the given suffix string.
54    * @param suffix String to append to indent level of brace
55    * @param colonIsDelim whether to include colons as statement delimiters
56    */

57   public ActionStartPrevStmtPlus(String JavaDoc suffix, boolean colonIsDelim) {
58     super();
59     _suffix = suffix;
60     _useColon = colonIsDelim;
61   }
62
63   /**
64    * Properly indents the line that the caret is currently on.
65    * Replaces all whitespace characters at the beginning of the
66    * line with the appropriate spacing or characters.
67    *
68    * @param doc AbstractDJDocument containing the line to be indented.
69    * @return true if the caller should update the current location itself,
70    * false if the indenter has already handled this
71    */

72   public boolean indentLine(AbstractDJDocument doc, int reason) {
73     boolean supResult = super.indentLine(doc, reason);
74     String JavaDoc indent = "";
75     int here = doc.getCurrentLocation();
76     
77     // Find end of previous statement (or end of case statement)
78
char[] delims = {';', '{', '}'};
79     int lineStart = doc.getLineStartPos(here);
80     int prevDelimiterPos;
81     try {
82       prevDelimiterPos = doc.findPrevDelimiter(lineStart, delims);
83     } catch (BadLocationException JavaDoc e) {
84       // Should not happen
85
throw new UnexpectedException(e);
86     }
87     
88     // For DOCSTART, align to left margin
89
if (prevDelimiterPos <= AbstractDJDocument.DOCSTART) {
90       doc.setTab(_suffix, here);
91       return supResult;
92     }
93     
94     try {
95       char delim = doc.getText(prevDelimiterPos, 1).charAt(0);
96       char[] ws = {' ', '\t', '\n', ';'};
97       if (delim == ';') {
98         int testPos = doc.findPrevCharPos(prevDelimiterPos, ws);
99         if (doc.getText(testPos,1).charAt(0) == '}') {
100           prevDelimiterPos = testPos;
101         }
102       }
103     } catch (BadLocationException JavaDoc e) {
104       //do nothing
105
}
106     
107     try {
108       // Jump over {-} region if delimiter was a close brace.
109
char delim = doc.getText(prevDelimiterPos, 1).charAt(0);
110       
111       if (delim == '}') {
112         //BraceReduction reduced = doc.getReduced();
113
//we're pretty sure the doc is in sync.
114
doc.resetReducedModelLocation();
115         
116         int dist = prevDelimiterPos - here + 1;
117         
118         doc.move(dist);
119         prevDelimiterPos -= doc.balanceBackward() - 1;
120         doc.move(-dist);
121         
122       }
123     }
124     catch (BadLocationException JavaDoc e) { throw new UnexpectedException(e); }
125     
126     
127     // Get indent of prev statement
128
try {
129       // Include colons as end of statement (ie. "case")
130
char[] indentDelims;
131       char[] indentDelimsWithColon = {';', '{', '}', ':'};
132       char[] indentDelimsWithoutColon = {';', '{', '}'};
133       if (_useColon) indentDelims = indentDelimsWithColon;
134       else indentDelims = indentDelimsWithoutColon;
135       
136       indent = doc.getIndentOfCurrStmt(prevDelimiterPos, indentDelims);
137       
138     } catch (BadLocationException JavaDoc e) {
139       throw new UnexpectedException(e);
140     }
141     
142     indent = indent + _suffix;
143     doc.setTab(indent, here);
144     return supResult;
145   }
146
147 // private boolean _isPrevNonWSCharEqualTo(AbstractDJDocument doc,int pos,char c) {
148
// try {
149
// int prevPos = doc.findPrevNonWSCharPos(pos);
150
// if (prevPos < 0) return false;
151
// return (doc.getText(prevPos,1).charAt(0) == c);
152
// }
153
// catch (BadLocationException e) {
154
// return false;
155
// }
156
// }
157
}
158
159
Popular Tags