-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathCodeArea.java
More file actions
384 lines (332 loc) · 12.7 KB
/
CodeArea.java
File metadata and controls
384 lines (332 loc) · 12.7 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package nodebox.client;
import nodebox.util.StringUtils;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.Element;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CodeArea extends JEditorPane implements UndoableEditListener {
/**
* The last search string is the last search the user actually typed.
*/
private String lastSearchString = "";
/**
* The current search string is the last search performed, whether selected text or typed text, and is
* re-performed when find next is invoked.
*/
private String currentSearchString = "";
public final static Logger LOG = Logger.getLogger(CodeArea.class.getName());
private Element rootElement;
private boolean wrap;
private UndoManager undoManager = new UndoManager();
public static InputMap defaultInputMap;
static {
defaultInputMap = new InputMap();
defaultInputMap.put(PlatformUtils.getKeyStroke(KeyEvent.VK_UP), DefaultEditorKit.beginAction);
defaultInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), PythonEditorKit.returnAction);
defaultInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), PythonEditorKit.tabAction);
defaultInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, Event.SHIFT_MASK), PythonEditorKit.dedentAction);
defaultInputMap.put(PlatformUtils.getKeyStroke('['), PythonEditorKit.dedentAction);
defaultInputMap.put(PlatformUtils.getKeyStroke(']'), PythonEditorKit.indentAction);
defaultInputMap.put(PlatformUtils.getKeyStroke(KeyEvent.VK_DOWN), DefaultEditorKit.endAction);
defaultInputMap.put(PlatformUtils.getKeyStroke(KeyEvent.VK_UP, Event.SHIFT_MASK), DefaultEditorKit.selectionBeginAction);
defaultInputMap.put(PlatformUtils.getKeyStroke(KeyEvent.VK_DOWN, Event.SHIFT_MASK), DefaultEditorKit.selectionEndAction);
}
private void init() {
this.setMargin(new Insets(0, 5, 0, 5));
setFont(Theme.EDITOR_FONT);
setEditorKit(new PythonEditorKit());
getDocument().addUndoableEditListener(undoManager);
rootElement = getDocument().getDefaultRootElement();
// todo:this code should be in the kit
for (KeyStroke ks : defaultInputMap.allKeys()) {
getInputMap().put(ks, defaultInputMap.get(ks));
}
defaultInputMap.put(PlatformUtils.getKeyStroke(KeyEvent.VK_Z), new UndoAction());
defaultInputMap.put(PlatformUtils.getKeyStroke(KeyEvent.VK_Z, Event.SHIFT_MASK), new RedoAction());
addMouseListener(new DragDetector());
}
public CodeArea() {
init();
}
public void discardAllEdits() {
undoManager.discardAllEdits();
}
public void undo() {
try {
undoManager.undo();
} catch (CannotUndoException ignored) {
}
}
public void redo() {
try {
undoManager.redo();
} catch (CannotRedoException ignored) {
}
}
public void setWrap(boolean wrap) {
this.wrap = wrap;
setSize(getSize());
}
public boolean isWrap() {
return wrap;
}
public void setSize(Dimension d) {
if (!wrap) {
if (d.width < getParent().getSize().width)
d.width = getParent().getSize().width;
}
super.setSize(d);
}
public boolean getScrollableTracksViewportWidth() {
return wrap;
}
/**
* Position the cursor at the first occurence of the given text and scrolls it into view.
* If the text is not found, nothing happens.
*
* @param text the text to find. Case-sensitive
* @return true if the text was found.
*/
public boolean find(String text) {
// There is a difference between the last searched string and the current find string.
// The last searched is the last typed find, whereas the current find is either
// the last typed *or* the selected text.
// Selected text is not saved as the last find.
String selectedText = getSelectedText();
if (text != null && !text.equals(selectedText)) {
lastSearchString = text;
}
currentSearchString = text;
return find(text, 0);
}
/**
* Position the cursor at the next occurence of the text given to the previous find call.
* If the text is not found, nothing happens.
*
* @return true if the text was found again.
*/
public boolean findAgain() {
return find(currentSearchString, getCaretPosition() + 1);
}
public boolean find(String text, int fromPos) {
int index = getText().indexOf(text, fromPos);
if (index >= 0) {
setCaretPosition(index - 1);
moveCaretPosition(index + text.length() - 1);
scrollToPos(index);
return true;
}
return false;
}
/**
* Return the string that will be used for searching. If text is selected, the
* selected text will be the search string. Otherwise, return the last search
* string used.
*
* @return the string used for searching.
*/
public String getSearchString() {
String selectedText = getSelectedText();
if (selectedText != null) {
return selectedText;
} else {
return lastSearchString;
}
}
/**
* Set the caret at the beginning of the given line and scrolls the line into view.
*
* @param line the line number, one-based
* @return true if the line could be found.
*/
public boolean goToLine(int line) {
int offset = getOffsetForLine(line);
if (offset >= 0) {
setCaretPosition(offset);
scrollToPos(offset);
return true;
}
return false;
}
/**
* Returns the position in the document's text of the beginning of the given line.
*
* @param line the line number, one-based
* @return the position at the beginning of the line, or -1 if the line number is invalid.
*/
public int getOffsetForLine(int line) {
if (line <= 0) {
line = 1;
}
if (line > rootElement.getElementCount()) {
line = rootElement.getElementCount();
}
Element lineEl = rootElement.getElement(line - 1);
if (lineEl != null) {
return lineEl.getStartOffset();
}
return -1;
}
/**
* Returns the line number of the documents' text offset.
*
* @param offset the offset
* @return the line-number, one-based
*/
public int getLineForOffset(int offset) {
return rootElement.getElementIndex(offset) + 1;
}
/**
* Return the column for the given offset. This is the position in the current line.
*
* @param offset the offset
* @return the column of the offset.
* @see #getLineForOffset(int) the line for the given offset.
*/
public int getColumnForOffset(int offset) {
int line = getLineForOffset(offset);
int begin = getOffsetForLine(line);
return offset - begin + 1;
}
public boolean scrollToPos(int index) {
try {
// todo: FIX scrolling to half of view, so line is in the middle of the view.
Rectangle r = modelToView(index);
Dimension pDim = getParent().getSize();
int halfHeight = pDim.height / 2;
scrollRectToVisible(new Rectangle(r.x, r.y - halfHeight, r.width, r.height));
return true;
} catch (BadLocationException e) {
return false;
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
super.paintComponent(g);
}
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
}
public class Dragger extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
}
}
public class DragDetector implements MouseListener, MouseMotionListener {
public DragDetector() {
dragWindow = new JWindow();
dragLabel = new JLabel();
dragLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
dragLabel.setText("12300.00");
dragLabel.setAlignmentX(0F);
dragWindow.getContentPane().add(dragLabel, BorderLayout.CENTER);
dragWindow.pack();
}
public void mousePressed(MouseEvent e) {
if ((e.getModifiersEx() & BTN1_CTRL_MASK) != BTN1_CTRL_MASK) return;
LOG.info("mouse pressed at " + e.getPoint());
int index = viewToModel(e.getPoint()); // Returns the correct coordinate.
String text = getText();
int[] range = StringUtils.numberRange(text, index);
if (range == null) return;
int left = range[0];
int right = range[1];
String number = StringUtils.numberForRange(text, range);
LOG.info("The number is " + number);
try {
value = Float.parseFloat(number);
} catch (NumberFormatException ex) {
LOG.log(Level.WARNING, "parsing gave error: " + number, ex);
}
try {
// Get position of rightmost character of the number.
Rectangle r = modelToView(left);
// Get the location of the code area.
Point p = CodeArea.this.getLocationOnScreen();
// The position of the window equals the bottom-left point of the rightmost character.
p.x += r.x;
p.y += r.y + r.height;
dragWindow.setLocation(p);
dragLabel.setText(number);
dragWindow.setVisible(true);
// Initialize the drag listener
prevX = startX = e.getX();
CodeArea.this.addMouseMotionListener(this);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
public void mouseReleased(MouseEvent e) {
if (dragWindow.isVisible()) {
dragWindow.setVisible(false);
CodeArea.this.removeMouseMotionListener(this);
}
}
public void mouseDragged(MouseEvent e) {
int newX = e.getX();
float delta = newX - prevX;
LOG.info("prev " + prevX + " newX " + newX + " delta " + delta);
//if ((e.getModifiersEx() & BTN1_CTRL_ALT_MASK) == BTN1_CTRL_ALT_MASK) delta /= 100;
//if ((e.getModifiersEx() & BTN1_CTRL_SHIFT_MASK) == BTN1_CTRL_SHIFT_MASK) delta *= 10;
value += delta;
dragLabel.setText("" + value);
prevX = newX;
e.consume();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
private JWindow dragWindow;
private JLabel dragLabel;
private int startX, prevX;
private float value;
private static final int BTN1_CTRL_MASK = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.CTRL_DOWN_MASK;
private static final int BTN1_CTRL_SHIFT_MASK = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.CTRL_DOWN_MASK | MouseEvent.SHIFT_DOWN_MASK;
private static final int BTN1_CTRL_ALT_MASK = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.CTRL_DOWN_MASK | MouseEvent.ALT_DOWN_MASK;
}
public String getLastSearchString() {
return lastSearchString;
}
public void setLastSearchString(String lastSearchString) {
this.lastSearchString = lastSearchString;
}
public String getCurrentSearchString() {
return currentSearchString;
}
public void setCurrentSearchString(String currentSearchString) {
this.currentSearchString = currentSearchString;
}
private class UndoAction extends AbstractAction {
private UndoAction() {
super("Undo");
}
public void actionPerformed(ActionEvent e) {
undo();
}
}
private class RedoAction extends AbstractAction {
private RedoAction() {
super("Redo");
}
public void actionPerformed(ActionEvent e) {
redo();
}
}
}