forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJFTextArea.java
More file actions
executable file
·173 lines (153 loc) · 4.54 KB
/
Copy pathJFTextArea.java
File metadata and controls
executable file
·173 lines (153 loc) · 4.54 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
package javaforce;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.undo.*;
/**
* Extends JTextArea to provide some extra features such as overwrite mode, and
* undo/redo
*/
public class JFTextArea extends JTextArea {
private static Toolkit toolkit = Toolkit.getDefaultToolkit();
private static boolean isOvertypeMode;
private Caret defaultCaret;
private Caret overtypeCaret;
private UndoManager undo;
public void clearHistory() {
undo.discardAllEdits();
}
private void init() {
//setup overwrite mode
setCaretColor(Color.black);
defaultCaret = getCaret();
overtypeCaret = new OvertypeCaret();
overtypeCaret.setBlinkRate(defaultCaret.getBlinkRate());
setOvertypeMode(false);
//setup undo manager
undo = new UndoManager();
// Listen for undo and redo events
getDocument().addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent evt) {
undo.addEdit(evt.getEdit());
}
});
// Create an undo action and add it to the text component
getActionMap().put("Undo", new AbstractAction("Undo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canUndo()) {
undo.undo();
}
} catch (CannotUndoException e) {
}
}
});
// Bind the undo action to ctl-Z
getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
// Create a redo action and add it to the text component
getActionMap().put("Redo", new AbstractAction("Redo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canRedo()) {
undo.redo();
}
} catch (CannotRedoException e) {
}
}
});
// Bind the redo action to ctl-Y
getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
}
public JFTextArea() {
super();
init();
}
public JFTextArea(int row, int column) {
super(row, column);
init();
}
/*
* Return the overtype/insert mode
*/
public boolean isOvertypeMode() {
return isOvertypeMode;
}
/*
* Set the caret to use depending on overtype/insert mode
*/
public void setOvertypeMode(boolean isOvertypeMode) {
this.isOvertypeMode = isOvertypeMode;
int pos = getCaretPosition();
if (isOvertypeMode()) {
setCaret(overtypeCaret);
} else {
setCaret(defaultCaret);
}
setCaretPosition(pos);
}
/*
* Override method from JComponent
*/
public void replaceSelection(String text) {
// Implement overtype mode by selecting the character at the current
// caret position
if (isOvertypeMode()) {
int pos = getCaretPosition();
if ((getSelectedText() == null) && (pos < getDocument().getLength())) {
moveCaretPosition(pos + 1);
}
}
super.replaceSelection(text);
}
/*
* Override method from JComponent
*/
protected void processKeyEvent(KeyEvent e) {
super.processKeyEvent(e);
// Handle release of Insert key to toggle overtype/insert mode
if (e.getID() == KeyEvent.KEY_RELEASED && e.getKeyCode() == KeyEvent.VK_INSERT) {
setOvertypeMode(!isOvertypeMode());
}
}
/*
* Paint a horizontal line the width of a column and 1 pixel high
*/
private class OvertypeCaret extends DefaultCaret {
/*
* The overtype caret will simply be a horizontal line one pixel high
* (once we determine where to paint it)
*/
public void paint(Graphics g) {
if (isVisible()) {
try {
JTextComponent component = getComponent();
TextUI mapper = component.getUI();
Rectangle r = mapper.modelToView(component, getDot());
g.setColor(component.getCaretColor());
int width = g.getFontMetrics().charWidth('w');
int y = r.y + r.height - 2;
g.drawLine(r.x, y, r.x + width - 2, y);
} catch (BadLocationException e) {
}
}
}
/*
* Damage must be overridden whenever the paint method is overridden
* (The damaged area is the area the caret is painted in. We must
* consider the area for the default caret and this caret)
*/
protected synchronized void damage(Rectangle r) {
if (r != null) {
JTextComponent component = getComponent();
x = r.x;
y = r.y;
width = component.getFontMetrics(component.getFont()).charWidth('w');
height = r.height;
repaint();
}
}
}
}