KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > InteractionsPane


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-2005 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.ui;
35
36 import javax.swing.*;
37 import javax.swing.text.*;
38 import java.awt.*;
39 import java.awt.event.KeyEvent JavaDoc;
40 import java.awt.datatransfer.*;
41
42 import java.util.List JavaDoc;
43 //import java.util.LinkedList;
44
import java.util.Vector JavaDoc;
45
46 import edu.rice.cs.util.swing.*;
47 import edu.rice.cs.util.UnexpectedException;
48 import edu.rice.cs.drjava.config.*;
49 import edu.rice.cs.drjava.*;
50 import edu.rice.cs.drjava.model.DJDocument;
51 import edu.rice.cs.util.OperationCanceledException;
52 import edu.rice.cs.drjava.model.repl.*;
53
54 /** The view component for repl interaction.
55  * @version $Id: InteractionsPane.java 4031 2006-11-15 22:09:06Z rcartwright $
56  */

57 public abstract class InteractionsPane extends AbstractDJPane implements OptionConstants, ClipboardOwner {
58
59   /** The custom keymap for the interactions pane. */
60   protected Keymap _keymap;
61   
62   /** Whether to draw text as antialiased. */
63   private boolean _antiAliasText = false;
64   
65   static StyledEditorKit EDITOR_KIT;
66   
67   static {
68     EDITOR_KIT = new InteractionsEditorKit();
69   }
70   
71   /** A runnable object that causes the editor to beep. */
72   protected Runnable JavaDoc _beep = new Runnable JavaDoc() {
73     public void run() { Toolkit.getDefaultToolkit().beep(); }
74   };
75
76   /** The OptionListener for TEXT_ANTIALIAS. */
77   private class AntiAliasOptionListener implements OptionListener<Boolean JavaDoc> {
78     public void optionChanged(OptionEvent<Boolean JavaDoc> oce) {
79       _antiAliasText = oce.value.booleanValue();
80       InteractionsPane.this.repaint();
81     }
82   }
83   
84   /** Returns a runnable object that beeps to the user. */
85   public Runnable JavaDoc getBeep() { return _beep; }
86
87   private InteractionsDJDocument _doc;
88   
89   private List JavaDoc<Integer JavaDoc> _listOfPrompt = new Vector JavaDoc<Integer JavaDoc>(); // Vector used because it is synchronized.
90

91   /** Creates an InteractionsPane with the given document.
92    * Uses default keymap name ("INTERACTIONS_KEYMAP")
93    * @param doc StyledDocument containing the interactions history.
94    */

95   public InteractionsPane(InteractionsDJDocument doc) { this("INTERACTIONS_KEYMAP", doc); }
96
97   /** Creates an InteractionsPane with the given document.
98    * @param keymapName the name of the keymap for this pane
99    * @param doc StyledDocument containing the interactions history.
100    */

101   public InteractionsPane(String JavaDoc keymapName, InteractionsDJDocument doc) {
102     super(doc);
103     _doc = doc;
104     //add actions for enter key, etc.
105
_keymap = addKeymap(keymapName, getKeymap());
106
107     setCaretPosition(doc.getLength());
108     setHighlighter(new ReverseHighlighter());
109     _highlightManager = new HighlightManager(this);
110     
111     if (CodeStatus.DEVELOPMENT) {
112       _antiAliasText = DrJava.getConfig().getSetting(TEXT_ANTIALIAS).booleanValue();
113     }
114     
115     // Setup color listeners.
116

117     new ForegroundColorListener(this);
118     new BackgroundColorListener(this);
119     
120     if (CodeStatus.DEVELOPMENT) {
121       OptionListener<Boolean JavaDoc> aaTemp = new AntiAliasOptionListener();
122       DrJava.getConfig().addOptionListener(OptionConstants.TEXT_ANTIALIAS, aaTemp);
123     }
124   }
125   
126   /** We lost ownership of what we put in the clipboard. */
127   public void lostOwnership(Clipboard clipboard, Transferable contents) {
128     // ignore
129
}
130
131   public void processKeyEvent(KeyEvent JavaDoc e) { super.processKeyEvent(e); }
132   
133   /** Assigns the given keystroke to the given action in this pane.
134    * @param stroke keystroke that triggers the action
135    * @param action Action to perform
136    */

137   public void addActionForKeyStroke(KeyStroke stroke, Action action) {
138     // we don't want multiple keys bound to the same action
139
KeyStroke[] keys = _keymap.getKeyStrokesForAction(action);
140     if (keys != null) {
141       for (int i = 0; i < keys.length; i++) {
142         _keymap.removeKeyStrokeBinding(keys[i]);
143       }
144     }
145     _keymap.addActionForKeyStroke(stroke, action);
146     setKeymap(_keymap);
147   }
148
149   /** Sets this pane's beep to be a different runnable object. Defaults to Toolkit.getDefaultToolkit().beep().
150    * @param beep Runnable command to notify the user
151    */

152   public void setBeep(Runnable JavaDoc beep) { _beep = beep; }
153
154   /** Highlights the given text with error highlight.
155    * @param offset the offset in the text
156    * @param length the length of the error to highlight
157    */

158   public void highlightError(int offset, int length) {
159     _highlightManager.addHighlight(offset, offset+length, ERROR_PAINTER);
160   }
161   
162   /** Overriding this method ensures that all new documents created in this editor pane use our editor kit
163    * (and thus our model).
164    */

165   protected EditorKit createDefaultEditorKit() { return EDITOR_KIT; }
166   
167   /** Enable anti-aliased text by overriding paintComponent. */
168   protected void paintComponent(Graphics g) {
169     if (CodeStatus.DEVELOPMENT) {
170       if (_antiAliasText && g instanceof Graphics2D) {
171         Graphics2D g2d = (Graphics2D)g;
172         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
173       }
174     }
175     super.paintComponent(g);
176   }
177
178   /** Returns the DJDocument held by the pane */
179   public DJDocument getDJDocument() { return _doc; }
180   
181   /** Updates the highlight if there is any. */
182   protected void _updateMatchHighlight() {
183     addToPromptList(getPromptPos());
184     int to = getCaretPosition();
185     int from = getDJDocument().balanceBackward(); //_doc()._reduced.balanceBackward();
186
if (from > -1) {
187       // Found a matching open brace to this close brace
188
from = to - from;
189       if (_notCrossesPrompt(to,from)) _addHighlight(from, to);
190       // Highlighter.Highlight[] _lites = getHighlighter().getHighlights();
191
}
192     // if this wasn't a close brace, check for an open brace
193
else {
194       // (getCaretPosition will be the start of the highlight)
195
from = to;
196       to = getDJDocument().balanceForward();
197       
198       if (to > -1) {
199         to = to + from;
200         if (_notCrossesPrompt(to,from)) _addHighlight(from - 1, to);
201 // Highlighter.Highlight[] _lites = getHighlighter().getHighlights();
202
}
203     }
204   }
205   
206   /** Returns the list of prompts. Used for tests. */
207   List JavaDoc<Integer JavaDoc> getPromptList() { return _listOfPrompt; }
208   
209   /** Resets the list of prompts. Called when the interactions pane is reset. */
210   public void resetPrompts() { _listOfPrompt.clear(); }
211   
212   /** Adds the position to the list of prompt positions. package private for tests. Does not necessarily run in
213     * event thread. _listOfPrompt is a Vector which is thread safe. */

214   void addToPromptList(int pos) {
215     if (! _listOfPrompt.contains(new Integer JavaDoc(pos))) _listOfPrompt.add(new Integer JavaDoc(pos));
216   }
217   
218   /** Returns true if the two locations do not have a prompt between them. */
219   private boolean _notCrossesPrompt(int to, int from) {
220 // DrJava.consoleErr().println("To: " + to + " , From: " + from);
221
boolean toReturn = true;
222     for (Integer JavaDoc prompt : _listOfPrompt) {
223       toReturn &= ((to >= prompt && from >= prompt) || (to <= prompt && from <= prompt));
224     }
225     return toReturn;
226   }
227   
228   /** Indent the given selection, for the given reason, in the current document.
229    * @param selStart - the selection start
230    * @param selEnd - the selection end
231    * @param reason - the reason for the indent
232    * @param pm - the ProgressMonitor used by the indenter
233    */

234   protected void indentLines(int selStart, int selEnd, int reason, ProgressMonitor pm) {
235     try {
236       _doc.indentLines(selStart, selEnd, reason, pm);
237       setCaretPosition(_doc.getCurrentLocation());
238     }
239     catch (OperationCanceledException oce) { throw new UnexpectedException(oce); }
240   }
241   
242   /** Returns true if the indent is to be performed. The code in the definitions pane prompts the user, but this
243    * requires a copy of mainframe, and a reason to do so. The user does not need to be prompted here. The cutoff
244    * in the definitions pane for the prompt is 10000 characters, which is unlikely to occur in the interactions
245    * pane very often if at all.
246    * @param selStart - the selection start
247    * @param selEnd - the selection end
248    */

249   protected boolean shouldIndent(int selStart, int selEnd) { return true; }
250   
251   /** Gets the current prompt position */
252   public abstract int getPromptPos();
253 }
Popular Tags