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;
038:
039: import edu.rice.cs.drjava.model.definitions.reducedmodel.*;
040: import edu.rice.cs.util.text.SwingDocumentInterface;
041: import edu.rice.cs.util.OperationCanceledException;
042: import edu.rice.cs.drjava.model.definitions.ClassNameNotFoundException;
043: import edu.rice.cs.drjava.model.definitions.indent.Indenter;
044:
045: import java.util.Vector;
046: import javax.swing.text.AttributeSet;
047: import javax.swing.text.BadLocationException;
048: import javax.swing.text.StyledDocument;
049: import javax.swing.ProgressMonitor;
050:
051: /** Interface to be collectively shared by the Definitions Document, Open Definitions Document,
052: * and the Interactions Document. Collectively represents a DrJava Document
053: */
054: public interface DJDocument extends SwingDocumentInterface {
055:
056: /** Gets the indent level.
057: * @return the indent level
058: */
059: public int getIndent();
060:
061: /** Set the indent to a particular number of spaces.
062: * @param indent the size of indent that you want for the document
063: */
064: public void setIndent(int indent);
065:
066: /**
067: * Return all highlight status info for text between start and end.
068: * This should collapse adjoining blocks with the same status into one.
069: */
070: public Vector<HighlightStatus> getHighlightStatus(int start, int end);
071:
072: /**
073: * Get the current location of the cursor in the document.
074: * Unlike the usual swing document model, which is stateless, because of our implementation
075: * of the underlying reduced model, we need to keep track of the current location.
076: * @return where the cursor is as the number of characters into the document
077: */
078: public int getCurrentLocation();
079:
080: /** Change the current location of the document
081: * @param loc the new absolute location
082: */
083: public void setCurrentLocation(int loc);
084:
085: /**
086: * The actual cursor movement logic. Helper for setCurrentLocation(int).
087: * @param dist the distance from the current location to the new location.
088: */
089: public void move(int dist);
090:
091: /**
092: * Forwarding method to find the match for the closing brace
093: * immediately to the left, assuming there is such a brace.
094: * @return the relative distance backwards to the offset before
095: * the matching brace.
096: */
097: public int balanceBackward();
098:
099: /**
100: * Forwarding method to find the match for the open brace
101: * immediately to the right, assuming there is such a brace.
102: * @return the relative distance forwards to the offset after
103: * the matching brace.
104: */
105: public int balanceForward();
106:
107: /** Returns the indent information for the current location. */
108: public IndentInfo getIndentInformation();
109:
110: public ReducedModelState stateAtRelLocation(int dist);
111:
112: public ReducedModelState getStateAtCurrent();
113:
114: public void resetReducedModelLocation();
115:
116: /**
117: * Searching backwards, finds the position of the enclosing brace.
118: * NB: ignores comments.
119: * @param pos Position to start from
120: * @param opening opening brace character
121: * @param closing closing brace character
122: * @return position of enclosing squiggly brace, or ERROR_INDEX if beginning
123: * of document is reached.
124: */
125: public int findPrevEnclosingBrace(int pos, char opening,
126: char closing) throws BadLocationException;
127:
128: /**
129: * Searching forwards, finds the position of the enclosing brace.
130: * NB: ignores comments.
131: * @param pos Position to start from
132: * @param opening opening brace character
133: * @param closing closing brace character
134: * @return position of enclosing squiggly brace, or ERROR_INDEX if beginning
135: * of document is reached.
136: */
137: public int findNextEnclosingBrace(int pos, char opening,
138: char closing) throws BadLocationException;
139:
140: /**
141: *
142: * Searching backwards, finds the position of the first character that is one
143: * of the given delimiters. Does not look for delimiters inside paren phrases.
144: * (eg. skips semicolons used inside for statements.)
145: * NB: ignores comments.
146: * @param pos Position to start from
147: * @param delims array of characters to search for
148: * @return position of first matching delimiter, or ERROR_INDEX if beginning
149: * of document is reached.
150: */
151: public int findPrevDelimiter(int pos, char[] delims)
152: throws BadLocationException;
153:
154: /**
155: * Searching backwards, finds the position of the first character that is one
156: * of the given delimiters. Will not look for delimiters inside a paren
157: * phrase if skipParenPhrases is true.
158: * NB: ignores comments.
159: * @param pos Position to start from
160: * @param delims array of characters to search for
161: * @param skipParenPhrases whether to look for delimiters inside paren phrases
162: * (eg. semicolons in a for statement)
163: * @return position of first matching delimiter, or ERROR_INDEX if beginning
164: * of document is reached.
165: */
166: public int findPrevDelimiter(int pos, char[] delims,
167: boolean skipParenPhrases) throws BadLocationException;
168:
169: /**
170: * This function finds the given character in the same statement as the given
171: * position, and before the given position. It is used by QuestionExistsCharInStmt and
172: * QuestionExistsCharInPrevStmt
173: */
174: public boolean findCharInStmtBeforePos(char findChar, int position);
175:
176: /**
177: * Finds the position of the first non-whitespace character before pos.
178: * NB: Skips comments and all whitespace, including newlines
179: * @param pos Position to start from
180: * @param whitespace chars considered as white space
181: * @return position of first non-whitespace character before pos,
182: * or ERROR_INDEX if begining of document is reached
183: */
184: public int findPrevCharPos(int pos, char[] whitespace)
185: throws BadLocationException;
186:
187: /** Default indentation - uses OTHER flag and no progress indicator.
188: * @param selStart the offset of the initial character of the region to indent
189: * @param selEnd the offset of the last character of the region to indent
190: */
191: public void indentLines(int selStart, int selEnd);
192:
193: /** Parameterized indentation for special-case handling.
194: * @param selStart the offset of the initial character of the region to indent
195: * @param selEnd the offset of the last character of the region to indent
196: * @param reason a flag from {@link edu.rice.cs.drjava.model.definitions.indent.Indenter Indenter}
197: * to indicate the reason for the indent (indent logic may vary slightly based on the trigger action)
198: * @param pm used to display progress, null if no reporting is desired
199: */
200: public void indentLines(int selStart, int selEnd,
201: Indenter.IndentReason reason, ProgressMonitor pm)
202: throws OperationCanceledException;
203:
204: /**
205: * Returns the "intelligent" beginning of line. If currPos is to
206: * the right of the first non-whitespace character, the position of the
207: * first non-whitespace character is returned. If currPos is at or
208: * to the left of the first non-whitespace character, the beginning of
209: * the line is returned.
210: * @param currPos A position on the current line
211: */
212: public int getIntelligentBeginLinePos(int currPos)
213: throws BadLocationException;;
214:
215: /**
216: * Returns the indent level of the start of the statement
217: * that the cursor is on. Uses a default set of delimiters.
218: * (';', '{', '}') and a default set of whitespace characters
219: * (' ', '\t', n', ',')
220: * @param pos Cursor position
221: */
222: public String getIndentOfCurrStmt(int pos)
223: throws BadLocationException;
224:
225: /**
226: * Returns the indent level of the start of the statement
227: * that the cursor is on. Uses a default set of whitespace characters.
228: * (' ', '\t', '\n', ',')
229: * @param pos Cursor position
230: */
231: public String getIndentOfCurrStmt(int pos, char[] delims)
232: throws BadLocationException;
233:
234: /**
235: * Returns the indent level of the start of the statement
236: * that the cursor is on.
237: * @param pos Cursor position
238: * @param delims Delimiter characters denoting end of statement
239: * @param whitespace characters to skip when looking for beginning of next statement
240: */
241: public String getIndentOfCurrStmt(int pos, char[] delims,
242: char[] whitespace) throws BadLocationException;
243:
244: /**
245: * Determines if the given character exists on the line where
246: * the given cursor position is. Does not search in quotes or comments.
247: * <p>
248: * <b>Does not work if character being searched for is a '/' or a '*'</b>
249: * @param pos Cursor position
250: * @param findChar Character to search for
251: * @return true if this node's rule holds.
252: */
253: public int findCharOnLine(int pos, char findChar);
254:
255: /**
256: * Returns the absolute position of the beginning of the
257: * current line. (Just after most recent newline, or DOCSTART)
258: * Doesn't ignore comments.
259: * @param pos Any position on the current line
260: * @return position of the beginning of this line
261: */
262: public int getLineStartPos(int pos);
263:
264: /**
265: * Returns the absolute position of the end of the current
266: * line. (At the next newline, or the end of the document.)
267: * @param pos Any position on the current line
268: * @return position of the end of this line
269: */
270: public int getLineEndPos(int pos);
271:
272: /**
273: * Returns the absolute position of the first non-whitespace character
274: * on the current line.
275: * NB: Doesn't ignore comments.
276: * @param pos position on the line
277: * @return position of first non-whitespace character on this line, or the end
278: * of the line if no non-whitespace character is found.
279: */
280: public int getLineFirstCharPos(int pos) throws BadLocationException;
281:
282: /**
283: * Finds the position of the first non-whitespace character after pos.
284: * NB: Skips comments and all whitespace, including newlines
285: * @param pos Position to start from
286: * @return position of first non-whitespace character after pos,
287: * or ERROR_INDEX if end of document is reached
288: */
289: public int getFirstNonWSCharPos(int pos)
290: throws BadLocationException;
291:
292: /**
293: * Similar to the single-argument version, but allows including comments.
294: * @param pos Position to start from
295: * @param acceptComments if true, find non-whitespace chars in comments
296: * @return position of first non-whitespace character after pos,
297: * or ERROR_INDEX if end of document is reached
298: */
299: public int getFirstNonWSCharPos(int pos, boolean acceptComments)
300: throws BadLocationException;
301:
302: /**
303: * Finds the position of the first non-whitespace character after pos.
304: * NB: Skips comments and all whitespace, including newlines
305: * @param pos Position to start from
306: * @param whitespace array of whitespace chars to ignore
307: * @param acceptComments if true, find non-whitespace chars in comments
308: * @return position of first non-whitespace character after pos,
309: * or ERROR_INDEX if end of document is reached
310: */
311: public int getFirstNonWSCharPos(int pos, char[] whitespace,
312: boolean acceptComments) throws BadLocationException;
313:
314: public int findPrevNonWSCharPos(int pos)
315: throws BadLocationException;
316:
317: /**
318: * Returns true if the given position is inside a paren phrase.
319: * @param pos the position we're looking at
320: * @return true if pos is immediately inside parentheses
321: */
322: public boolean posInParenPhrase(int pos);
323:
324: /** Returns true if the reduced model's current position is inside a paren phrase.
325: * @return true if pos is immediately inside parentheses
326: */
327: public boolean posInParenPhrase();
328:
329: /** Gets the number of whitespace characters between the current location and the rest of the document or the
330: * first non-whitespace character, whichever comes first.
331: * @return the number of whitespace characters
332: */
333: public int getWhiteSpace();
334:
335: /** Sets text between previous newline and first non-whitespace character of the line containing pos to tab.
336: * @param tab String to be placed between previous newline and first non-whitespace character
337: */
338: public void setTab(String tab, int pos);
339:
340: /** Inserts a string of text into the document. It turns out that this is not where we should do custom processing
341: * of the insert; that is done in {@link AbstractDJDocument#insertUpdate}.
342: */
343: public void insertString(int offset, String str, AttributeSet a)
344: throws BadLocationException;
345:
346: /** Removes a block of text from the specified location. We don't update the reduced model here; that happens
347: * in {@link AbstractDJDocument#removeUpdate}.
348: */
349: public void remove(int offset, int len) throws BadLocationException;
350:
351: /** Gets the entire text of the document. Without this operation, a client must use locking to perform this
352: * task safely.
353: */
354: public String getText();
355:
356: /** Clears the entire text of the document. Without this operation, a client must use locking to perform this
357: * task safely.
358: */
359: public void clear();
360:
361: /* Locking operations */
362:
363: /** Swing-style acquireReadLock(). */
364: public void acquireReadLock();
365:
366: /** Swing-style releaseReadLock(). */
367: public void releaseReadLock();
368:
369: /** Swing-style writeLock(). */
370: public void acquireWriteLock();
371:
372: /** Swing-style writeUnlock(). */
373: public void releaseWriteLock();
374: }
|