|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.ui; |
|
38 |
| |
|
39 |
| import javax.swing.*; |
|
40 |
| import javax.swing.text.*; |
|
41 |
| import java.awt.*; |
|
42 |
| import javax.swing.undo.*; |
|
43 |
| import javax.swing.event.*; |
|
44 |
| import java.awt.event.*; |
|
45 |
| |
|
46 |
| import edu.rice.cs.drjava.model.definitions.*; |
|
47 |
| |
|
48 |
| import java.awt.event.KeyEvent; |
|
49 |
| import java.awt.datatransfer.*; |
|
50 |
| import java.util.Vector; |
|
51 |
| |
|
52 |
| import edu.rice.cs.util.OperationCanceledException; |
|
53 |
| import edu.rice.cs.util.Log; |
|
54 |
| import edu.rice.cs.util.swing.*; |
|
55 |
| import edu.rice.cs.util.swing.Utilities; |
|
56 |
| import edu.rice.cs.util.UnexpectedException; |
|
57 |
| import edu.rice.cs.drjava.config.*; |
|
58 |
| import edu.rice.cs.drjava.*; |
|
59 |
| import edu.rice.cs.drjava.model.DJDocument; |
|
60 |
| import edu.rice.cs.drjava.model.definitions.indent.Indenter; |
|
61 |
| import edu.rice.cs.drjava.model.repl.*; |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| public abstract class InteractionsPane extends AbstractDJPane implements OptionConstants, ClipboardOwner { |
|
67 |
| |
|
68 |
| public static Log LOG = new Log("InteractionsPane.txt", false); |
|
69 |
| |
|
70 |
| |
|
71 |
| private volatile UndoAction _undoAction; |
|
72 |
| private volatile RedoAction _redoAction; |
|
73 |
| public volatile boolean _inCompoundEdit = false; |
|
74 |
| private volatile int _compoundEditKey; |
|
75 |
| private volatile boolean deleteCEBool = true; |
|
76 |
| |
|
77 |
| |
|
78 |
| protected final Keymap _keymap; |
|
79 |
| |
|
80 |
| |
|
81 |
| private boolean _antiAliasText = false; |
|
82 |
| |
|
83 |
| static StyledEditorKit EDITOR_KIT; |
|
84 |
| |
|
85 |
21
| static { EDITOR_KIT = new InteractionsEditorKit(); }
|
|
86 |
| |
|
87 |
| |
|
88 |
| protected Runnable _beep = new Runnable() { |
|
89 |
0
| public void run() { Toolkit.getDefaultToolkit().beep(); }
|
|
90 |
| }; |
|
91 |
| |
|
92 |
| |
|
93 |
| private class AntiAliasOptionListener implements OptionListener<Boolean> { |
|
94 |
0
| public void optionChanged(OptionEvent<Boolean> oce) {
|
|
95 |
0
| _antiAliasText = oce.value.booleanValue();
|
|
96 |
0
| InteractionsPane.this.repaint();
|
|
97 |
| } |
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| private class leftUndoBreak extends MouseAdapter { |
|
105 |
0
| public void mouseClicked(MouseEvent e){
|
|
106 |
0
| endCompoundEdit();
|
|
107 |
| } |
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
203
| public Runnable getBeep() { return _beep; }
|
|
112 |
| |
|
113 |
| private final InteractionsDJDocument _doc; |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
165
| public InteractionsPane(InteractionsDJDocument doc) { this("INTERACTIONS_KEYMAP", doc); }
|
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
203
| public InteractionsPane(String keymapName, InteractionsDJDocument doc) {
|
|
128 |
203
| super(doc);
|
|
129 |
203
| _doc = doc;
|
|
130 |
| |
|
131 |
203
| _keymap = addKeymap(keymapName, getKeymap());
|
|
132 |
| |
|
133 |
203
| setCaretPosition(doc.getLength());
|
|
134 |
| |
|
135 |
203
| setHighlighter(new ReverseHighlighter());
|
|
136 |
203
| _highlightManager = new HighlightManager(this);
|
|
137 |
| |
|
138 |
203
| _antiAliasText = DrJava.getConfig().getSetting(TEXT_ANTIALIAS).booleanValue();
|
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
203
| new ForegroundColorListener(this);
|
|
145 |
203
| new BackgroundColorListener(this);
|
|
146 |
| |
|
147 |
203
| OptionListener<Boolean> aaTemp = new AntiAliasOptionListener();
|
|
148 |
203
| DrJava.getConfig().addOptionListener(OptionConstants.TEXT_ANTIALIAS, aaTemp);
|
|
149 |
| |
|
150 |
203
| _resetUndo();
|
|
151 |
203
| addMouseListener(new leftUndoBreak());
|
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
0
| public void lostOwnership(Clipboard clipboard, Transferable contents) {
|
|
156 |
| |
|
157 |
| } |
|
158 |
| |
|
159 |
| |
|
160 |
21
| public void processKeyEvent(KeyEvent e) {
|
|
161 |
| |
|
162 |
| |
|
163 |
21
| if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE && deleteCEBool){
|
|
164 |
0
| endCompoundEdit();
|
|
165 |
0
| deleteCEBool=false;
|
|
166 |
| } |
|
167 |
21
| else if(e.getID()==KeyEvent.KEY_PRESSED && e.getKeyCode() != KeyEvent.VK_BACK_SPACE){
|
|
168 |
8
| deleteCEBool = true;
|
|
169 |
| } |
|
170 |
| |
|
171 |
21
| KeyStroke ks = KeyStroke.getKeyStrokeForEvent(e);
|
|
172 |
21
| Action a = KeyBindingManager.ONLY.get(ks);
|
|
173 |
| |
|
174 |
21
| if ((ks != KeyStrokeOption.NULL_KEYSTROKE) && (a != null)) {
|
|
175 |
1
| endCompoundEdit();
|
|
176 |
| } |
|
177 |
| |
|
178 |
2
| if ((e.getModifiers() & InputEvent.SHIFT_MASK)!=0 && e.getKeyCode()==KeyEvent.VK_ENTER) endCompoundEdit();
|
|
179 |
| |
|
180 |
21
| super.processKeyEvent(e);
|
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
2475
| public void addActionForKeyStroke(KeyStroke stroke, Action action) {
|
|
188 |
| |
|
189 |
2475
| KeyStroke[] keys = _keymap.getKeyStrokesForAction(action);
|
|
190 |
2475
| if (keys != null) {
|
|
191 |
660
| for (int i = 0; i < keys.length; i++) _keymap.removeKeyStrokeBinding(keys[i]);
|
|
192 |
| } |
|
193 |
2475
| _keymap.addActionForKeyStroke(stroke, action);
|
|
194 |
2475
| setKeymap(_keymap);
|
|
195 |
| } |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
1624
| public void addActionForKeyStroke(Vector<KeyStroke> stroke, Action action) {
|
|
202 |
| |
|
203 |
1624
| KeyStroke[] keys = _keymap.getKeyStrokesForAction(action);
|
|
204 |
1624
| if (keys != null) {
|
|
205 |
0
| for (int i = 0; i < keys.length; i++) _keymap.removeKeyStrokeBinding(keys[i]);
|
|
206 |
| } |
|
207 |
1624
| for (KeyStroke ks: stroke) {
|
|
208 |
1624
| _keymap.addActionForKeyStroke(ks, action);
|
|
209 |
| } |
|
210 |
1624
| setKeymap(_keymap);
|
|
211 |
| } |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
15
| public void setBeep(Runnable beep) { _beep = beep; }
|
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
0
| public void highlightError(int offset, int length) {
|
|
223 |
0
| _highlightManager.addHighlight(offset, offset+length, ERROR_PAINTER);
|
|
224 |
| } |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
406
| protected EditorKit createDefaultEditorKit() { return EDITOR_KIT; }
|
|
230 |
| |
|
231 |
| |
|
232 |
0
| protected void paintComponent(Graphics g) {
|
|
233 |
0
| if (g == null) return;
|
|
234 |
0
| if (_antiAliasText && g instanceof Graphics2D) {
|
|
235 |
0
| Graphics2D g2d = (Graphics2D)g;
|
|
236 |
0
| g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
|
237 |
| } |
|
238 |
0
| super.paintComponent(g);
|
|
239 |
| } |
|
240 |
| |
|
241 |
| |
|
242 |
428
| public DJDocument getDJDocument() { return _doc; }
|
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
14
| protected void matchUpdate(int offset, boolean opening) {
|
|
249 |
0
| if (! _doc.hasPrompt()) return;
|
|
250 |
14
| _doc.setCurrentLocation(offset);
|
|
251 |
14
| _removePreviousHighlight();
|
|
252 |
| |
|
253 |
14
| int caretPos = getCaretPosition();
|
|
254 |
| |
|
255 |
14
| if (opening) {
|
|
256 |
| |
|
257 |
| |
|
258 |
0
| int to = _doc.balanceForward();
|
|
259 |
| |
|
260 |
0
| if (to > -1) {
|
|
261 |
0
| int end = caretPos + to;
|
|
262 |
0
| _addHighlight(caretPos - 1, end);
|
|
263 |
| } |
|
264 |
| } |
|
265 |
| else { |
|
266 |
14
| int from = _doc.balanceBackward();
|
|
267 |
14
| if (from > -1) {
|
|
268 |
14
| int start = caretPos - from;
|
|
269 |
14
| _addHighlight(start, caretPos);
|
|
270 |
| } |
|
271 |
| } |
|
272 |
| } |
|
273 |
| |
|
274 |
| |
|
275 |
412
| protected void updateStatusField() { }
|
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
0
| protected void indentLines(int selStart, int selEnd, Indenter.IndentReason reason, ProgressMonitor pm) {
|
|
284 |
0
| assert EventQueue.isDispatchThread();
|
|
285 |
0
| try {
|
|
286 |
0
| _doc.indentLines(selStart, selEnd, reason, pm);
|
|
287 |
0
| setCaretPos(_doc.getCurrentLocation());
|
|
288 |
| } |
|
289 |
0
| catch (OperationCanceledException oce) { throw new UnexpectedException(oce); }
|
|
290 |
| } |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
0
| protected boolean shouldIndent(int selStart, int selEnd) { return true; }
|
|
300 |
| |
|
301 |
| |
|
302 |
| public abstract int getPromptPos(); |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| private final UndoableEditListener _undoListener = new UndoableEditListener() { |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
260
| public void undoableEditHappened(UndoableEditEvent e) {
|
|
313 |
260
| assert EventQueue.isDispatchThread() || Utilities.TEST_MODE;
|
|
314 |
260
| UndoableEdit undo = e.getEdit();
|
|
315 |
260
| LOG.log("In undoableEditHappened - _inCompoundEdit is "+ _inCompoundEdit);
|
|
316 |
260
| if (! _inCompoundEdit) {
|
|
317 |
37
| CompoundUndoManager undoMan = _doc.getUndoManager();
|
|
318 |
37
| _inCompoundEdit = true;
|
|
319 |
37
| _compoundEditKey = undoMan.startCompoundEdit();
|
|
320 |
37
| getUndoAction().updateUndoState();
|
|
321 |
37
| getRedoAction().updateRedoState();
|
|
322 |
| } |
|
323 |
260
| _doc.getUndoManager().addEdit(undo);
|
|
324 |
260
| getRedoAction().setEnabled(false);
|
|
325 |
| } |
|
326 |
| }; |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
13
| public void endCompoundEdit() {
|
|
331 |
13
| if (_inCompoundEdit) {
|
|
332 |
12
| CompoundUndoManager undoMan = _doc.getUndoManager();
|
|
333 |
12
| _inCompoundEdit = false;
|
|
334 |
12
| undoMan.endCompoundEdit(_compoundEditKey);
|
|
335 |
| } |
|
336 |
| } |
|
337 |
| |
|
338 |
| |
|
339 |
188
| public UndoAction getUndoAction() { return _undoAction; }
|
|
340 |
| |
|
341 |
| |
|
342 |
448
| public RedoAction getRedoAction() { return _redoAction; }
|
|
343 |
| |
|
344 |
| |
|
345 |
| public class UndoAction extends AbstractAction { |
|
346 |
| |
|
347 |
| |
|
348 |
203
| private UndoAction() {
|
|
349 |
203
| super("Undo");
|
|
350 |
203
| setEnabled(false);
|
|
351 |
| } |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
0
| public void actionPerformed(ActionEvent e) {
|
|
357 |
0
| try {
|
|
358 |
| |
|
359 |
0
| _doc.getUndoManager().undo();
|
|
360 |
0
| _doc.updateModifiedSinceSave();
|
|
361 |
| } |
|
362 |
| catch (CannotUndoException ex) { |
|
363 |
0
| throw new UnexpectedException(ex);
|
|
364 |
| } |
|
365 |
0
| updateUndoState();
|
|
366 |
0
| _redoAction.updateRedoState();
|
|
367 |
| } |
|
368 |
| |
|
369 |
| |
|
370 |
250
| protected void updateUndoState() {
|
|
371 |
250
| if (_doc.undoManagerCanUndo() && isEditable()) {
|
|
372 |
37
| setEnabled(true);
|
|
373 |
37
| putValue(Action.NAME, _doc.getUndoManager().getUndoPresentationName());
|
|
374 |
| } |
|
375 |
| else { |
|
376 |
213
| setEnabled(false);
|
|
377 |
213
| putValue(Action.NAME, "Undo");
|
|
378 |
| } |
|
379 |
| } |
|
380 |
| } |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| public class RedoAction extends AbstractAction { |
|
385 |
| |
|
386 |
| |
|
387 |
203
| private RedoAction() {
|
|
388 |
203
| super("Redo");
|
|
389 |
203
| setEnabled(false);
|
|
390 |
| } |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
0
| public void actionPerformed(ActionEvent e) {
|
|
396 |
0
| try {
|
|
397 |
0
| _doc.getUndoManager().redo();
|
|
398 |
0
| _doc.updateModifiedSinceSave();
|
|
399 |
| } catch (CannotRedoException ex) { |
|
400 |
0
| throw new UnexpectedException(ex);
|
|
401 |
| } |
|
402 |
0
| updateRedoState();
|
|
403 |
0
| _undoAction.updateUndoState();
|
|
404 |
| } |
|
405 |
| |
|
406 |
| |
|
407 |
250
| protected void updateRedoState() {
|
|
408 |
250
| if (_doc.undoManagerCanRedo() && isEditable()) {
|
|
409 |
0
| setEnabled(true);
|
|
410 |
0
| putValue(Action.NAME, _doc.getUndoManager().getRedoPresentationName());
|
|
411 |
| } |
|
412 |
| else { |
|
413 |
250
| setEnabled(false);
|
|
414 |
250
| putValue(Action.NAME, "Redo");
|
|
415 |
| } |
|
416 |
| } |
|
417 |
| } |
|
418 |
| |
|
419 |
| |
|
420 |
10
| private void resetUndo() {
|
|
421 |
10
| _doc.getUndoManager().discardAllEdits();
|
|
422 |
10
| _undoAction.updateUndoState();
|
|
423 |
10
| _redoAction.updateRedoState();
|
|
424 |
| } |
|
425 |
| |
|
426 |
| |
|
427 |
10
| public void discardUndoEdits() {
|
|
428 |
10
| endCompoundEdit();
|
|
429 |
10
| resetUndo();
|
|
430 |
| } |
|
431 |
| |
|
432 |
| |
|
433 |
203
| public void _resetUndo() {
|
|
434 |
203
| if (_undoAction == null) _undoAction = new UndoAction();
|
|
435 |
203
| if (_redoAction == null) _redoAction = new RedoAction();
|
|
436 |
| |
|
437 |
203
| _doc.resetUndoManager();
|
|
438 |
| |
|
439 |
203
| _doc.addUndoableEditListener(_undoListener);
|
|
440 |
203
| _undoAction.updateUndoState();
|
|
441 |
203
| _redoAction.updateRedoState();
|
|
442 |
| } |
|
443 |
| } |