|
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.model.definitions; |
|
38 |
| |
|
39 |
| import edu.rice.cs.drjava.ui.AbstractDJPane; |
|
40 |
| import edu.rice.cs.util.UnexpectedException; |
|
41 |
| |
|
42 |
| import javax.swing.text.*; |
|
43 |
| import javax.swing.Action; |
|
44 |
| import java.awt.event.ActionEvent; |
|
45 |
| import edu.rice.cs.drjava.model.GlobalEventNotifier; |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| public class DefinitionsEditorKit extends StyledEditorKit { |
|
54 |
| |
|
55 |
| public static final String DELIMITERS = "!@%^&*()-=+[]{};:'\",.<>/?"; |
|
56 |
| |
|
57 |
| private GlobalEventNotifier _notifier; |
|
58 |
| private Action[] _actions; |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
607
| public DefinitionsEditorKit(GlobalEventNotifier notifier) {
|
|
64 |
607
| _notifier = notifier;
|
|
65 |
607
| Action[] supActions = super.getActions();
|
|
66 |
607
| _actions = new Action[supActions.length];
|
|
67 |
607
| for(int i = 0; i < _actions.length; ++i) {
|
|
68 |
44311
| Action a = supActions[i];
|
|
69 |
44311
| Object name = a.getValue("Name");
|
|
70 |
44311
| if (name.equals(beginWordAction))
|
|
71 |
607
| _actions[i] = new BeginWordAction(beginWordAction, false);
|
|
72 |
43704
| else if (name.equals(endWordAction))
|
|
73 |
607
| _actions[i] = new EndWordAction(endWordAction, false);
|
|
74 |
43097
| else if (name.equals(nextWordAction))
|
|
75 |
607
| _actions[i] = new NextWordAction(nextWordAction, false);
|
|
76 |
42490
| else if (name.equals(previousWordAction))
|
|
77 |
607
| _actions[i] = new PreviousWordAction(previousWordAction, false);
|
|
78 |
41883
| else if (name.equals(selectionNextWordAction))
|
|
79 |
607
| _actions[i] = new NextWordAction(selectionNextWordAction, true);
|
|
80 |
41276
| else if (name.equals(selectionPreviousWordAction))
|
|
81 |
607
| _actions[i] = new PreviousWordAction(selectionPreviousWordAction, true);
|
|
82 |
40669
| else if (name.equals(selectWordAction))
|
|
83 |
607
| _actions[i] = new SelectWordAction();
|
|
84 |
40062
| else _actions[i] = a;
|
|
85 |
| } |
|
86 |
| } |
|
87 |
| |
|
88 |
93
| public Action[] getActions() { return _actions; }
|
|
89 |
| |
|
90 |
| private static ViewFactory _factory = new ViewFactory() { |
|
91 |
597
| public View create(Element elem) {
|
|
92 |
| |
|
93 |
| |
|
94 |
597
| return new ColoringView(elem);
|
|
95 |
| } |
|
96 |
| }; |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
0
| public DefinitionsDocument createNewDocument() { return _createDefaultTypedDocument(); }
|
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
0
| private DefinitionsDocument _createDefaultTypedDocument() { return new DefinitionsDocument(_notifier); }
|
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
187
| public String getContentType() { return "text/java"; }
|
|
122 |
| |
|
123 |
| |
|
124 |
780
| public final ViewFactory getViewFactory() { return _factory; }
|
|
125 |
| |
|
126 |
| |
|
127 |
| static class BeginWordAction extends TextAction { |
|
128 |
| private boolean _select; |
|
129 |
| |
|
130 |
1214
| BeginWordAction(String nm, boolean b) {
|
|
131 |
1214
| super(nm);
|
|
132 |
1214
| _select = b;
|
|
133 |
| } |
|
134 |
| |
|
135 |
0
| public void actionPerformed(ActionEvent e) {
|
|
136 |
0
| AbstractDJPane target = (AbstractDJPane) getTextComponent(e);
|
|
137 |
0
| if (target != null) {
|
|
138 |
0
| try {
|
|
139 |
0
| int offs = target.getCaretPosition();
|
|
140 |
0
| final String text = target.getDocument().getText(0, offs);
|
|
141 |
0
| while(offs > 0) {
|
|
142 |
0
| char chPrev = text.charAt(offs - 1);
|
|
143 |
0
| if ((DELIMITERS.indexOf(chPrev) >= 0) || (Character.isWhitespace(chPrev))) {
|
|
144 |
0
| break;
|
|
145 |
| } |
|
146 |
0
| --offs;
|
|
147 |
0
| if (offs == 0) break;
|
|
148 |
0
| char ch = text.charAt(offs);
|
|
149 |
0
| chPrev = text.charAt(offs - 1);
|
|
150 |
0
| if ((DELIMITERS.indexOf(ch) >= 0) || (DELIMITERS.indexOf(chPrev) >= 0)
|
|
151 |
| || Character.isWhitespace(ch) || Character.isWhitespace(chPrev)) { |
|
152 |
0
| break;
|
|
153 |
| } |
|
154 |
| } |
|
155 |
0
| if (_select) {
|
|
156 |
0
| target.moveCaretPosition(offs);
|
|
157 |
| } else { |
|
158 |
0
| target.setCaretPosition(offs);
|
|
159 |
| } |
|
160 |
| } |
|
161 |
0
| catch(BadLocationException ble) { throw new UnexpectedException(ble); }
|
|
162 |
| } |
|
163 |
| } |
|
164 |
| } |
|
165 |
| |
|
166 |
| |
|
167 |
| static class EndWordAction extends TextAction { |
|
168 |
| private boolean _select; |
|
169 |
| |
|
170 |
1214
| EndWordAction(String nm, boolean b) {
|
|
171 |
1214
| super(nm);
|
|
172 |
1214
| _select = b;
|
|
173 |
| } |
|
174 |
| |
|
175 |
0
| public void actionPerformed(ActionEvent e) {
|
|
176 |
0
| AbstractDJPane target = (AbstractDJPane) getTextComponent(e);
|
|
177 |
0
| if (target != null) {
|
|
178 |
0
| try {
|
|
179 |
0
| int offs = target.getCaretPosition();
|
|
180 |
0
| final int iOffs = offs;
|
|
181 |
0
| final String text = target.getDocument().getText(iOffs,target.getDocument().getLength()-iOffs);
|
|
182 |
0
| while((offs-iOffs)<text.length()-1) {
|
|
183 |
0
| ++offs;
|
|
184 |
0
| char ch = text.charAt(offs-iOffs);
|
|
185 |
0
| if ((DELIMITERS.indexOf(ch) >= 0) || Character.isWhitespace(ch)) {
|
|
186 |
0
| break;
|
|
187 |
| } |
|
188 |
| } |
|
189 |
0
| if (_select) {
|
|
190 |
0
| target.moveCaretPosition(offs);
|
|
191 |
| } else { |
|
192 |
0
| target.setCaretPosition(offs);
|
|
193 |
| } |
|
194 |
| } |
|
195 |
0
| catch(BadLocationException ble) { throw new UnexpectedException(ble); }
|
|
196 |
| } |
|
197 |
| } |
|
198 |
| } |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| static class PreviousWordAction extends TextAction { |
|
205 |
| private boolean _select; |
|
206 |
| |
|
207 |
1214
| PreviousWordAction(String nm, boolean b) {
|
|
208 |
1214
| super(nm);
|
|
209 |
1214
| _select = b;
|
|
210 |
| } |
|
211 |
| |
|
212 |
0
| public void actionPerformed(ActionEvent e) {
|
|
213 |
0
| AbstractDJPane target = (AbstractDJPane) getTextComponent(e);
|
|
214 |
0
| if (target != null) {
|
|
215 |
0
| try {
|
|
216 |
0
| int offs = target.getCaretPosition();
|
|
217 |
0
| final String text = target.getDocument().getText(0,offs);
|
|
218 |
0
| while(offs > 0) {
|
|
219 |
0
| --offs;
|
|
220 |
0
| if (offs == 0) break;
|
|
221 |
0
| char ch = text.charAt(offs);
|
|
222 |
0
| char chPrev = text.charAt(offs - 1);
|
|
223 |
0
| if (Character.isWhitespace(ch) && Character.isWhitespace(chPrev) && ch!='\n') continue;
|
|
224 |
0
| else if (DELIMITERS.indexOf(ch) >= 0 || DELIMITERS.indexOf(chPrev) >= 0 ||
|
|
225 |
| (offs >= 2 && Character.isWhitespace(chPrev) && !Character.isWhitespace(text.charAt(offs-2)) && ch!='\n')) |
|
226 |
0
| break;
|
|
227 |
0
| else if (Character.isWhitespace(chPrev) && !Character.isWhitespace(ch)) break;
|
|
228 |
0
| else if (!Character.isWhitespace(chPrev) && ch == '\n') break;
|
|
229 |
0
| else if (Character.isWhitespace(chPrev) && ch=='\n') {
|
|
230 |
0
| while(Character.isWhitespace(chPrev) && (offs > 0)) {
|
|
231 |
0
| --offs;
|
|
232 |
0
| ch = text.charAt(offs);
|
|
233 |
0
| chPrev = text.charAt(offs - 1);
|
|
234 |
| } |
|
235 |
0
| break;
|
|
236 |
| } |
|
237 |
| } |
|
238 |
0
| if (_select) target.moveCaretPosition(offs);
|
|
239 |
0
| else target.setCaretPosition(offs);
|
|
240 |
| } |
|
241 |
0
| catch(BadLocationException ble) { throw new UnexpectedException(ble); }
|
|
242 |
| } |
|
243 |
| } |
|
244 |
| } |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| static class NextWordAction extends TextAction { |
|
250 |
| private boolean _select; |
|
251 |
| |
|
252 |
1214
| NextWordAction(String nm, boolean b) {
|
|
253 |
1214
| super(nm);
|
|
254 |
1214
| _select = b;
|
|
255 |
| } |
|
256 |
| |
|
257 |
0
| public void actionPerformed(ActionEvent e) {
|
|
258 |
0
| AbstractDJPane target = (AbstractDJPane) getTextComponent(e);
|
|
259 |
0
| if (target != null) {
|
|
260 |
0
| try {
|
|
261 |
0
| int offs = target.getCaretPosition();
|
|
262 |
0
| final int iOffs = offs;
|
|
263 |
0
| final String text = target.getDocument().getText(iOffs,target.getDocument().getLength() - iOffs);
|
|
264 |
0
| final int len = text.length();
|
|
265 |
0
| while((offs-iOffs) < len) {
|
|
266 |
0
| ++offs;
|
|
267 |
0
| if (offs-iOffs == len)
|
|
268 |
0
| break;
|
|
269 |
0
| char ch = text.charAt(offs-iOffs);
|
|
270 |
0
| char chPrev = text.charAt(offs-iOffs - 1);
|
|
271 |
0
| if ((DELIMITERS.indexOf(ch) >= 0) ||
|
|
272 |
| (DELIMITERS.indexOf(chPrev) >= 0) || |
|
273 |
| Character.isWhitespace(chPrev) || |
|
274 |
| ch == '\n') { |
|
275 |
0
| while((offs-iOffs<len) && Character.isWhitespace(ch) && ch != '\n') {
|
|
276 |
0
| if (DELIMITERS.indexOf(chPrev) >= 0)
|
|
277 |
0
| break;
|
|
278 |
0
| ++offs;
|
|
279 |
0
| ch = text.charAt(offs-iOffs);
|
|
280 |
| } |
|
281 |
0
| if (ch == '\n' && Character.isWhitespace(text.charAt(offs - iOffs - 1))) continue;
|
|
282 |
0
| else break;
|
|
283 |
| } |
|
284 |
| |
|
285 |
0
| if (!Character.isWhitespace(chPrev) && Character.isWhitespace(ch)) {
|
|
286 |
0
| int offs0 = offs;
|
|
287 |
0
| while((offs-iOffs)<(len-1) && ch!='\n' && Character.isWhitespace(ch)) {
|
|
288 |
0
| ++offs;
|
|
289 |
0
| ch = text.charAt(offs-iOffs);
|
|
290 |
0
| chPrev = text.charAt(offs-iOffs - 1);
|
|
291 |
| } |
|
292 |
0
| offs = offs0;
|
|
293 |
0
| if (ch=='\n') break;
|
|
294 |
0
| else continue;
|
|
295 |
| } |
|
296 |
| } |
|
297 |
0
| if (_select) target.moveCaretPosition(offs);
|
|
298 |
0
| else target.setCaretPosition(offs);
|
|
299 |
| } |
|
300 |
0
| catch(BadLocationException ble) { throw new UnexpectedException(ble); }
|
|
301 |
| } |
|
302 |
| } |
|
303 |
| } |
|
304 |
| |
|
305 |
| |
|
306 |
| static class SelectWordAction extends TextAction { |
|
307 |
| private Action start; |
|
308 |
| private Action end; |
|
309 |
| |
|
310 |
607
| public SelectWordAction() {
|
|
311 |
607
| super(selectWordAction);
|
|
312 |
607
| start = new BeginWordAction("pigdog", false);
|
|
313 |
607
| end = new EndWordAction("pigdog", true);
|
|
314 |
| } |
|
315 |
0
| public void actionPerformed(ActionEvent e) {
|
|
316 |
0
| start.actionPerformed(e);
|
|
317 |
0
| end.actionPerformed(e);
|
|
318 |
| } |
|
319 |
| } |
|
320 |
| } |