1 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 ; 48 import java.util.LinkedList ; 49 import javax.swing.text.AbstractDocument ; 50 51 import static edu.rice.cs.drjava.model.definitions.ColoringView.*; 52 53 56 public class InteractionsDJDocument extends AbstractDJDocument { 57 58 62 private boolean _toClear = false; 63 64 65 public InteractionsDJDocument() { super(); } 66 67 protected int startCompoundEdit() { return 0; } 68 protected void endCompoundEdit(int key) { } 69 protected void endLastCompoundEdit() { } 70 protected void addUndoRedo(AbstractDocument.DefaultDocumentEvent chng, Runnable undoCommand, Runnable doCommand) { } 71 protected void _styleChanged() { } 72 73 74 protected Indenter makeNewIndenter(int indentLevel) { return new Indenter(indentLevel); } 75 76 83 private List <Pair<Pair<Integer ,Integer >,String >> _stylesList = new LinkedList <Pair<Pair<Integer ,Integer >,String >>(); 84 85 86 public void addColoring(int start, int end, String 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 ,Integer >,String > 94 (new Pair<Integer ,Integer >(new Integer (start),new Integer (end)), style)); 95 } 96 } 97 98 99 List <Pair<Pair<Integer , Integer >, String >> getStylesList() { return _stylesList; } 100 101 104 public boolean setColoring(int point, Graphics g) { 105 synchronized(_stylesList) { 106 for(Pair<Pair<Integer ,Integer >,String > p : _stylesList) { 107 Pair<Integer ,Integer > loc = p.first(); 108 if (loc.first() <= point && loc.second() >= point) { 109 if (p.second().equals(InteractionsDocument.ERROR_STYLE)) { 110 g.setColor(ERROR_COLOR); 112 g.setFont(g.getFont().deriveFont(Font.BOLD)); 113 } 114 else if (p.second().equals(InteractionsDocument.DEBUGGER_STYLE)) { 115 g.setColor(DEBUGGER_COLOR); 117 g.setFont(g.getFont().deriveFont(Font.BOLD)); 118 } 119 else if (p.second().equals(ConsoleDocument.SYSTEM_OUT_STYLE)) { 120 g.setColor(INTERACTIONS_SYSTEM_OUT_COLOR); 122 g.setFont(MAIN_FONT); 123 } 124 else if (p.second().equals(ConsoleDocument.SYSTEM_IN_STYLE)) { 125 g.setColor(INTERACTIONS_SYSTEM_IN_COLOR); 127 g.setFont(MAIN_FONT); 128 } 129 else if (p.second().equals(ConsoleDocument.SYSTEM_ERR_STYLE)) { 130 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; 151 152 return true; 153 } 154 } 155 return false; 156 } 157 } 158 159 160 public void setBoldFonts(int point, Graphics g) { 161 synchronized(_stylesList) { 162 for(Pair<Pair<Integer ,Integer >,String > p : _stylesList) { 163 Pair<Integer ,Integer > 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 177 public void clearColoring() { synchronized(_stylesList) { _toClear = true; } } 178 179 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 201 public void appendExceptionResult(String exceptionClass, String message, String stackTrace, String styleName) { 202 203 String 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 if (! stackTrace.trim().equals("")) { 216 BufferedReader reader = new BufferedReader(new StringReader(stackTrace)); 217 218 String line; 219 while ((line = reader.readLine()) != null) { 222 String fileName; 223 int lineNumber; 224 225 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 String lineNumStr = line.substring(colonLoc + 1, closeLoc); 235 try { 236 lineNumber = Integer.parseInt(lineNumStr); 237 fileName = line.substring(openLoc + 1, colonLoc); 238 } 239 catch (NumberFormatException nfe) { 240 } 242 } 243 } 244 } 245 246 insertText(getLength(), line, styleName); 247 248 263 264 insertText(getLength(), "\n", styleName); 266 267 } } 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 |