KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > ColoringView


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.definitions;
35
36 import javax.swing.text.*;
37 import java.awt.*;
38 import javax.swing.event.DocumentEvent JavaDoc;
39 // TODO: Check synchronization.
40
import java.util.Vector JavaDoc;
41
42 import edu.rice.cs.drjava.DrJava;
43 import edu.rice.cs.drjava.model.*;
44 import edu.rice.cs.drjava.model.repl.InteractionsDJDocument;
45 import edu.rice.cs.drjava.config.OptionConstants;
46 import edu.rice.cs.drjava.config.OptionEvent;
47 import edu.rice.cs.drjava.config.OptionListener;
48 import edu.rice.cs.drjava.model.definitions.reducedmodel.*;
49 import edu.rice.cs.util.text.EditDocumentInterface;
50
51 /**
52  * This view class renders text on the screen using the reduced model info.
53  * By extending WrappedPlainView, we only have to override the parts we want to.
54  * Here we only override drawUnselectedText. We may want to override
55  * drawSelectedText at some point. As of 2002/06/17, we now extend PlainView because
56  * WrappedPlainView was causing bugs related to resizing the viewport of the
57  * definitions scroll pane.
58  *
59  * @version $Id: ColoringView.java 4076 2007-01-19 23:01:04Z dlsmith $
60  */

61 public class ColoringView extends PlainView implements OptionConstants {
62  
63   public static Color COMMENTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_COMMENT_COLOR);
64   public static Color DOUBLE_QUOTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_DOUBLE_QUOTED_COLOR);
65   public static Color SINGLE_QUOTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_SINGLE_QUOTED_COLOR);
66   public static Color NORMAL_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_NORMAL_COLOR);
67   public static Color KEYWORD_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_KEYWORD_COLOR);
68   public static Color NUMBER_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_NUMBER_COLOR);
69   public static Color TYPE_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_TYPE_COLOR);
70   public static Font MAIN_FONT = DrJava.getConfig().getSetting(FONT_MAIN);
71   
72   //Interactions only colors
73
public static Color INTERACTIONS_SYSTEM_ERR_COLOR = DrJava.getConfig().getSetting(SYSTEM_ERR_COLOR);
74   public static Color INTERACTIONS_SYSTEM_IN_COLOR = DrJava.getConfig().getSetting(SYSTEM_IN_COLOR);
75   public static Color INTERACTIONS_SYSTEM_OUT_COLOR = DrJava.getConfig().getSetting(SYSTEM_OUT_COLOR);
76   //Renamed as to avoid confusion with the one in option constants
77
public static Color ERROR_COLOR = DrJava.getConfig().getSetting(INTERACTIONS_ERROR_COLOR);
78   public static Color DEBUGGER_COLOR = DrJava.getConfig().getSetting(DEBUG_MESSAGE_COLOR);
79   
80   /** Constructs a new coloring view.
81    * @param elem the element
82    */

83   public ColoringView(Element elem) {
84     super(elem);
85
86     // Listen for updates to configurable colors
87
final ColorOptionListener col = new ColorOptionListener();
88     final FontOptionListener fol = new FontOptionListener();
89     
90     Document doc = getDocument();
91     if (doc instanceof AbstractDJDocument) {
92       // delete the old color listeners, because they're hanging onto the wrong coloringview
93
// add color listeners to highlight keywords etc
94
DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_COMMENT_COLOR, col);
95       DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_DOUBLE_QUOTED_COLOR, col);
96       DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_SINGLE_QUOTED_COLOR, col);
97       DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_NORMAL_COLOR, col);
98       DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_KEYWORD_COLOR, col);
99       DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_NUMBER_COLOR, col);
100       DrJava.getConfig().addOptionListener( OptionConstants.DEFINITIONS_TYPE_COLOR, col);
101       DrJava.getConfig().addOptionListener( OptionConstants.FONT_MAIN, fol);
102       
103       DrJava.getConfig().addOptionListener( OptionConstants.SYSTEM_ERR_COLOR, col);
104       DrJava.getConfig().addOptionListener( OptionConstants.SYSTEM_IN_COLOR, col);
105       DrJava.getConfig().addOptionListener( OptionConstants.SYSTEM_OUT_COLOR, col);
106       DrJava.getConfig().addOptionListener( OptionConstants.INTERACTIONS_ERROR_COLOR, col);
107       DrJava.getConfig().addOptionListener( OptionConstants.DEBUG_MESSAGE_COLOR, col);
108       
109     }
110     
111     if (doc instanceof DefinitionsDocument) {
112       // remove the listeners when the document closes
113
((DefinitionsDocument)doc).addDocumentClosedListener(new DocumentClosedListener() {
114         public void close() {
115           DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_COMMENT_COLOR, col);
116           DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_DOUBLE_QUOTED_COLOR, col);
117           DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_SINGLE_QUOTED_COLOR, col);
118           DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_NORMAL_COLOR, col);
119           DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_KEYWORD_COLOR, col);
120           DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_NUMBER_COLOR, col);
121           DrJava.getConfig().removeOptionListener( OptionConstants.DEFINITIONS_TYPE_COLOR, col);
122           DrJava.getConfig().removeOptionListener( OptionConstants.FONT_MAIN, fol);
123           DrJava.getConfig().removeOptionListener( OptionConstants.SYSTEM_ERR_COLOR, col);
124           DrJava.getConfig().removeOptionListener( OptionConstants.SYSTEM_IN_COLOR, col);
125           DrJava.getConfig().removeOptionListener( OptionConstants.SYSTEM_OUT_COLOR, col);
126           DrJava.getConfig().removeOptionListener( OptionConstants.INTERACTIONS_ERROR_COLOR, col);
127           DrJava.getConfig().removeOptionListener( OptionConstants.DEBUG_MESSAGE_COLOR, col);
128         }
129       });
130     }
131   }
132
133   /** Renders the given range in the model as normal unselected text.
134    * Note that this is text that's all on one line. The superclass deals
135    * with breaking lines and such. So all we have to do here is draw the
136    * text on [p0,p1) in the model. We have to start drawing at (x,y), and
137    * the function returns the x coordinate when we're done.
138    *
139    * @param g The graphics context
140    * @param x The starting X coordinate
141    * @param y The starting Y coordinate
142    * @param start The beginning position in the model
143    * @param end The ending position in the model
144    * @return The x coordinate at the end of the range
145    * @throws BadLocationException If the range is invalid
146    */

147   protected int drawUnselectedText(Graphics g, int x, int y, int start, int end) throws BadLocationException {
148         
149     // Might be a PlainDocument (when AbstractDJPane is first constructed).
150
// See comments for DefinitionsEditorKit.createNewDocument() for details.
151
Document doc = getDocument();
152     AbstractDJDocument _doc = null;
153     if (doc instanceof AbstractDJDocument) _doc = (AbstractDJDocument) doc;
154     else return x; // return if there is no AbstracDJDocument
155

156     // If there's nothing to show, don't do anything!
157
// For some reason I don't understand we tend to get called sometimes to render a zero-length area.
158
if (start == end) return x;
159
160     Vector JavaDoc<HighlightStatus> stats = _doc.getHighlightStatus(start, end);
161     if (stats.size() < 1) throw new RuntimeException JavaDoc("GetHighlightStatus returned nothing!");
162     
163     for (int i = 0; i < stats.size(); i++) {
164       HighlightStatus stat = stats.get(i);
165       int length = stat.getLength();
166       int location = stat.getLocation();
167       // If this highlight status extends past p1, end at p1
168
if (location + length > end) length = end - stat.getLocation();
169       
170       Segment text = getLineBuffer();
171       
172       if (!(_doc instanceof InteractionsDJDocument) || !((InteractionsDJDocument)_doc).setColoring((start+end)/2,g))
173         setFormattingForState(g, stat.getState());
174       // else
175
// DrJava.consoleErr().println("Highlight: p0="+p0+"; p1="+p1+"; location="+location+"; color="+g.getColor()+"; text="+text);
176

177       //
178
// DrJava.consoleErr().println("Highlight: loc=" + location + " length=" +
179
// length + " state=" + stat.getState() +
180
// " text=" + text);
181
//
182
_doc.getText(location, length, text);
183       x = Utilities.drawTabbedText(text, x, y, g, this, location);
184     }
185     //DrJava.consoleErr().println("returning x: " + x);
186
return x;
187   }
188
189   /** Draws the selected text image at the specified location.
190    * @param g The text image
191    * @param x The x coordinate for the drawn text
192    * @param y The y coordinate for the drawn text
193    * @param start The beginning position in the model
194    * @param end The end position in the model
195    * @return The location of the end of the image (range)
196    * @throws BadLocationException
197    */

198   protected int drawSelectedText(Graphics g, int x, int y, int start, int end) throws BadLocationException {
199     /*
200      DrJava.consoleErr().println("drawSelected: " + p0 + "-" + p1 +
201      " doclen=" + _doc.getLength() +" x="+x+" y="+y);
202      */

203     EditDocumentInterface doc = (EditDocumentInterface) getDocument();
204     if (doc instanceof InteractionsDJDocument) ((InteractionsDJDocument)doc).setBoldFonts(end,g);
205     
206     return super.drawSelectedText(g, x, y, start, end);
207   }
208
209   /** Given a particular state, assign it a color.
210    * @param g Graphics object
211    * @param state a given state
212    */

213   private void setFormattingForState(Graphics g, int state) {
214     switch (state) {
215       case HighlightStatus.NORMAL:
216         g.setColor(NORMAL_COLOR);
217         break;
218       case HighlightStatus.COMMENTED:
219         g.setColor(COMMENTED_COLOR);
220         break;
221       case HighlightStatus.SINGLE_QUOTED:
222         g.setColor(SINGLE_QUOTED_COLOR);
223         break;
224       case HighlightStatus.DOUBLE_QUOTED:
225         g.setColor(DOUBLE_QUOTED_COLOR);
226         break;
227       case HighlightStatus.KEYWORD:
228         g.setColor(KEYWORD_COLOR);
229         break;
230       case HighlightStatus.NUMBER:
231         g.setColor(NUMBER_COLOR);
232         break;
233       case HighlightStatus.TYPE:
234         g.setColor(TYPE_COLOR);
235         break;
236       default:
237         throw new RuntimeException JavaDoc("Can't get color for invalid state: " + state);
238     }
239     g.setFont(MAIN_FONT);
240   }
241
242   /** Called when a change occurs.
243    * @param changes document changes
244    * @param a a Shape
245    * @param f a ViewFactory
246    */

247   public void changedUpdate(DocumentEvent JavaDoc changes, Shape a, ViewFactory f) {
248     super.changedUpdate(changes, a, f);
249     // Make sure we redraw since something changed in the formatting
250
Container c = getContainer();
251     if (c != null) c.repaint();
252   }
253
254   /** Called when an OptionListener perceives a change in any of the colors */
255   public void updateColors() {
256
257     COMMENTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_COMMENT_COLOR);
258     DOUBLE_QUOTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_DOUBLE_QUOTED_COLOR);
259     SINGLE_QUOTED_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_SINGLE_QUOTED_COLOR);
260     NORMAL_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_NORMAL_COLOR);
261     KEYWORD_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_KEYWORD_COLOR);
262     NUMBER_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_NUMBER_COLOR);
263     TYPE_COLOR = DrJava.getConfig().getSetting(DEFINITIONS_TYPE_COLOR);
264     
265     INTERACTIONS_SYSTEM_ERR_COLOR = DrJava.getConfig().getSetting(SYSTEM_ERR_COLOR);
266     INTERACTIONS_SYSTEM_IN_COLOR = DrJava.getConfig().getSetting(SYSTEM_IN_COLOR);
267     INTERACTIONS_SYSTEM_OUT_COLOR = DrJava.getConfig().getSetting(SYSTEM_OUT_COLOR);
268     ERROR_COLOR = DrJava.getConfig().getSetting(INTERACTIONS_ERROR_COLOR);
269     DEBUGGER_COLOR = DrJava.getConfig().getSetting(DEBUG_MESSAGE_COLOR);
270
271     // Avoid the ColoringView that does not have a container.
272
if ( getContainer() != null) getContainer().repaint();
273   }
274
275   /** The OptionListeners for DEFINITIONS COLORs */
276   private class ColorOptionListener implements OptionListener<Color> {
277     public void optionChanged(OptionEvent<Color> oce) { updateColors(); }
278   }
279   
280   private static class FontOptionListener implements OptionListener<Font> {
281     public void optionChanged(OptionEvent<Font> oce) {
282       MAIN_FONT = DrJava.getConfig().getSetting(FONT_MAIN);
283     }
284   }
285 }
Popular Tags