KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.text.*;
37 import edu.rice.cs.util.UnexpectedException;
38 import edu.rice.cs.drjava.model.AbstractDJDocument;
39
40 /**
41  * Indents the current line in the document to the indent level of the
42  * start of the previous line, adds several lines of text at that indent level,
43  * and moves the cursor to a particular line and position.
44  * @version $Id: ActionStartPrevLinePlusMultiline.java 3901 2006-06-30 05:28:11Z rcartwright $
45  */

46 class ActionStartPrevLinePlusMultiline extends IndentRuleAction {
47   private String JavaDoc[] _suffices;
48   private int _line = 0;
49   // private int _position = 0;
50
private int _offset = 0;
51
52   /**
53    * Creates a multiline insert rule. It should be noted that although the suffices
54    * are referred to as "lines", this class simply appends the strings with a
55    * number of spaces for padding. Any newline characters you intend to place
56    * in the document must be explicitly placed within the input strings.
57    * Typically, all but the last "line" will have a '\n' character at the end.
58    * @param suffices the new lines to be added
59    * @param line the line on which to place the cursor
60    * @param position the character within the line string before which to place
61    * the cursor
62    * @throws IllegalArgumentException if the integer params are negative or
63    * outside the appropriate bounds
64    */

65   public ActionStartPrevLinePlusMultiline(String JavaDoc suffices[],
66                                           int line, int position) {
67     _suffices = suffices;
68     
69     // do bounds checking up-front
70
if ((line >= 0) && (line < suffices.length)) {
71       _line = line;
72     }
73     else {
74       throw new IllegalArgumentException JavaDoc
75         ("The specified line was outside the bounds of the specified array.");
76     }
77     
78     if ((position < 0) || (position > suffices[line].length())) {
79       throw new IllegalArgumentException JavaDoc
80         ("The specified position was not within the bounds of the specified line.");
81     }
82 // else {
83
// _position = position;
84
// }
85

86     
87     // pre-compute the relative offset (without indents) of the new position
88
for (int i = 0; i < line; i++) {
89       _offset += _suffices[i].length();
90     }
91     _offset += position;
92   }
93   
94   /**
95    * Indents the line according to the previous line, with the suffix lines added
96    * and the cursor moved to a specific location.
97    * If on the first line, indent is set to 0.
98    * @param doc AbstractDJDocument containing the line to be indented.
99    * @return this is always false, since we are updating the cursor location
100    */

101   public boolean indentLine(AbstractDJDocument doc, int reason) {
102     super.indentLine(doc, reason);
103     try {
104       // Find start of line
105
int here = doc.getCurrentLocation();
106       int startLine = doc.getLineStartPos(here);
107
108       if (startLine > AbstractDJDocument.DOCSTART) {
109         // Find prefix of previous line
110
int startPrevLine = doc.getLineStartPos(startLine - 1);
111         int firstChar = doc.getLineFirstCharPos(startPrevLine);
112         String JavaDoc prefix = doc.getText(startPrevLine, firstChar - startPrevLine);
113         
114         // indent and add the suffices
115
for (int i = 0; i < _suffices.length; i++) {
116           doc.setTab(prefix + _suffices[i], here);
117           here += prefix.length() + _suffices[i].length();
118         }
119         
120         // move the cursor to the appropriate position
121
int newPos = startLine + _offset + (prefix.length() * (_line + 1));
122         doc.setCurrentLocation(newPos);
123       }
124       else {
125         // On first line
126
for (int i = 0; i < _suffices.length; i++) {
127           doc.setTab(_suffices[i], here);
128           here += _suffices[i].length();
129         }
130       }
131       return false;
132     }
133     catch (BadLocationException e) {
134       // Shouldn't happen
135
throw new UnexpectedException(e);
136     }
137   }
138 }
139
Popular Tags