1 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 ; 39 import java.util.Vector ; 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 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 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 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 83 public ColoringView(Element elem) { 84 super(elem); 85 86 final ColorOptionListener col = new ColorOptionListener(); 88 final FontOptionListener fol = new FontOptionListener(); 89 90 Document doc = getDocument(); 91 if (doc instanceof AbstractDJDocument) { 92 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 ((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 147 protected int drawUnselectedText(Graphics g, int x, int y, int start, int end) throws BadLocationException { 148 149 Document doc = getDocument(); 152 AbstractDJDocument _doc = null; 153 if (doc instanceof AbstractDJDocument) _doc = (AbstractDJDocument) doc; 154 else return x; 156 if (start == end) return x; 159 160 Vector <HighlightStatus> stats = _doc.getHighlightStatus(start, end); 161 if (stats.size() < 1) throw new RuntimeException ("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 (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 177 _doc.getText(location, length, text); 183 x = Utilities.drawTabbedText(text, x, y, g, this, location); 184 } 185 return x; 187 } 188 189 198 protected int drawSelectedText(Graphics g, int x, int y, int start, int end) throws BadLocationException { 199 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 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 ("Can't get color for invalid state: " + state); 238 } 239 g.setFont(MAIN_FONT); 240 } 241 242 247 public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) { 248 super.changedUpdate(changes, a, f); 249 Container c = getContainer(); 251 if (c != null) c.repaint(); 252 } 253 254 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 if ( getContainer() != null) getContainer().repaint(); 273 } 274 275 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 |