1 33 34 package edu.rice.cs.drjava.ui; 35 36 import javax.swing.*; 37 import javax.swing.text.*; 38 import java.awt.*; 39 import java.awt.event.KeyEvent ; 40 import java.awt.datatransfer.*; 41 42 import java.util.List ; 43 import java.util.Vector ; 45 46 import edu.rice.cs.util.swing.*; 47 import edu.rice.cs.util.UnexpectedException; 48 import edu.rice.cs.drjava.config.*; 49 import edu.rice.cs.drjava.*; 50 import edu.rice.cs.drjava.model.DJDocument; 51 import edu.rice.cs.util.OperationCanceledException; 52 import edu.rice.cs.drjava.model.repl.*; 53 54 57 public abstract class InteractionsPane extends AbstractDJPane implements OptionConstants, ClipboardOwner { 58 59 60 protected Keymap _keymap; 61 62 63 private boolean _antiAliasText = false; 64 65 static StyledEditorKit EDITOR_KIT; 66 67 static { 68 EDITOR_KIT = new InteractionsEditorKit(); 69 } 70 71 72 protected Runnable _beep = new Runnable () { 73 public void run() { Toolkit.getDefaultToolkit().beep(); } 74 }; 75 76 77 private class AntiAliasOptionListener implements OptionListener<Boolean > { 78 public void optionChanged(OptionEvent<Boolean > oce) { 79 _antiAliasText = oce.value.booleanValue(); 80 InteractionsPane.this.repaint(); 81 } 82 } 83 84 85 public Runnable getBeep() { return _beep; } 86 87 private InteractionsDJDocument _doc; 88 89 private List <Integer > _listOfPrompt = new Vector <Integer >(); 91 95 public InteractionsPane(InteractionsDJDocument doc) { this("INTERACTIONS_KEYMAP", doc); } 96 97 101 public InteractionsPane(String keymapName, InteractionsDJDocument doc) { 102 super(doc); 103 _doc = doc; 104 _keymap = addKeymap(keymapName, getKeymap()); 106 107 setCaretPosition(doc.getLength()); 108 setHighlighter(new ReverseHighlighter()); 109 _highlightManager = new HighlightManager(this); 110 111 if (CodeStatus.DEVELOPMENT) { 112 _antiAliasText = DrJava.getConfig().getSetting(TEXT_ANTIALIAS).booleanValue(); 113 } 114 115 117 new ForegroundColorListener(this); 118 new BackgroundColorListener(this); 119 120 if (CodeStatus.DEVELOPMENT) { 121 OptionListener<Boolean > aaTemp = new AntiAliasOptionListener(); 122 DrJava.getConfig().addOptionListener(OptionConstants.TEXT_ANTIALIAS, aaTemp); 123 } 124 } 125 126 127 public void lostOwnership(Clipboard clipboard, Transferable contents) { 128 } 130 131 public void processKeyEvent(KeyEvent e) { super.processKeyEvent(e); } 132 133 137 public void addActionForKeyStroke(KeyStroke stroke, Action action) { 138 KeyStroke[] keys = _keymap.getKeyStrokesForAction(action); 140 if (keys != null) { 141 for (int i = 0; i < keys.length; i++) { 142 _keymap.removeKeyStrokeBinding(keys[i]); 143 } 144 } 145 _keymap.addActionForKeyStroke(stroke, action); 146 setKeymap(_keymap); 147 } 148 149 152 public void setBeep(Runnable beep) { _beep = beep; } 153 154 158 public void highlightError(int offset, int length) { 159 _highlightManager.addHighlight(offset, offset+length, ERROR_PAINTER); 160 } 161 162 165 protected EditorKit createDefaultEditorKit() { return EDITOR_KIT; } 166 167 168 protected void paintComponent(Graphics g) { 169 if (CodeStatus.DEVELOPMENT) { 170 if (_antiAliasText && g instanceof Graphics2D) { 171 Graphics2D g2d = (Graphics2D)g; 172 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 173 } 174 } 175 super.paintComponent(g); 176 } 177 178 179 public DJDocument getDJDocument() { return _doc; } 180 181 182 protected void _updateMatchHighlight() { 183 addToPromptList(getPromptPos()); 184 int to = getCaretPosition(); 185 int from = getDJDocument().balanceBackward(); if (from > -1) { 187 from = to - from; 189 if (_notCrossesPrompt(to,from)) _addHighlight(from, to); 190 } 192 else { 194 from = to; 196 to = getDJDocument().balanceForward(); 197 198 if (to > -1) { 199 to = to + from; 200 if (_notCrossesPrompt(to,from)) _addHighlight(from - 1, to); 201 } 203 } 204 } 205 206 207 List <Integer > getPromptList() { return _listOfPrompt; } 208 209 210 public void resetPrompts() { _listOfPrompt.clear(); } 211 212 214 void addToPromptList(int pos) { 215 if (! _listOfPrompt.contains(new Integer (pos))) _listOfPrompt.add(new Integer (pos)); 216 } 217 218 219 private boolean _notCrossesPrompt(int to, int from) { 220 boolean toReturn = true; 222 for (Integer prompt : _listOfPrompt) { 223 toReturn &= ((to >= prompt && from >= prompt) || (to <= prompt && from <= prompt)); 224 } 225 return toReturn; 226 } 227 228 234 protected void indentLines(int selStart, int selEnd, int reason, ProgressMonitor pm) { 235 try { 236 _doc.indentLines(selStart, selEnd, reason, pm); 237 setCaretPosition(_doc.getCurrentLocation()); 238 } 239 catch (OperationCanceledException oce) { throw new UnexpectedException(oce); } 240 } 241 242 249 protected boolean shouldIndent(int selStart, int selEnd) { return true; } 250 251 252 public abstract int getPromptPos(); 253 } | Popular Tags |