KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > InteractionsDJDocument


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.model.repl;
35
36 import edu.rice.cs.drjava.model.AbstractDJDocument;
37 import edu.rice.cs.drjava.model.definitions.indent.Indenter;
38 import edu.rice.cs.drjava.model.definitions.reducedmodel.*;
39
40 import edu.rice.cs.plt.tuple.Pair;
41 import edu.rice.cs.util.UnexpectedException;
42 import edu.rice.cs.util.text.EditDocumentException;
43 import edu.rice.cs.util.text.ConsoleDocument;
44
45 import java.io.*;
46 import java.awt.*;
47 import java.util.List JavaDoc;
48 import java.util.LinkedList JavaDoc;
49 import javax.swing.text.AbstractDocument JavaDoc;
50
51 import static edu.rice.cs.drjava.model.definitions.ColoringView.*;
52
53 /** Represents the Interactions Document Adapter. Extends from the Abstract DrJava Document,
54  * which contains shared code between the interactions and definitions documents.
55  */

56 public class InteractionsDJDocument extends AbstractDJDocument {
57   
58   /** Holds a flag telling the adapter that the interpreter was recently reset, and to reset the styles list
59    * the next time a style is added. Cannot reset immediately because then the styles would be lost while
60    * the interactions pane is resetting.
61    */

62   private boolean _toClear = false;
63   
64   /** Standard constructor. Currently does nothing */
65   public InteractionsDJDocument() { super(); }
66   
67   protected int startCompoundEdit() { return 0; /* Do nothing */ }
68   protected void endCompoundEdit(int key) { /* Do nothing */ }
69   protected void endLastCompoundEdit() { /* Do nothing */ }
70   protected void addUndoRedo(AbstractDocument.DefaultDocumentEvent JavaDoc chng, Runnable JavaDoc undoCommand, Runnable JavaDoc doCommand) { }
71   protected void _styleChanged() { /* Do nothing */ }
72   
73   /** Returns a new indenter. Eventually to be used to return an interactions indenter */
74   protected Indenter makeNewIndenter(int indentLevel) { return new Indenter(indentLevel); }
75   
76   /** A list of styles and their locations. This list holds pairs of locations in the document and styles, which
77    * is basically a map of regions where the coloring view that is now attached to the Interactions Pane is not
78    * allowed to use the reduced model to determine the color settings when rendering text. We keep a list of all
79    * places where styles not considered by the reduced model are being used, such as System.out, System.err,
80    * and the various return styles for Strings and other Objects. Since the LinkedList class is not thread safe,
81    * we have to synchronized all methods that access pointers in _stylesList and the associated boolean _toClear.
82    */

83   private List JavaDoc<Pair<Pair<Integer JavaDoc,Integer JavaDoc>,String JavaDoc>> _stylesList = new LinkedList JavaDoc<Pair<Pair<Integer JavaDoc,Integer JavaDoc>,String JavaDoc>>();
84   
85   /** Adds the given coloring style to the styles list. */
86   public void addColoring(int start, int end, String JavaDoc style) {
87     synchronized(_stylesList) {
88       if (_toClear) {
89         _stylesList.clear();
90         _toClear = false;
91       }
92       if (style != null)
93         _stylesList.add(0, new Pair<Pair<Integer JavaDoc,Integer JavaDoc>,String JavaDoc>
94                         (new Pair<Integer JavaDoc,Integer JavaDoc>(new Integer JavaDoc(start),new Integer JavaDoc(end)), style));
95     }
96   }
97   
98   /** Package protected accessor method used for test cases */
99   List JavaDoc<Pair<Pair<Integer JavaDoc, Integer JavaDoc>, String JavaDoc>> getStylesList() { return _stylesList; }
100   
101   /** Attempts to set the coloring on the graphics based upon the content of the styles list
102    * returns false if the point is not in the list.
103    */

104   public boolean setColoring(int point, Graphics g) {
105     synchronized(_stylesList) {
106       for(Pair<Pair<Integer JavaDoc,Integer JavaDoc>,String JavaDoc> p : _stylesList) {
107         Pair<Integer JavaDoc,Integer JavaDoc> loc = p.first();
108         if (loc.first() <= point && loc.second() >= point) {
109           if (p.second().equals(InteractionsDocument.ERROR_STYLE)) {
110             //DrJava.consoleErr().println("Error Style");
111
g.setColor(ERROR_COLOR);
112             g.setFont(g.getFont().deriveFont(Font.BOLD));
113           }
114           else if (p.second().equals(InteractionsDocument.DEBUGGER_STYLE)) {
115             //DrJava.consoleErr().println("Debugger Style");
116
g.setColor(DEBUGGER_COLOR);
117             g.setFont(g.getFont().deriveFont(Font.BOLD));
118           }
119           else if (p.second().equals(ConsoleDocument.SYSTEM_OUT_STYLE)) {
120             //DrJava.consoleErr().println("System.out Style");
121
g.setColor(INTERACTIONS_SYSTEM_OUT_COLOR);
122             g.setFont(MAIN_FONT);
123           }
124           else if (p.second().equals(ConsoleDocument.SYSTEM_IN_STYLE)) {
125             //DrJava.consoleErr().println("System.in Style");
126
g.setColor(INTERACTIONS_SYSTEM_IN_COLOR);
127             g.setFont(MAIN_FONT);
128           }
129           else if (p.second().equals(ConsoleDocument.SYSTEM_ERR_STYLE)) {
130             //DrJava.consoleErr().println("System.err Style");
131
g.setColor(INTERACTIONS_SYSTEM_ERR_COLOR);
132             g.setFont(MAIN_FONT);
133           }
134           else if (p.second().equals(InteractionsDocument.OBJECT_RETURN_STYLE)) {
135             g.setColor(NORMAL_COLOR);
136             g.setFont(MAIN_FONT);
137           }
138           else if (p.second().equals(InteractionsDocument.STRING_RETURN_STYLE)) {
139             g.setColor(DOUBLE_QUOTED_COLOR);
140             g.setFont(MAIN_FONT);
141           }
142           else if (p.second().equals(InteractionsDocument.NUMBER_RETURN_STYLE)) {
143             g.setColor(NUMBER_COLOR);
144             g.setFont(MAIN_FONT);
145           }
146           else if (p.second().equals(InteractionsDocument.CHARACTER_RETURN_STYLE)) {
147             g.setColor(SINGLE_QUOTED_COLOR);
148             g.setFont(MAIN_FONT);
149           }
150           else return false; /* Normal text color */
151           
152           return true;
153         }
154       }
155       return false;
156     }
157   }
158   
159   /** Attempts to set the font on the graphics context based upon the styles held in the styles list. */
160   public void setBoldFonts(int point, Graphics g) {
161     synchronized(_stylesList) {
162       for(Pair<Pair<Integer JavaDoc,Integer JavaDoc>,String JavaDoc> p : _stylesList) {
163         Pair<Integer JavaDoc,Integer JavaDoc> loc = p.first();
164         if (loc.first() <= point && loc.second() >= point) {
165           if (p.second().equals(InteractionsDocument.ERROR_STYLE))
166             g.setFont(g.getFont().deriveFont(Font.BOLD));
167           else if (p.second().equals(InteractionsDocument.DEBUGGER_STYLE))
168             g.setFont(g.getFont().deriveFont(Font.BOLD));
169           else g.setFont(MAIN_FONT);
170           return;
171         }
172       }
173     }
174   }
175     
176   /** Called when the Interactions pane is reset. */
177   public void clearColoring() { synchronized(_stylesList) { _toClear = true; } }
178   
179   /** Returns true iff the end of the current interaction is an open comment block
180    * @return true iff the end of the current interaction is an open comment block
181    */

182   public boolean inCommentBlock() {
183     acquireReadLock();
184     try {
185       synchronized(_reduced) {
186         resetReducedModelLocation();
187         ReducedModelState state = stateAtRelLocation(getLength() - _currentLocation);
188         boolean toReturn = (state.equals(ReducedModelStates.INSIDE_BLOCK_COMMENT));
189         return toReturn;
190       }
191     }
192     finally { releaseReadLock(); }
193   }
194   
195   /** Inserts the given exception data into the document with the given style.
196    * @param exceptionClass Name of the exception that was thrown
197    * @param message Message contained in the exception
198    * @param stackTrace String representation of the stack trace
199    * @param styleName name of the style for formatting the exception
200    */

201   public void appendExceptionResult(String JavaDoc exceptionClass, String JavaDoc message, String JavaDoc stackTrace, String JavaDoc styleName) {
202     
203     String JavaDoc c = exceptionClass;
204     if (c.indexOf('.') != -1) c = c.substring(c.lastIndexOf('.') + 1, c.length());
205     
206     acquireWriteLock();
207     try {
208       insertText(getLength(), c + ": " + message + "\n", styleName);
209       
210       // An example stack trace:
211
//
212
// java.lang.IllegalMonitorStateException:
213
// at java.lang.Object.wait(Native Method)
214
// at java.lang.Object.wait(Object.java:425)
215
if (! stackTrace.trim().equals("")) {
216         BufferedReader reader = new BufferedReader(new StringReader(stackTrace));
217         
218         String JavaDoc line;
219         // a line is parsable if it has ( then : then ), with some
220
// text between each of those
221
while ((line = reader.readLine()) != null) {
222           String JavaDoc fileName;
223           int lineNumber;
224           
225           // TODO: Why is this stuff here??
226
int openLoc = line.indexOf('(');
227           if (openLoc != -1) {
228             int closeLoc = line.indexOf(')', openLoc + 1);
229             
230             if (closeLoc != -1) {
231               int colonLoc = line.indexOf(':', openLoc + 1);
232               if ((colonLoc > openLoc) && (colonLoc < closeLoc)) {
233                 // ok this line is parsable!
234
String JavaDoc lineNumStr = line.substring(colonLoc + 1, closeLoc);
235                 try {
236                   lineNumber = Integer.parseInt(lineNumStr);
237                   fileName = line.substring(openLoc + 1, colonLoc);
238                 }
239                 catch (NumberFormatException JavaDoc nfe) {
240                   // do nothing; we failed at parsing
241
}
242               }
243             }
244           }
245           
246           insertText(getLength(), line, styleName);
247           
248           // OK, now if fileName != null we did parse out fileName
249
// and lineNumber.
250
// Here's where we'd add the button, etc.
251
/*
252            if (fileName != null) {
253            JButton button = new JButton("go");
254            button.addActionListener(new ExceptionButtonListener(fileName, lineNumber));
255            SimpleAttributeSet buttonSet = new SimpleAttributeSet(set);
256            StyleConstants.setComponent(buttonSet, button);
257            insertString(getLength(), " ", null);
258            insertString(getLength() - 1, " ", buttonSet);
259            JOptionPane.showMessageDialog(null, "button in");
260            insertString(getLength(), " ", null);
261            JOptionPane.showMessageDialog(null, "extra space");
262            }*/

263           
264           //JOptionPane.showMessageDialog(null, "\\n");
265
insertText(getLength(), "\n", styleName);
266           
267         } // end the while
268
}
269     }
270     catch (IOException ioe) { throw new UnexpectedException(ioe); }
271     catch (EditDocumentException ble) { throw new UnexpectedException(ble); }
272     finally { releaseWriteLock(); }
273   }
274 }
275
Popular Tags