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.ui;
038:
039: import edu.rice.cs.drjava.DrJava;
040: import edu.rice.cs.drjava.config.*;
041: import edu.rice.cs.drjava.model.*;
042: import edu.rice.cs.drjava.model.definitions.indent.Indenter;
043:
044: import edu.rice.cs.util.swing.*;
045: import edu.rice.cs.util.swing.Utilities;
046: import edu.rice.cs.util.text.SwingDocument;
047:
048: import java.awt.*;
049: import javax.swing.*;
050: import javax.swing.event.*;
051: import javax.swing.text.*;
052: import java.awt.dnd.*;
053: import java.awt.datatransfer.*;
054: import edu.rice.cs.drjava.DrJavaRoot;
055:
056: /** This pane class for a SwingDocument. */
057: public abstract class AbstractDJPane extends JTextPane implements
058: OptionConstants, DropTargetListener {
059:
060: // ------------ FIELDS -----------
061:
062: /* The amount of the visible pane to scroll on a single click (Swing's default is .1) */
063: private static final double SCROLL_UNIT = .05;
064:
065: /** Paren/brace/bracket matching highlight color. */
066: static ReverseHighlighter.DrJavaHighlightPainter MATCH_PAINTER;
067:
068: static {
069: Color highColor = DrJava.getConfig().getSetting(
070: DEFINITIONS_MATCH_COLOR);
071: MATCH_PAINTER = new ReverseHighlighter.DrJavaHighlightPainter(
072: highColor);
073: }
074:
075: /** Highlight painter for selected errors in the defs doc. */
076: static ReverseHighlighter.DrJavaHighlightPainter ERROR_PAINTER = new ReverseHighlighter.DrJavaHighlightPainter(
077: DrJava.getConfig().getSetting(COMPILER_ERROR_COLOR));
078:
079: protected volatile HighlightManager _highlightManager;
080:
081: /** Looks for changes in the caret position to see if a paren/brace/bracket highlight is needed. */
082: protected final CaretListener _matchListener = new CaretListener() {
083:
084: /** Checks caret position to see if it needs to set or remove a highlight from the document. Only modifies the
085: * document--not any GUI classes.
086: * @param e the event fired by the caret position change
087: */
088: public void caretUpdate(CaretEvent e) {
089: matchUpdate(e.getDot());
090: }
091: };
092:
093: /** Our current paren/brace/bracket matching highlight. */
094: protected volatile HighlightManager.HighlightInfo _matchHighlight = null;
095:
096: protected final SwingDocument NULL_DOCUMENT = new SwingDocument();
097:
098: //--------- CONSTRUCTOR ----------
099:
100: AbstractDJPane(SwingDocument doc) {
101: super (doc);
102: setContentType("text/java");
103:
104: // Add listener that checks if highlighting matching braces must be updated
105: addCaretListener(_matchListener);
106: }
107:
108: //--------- METHODS -----------
109:
110: /** Adds a highlight to the document. Called by _updateMatchHighlight().
111: * @param from start of highlight
112: * @param to end of highlight
113: */
114: protected void _addHighlight(int from, int to) {
115: _matchHighlight = _highlightManager.addHighlight(from, to,
116: MATCH_PAINTER);
117: }
118:
119: /** Updates the document location and checks caret position to see if it needs to set or remove a highlight from the
120: * document. When the cursor is immediately right of a ')', '}', or ']', it highlights up to the matching '(', '{",
121: * or '[', respectively. This method must execute directly as part of the document update. If cannot be deferred
122: * using invokeLater. Only modifies fields added to DefaultStyledDocument)---not any Swing library classes. Can be
123: * executed outside the event thread.
124: * @param offset the new offset of the caret
125: */
126: protected abstract void matchUpdate(int offset);
127:
128: /** Removes the previous highlight so document is cleared when caret position changes. Assumes ReadLock is already
129: * held. Can be executed from outside the event thread. */
130: protected void _removePreviousHighlight() {
131: if (_matchHighlight != null) {
132: _matchHighlight.remove();
133: //_highlightManager.removeHighlight((HighlightManager.HighlightInfo)_matchHighlight);
134: _matchHighlight = null;
135: }
136: }
137:
138: /** A length checked version of setCaretPostion(int pos) that ensures pos is within the DJDocument. */
139: public void setCaretPos(int pos) {
140: DJDocument doc = getDJDocument();
141: doc.acquireReadLock();
142: try {
143: int len = doc.getLength();
144: if (pos > len) {
145: setCaretPosition(len);
146: return;
147: }
148: setCaretPosition(pos);
149: } finally {
150: doc.releaseReadLock();
151: }
152: }
153:
154: public int getScrollableUnitIncrement(Rectangle visibleRectangle,
155: int orientation, int direction) {
156: return (int) (visibleRectangle.getHeight() * SCROLL_UNIT);
157: }
158:
159: /** Runs indent(int) with a default value of Indenter.IndentReason.OTHER */
160: public void indent() {
161: indent(Indenter.IndentReason.OTHER);
162: }
163:
164: /** Perform an indent either on the current line or on the given selected box of text. Calls are sent to GlobalModel
165: * which are then forwarded on to the document. Hopefully the indent code will be fixed and corrected so this
166: * doesn't look so ugly. The purpose is to divorce the pane from the document so we can just pass a document to
167: * DefinitionsPane and that's all it cares about.
168: * @param reason the action that spawned this indent action. Enter presses are special, so that stars are inserted
169: * when lines in a multiline comment are broken up.
170: */
171: public void indent(final Indenter.IndentReason reason) {
172:
173: /** Because indent() is a function called directly by the Keymap, it does not go through the regular insertString
174: * channels. Thus it may not be in sync with the document's internal position. For that reason, we grab the
175: * caretPostion and set the current location to that value before calling the insertLine operation. The logic
176: * for a single line insert is very dependent on the current location.
177: */
178:
179: // Is this action still necessary? Answer: yes! Without this line, the caret often moves when the user hits "tab"
180: getDJDocument().setCurrentLocation(getCaretPosition());
181:
182: // The _reduced lock within DefinitionsDocument should be probably be set as well
183:
184: final int selStart = getSelectionStart();
185: final int selEnd = getSelectionEnd();
186:
187: ProgressMonitor pm = null;
188: //= new ProgressMonitor(_mainFrame, "Indenting...",
189: // null, 0, selEnd - selStart);
190:
191: //pm.setProgress(0);
192: // 3 seconds before displaying the progress bar.
193: //pm.setMillisToDecideToPopup(3000);
194:
195: // XXX: Temporary hack because of slow indent...
196: // Prompt if more than 10000 characters to be indented, then do the indent
197: if (shouldIndent(selStart, selEnd)) {
198: indentLines(selStart, selEnd, reason, pm);
199: }
200:
201: }
202:
203: /** Indents the given selection, for the given reason, in the current document.
204: * @param selStart - the selection start
205: * @param selEnd - the selection end
206: * @param reason - the reason for the indent
207: * @param pm - the ProgressMonitor used by the indenter
208: */
209: protected abstract void indentLines(int selStart, int selEnd,
210: Indenter.IndentReason reason, ProgressMonitor pm);
211:
212: /** Returns true if the indent is to be performed.
213: * @param selStart - the selection start
214: * @param selEnd - the selection end
215: */
216: protected abstract boolean shouldIndent(int selStart, int selEnd);
217:
218: /** Returns the DJDocument held by the pane. */
219: public abstract DJDocument getDJDocument();
220:
221: /** Drag and drop target. */
222: DropTarget dropTarget = new DropTarget(this , this );
223:
224: /** User dragged something into the component. */
225: public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
226: DrJavaRoot.dragEnter(dropTargetDragEvent);
227: }
228:
229: public void dragExit(DropTargetEvent dropTargetEvent) {
230: }
231:
232: public void dragOver(DropTargetDragEvent dropTargetDragEvent) {
233: }
234:
235: public void dropActionChanged(
236: DropTargetDragEvent dropTargetDragEvent) {
237: }
238:
239: /** User dropped something on the component. */
240: public synchronized void drop(
241: DropTargetDropEvent dropTargetDropEvent) {
242: DrJavaRoot.drop(dropTargetDropEvent);
243: }
244: }
|