1 33 34 package edu.rice.cs.drjava.ui; 35 36 import javax.swing.*; 37 import javax.swing.text.*; 38 import javax.swing.event.DocumentListener ; 39 import javax.swing.event.DocumentEvent ; 40 import java.awt.event.ActionEvent ; 41 import java.awt.Color ; 42 import java.awt.Font ; 43 44 import java.io.Serializable ; 45 46 import edu.rice.cs.util.swing.Utilities; 47 import edu.rice.cs.util.text.ConsoleDocument; 48 49 import edu.rice.cs.drjava.DrJava; 50 import edu.rice.cs.drjava.config.OptionConstants; 51 import edu.rice.cs.drjava.config.OptionListener; 52 import edu.rice.cs.drjava.config.OptionEvent; 53 import edu.rice.cs.drjava.model.repl.*; 54 import edu.rice.cs.drjava.platform.PlatformFactory; 55 import edu.rice.cs.drjava.model.ClipboardHistoryModel; 56 57 60 public abstract class AbstractConsoleController implements Serializable { 61 62 protected InteractionsDJDocument _adapter; 63 64 65 protected InteractionsPane _pane; 66 67 68 protected SimpleAttributeSet _defaultStyle; 69 70 71 protected final SimpleAttributeSet _systemOutStyle; 72 73 74 protected final SimpleAttributeSet _systemErrStyle; 75 76 78 79 Action switchToPrevPaneAction; 80 81 82 Action switchToNextPaneAction; 83 84 87 protected AbstractConsoleController(InteractionsDJDocument adapter, InteractionsPane pane) { 88 _adapter = adapter; 89 _pane = pane; 90 _defaultStyle = new SimpleAttributeSet(); 91 _systemOutStyle = new SimpleAttributeSet(); 92 _systemErrStyle = new SimpleAttributeSet(); 93 } 94 95 96 public abstract ConsoleDocument getConsoleDoc(); 97 98 99 protected void _init() { 100 _addDocumentStyles(); 101 _setupModel(); 102 _setupView(); 103 } 104 105 106 protected void _addDocumentStyles() { 107 _adapter.setDocStyle(ConsoleDocument.DEFAULT_STYLE, _defaultStyle); 109 DrJava.getConfig().addOptionListener(OptionConstants.DEFINITIONS_NORMAL_COLOR, 110 new OptionListener<Color >() { 111 public void optionChanged(OptionEvent<Color > oe) { 112 setDefaultFont(oe.value); 113 } 114 }); 115 116 _systemOutStyle.addAttributes(_defaultStyle); 118 _systemOutStyle.addAttribute(StyleConstants.Foreground, 119 DrJava.getConfig().getSetting(OptionConstants.SYSTEM_OUT_COLOR)); 120 _adapter.setDocStyle(ConsoleDocument.SYSTEM_OUT_STYLE, _systemOutStyle); 121 DrJava.getConfig().addOptionListener(OptionConstants.SYSTEM_OUT_COLOR, 122 new OptionListener<Color >() { 123 public void optionChanged(OptionEvent<Color > oe) { 124 _systemOutStyle.addAttribute(StyleConstants.Foreground, oe.value); 125 } 126 }); 127 128 _systemErrStyle.addAttributes(_defaultStyle); 130 _systemErrStyle.addAttribute(StyleConstants.Foreground, 131 DrJava.getConfig().getSetting(OptionConstants.SYSTEM_ERR_COLOR)); 132 _adapter.setDocStyle(ConsoleDocument.SYSTEM_ERR_STYLE, _systemErrStyle); 133 DrJava.getConfig().addOptionListener(OptionConstants.SYSTEM_ERR_COLOR, 134 new OptionListener<Color >() { 135 public void optionChanged(OptionEvent<Color > oe) { 136 _systemErrStyle.addAttribute(StyleConstants.Foreground, oe.value); 137 } 138 }); 139 } 140 141 146 public void setDefaultFont(Font f) { 147 Color c = DrJava.getConfig().getSetting(OptionConstants.DEFINITIONS_NORMAL_COLOR); 148 setDefaultFont(f, c); 149 } 150 151 155 public void setDefaultFont(Color c) { 156 Font f = DrJava.getConfig().getSetting(OptionConstants.FONT_MAIN); 157 setDefaultFont(f, c); 158 } 159 160 165 public void setDefaultFont(Font f, Color c) { 166 if (PlatformFactory.ONLY.isMacPlatform()) { 167 SimpleAttributeSet fontSet = new SimpleAttributeSet(); 168 StyleConstants.setFontFamily(fontSet, f.getFamily()); 169 StyleConstants.setFontSize(fontSet, f.getSize()); 170 StyleConstants.setBold(fontSet, f.isBold()); 171 StyleConstants.setItalic(fontSet, f.isItalic()); 172 if (c != null) { 173 StyleConstants.setForeground(fontSet, c); 174 } 175 _adapter.setCharacterAttributes(0, _adapter.getLength()+1, fontSet, false); 176 _pane.setCharacterAttributes(fontSet, false); 177 _updateStyles(fontSet); 178 } 179 } 180 181 184 protected void _updateStyles(AttributeSet newSet) { 185 _defaultStyle.addAttributes(newSet); 186 _systemOutStyle.addAttributes(newSet); 187 _systemErrStyle.addAttributes(newSet); 188 } 189 190 191 protected abstract void _setupModel(); 192 193 197 class CaretUpdateListener implements DocumentListener { 198 public void insertUpdate(final DocumentEvent e) { 199 SwingUtilities.invokeLater(new Runnable () { 202 public void run() { 203 204 ConsoleDocument doc = getConsoleDoc(); 205 doc.acquireReadLock(); try { 207 int caretPos = _pane.getCaretPosition(); 208 int promptPos = doc.getPromptPos(); 209 int length = doc.getLength(); 210 211 213 int prevPromptPos = promptPos; 215 if (e.getOffset() < promptPos) { 216 prevPromptPos = promptPos - e.getLength(); 219 } 220 221 if (! doc.hasPrompt()) { 222 moveToEnd(); 225 } 226 else if (promptPos <= length) { 229 if (caretPos < prevPromptPos) { 230 moveToPrompt(); 233 } 234 else { 235 int size = promptPos - prevPromptPos; 238 if (size > 0) moveTo(caretPos + size); 239 } 240 } 241 } 242 finally { doc.releaseReadLock(); } 243 } 244 }); 245 } 246 247 public void removeUpdate(DocumentEvent e) { _ensureLegalCaretPos(); } 248 public void changedUpdate(DocumentEvent e) { _ensureLegalCaretPos(); } 249 250 protected void _ensureLegalCaretPos() { 251 Utilities.invokeLater(new Runnable () { 252 public void run() { 253 ConsoleDocument doc = getConsoleDoc(); 254 doc.acquireReadLock(); 255 try { 256 int length = doc.getLength(); 257 if (_pane.getCaretPosition() > length) _pane.setCaretPosition(length); 258 } 259 finally { doc.releaseReadLock(); } 260 } 261 }); 262 } 263 } 264 265 266 267 protected void _setupView() { 268 KeyStroke beginLineKey = DrJava.getConfig().getSetting(OptionConstants.KEY_BEGIN_LINE); 269 _pane.addActionForKeyStroke(beginLineKey, gotoPromptPosAction); 270 _pane.addActionForKeyStroke(KeyBindingManager.Singleton.addShiftModifier(beginLineKey), 271 selectToPromptPosAction); 272 KeyStroke endLineKey = DrJava.getConfig().getSetting(OptionConstants.KEY_END_LINE); 273 _pane.addActionForKeyStroke(endLineKey, gotoEndAction); 274 _pane.addActionForKeyStroke(KeyBindingManager.Singleton.addShiftModifier(endLineKey), 275 selectToEndAction); 276 277 DrJava.getConfig().addOptionListener(OptionConstants.KEY_BEGIN_LINE, 278 new OptionListener<KeyStroke>() { 279 public void optionChanged(OptionEvent<KeyStroke> oe) { 280 _pane.addActionForKeyStroke(oe.value, gotoPromptPosAction); 281 _pane.addActionForKeyStroke(KeyBindingManager.Singleton.addShiftModifier(oe.value), 282 selectToPromptPosAction); 283 } 284 }); 285 DrJava.getConfig().addOptionListener(OptionConstants.KEY_END_LINE, 286 new OptionListener<KeyStroke>() { 287 public void optionChanged(OptionEvent<KeyStroke> oe) { 288 _pane.addActionForKeyStroke(oe.value, gotoEndAction); 289 _pane.addActionForKeyStroke(KeyBindingManager.Singleton.addShiftModifier(oe.value), 290 selectToEndAction); 291 } 292 }); 293 294 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_CUT), cutAction); 295 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_COPY), copyAction); 296 DrJava.getConfig().addOptionListener(OptionConstants.KEY_CUT, new OptionListener<KeyStroke>() { 297 public void optionChanged(OptionEvent<KeyStroke> oe) { 298 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_CUT), cutAction); 299 } 300 }); 301 DrJava.getConfig().addOptionListener(OptionConstants.KEY_COPY, new OptionListener<KeyStroke>() { 302 public void optionChanged(OptionEvent<KeyStroke> oe) { 303 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_COPY), copyAction); 304 } 305 }); 306 } 307 308 309 Action cutAction = new DefaultEditorKit.CutAction() { 310 public void actionPerformed(ActionEvent e) { 311 if (_pane.getSelectedText()!=null) { 312 super.actionPerformed(e); 313 String s = edu.rice.cs.util.swing.Utilities.getClipboardSelection(_pane); 314 if ((s!=null) && (s.length()!=0)) { ClipboardHistoryModel.singleton().put(s); } 315 } 316 } 317 }; 318 319 320 Action copyAction = new DefaultEditorKit.CopyAction() { 321 public void actionPerformed(ActionEvent e) { 322 if (_pane.getSelectedText()!=null) { 323 super.actionPerformed(e); 324 String s = edu.rice.cs.util.swing.Utilities.getClipboardSelection(_pane); 325 if ((s!=null) && (s.length()!=0)){ ClipboardHistoryModel.singleton().put(s); } 326 } 327 } 328 }; 329 330 331 public InteractionsDJDocument getDocumentAdapter() { return _adapter; } 332 333 334 public InteractionsPane getPane() { return _pane; } 335 336 339 protected boolean _busy() { return ! getConsoleDoc().hasPrompt(); } 340 341 342 AbstractAction newLineAction = new AbstractAction() { 343 public void actionPerformed(ActionEvent e) { getConsoleDoc().insertNewLine(_pane.getCaretPosition()); } 344 }; 345 346 347 AbstractAction clearCurrentAction = new AbstractAction() { 348 public void actionPerformed(ActionEvent e) { getConsoleDoc().clearCurrentInput(); } 349 }; 350 351 352 AbstractAction gotoEndAction = new AbstractAction() { 353 public void actionPerformed(ActionEvent e) { moveToEnd(); } 354 }; 355 356 357 AbstractAction selectToEndAction = new AbstractAction() { 358 public void actionPerformed(ActionEvent e) { _pane.moveCaretPosition(getConsoleDoc().getLength()); } 359 }; 360 361 362 AbstractAction gotoPromptPosAction = new AbstractAction() { 363 public void actionPerformed(ActionEvent e) { moveToPrompt(); } 364 }; 365 366 367 AbstractAction selectToPromptPosAction = new AbstractAction() { 368 public void actionPerformed(ActionEvent e) { 369 _pane.moveCaretPosition(getConsoleDoc().getPromptPos()); 371 } 372 }; 373 374 375 void moveToEnd() { moveTo(getConsoleDoc().getLength()); } 376 377 378 void moveToPrompt() { moveTo(getConsoleDoc().getPromptPos()); } 379 380 381 void moveTo(int pos) { 382 if (pos < 0) pos = 0; 384 else { 385 int maxLen = getConsoleDoc().getLength(); 386 if (pos > maxLen) pos = maxLen; 387 } 388 _pane.setCaretPosition(pos); 389 } 390 391 public void setPrevPaneAction(Action a) { 392 switchToPrevPaneAction = a; 393 394 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_PREVIOUS_PANE), 396 switchToPrevPaneAction); 397 DrJava.getConfig().addOptionListener(OptionConstants.KEY_PREVIOUS_PANE, new OptionListener<KeyStroke>() { 398 public void optionChanged(OptionEvent<KeyStroke> oe) { 399 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_PREVIOUS_PANE), 400 switchToPrevPaneAction); 401 } 402 }); 403 } 404 405 public void setNextPaneAction(Action a) { 406 switchToNextPaneAction = a; 407 408 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_NEXT_PANE), 411 switchToNextPaneAction); 412 DrJava.getConfig().addOptionListener(OptionConstants.KEY_NEXT_PANE, new OptionListener<KeyStroke>() { 413 public void optionChanged(OptionEvent<KeyStroke> oe) { 414 _pane.addActionForKeyStroke(DrJava.getConfig().getSetting(OptionConstants.KEY_NEXT_PANE), 415 switchToNextPaneAction); 416 } 417 }); 418 } 419 } 420 | Popular Tags |