author | yan |
Thu, 05 Dec 2013 18:04:12 +0400 | |
changeset 21982 | fd6e5fe509df |
parent 20458 | f2423fb3fd19 |
child 22260 | c9185e010e03 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing.text; |
|
26 |
||
12418
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
27 |
import sun.awt.SunToolkit; |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
28 |
|
2 | 29 |
import java.io.*; |
30 |
import java.awt.*; |
|
31 |
import java.awt.event.ActionEvent; |
|
32 |
import java.text.*; |
|
33 |
import javax.swing.Action; |
|
34 |
import javax.swing.KeyStroke; |
|
35 |
import javax.swing.SwingConstants; |
|
36 |
import javax.swing.UIManager; |
|
37 |
||
38 |
/** |
|
39 |
* This is the set of things needed by a text component |
|
40 |
* to be a reasonably functioning editor for some <em>type</em> |
|
41 |
* of text document. This implementation provides a default |
|
42 |
* implementation which treats text as plain text and |
|
43 |
* provides a minimal set of actions for a simple editor. |
|
21982 | 44 |
* |
2 | 45 |
* <dl> |
46 |
* <dt><b><font size=+1>Newlines</font></b> |
|
47 |
* <dd> |
|
48 |
* There are two properties which deal with newlines. The |
|
49 |
* system property, <code>line.separator</code>, is defined to be |
|
50 |
* platform-dependent, either "\n", "\r", or "\r\n". There is also |
|
51 |
* a property defined in <code>DefaultEditorKit</code>, called |
|
52 |
* <a href=#EndOfLineStringProperty><code>EndOfLineStringProperty</code></a>, |
|
53 |
* which is defined automatically when a document is loaded, to be |
|
54 |
* the first occurrence of any of the newline characters. |
|
55 |
* When a document is loaded, <code>EndOfLineStringProperty</code> |
|
56 |
* is set appropriately, and when the document is written back out, the |
|
57 |
* <code>EndOfLineStringProperty</code> is used. But while the document |
|
58 |
* is in memory, the "\n" character is used to define a |
|
59 |
* newline, regardless of how the newline is defined when |
|
60 |
* the document is on disk. Therefore, for searching purposes, |
|
61 |
* "\n" should always be used. When a new document is created, |
|
62 |
* and the <code>EndOfLineStringProperty</code> has not been defined, |
|
63 |
* it will use the System property when writing out the |
|
64 |
* document. |
|
65 |
* <p>Note that <code>EndOfLineStringProperty</code> is set |
|
66 |
* on the <code>Document</code> using the <code>get/putProperty</code> |
|
67 |
* methods. Subclasses may override this behavior. |
|
68 |
* |
|
69 |
* </dl> |
|
70 |
* |
|
71 |
* @author Timothy Prinzing |
|
72 |
*/ |
|
73 |
public class DefaultEditorKit extends EditorKit { |
|
74 |
||
75 |
/** |
|
76 |
* default constructor for DefaultEditorKit |
|
77 |
*/ |
|
78 |
public DefaultEditorKit() { |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Gets the MIME type of the data that this |
|
83 |
* kit represents support for. The default |
|
84 |
* is <code>text/plain</code>. |
|
85 |
* |
|
86 |
* @return the type |
|
87 |
*/ |
|
88 |
public String getContentType() { |
|
89 |
return "text/plain"; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Fetches a factory that is suitable for producing |
|
94 |
* views of any models that are produced by this |
|
95 |
* kit. The default is to have the UI produce the |
|
96 |
* factory, so this method has no implementation. |
|
97 |
* |
|
98 |
* @return the view factory |
|
99 |
*/ |
|
100 |
public ViewFactory getViewFactory() { |
|
101 |
return null; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Fetches the set of commands that can be used |
|
106 |
* on a text component that is using a model and |
|
107 |
* view produced by this kit. |
|
108 |
* |
|
109 |
* @return the command list |
|
110 |
*/ |
|
111 |
public Action[] getActions() { |
|
112 |
return defaultActions; |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Fetches a caret that can navigate through views |
|
117 |
* produced by the associated ViewFactory. |
|
118 |
* |
|
119 |
* @return the caret |
|
120 |
*/ |
|
121 |
public Caret createCaret() { |
|
122 |
return null; |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Creates an uninitialized text storage model (PlainDocument) |
|
127 |
* that is appropriate for this type of editor. |
|
128 |
* |
|
129 |
* @return the model |
|
130 |
*/ |
|
131 |
public Document createDefaultDocument() { |
|
132 |
return new PlainDocument(); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Inserts content from the given stream which is expected |
|
137 |
* to be in a format appropriate for this kind of content |
|
138 |
* handler. |
|
139 |
* |
|
140 |
* @param in The stream to read from |
|
141 |
* @param doc The destination for the insertion. |
|
142 |
* @param pos The location in the document to place the |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
12418
diff
changeset
|
143 |
* content >=0. |
2 | 144 |
* @exception IOException on any I/O error |
145 |
* @exception BadLocationException if pos represents an invalid |
|
146 |
* location within the document. |
|
147 |
*/ |
|
148 |
public void read(InputStream in, Document doc, int pos) |
|
149 |
throws IOException, BadLocationException { |
|
150 |
||
151 |
read(new InputStreamReader(in), doc, pos); |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Writes content from a document to the given stream |
|
156 |
* in a format appropriate for this kind of content handler. |
|
157 |
* |
|
158 |
* @param out The stream to write to |
|
159 |
* @param doc The source for the write. |
|
160 |
* @param pos The location in the document to fetch the |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
12418
diff
changeset
|
161 |
* content >=0. |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
12418
diff
changeset
|
162 |
* @param len The amount to write out >=0. |
2 | 163 |
* @exception IOException on any I/O error |
164 |
* @exception BadLocationException if pos represents an invalid |
|
165 |
* location within the document. |
|
166 |
*/ |
|
167 |
public void write(OutputStream out, Document doc, int pos, int len) |
|
168 |
throws IOException, BadLocationException { |
|
169 |
OutputStreamWriter osw = new OutputStreamWriter(out); |
|
170 |
||
171 |
write(osw, doc, pos, len); |
|
172 |
osw.flush(); |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* Gets the input attributes for the pane. This method exists for |
|
177 |
* the benefit of StyledEditorKit so that the read method will |
|
178 |
* pick up the correct attributes to apply to inserted text. |
|
179 |
* This class's implementation simply returns null. |
|
180 |
* |
|
181 |
* @return null |
|
182 |
*/ |
|
183 |
MutableAttributeSet getInputAttributes() { |
|
184 |
return null; |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Inserts content from the given stream, which will be |
|
189 |
* treated as plain text. |
|
190 |
* |
|
191 |
* @param in The stream to read from |
|
192 |
* @param doc The destination for the insertion. |
|
193 |
* @param pos The location in the document to place the |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
12418
diff
changeset
|
194 |
* content >=0. |
2 | 195 |
* @exception IOException on any I/O error |
196 |
* @exception BadLocationException if pos represents an invalid |
|
197 |
* location within the document. |
|
198 |
*/ |
|
199 |
public void read(Reader in, Document doc, int pos) |
|
200 |
throws IOException, BadLocationException { |
|
201 |
||
202 |
char[] buff = new char[4096]; |
|
203 |
int nch; |
|
204 |
boolean lastWasCR = false; |
|
205 |
boolean isCRLF = false; |
|
206 |
boolean isCR = false; |
|
207 |
int last; |
|
208 |
boolean wasEmpty = (doc.getLength() == 0); |
|
209 |
AttributeSet attr = getInputAttributes(); |
|
210 |
||
211 |
// Read in a block at a time, mapping \r\n to \n, as well as single |
|
212 |
// \r's to \n's. If a \r\n is encountered, \r\n will be set as the |
|
213 |
// newline string for the document, if \r is encountered it will |
|
214 |
// be set as the newline character, otherwise the newline property |
|
215 |
// for the document will be removed. |
|
216 |
while ((nch = in.read(buff, 0, buff.length)) != -1) { |
|
217 |
last = 0; |
|
218 |
for(int counter = 0; counter < nch; counter++) { |
|
219 |
switch(buff[counter]) { |
|
220 |
case '\r': |
|
221 |
if (lastWasCR) { |
|
222 |
isCR = true; |
|
223 |
if (counter == 0) { |
|
224 |
doc.insertString(pos, "\n", attr); |
|
225 |
pos++; |
|
226 |
} |
|
227 |
else { |
|
228 |
buff[counter - 1] = '\n'; |
|
229 |
} |
|
230 |
} |
|
231 |
else { |
|
232 |
lastWasCR = true; |
|
233 |
} |
|
234 |
break; |
|
235 |
case '\n': |
|
236 |
if (lastWasCR) { |
|
237 |
if (counter > (last + 1)) { |
|
238 |
doc.insertString(pos, new String(buff, last, |
|
239 |
counter - last - 1), attr); |
|
240 |
pos += (counter - last - 1); |
|
241 |
} |
|
242 |
// else nothing to do, can skip \r, next write will |
|
243 |
// write \n |
|
244 |
lastWasCR = false; |
|
245 |
last = counter; |
|
246 |
isCRLF = true; |
|
247 |
} |
|
248 |
break; |
|
249 |
default: |
|
250 |
if (lastWasCR) { |
|
251 |
isCR = true; |
|
252 |
if (counter == 0) { |
|
253 |
doc.insertString(pos, "\n", attr); |
|
254 |
pos++; |
|
255 |
} |
|
256 |
else { |
|
257 |
buff[counter - 1] = '\n'; |
|
258 |
} |
|
259 |
lastWasCR = false; |
|
260 |
} |
|
261 |
break; |
|
262 |
} |
|
263 |
} |
|
264 |
if (last < nch) { |
|
265 |
if(lastWasCR) { |
|
266 |
if (last < (nch - 1)) { |
|
267 |
doc.insertString(pos, new String(buff, last, |
|
268 |
nch - last - 1), attr); |
|
269 |
pos += (nch - last - 1); |
|
270 |
} |
|
271 |
} |
|
272 |
else { |
|
273 |
doc.insertString(pos, new String(buff, last, |
|
274 |
nch - last), attr); |
|
275 |
pos += (nch - last); |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
if (lastWasCR) { |
|
280 |
doc.insertString(pos, "\n", attr); |
|
281 |
isCR = true; |
|
282 |
} |
|
283 |
if (wasEmpty) { |
|
284 |
if (isCRLF) { |
|
285 |
doc.putProperty(EndOfLineStringProperty, "\r\n"); |
|
286 |
} |
|
287 |
else if (isCR) { |
|
288 |
doc.putProperty(EndOfLineStringProperty, "\r"); |
|
289 |
} |
|
290 |
else { |
|
291 |
doc.putProperty(EndOfLineStringProperty, "\n"); |
|
292 |
} |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* Writes content from a document to the given stream |
|
298 |
* as plain text. |
|
299 |
* |
|
300 |
* @param out The stream to write to |
|
301 |
* @param doc The source for the write. |
|
302 |
* @param pos The location in the document to fetch the |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
12418
diff
changeset
|
303 |
* content from >=0. |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
12418
diff
changeset
|
304 |
* @param len The amount to write out >=0. |
2 | 305 |
* @exception IOException on any I/O error |
306 |
* @exception BadLocationException if pos is not within 0 and |
|
307 |
* the length of the document. |
|
308 |
*/ |
|
309 |
public void write(Writer out, Document doc, int pos, int len) |
|
310 |
throws IOException, BadLocationException { |
|
311 |
||
312 |
if ((pos < 0) || ((pos + len) > doc.getLength())) { |
|
313 |
throw new BadLocationException("DefaultEditorKit.write", pos); |
|
314 |
} |
|
315 |
Segment data = new Segment(); |
|
316 |
int nleft = len; |
|
317 |
int offs = pos; |
|
318 |
Object endOfLineProperty = doc.getProperty(EndOfLineStringProperty); |
|
319 |
if (endOfLineProperty == null) { |
|
320 |
try { |
|
321 |
endOfLineProperty = System.getProperty("line.separator"); |
|
322 |
} catch (SecurityException se) { } |
|
323 |
} |
|
324 |
String endOfLine; |
|
325 |
if (endOfLineProperty instanceof String) { |
|
326 |
endOfLine = (String)endOfLineProperty; |
|
327 |
} |
|
328 |
else { |
|
329 |
endOfLine = null; |
|
330 |
} |
|
331 |
if (endOfLineProperty != null && !endOfLine.equals("\n")) { |
|
332 |
// There is an end of line string that isn't \n, have to iterate |
|
333 |
// through and find all \n's and translate to end of line string. |
|
334 |
while (nleft > 0) { |
|
335 |
int n = Math.min(nleft, 4096); |
|
336 |
doc.getText(offs, n, data); |
|
337 |
int last = data.offset; |
|
338 |
char[] array = data.array; |
|
339 |
int maxCounter = last + data.count; |
|
340 |
for (int counter = last; counter < maxCounter; counter++) { |
|
341 |
if (array[counter] == '\n') { |
|
342 |
if (counter > last) { |
|
343 |
out.write(array, last, counter - last); |
|
344 |
} |
|
345 |
out.write(endOfLine); |
|
346 |
last = counter + 1; |
|
347 |
} |
|
348 |
} |
|
349 |
if (maxCounter > last) { |
|
350 |
out.write(array, last, maxCounter - last); |
|
351 |
} |
|
352 |
offs += n; |
|
353 |
nleft -= n; |
|
354 |
} |
|
355 |
} |
|
356 |
else { |
|
357 |
// Just write out text, will already have \n, no mapping to |
|
358 |
// do. |
|
359 |
while (nleft > 0) { |
|
360 |
int n = Math.min(nleft, 4096); |
|
361 |
doc.getText(offs, n, data); |
|
362 |
out.write(data.array, data.offset, data.count); |
|
363 |
offs += n; |
|
364 |
nleft -= n; |
|
365 |
} |
|
366 |
} |
|
367 |
out.flush(); |
|
368 |
} |
|
369 |
||
370 |
||
371 |
/** |
|
372 |
* When reading a document if a CRLF is encountered a property |
|
373 |
* with this name is added and the value will be "\r\n". |
|
374 |
*/ |
|
375 |
public static final String EndOfLineStringProperty = "__EndOfLine__"; |
|
376 |
||
377 |
// --- names of well-known actions --------------------------- |
|
378 |
||
379 |
/** |
|
380 |
* Name of the action to place content into the associated |
|
381 |
* document. If there is a selection, it is removed before |
|
382 |
* the new content is added. |
|
383 |
* @see #getActions |
|
384 |
*/ |
|
385 |
public static final String insertContentAction = "insert-content"; |
|
386 |
||
387 |
/** |
|
388 |
* Name of the action to place a line/paragraph break into |
|
389 |
* the document. If there is a selection, it is removed before |
|
390 |
* the break is added. |
|
391 |
* @see #getActions |
|
392 |
*/ |
|
393 |
public static final String insertBreakAction = "insert-break"; |
|
394 |
||
395 |
/** |
|
396 |
* Name of the action to place a tab character into |
|
397 |
* the document. If there is a selection, it is removed before |
|
398 |
* the tab is added. |
|
399 |
* @see #getActions |
|
400 |
*/ |
|
401 |
public static final String insertTabAction = "insert-tab"; |
|
402 |
||
403 |
/** |
|
404 |
* Name of the action to delete the character of content that |
|
405 |
* precedes the current caret position. |
|
406 |
* @see #getActions |
|
407 |
*/ |
|
408 |
public static final String deletePrevCharAction = "delete-previous"; |
|
409 |
||
410 |
/** |
|
411 |
* Name of the action to delete the character of content that |
|
412 |
* follows the current caret position. |
|
413 |
* @see #getActions |
|
414 |
*/ |
|
415 |
public static final String deleteNextCharAction = "delete-next"; |
|
416 |
||
417 |
/** |
|
418 |
* Name of the action to delete the word that |
|
419 |
* follows the beginning of the selection. |
|
420 |
* @see #getActions |
|
421 |
* @see JTextComponent#getSelectionStart |
|
422 |
* @since 1.6 |
|
423 |
*/ |
|
424 |
public static final String deleteNextWordAction = "delete-next-word"; |
|
425 |
||
426 |
/** |
|
427 |
* Name of the action to delete the word that |
|
428 |
* precedes the beginning of the selection. |
|
429 |
* @see #getActions |
|
430 |
* @see JTextComponent#getSelectionStart |
|
431 |
* @since 1.6 |
|
432 |
*/ |
|
433 |
public static final String deletePrevWordAction = "delete-previous-word"; |
|
434 |
||
435 |
/** |
|
436 |
* Name of the action to set the editor into read-only |
|
437 |
* mode. |
|
438 |
* @see #getActions |
|
439 |
*/ |
|
440 |
public static final String readOnlyAction = "set-read-only"; |
|
441 |
||
442 |
/** |
|
443 |
* Name of the action to set the editor into writeable |
|
444 |
* mode. |
|
445 |
* @see #getActions |
|
446 |
*/ |
|
447 |
public static final String writableAction = "set-writable"; |
|
448 |
||
449 |
/** |
|
450 |
* Name of the action to cut the selected region |
|
451 |
* and place the contents into the system clipboard. |
|
452 |
* @see JTextComponent#cut |
|
453 |
* @see #getActions |
|
454 |
*/ |
|
455 |
public static final String cutAction = "cut-to-clipboard"; |
|
456 |
||
457 |
/** |
|
458 |
* Name of the action to copy the selected region |
|
459 |
* and place the contents into the system clipboard. |
|
460 |
* @see JTextComponent#copy |
|
461 |
* @see #getActions |
|
462 |
*/ |
|
463 |
public static final String copyAction = "copy-to-clipboard"; |
|
464 |
||
465 |
/** |
|
466 |
* Name of the action to paste the contents of the |
|
467 |
* system clipboard into the selected region, or before the |
|
468 |
* caret if nothing is selected. |
|
469 |
* @see JTextComponent#paste |
|
470 |
* @see #getActions |
|
471 |
*/ |
|
472 |
public static final String pasteAction = "paste-from-clipboard"; |
|
473 |
||
474 |
/** |
|
475 |
* Name of the action to create a beep. |
|
476 |
* @see #getActions |
|
477 |
*/ |
|
478 |
public static final String beepAction = "beep"; |
|
479 |
||
480 |
/** |
|
481 |
* Name of the action to page up vertically. |
|
482 |
* @see #getActions |
|
483 |
*/ |
|
484 |
public static final String pageUpAction = "page-up"; |
|
485 |
||
486 |
/** |
|
487 |
* Name of the action to page down vertically. |
|
488 |
* @see #getActions |
|
489 |
*/ |
|
490 |
public static final String pageDownAction = "page-down"; |
|
491 |
||
492 |
/** |
|
493 |
* Name of the action to page up vertically, and move the |
|
494 |
* selection. |
|
495 |
* @see #getActions |
|
496 |
*/ |
|
497 |
/*public*/ static final String selectionPageUpAction = "selection-page-up"; |
|
498 |
||
499 |
/** |
|
500 |
* Name of the action to page down vertically, and move the |
|
501 |
* selection. |
|
502 |
* @see #getActions |
|
503 |
*/ |
|
504 |
/*public*/ static final String selectionPageDownAction = "selection-page-down"; |
|
505 |
||
506 |
/** |
|
507 |
* Name of the action to page left horizontally, and move the |
|
508 |
* selection. |
|
509 |
* @see #getActions |
|
510 |
*/ |
|
511 |
/*public*/ static final String selectionPageLeftAction = "selection-page-left"; |
|
512 |
||
513 |
/** |
|
514 |
* Name of the action to page right horizontally, and move the |
|
515 |
* selection. |
|
516 |
* @see #getActions |
|
517 |
*/ |
|
518 |
/*public*/ static final String selectionPageRightAction = "selection-page-right"; |
|
519 |
||
520 |
/** |
|
521 |
* Name of the Action for moving the caret |
|
522 |
* logically forward one position. |
|
523 |
* @see #getActions |
|
524 |
*/ |
|
525 |
public static final String forwardAction = "caret-forward"; |
|
526 |
||
527 |
/** |
|
528 |
* Name of the Action for moving the caret |
|
529 |
* logically backward one position. |
|
530 |
* @see #getActions |
|
531 |
*/ |
|
532 |
public static final String backwardAction = "caret-backward"; |
|
533 |
||
534 |
/** |
|
535 |
* Name of the Action for extending the selection |
|
536 |
* by moving the caret logically forward one position. |
|
537 |
* @see #getActions |
|
538 |
*/ |
|
539 |
public static final String selectionForwardAction = "selection-forward"; |
|
540 |
||
541 |
/** |
|
542 |
* Name of the Action for extending the selection |
|
543 |
* by moving the caret logically backward one position. |
|
544 |
* @see #getActions |
|
545 |
*/ |
|
546 |
public static final String selectionBackwardAction = "selection-backward"; |
|
547 |
||
548 |
/** |
|
549 |
* Name of the Action for moving the caret |
|
550 |
* logically upward one position. |
|
551 |
* @see #getActions |
|
552 |
*/ |
|
553 |
public static final String upAction = "caret-up"; |
|
554 |
||
555 |
/** |
|
556 |
* Name of the Action for moving the caret |
|
557 |
* logically downward one position. |
|
558 |
* @see #getActions |
|
559 |
*/ |
|
560 |
public static final String downAction = "caret-down"; |
|
561 |
||
562 |
/** |
|
563 |
* Name of the Action for moving the caret |
|
564 |
* logically upward one position, extending the selection. |
|
565 |
* @see #getActions |
|
566 |
*/ |
|
567 |
public static final String selectionUpAction = "selection-up"; |
|
568 |
||
569 |
/** |
|
570 |
* Name of the Action for moving the caret |
|
571 |
* logically downward one position, extending the selection. |
|
572 |
* @see #getActions |
|
573 |
*/ |
|
574 |
public static final String selectionDownAction = "selection-down"; |
|
575 |
||
576 |
/** |
|
577 |
* Name of the <code>Action</code> for moving the caret |
|
578 |
* to the beginning of a word. |
|
579 |
* @see #getActions |
|
580 |
*/ |
|
581 |
public static final String beginWordAction = "caret-begin-word"; |
|
582 |
||
583 |
/** |
|
584 |
* Name of the Action for moving the caret |
|
585 |
* to the end of a word. |
|
586 |
* @see #getActions |
|
587 |
*/ |
|
588 |
public static final String endWordAction = "caret-end-word"; |
|
589 |
||
590 |
/** |
|
591 |
* Name of the <code>Action</code> for moving the caret |
|
592 |
* to the beginning of a word, extending the selection. |
|
593 |
* @see #getActions |
|
594 |
*/ |
|
595 |
public static final String selectionBeginWordAction = "selection-begin-word"; |
|
596 |
||
597 |
/** |
|
598 |
* Name of the Action for moving the caret |
|
599 |
* to the end of a word, extending the selection. |
|
600 |
* @see #getActions |
|
601 |
*/ |
|
602 |
public static final String selectionEndWordAction = "selection-end-word"; |
|
603 |
||
604 |
/** |
|
605 |
* Name of the <code>Action</code> for moving the caret to the |
|
606 |
* beginning of the previous word. |
|
607 |
* @see #getActions |
|
608 |
*/ |
|
609 |
public static final String previousWordAction = "caret-previous-word"; |
|
610 |
||
611 |
/** |
|
612 |
* Name of the <code>Action</code> for moving the caret to the |
|
613 |
* beginning of the next word. |
|
614 |
* @see #getActions |
|
615 |
*/ |
|
616 |
public static final String nextWordAction = "caret-next-word"; |
|
617 |
||
618 |
/** |
|
619 |
* Name of the <code>Action</code> for moving the selection to the |
|
620 |
* beginning of the previous word, extending the selection. |
|
621 |
* @see #getActions |
|
622 |
*/ |
|
623 |
public static final String selectionPreviousWordAction = "selection-previous-word"; |
|
624 |
||
625 |
/** |
|
626 |
* Name of the <code>Action</code> for moving the selection to the |
|
627 |
* beginning of the next word, extending the selection. |
|
628 |
* @see #getActions |
|
629 |
*/ |
|
630 |
public static final String selectionNextWordAction = "selection-next-word"; |
|
631 |
||
632 |
/** |
|
633 |
* Name of the <code>Action</code> for moving the caret |
|
634 |
* to the beginning of a line. |
|
635 |
* @see #getActions |
|
636 |
*/ |
|
637 |
public static final String beginLineAction = "caret-begin-line"; |
|
638 |
||
639 |
/** |
|
640 |
* Name of the <code>Action</code> for moving the caret |
|
641 |
* to the end of a line. |
|
642 |
* @see #getActions |
|
643 |
*/ |
|
644 |
public static final String endLineAction = "caret-end-line"; |
|
645 |
||
646 |
/** |
|
647 |
* Name of the <code>Action</code> for moving the caret |
|
648 |
* to the beginning of a line, extending the selection. |
|
649 |
* @see #getActions |
|
650 |
*/ |
|
651 |
public static final String selectionBeginLineAction = "selection-begin-line"; |
|
652 |
||
653 |
/** |
|
654 |
* Name of the <code>Action</code> for moving the caret |
|
655 |
* to the end of a line, extending the selection. |
|
656 |
* @see #getActions |
|
657 |
*/ |
|
658 |
public static final String selectionEndLineAction = "selection-end-line"; |
|
659 |
||
660 |
/** |
|
661 |
* Name of the <code>Action</code> for moving the caret |
|
662 |
* to the beginning of a paragraph. |
|
663 |
* @see #getActions |
|
664 |
*/ |
|
665 |
public static final String beginParagraphAction = "caret-begin-paragraph"; |
|
666 |
||
667 |
/** |
|
668 |
* Name of the <code>Action</code> for moving the caret |
|
669 |
* to the end of a paragraph. |
|
670 |
* @see #getActions |
|
671 |
*/ |
|
672 |
public static final String endParagraphAction = "caret-end-paragraph"; |
|
673 |
||
674 |
/** |
|
675 |
* Name of the <code>Action</code> for moving the caret |
|
676 |
* to the beginning of a paragraph, extending the selection. |
|
677 |
* @see #getActions |
|
678 |
*/ |
|
679 |
public static final String selectionBeginParagraphAction = "selection-begin-paragraph"; |
|
680 |
||
681 |
/** |
|
682 |
* Name of the <code>Action</code> for moving the caret |
|
683 |
* to the end of a paragraph, extending the selection. |
|
684 |
* @see #getActions |
|
685 |
*/ |
|
686 |
public static final String selectionEndParagraphAction = "selection-end-paragraph"; |
|
687 |
||
688 |
/** |
|
689 |
* Name of the <code>Action</code> for moving the caret |
|
690 |
* to the beginning of the document. |
|
691 |
* @see #getActions |
|
692 |
*/ |
|
693 |
public static final String beginAction = "caret-begin"; |
|
694 |
||
695 |
/** |
|
696 |
* Name of the <code>Action</code> for moving the caret |
|
697 |
* to the end of the document. |
|
698 |
* @see #getActions |
|
699 |
*/ |
|
700 |
public static final String endAction = "caret-end"; |
|
701 |
||
702 |
/** |
|
703 |
* Name of the <code>Action</code> for moving the caret |
|
704 |
* to the beginning of the document. |
|
705 |
* @see #getActions |
|
706 |
*/ |
|
707 |
public static final String selectionBeginAction = "selection-begin"; |
|
708 |
||
709 |
/** |
|
710 |
* Name of the Action for moving the caret |
|
711 |
* to the end of the document. |
|
712 |
* @see #getActions |
|
713 |
*/ |
|
714 |
public static final String selectionEndAction = "selection-end"; |
|
715 |
||
716 |
/** |
|
717 |
* Name of the Action for selecting a word around the caret. |
|
718 |
* @see #getActions |
|
719 |
*/ |
|
720 |
public static final String selectWordAction = "select-word"; |
|
721 |
||
722 |
/** |
|
723 |
* Name of the Action for selecting a line around the caret. |
|
724 |
* @see #getActions |
|
725 |
*/ |
|
726 |
public static final String selectLineAction = "select-line"; |
|
727 |
||
728 |
/** |
|
729 |
* Name of the Action for selecting a paragraph around the caret. |
|
730 |
* @see #getActions |
|
731 |
*/ |
|
732 |
public static final String selectParagraphAction = "select-paragraph"; |
|
733 |
||
734 |
/** |
|
735 |
* Name of the Action for selecting the entire document |
|
736 |
* @see #getActions |
|
737 |
*/ |
|
738 |
public static final String selectAllAction = "select-all"; |
|
739 |
||
740 |
/** |
|
741 |
* Name of the Action for removing selection |
|
742 |
* @see #getActions |
|
743 |
*/ |
|
744 |
/*public*/ static final String unselectAction = "unselect"; |
|
745 |
||
746 |
/** |
|
747 |
* Name of the Action for toggling the component's orientation. |
|
748 |
* @see #getActions |
|
749 |
*/ |
|
750 |
/*public*/ static final String toggleComponentOrientationAction |
|
751 |
= "toggle-componentOrientation"; |
|
752 |
||
753 |
/** |
|
754 |
* Name of the action that is executed by default if |
|
755 |
* a <em>key typed event</em> is received and there |
|
756 |
* is no keymap entry. |
|
757 |
* @see #getActions |
|
758 |
*/ |
|
759 |
public static final String defaultKeyTypedAction = "default-typed"; |
|
760 |
||
761 |
// --- Action implementations --------------------------------- |
|
762 |
||
763 |
private static final Action[] defaultActions = { |
|
764 |
new InsertContentAction(), new DeletePrevCharAction(), |
|
765 |
new DeleteNextCharAction(), new ReadOnlyAction(), |
|
766 |
new DeleteWordAction(deletePrevWordAction), |
|
767 |
new DeleteWordAction(deleteNextWordAction), |
|
768 |
new WritableAction(), new CutAction(), |
|
769 |
new CopyAction(), new PasteAction(), |
|
770 |
new VerticalPageAction(pageUpAction, -1, false), |
|
771 |
new VerticalPageAction(pageDownAction, 1, false), |
|
772 |
new VerticalPageAction(selectionPageUpAction, -1, true), |
|
773 |
new VerticalPageAction(selectionPageDownAction, 1, true), |
|
774 |
new PageAction(selectionPageLeftAction, true, true), |
|
775 |
new PageAction(selectionPageRightAction, false, true), |
|
776 |
new InsertBreakAction(), new BeepAction(), |
|
777 |
new NextVisualPositionAction(forwardAction, false, |
|
778 |
SwingConstants.EAST), |
|
779 |
new NextVisualPositionAction(backwardAction, false, |
|
780 |
SwingConstants.WEST), |
|
781 |
new NextVisualPositionAction(selectionForwardAction, true, |
|
782 |
SwingConstants.EAST), |
|
783 |
new NextVisualPositionAction(selectionBackwardAction, true, |
|
784 |
SwingConstants.WEST), |
|
785 |
new NextVisualPositionAction(upAction, false, |
|
786 |
SwingConstants.NORTH), |
|
787 |
new NextVisualPositionAction(downAction, false, |
|
788 |
SwingConstants.SOUTH), |
|
789 |
new NextVisualPositionAction(selectionUpAction, true, |
|
790 |
SwingConstants.NORTH), |
|
791 |
new NextVisualPositionAction(selectionDownAction, true, |
|
792 |
SwingConstants.SOUTH), |
|
793 |
new BeginWordAction(beginWordAction, false), |
|
794 |
new EndWordAction(endWordAction, false), |
|
795 |
new BeginWordAction(selectionBeginWordAction, true), |
|
796 |
new EndWordAction(selectionEndWordAction, true), |
|
797 |
new PreviousWordAction(previousWordAction, false), |
|
798 |
new NextWordAction(nextWordAction, false), |
|
799 |
new PreviousWordAction(selectionPreviousWordAction, true), |
|
800 |
new NextWordAction(selectionNextWordAction, true), |
|
801 |
new BeginLineAction(beginLineAction, false), |
|
802 |
new EndLineAction(endLineAction, false), |
|
803 |
new BeginLineAction(selectionBeginLineAction, true), |
|
804 |
new EndLineAction(selectionEndLineAction, true), |
|
805 |
new BeginParagraphAction(beginParagraphAction, false), |
|
806 |
new EndParagraphAction(endParagraphAction, false), |
|
807 |
new BeginParagraphAction(selectionBeginParagraphAction, true), |
|
808 |
new EndParagraphAction(selectionEndParagraphAction, true), |
|
809 |
new BeginAction(beginAction, false), |
|
810 |
new EndAction(endAction, false), |
|
811 |
new BeginAction(selectionBeginAction, true), |
|
812 |
new EndAction(selectionEndAction, true), |
|
813 |
new DefaultKeyTypedAction(), new InsertTabAction(), |
|
814 |
new SelectWordAction(), new SelectLineAction(), |
|
815 |
new SelectParagraphAction(), new SelectAllAction(), |
|
816 |
new UnselectAction(), new ToggleComponentOrientationAction(), |
|
817 |
new DumpModelAction() |
|
818 |
}; |
|
819 |
||
820 |
/** |
|
821 |
* The action that is executed by default if |
|
822 |
* a <em>key typed event</em> is received and there |
|
823 |
* is no keymap entry. There is a variation across |
|
824 |
* different VM's in what gets sent as a <em>key typed</em> |
|
825 |
* event, and this action tries to filter out the undesired |
|
826 |
* events. This filters the control characters and those |
|
827 |
* with the ALT modifier. It allows Control-Alt sequences |
|
828 |
* through as these form legitimate unicode characters on |
|
829 |
* some PC keyboards. |
|
830 |
* <p> |
|
831 |
* If the event doesn't get filtered, it will try to insert |
|
832 |
* content into the text editor. The content is fetched |
|
833 |
* from the command string of the ActionEvent. The text |
|
834 |
* entry is done through the <code>replaceSelection</code> |
|
835 |
* method on the target text component. This is the |
|
836 |
* action that will be fired for most text entry tasks. |
|
837 |
* <p> |
|
838 |
* <strong>Warning:</strong> |
|
839 |
* Serialized objects of this class will not be compatible with |
|
840 |
* future Swing releases. The current serialization support is |
|
841 |
* appropriate for short term storage or RMI between applications running |
|
842 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 843 |
* of all JavaBeans™ |
2 | 844 |
* has been added to the <code>java.beans</code> package. |
845 |
* Please see {@link java.beans.XMLEncoder}. |
|
846 |
* |
|
847 |
* @see DefaultEditorKit#defaultKeyTypedAction |
|
848 |
* @see DefaultEditorKit#getActions |
|
849 |
* @see Keymap#setDefaultAction |
|
850 |
* @see Keymap#getDefaultAction |
|
851 |
*/ |
|
852 |
public static class DefaultKeyTypedAction extends TextAction { |
|
853 |
||
854 |
/** |
|
855 |
* Creates this object with the appropriate identifier. |
|
856 |
*/ |
|
857 |
public DefaultKeyTypedAction() { |
|
858 |
super(defaultKeyTypedAction); |
|
859 |
} |
|
860 |
||
861 |
/** |
|
862 |
* The operation to perform when this action is triggered. |
|
863 |
* |
|
864 |
* @param e the action event |
|
865 |
*/ |
|
866 |
public void actionPerformed(ActionEvent e) { |
|
867 |
JTextComponent target = getTextComponent(e); |
|
868 |
if ((target != null) && (e != null)) { |
|
869 |
if ((! target.isEditable()) || (! target.isEnabled())) { |
|
870 |
return; |
|
871 |
} |
|
872 |
String content = e.getActionCommand(); |
|
873 |
int mod = e.getModifiers(); |
|
12418
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
874 |
if ((content != null) && (content.length() > 0)) { |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
875 |
boolean isPrintableMask = true; |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
876 |
Toolkit tk = Toolkit.getDefaultToolkit(); |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
877 |
if (tk instanceof SunToolkit) { |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
878 |
isPrintableMask = ((SunToolkit)tk).isPrintableCharacterModifiersMask(mod); |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
879 |
} |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
880 |
|
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
881 |
if (isPrintableMask) { |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
882 |
char c = content.charAt(0); |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
883 |
if ((c >= 0x20) && (c != 0x7F)) { |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
884 |
target.replaceSelection(content); |
f749f1ed92ca
7124286: [macosx] Option modifier should work like AltGr as in Apple jdk
leonidr
parents:
7668
diff
changeset
|
885 |
} |
2 | 886 |
} |
887 |
} |
|
888 |
} |
|
889 |
} |
|
890 |
} |
|
891 |
||
892 |
/** |
|
893 |
* Places content into the associated document. |
|
894 |
* If there is a selection, it is removed before |
|
895 |
* the new content is added. |
|
896 |
* <p> |
|
897 |
* <strong>Warning:</strong> |
|
898 |
* Serialized objects of this class will not be compatible with |
|
899 |
* future Swing releases. The current serialization support is |
|
900 |
* appropriate for short term storage or RMI between applications running |
|
901 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 902 |
* of all JavaBeans™ |
2 | 903 |
* has been added to the <code>java.beans</code> package. |
904 |
* Please see {@link java.beans.XMLEncoder}. |
|
905 |
* |
|
906 |
* @see DefaultEditorKit#insertContentAction |
|
907 |
* @see DefaultEditorKit#getActions |
|
908 |
*/ |
|
909 |
public static class InsertContentAction extends TextAction { |
|
910 |
||
911 |
/** |
|
912 |
* Creates this object with the appropriate identifier. |
|
913 |
*/ |
|
914 |
public InsertContentAction() { |
|
915 |
super(insertContentAction); |
|
916 |
} |
|
917 |
||
918 |
/** |
|
919 |
* The operation to perform when this action is triggered. |
|
920 |
* |
|
921 |
* @param e the action event |
|
922 |
*/ |
|
923 |
public void actionPerformed(ActionEvent e) { |
|
924 |
JTextComponent target = getTextComponent(e); |
|
925 |
if ((target != null) && (e != null)) { |
|
926 |
if ((! target.isEditable()) || (! target.isEnabled())) { |
|
927 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
928 |
return; |
|
929 |
} |
|
930 |
String content = e.getActionCommand(); |
|
931 |
if (content != null) { |
|
932 |
target.replaceSelection(content); |
|
933 |
} else { |
|
934 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
935 |
} |
|
936 |
} |
|
937 |
} |
|
938 |
} |
|
939 |
||
940 |
/** |
|
941 |
* Places a line/paragraph break into the document. |
|
942 |
* If there is a selection, it is removed before |
|
943 |
* the break is added. |
|
944 |
* <p> |
|
945 |
* <strong>Warning:</strong> |
|
946 |
* Serialized objects of this class will not be compatible with |
|
947 |
* future Swing releases. The current serialization support is |
|
948 |
* appropriate for short term storage or RMI between applications running |
|
949 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 950 |
* of all JavaBeans™ |
2 | 951 |
* has been added to the <code>java.beans</code> package. |
952 |
* Please see {@link java.beans.XMLEncoder}. |
|
953 |
* |
|
954 |
* @see DefaultEditorKit#insertBreakAction |
|
955 |
* @see DefaultEditorKit#getActions |
|
956 |
*/ |
|
957 |
public static class InsertBreakAction extends TextAction { |
|
958 |
||
959 |
/** |
|
960 |
* Creates this object with the appropriate identifier. |
|
961 |
*/ |
|
962 |
public InsertBreakAction() { |
|
963 |
super(insertBreakAction); |
|
964 |
} |
|
965 |
||
966 |
/** |
|
967 |
* The operation to perform when this action is triggered. |
|
968 |
* |
|
969 |
* @param e the action event |
|
970 |
*/ |
|
971 |
public void actionPerformed(ActionEvent e) { |
|
972 |
JTextComponent target = getTextComponent(e); |
|
973 |
if (target != null) { |
|
974 |
if ((! target.isEditable()) || (! target.isEnabled())) { |
|
975 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
976 |
return; |
|
977 |
} |
|
978 |
target.replaceSelection("\n"); |
|
979 |
} |
|
980 |
} |
|
981 |
} |
|
982 |
||
983 |
/** |
|
984 |
* Places a tab character into the document. If there |
|
985 |
* is a selection, it is removed before the tab is added. |
|
986 |
* <p> |
|
987 |
* <strong>Warning:</strong> |
|
988 |
* Serialized objects of this class will not be compatible with |
|
989 |
* future Swing releases. The current serialization support is |
|
990 |
* appropriate for short term storage or RMI between applications running |
|
991 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 992 |
* of all JavaBeans™ |
2 | 993 |
* has been added to the <code>java.beans</code> package. |
994 |
* Please see {@link java.beans.XMLEncoder}. |
|
995 |
* |
|
996 |
* @see DefaultEditorKit#insertTabAction |
|
997 |
* @see DefaultEditorKit#getActions |
|
998 |
*/ |
|
999 |
public static class InsertTabAction extends TextAction { |
|
1000 |
||
1001 |
/** |
|
1002 |
* Creates this object with the appropriate identifier. |
|
1003 |
*/ |
|
1004 |
public InsertTabAction() { |
|
1005 |
super(insertTabAction); |
|
1006 |
} |
|
1007 |
||
1008 |
/** |
|
1009 |
* The operation to perform when this action is triggered. |
|
1010 |
* |
|
1011 |
* @param e the action event |
|
1012 |
*/ |
|
1013 |
public void actionPerformed(ActionEvent e) { |
|
1014 |
JTextComponent target = getTextComponent(e); |
|
1015 |
if (target != null) { |
|
1016 |
if ((! target.isEditable()) || (! target.isEnabled())) { |
|
1017 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1018 |
return; |
|
1019 |
} |
|
1020 |
target.replaceSelection("\t"); |
|
1021 |
} |
|
1022 |
} |
|
1023 |
} |
|
1024 |
||
1025 |
/* |
|
1026 |
* Deletes the character of content that precedes the |
|
1027 |
* current caret position. |
|
1028 |
* @see DefaultEditorKit#deletePrevCharAction |
|
1029 |
* @see DefaultEditorKit#getActions |
|
1030 |
*/ |
|
1031 |
static class DeletePrevCharAction extends TextAction { |
|
1032 |
||
1033 |
/** |
|
1034 |
* Creates this object with the appropriate identifier. |
|
1035 |
*/ |
|
1036 |
DeletePrevCharAction() { |
|
1037 |
super(deletePrevCharAction); |
|
1038 |
} |
|
1039 |
||
1040 |
/** |
|
1041 |
* The operation to perform when this action is triggered. |
|
1042 |
* |
|
1043 |
* @param e the action event |
|
1044 |
*/ |
|
1045 |
public void actionPerformed(ActionEvent e) { |
|
1046 |
JTextComponent target = getTextComponent(e); |
|
1047 |
boolean beep = true; |
|
1048 |
if ((target != null) && (target.isEditable())) { |
|
1049 |
try { |
|
1050 |
Document doc = target.getDocument(); |
|
1051 |
Caret caret = target.getCaret(); |
|
1052 |
int dot = caret.getDot(); |
|
1053 |
int mark = caret.getMark(); |
|
1054 |
if (dot != mark) { |
|
1055 |
doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); |
|
1056 |
beep = false; |
|
1057 |
} else if (dot > 0) { |
|
1058 |
int delChars = 1; |
|
1059 |
||
1060 |
if (dot > 1) { |
|
1061 |
String dotChars = doc.getText(dot - 2, 2); |
|
1062 |
char c0 = dotChars.charAt(0); |
|
1063 |
char c1 = dotChars.charAt(1); |
|
1064 |
||
1065 |
if (c0 >= '\uD800' && c0 <= '\uDBFF' && |
|
1066 |
c1 >= '\uDC00' && c1 <= '\uDFFF') { |
|
1067 |
delChars = 2; |
|
1068 |
} |
|
1069 |
} |
|
1070 |
||
1071 |
doc.remove(dot - delChars, delChars); |
|
1072 |
beep = false; |
|
1073 |
} |
|
1074 |
} catch (BadLocationException bl) { |
|
1075 |
} |
|
1076 |
} |
|
1077 |
if (beep) { |
|
1078 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1079 |
} |
|
1080 |
} |
|
1081 |
} |
|
1082 |
||
1083 |
/* |
|
1084 |
* Deletes the character of content that follows the |
|
1085 |
* current caret position. |
|
1086 |
* @see DefaultEditorKit#deleteNextCharAction |
|
1087 |
* @see DefaultEditorKit#getActions |
|
1088 |
*/ |
|
1089 |
static class DeleteNextCharAction extends TextAction { |
|
1090 |
||
1091 |
/* Create this object with the appropriate identifier. */ |
|
1092 |
DeleteNextCharAction() { |
|
1093 |
super(deleteNextCharAction); |
|
1094 |
} |
|
1095 |
||
1096 |
/** The operation to perform when this action is triggered. */ |
|
1097 |
public void actionPerformed(ActionEvent e) { |
|
1098 |
JTextComponent target = getTextComponent(e); |
|
1099 |
boolean beep = true; |
|
1100 |
if ((target != null) && (target.isEditable())) { |
|
1101 |
try { |
|
1102 |
Document doc = target.getDocument(); |
|
1103 |
Caret caret = target.getCaret(); |
|
1104 |
int dot = caret.getDot(); |
|
1105 |
int mark = caret.getMark(); |
|
1106 |
if (dot != mark) { |
|
1107 |
doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); |
|
1108 |
beep = false; |
|
1109 |
} else if (dot < doc.getLength()) { |
|
1110 |
int delChars = 1; |
|
1111 |
||
1112 |
if (dot < doc.getLength() - 1) { |
|
1113 |
String dotChars = doc.getText(dot, 2); |
|
1114 |
char c0 = dotChars.charAt(0); |
|
1115 |
char c1 = dotChars.charAt(1); |
|
1116 |
||
1117 |
if (c0 >= '\uD800' && c0 <= '\uDBFF' && |
|
1118 |
c1 >= '\uDC00' && c1 <= '\uDFFF') { |
|
1119 |
delChars = 2; |
|
1120 |
} |
|
1121 |
} |
|
1122 |
||
1123 |
doc.remove(dot, delChars); |
|
1124 |
beep = false; |
|
1125 |
} |
|
1126 |
} catch (BadLocationException bl) { |
|
1127 |
} |
|
1128 |
} |
|
1129 |
if (beep) { |
|
1130 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1131 |
} |
|
1132 |
} |
|
1133 |
} |
|
1134 |
||
1135 |
||
1136 |
/* |
|
1137 |
* Deletes the word that precedes/follows the beginning of the selection. |
|
1138 |
* @see DefaultEditorKit#getActions |
|
1139 |
*/ |
|
1140 |
static class DeleteWordAction extends TextAction { |
|
1141 |
DeleteWordAction(String name) { |
|
1142 |
super(name); |
|
1143 |
assert (name == deletePrevWordAction) |
|
1144 |
|| (name == deleteNextWordAction); |
|
1145 |
} |
|
1146 |
/** |
|
1147 |
* The operation to perform when this action is triggered. |
|
1148 |
* |
|
1149 |
* @param e the action event |
|
1150 |
*/ |
|
1151 |
public void actionPerformed(ActionEvent e) { |
|
1152 |
final JTextComponent target = getTextComponent(e); |
|
1153 |
if ((target != null) && (e != null)) { |
|
1154 |
if ((! target.isEditable()) || (! target.isEnabled())) { |
|
1155 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1156 |
return; |
|
1157 |
} |
|
1158 |
boolean beep = true; |
|
1159 |
try { |
|
1160 |
final int start = target.getSelectionStart(); |
|
1161 |
final Element line = |
|
1162 |
Utilities.getParagraphElement(target, start); |
|
1163 |
int end; |
|
1164 |
if (deleteNextWordAction == getValue(Action.NAME)) { |
|
1165 |
end = Utilities. |
|
1166 |
getNextWordInParagraph(target, line, start, false); |
|
1167 |
if (end == java.text.BreakIterator.DONE) { |
|
1168 |
//last word in the paragraph |
|
1169 |
final int endOfLine = line.getEndOffset(); |
|
1170 |
if (start == endOfLine - 1) { |
|
1171 |
//for last position remove last \n |
|
1172 |
end = endOfLine; |
|
1173 |
} else { |
|
1174 |
//remove to the end of the paragraph |
|
1175 |
end = endOfLine - 1; |
|
1176 |
} |
|
1177 |
} |
|
1178 |
} else { |
|
1179 |
end = Utilities. |
|
1180 |
getPrevWordInParagraph(target, line, start); |
|
1181 |
if (end == java.text.BreakIterator.DONE) { |
|
1182 |
//there is no previous word in the paragraph |
|
1183 |
final int startOfLine = line.getStartOffset(); |
|
1184 |
if (start == startOfLine) { |
|
1185 |
//for first position remove previous \n |
|
1186 |
end = startOfLine - 1; |
|
1187 |
} else { |
|
1188 |
//remove to the start of the paragraph |
|
1189 |
end = startOfLine; |
|
1190 |
} |
|
1191 |
} |
|
1192 |
} |
|
1193 |
int offs = Math.min(start, end); |
|
1194 |
int len = Math.abs(end - start); |
|
1195 |
if (offs >= 0) { |
|
1196 |
target.getDocument().remove(offs, len); |
|
1197 |
beep = false; |
|
1198 |
} |
|
1199 |
} catch (BadLocationException ignore) { |
|
1200 |
} |
|
1201 |
if (beep) { |
|
1202 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1203 |
} |
|
1204 |
} |
|
1205 |
} |
|
1206 |
} |
|
1207 |
||
1208 |
||
1209 |
/* |
|
1210 |
* Sets the editor into read-only mode. |
|
1211 |
* @see DefaultEditorKit#readOnlyAction |
|
1212 |
* @see DefaultEditorKit#getActions |
|
1213 |
*/ |
|
1214 |
static class ReadOnlyAction extends TextAction { |
|
1215 |
||
1216 |
/* Create this object with the appropriate identifier. */ |
|
1217 |
ReadOnlyAction() { |
|
1218 |
super(readOnlyAction); |
|
1219 |
} |
|
1220 |
||
1221 |
/** |
|
1222 |
* The operation to perform when this action is triggered. |
|
1223 |
* |
|
1224 |
* @param e the action event |
|
1225 |
*/ |
|
1226 |
public void actionPerformed(ActionEvent e) { |
|
1227 |
JTextComponent target = getTextComponent(e); |
|
1228 |
if (target != null) { |
|
1229 |
target.setEditable(false); |
|
1230 |
} |
|
1231 |
} |
|
1232 |
} |
|
1233 |
||
1234 |
/* |
|
1235 |
* Sets the editor into writeable mode. |
|
1236 |
* @see DefaultEditorKit#writableAction |
|
1237 |
* @see DefaultEditorKit#getActions |
|
1238 |
*/ |
|
1239 |
static class WritableAction extends TextAction { |
|
1240 |
||
1241 |
/* Create this object with the appropriate identifier. */ |
|
1242 |
WritableAction() { |
|
1243 |
super(writableAction); |
|
1244 |
} |
|
1245 |
||
1246 |
/** |
|
1247 |
* The operation to perform when this action is triggered. |
|
1248 |
* |
|
1249 |
* @param e the action event |
|
1250 |
*/ |
|
1251 |
public void actionPerformed(ActionEvent e) { |
|
1252 |
JTextComponent target = getTextComponent(e); |
|
1253 |
if (target != null) { |
|
1254 |
target.setEditable(true); |
|
1255 |
} |
|
1256 |
} |
|
1257 |
} |
|
1258 |
||
1259 |
/** |
|
1260 |
* Cuts the selected region and place its contents |
|
1261 |
* into the system clipboard. |
|
1262 |
* <p> |
|
1263 |
* <strong>Warning:</strong> |
|
1264 |
* Serialized objects of this class will not be compatible with |
|
1265 |
* future Swing releases. The current serialization support is |
|
1266 |
* appropriate for short term storage or RMI between applications running |
|
1267 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 1268 |
* of all JavaBeans™ |
2 | 1269 |
* has been added to the <code>java.beans</code> package. |
1270 |
* Please see {@link java.beans.XMLEncoder}. |
|
1271 |
* |
|
1272 |
* @see DefaultEditorKit#cutAction |
|
1273 |
* @see DefaultEditorKit#getActions |
|
1274 |
*/ |
|
1275 |
public static class CutAction extends TextAction { |
|
1276 |
||
1277 |
/** Create this object with the appropriate identifier. */ |
|
1278 |
public CutAction() { |
|
1279 |
super(cutAction); |
|
1280 |
} |
|
1281 |
||
1282 |
/** |
|
1283 |
* The operation to perform when this action is triggered. |
|
1284 |
* |
|
1285 |
* @param e the action event |
|
1286 |
*/ |
|
1287 |
public void actionPerformed(ActionEvent e) { |
|
1288 |
JTextComponent target = getTextComponent(e); |
|
1289 |
if (target != null) { |
|
1290 |
target.cut(); |
|
1291 |
} |
|
1292 |
} |
|
1293 |
} |
|
1294 |
||
1295 |
/** |
|
1296 |
* Copies the selected region and place its contents |
|
1297 |
* into the system clipboard. |
|
1298 |
* <p> |
|
1299 |
* <strong>Warning:</strong> |
|
1300 |
* Serialized objects of this class will not be compatible with |
|
1301 |
* future Swing releases. The current serialization support is |
|
1302 |
* appropriate for short term storage or RMI between applications running |
|
1303 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 1304 |
* of all JavaBeans™ |
2 | 1305 |
* has been added to the <code>java.beans</code> package. |
1306 |
* Please see {@link java.beans.XMLEncoder}. |
|
1307 |
* |
|
1308 |
* @see DefaultEditorKit#copyAction |
|
1309 |
* @see DefaultEditorKit#getActions |
|
1310 |
*/ |
|
1311 |
public static class CopyAction extends TextAction { |
|
1312 |
||
1313 |
/** Create this object with the appropriate identifier. */ |
|
1314 |
public CopyAction() { |
|
1315 |
super(copyAction); |
|
1316 |
} |
|
1317 |
||
1318 |
/** |
|
1319 |
* The operation to perform when this action is triggered. |
|
1320 |
* |
|
1321 |
* @param e the action event |
|
1322 |
*/ |
|
1323 |
public void actionPerformed(ActionEvent e) { |
|
1324 |
JTextComponent target = getTextComponent(e); |
|
1325 |
if (target != null) { |
|
1326 |
target.copy(); |
|
1327 |
} |
|
1328 |
} |
|
1329 |
} |
|
1330 |
||
1331 |
/** |
|
1332 |
* Pastes the contents of the system clipboard into the |
|
1333 |
* selected region, or before the caret if nothing is |
|
1334 |
* selected. |
|
1335 |
* <p> |
|
1336 |
* <strong>Warning:</strong> |
|
1337 |
* Serialized objects of this class will not be compatible with |
|
1338 |
* future Swing releases. The current serialization support is |
|
1339 |
* appropriate for short term storage or RMI between applications running |
|
1340 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 1341 |
* of all JavaBeans™ |
2 | 1342 |
* has been added to the <code>java.beans</code> package. |
1343 |
* Please see {@link java.beans.XMLEncoder}. |
|
1344 |
* |
|
1345 |
* @see DefaultEditorKit#pasteAction |
|
1346 |
* @see DefaultEditorKit#getActions |
|
1347 |
*/ |
|
1348 |
public static class PasteAction extends TextAction { |
|
1349 |
||
1350 |
/** Create this object with the appropriate identifier. */ |
|
1351 |
public PasteAction() { |
|
1352 |
super(pasteAction); |
|
1353 |
} |
|
1354 |
||
1355 |
/** |
|
1356 |
* The operation to perform when this action is triggered. |
|
1357 |
* |
|
1358 |
* @param e the action event |
|
1359 |
*/ |
|
1360 |
public void actionPerformed(ActionEvent e) { |
|
1361 |
JTextComponent target = getTextComponent(e); |
|
1362 |
if (target != null) { |
|
1363 |
target.paste(); |
|
1364 |
} |
|
1365 |
} |
|
1366 |
} |
|
1367 |
||
1368 |
/** |
|
1369 |
* Creates a beep. |
|
1370 |
* <p> |
|
1371 |
* <strong>Warning:</strong> |
|
1372 |
* Serialized objects of this class will not be compatible with |
|
1373 |
* future Swing releases. The current serialization support is |
|
1374 |
* appropriate for short term storage or RMI between applications running |
|
1375 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 1376 |
* of all JavaBeans™ |
2 | 1377 |
* has been added to the <code>java.beans</code> package. |
1378 |
* Please see {@link java.beans.XMLEncoder}. |
|
1379 |
* |
|
1380 |
* @see DefaultEditorKit#beepAction |
|
1381 |
* @see DefaultEditorKit#getActions |
|
1382 |
*/ |
|
1383 |
public static class BeepAction extends TextAction { |
|
1384 |
||
1385 |
/** Create this object with the appropriate identifier. */ |
|
1386 |
public BeepAction() { |
|
1387 |
super(beepAction); |
|
1388 |
} |
|
1389 |
||
1390 |
/** |
|
1391 |
* The operation to perform when this action is triggered. |
|
1392 |
* |
|
1393 |
* @param e the action event |
|
1394 |
*/ |
|
1395 |
public void actionPerformed(ActionEvent e) { |
|
1396 |
JTextComponent target = getTextComponent(e); |
|
1397 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1398 |
} |
|
1399 |
} |
|
1400 |
||
1401 |
/** |
|
1402 |
* Scrolls up/down vertically. The select version of this action extends |
|
1403 |
* the selection, instead of simply moving the caret. |
|
1404 |
* |
|
1405 |
* @see DefaultEditorKit#pageUpAction |
|
1406 |
* @see DefaultEditorKit#pageDownAction |
|
1407 |
* @see DefaultEditorKit#getActions |
|
1408 |
*/ |
|
1409 |
static class VerticalPageAction extends TextAction { |
|
1410 |
||
1411 |
/** Create this object with the appropriate identifier. */ |
|
1412 |
public VerticalPageAction(String nm, int direction, boolean select) { |
|
1413 |
super(nm); |
|
1414 |
this.select = select; |
|
1415 |
this.direction = direction; |
|
1416 |
} |
|
1417 |
||
1418 |
/** The operation to perform when this action is triggered. */ |
|
1419 |
public void actionPerformed(ActionEvent e) { |
|
1420 |
JTextComponent target = getTextComponent(e); |
|
1421 |
if (target != null) { |
|
1422 |
Rectangle visible = target.getVisibleRect(); |
|
1423 |
Rectangle newVis = new Rectangle(visible); |
|
1424 |
int selectedIndex = target.getCaretPosition(); |
|
1425 |
int scrollAmount = direction * |
|
1426 |
target.getScrollableBlockIncrement( |
|
1427 |
visible, SwingConstants.VERTICAL, direction); |
|
1428 |
int initialY = visible.y; |
|
1429 |
Caret caret = target.getCaret(); |
|
1430 |
Point magicPosition = caret.getMagicCaretPosition(); |
|
1431 |
||
1432 |
if (selectedIndex != -1) { |
|
1433 |
try { |
|
1434 |
Rectangle dotBounds = target.modelToView( |
|
1435 |
selectedIndex); |
|
1436 |
int x = (magicPosition != null) ? magicPosition.x : |
|
1437 |
dotBounds.x; |
|
1438 |
int h = dotBounds.height; |
|
1439 |
if (h > 0) { |
|
1440 |
// We want to scroll by a multiple of caret height, |
|
1441 |
// rounding towards lower integer |
|
1442 |
scrollAmount = scrollAmount / h * h; |
|
1443 |
} |
|
1444 |
newVis.y = constrainY(target, |
|
1445 |
initialY + scrollAmount, visible.height); |
|
1446 |
||
1447 |
int newIndex; |
|
1448 |
||
1449 |
if (visible.contains(dotBounds.x, dotBounds.y)) { |
|
1450 |
// Dot is currently visible, base the new |
|
1451 |
// location off the old, or |
|
1452 |
newIndex = target.viewToModel( |
|
1453 |
new Point(x, constrainY(target, |
|
1454 |
dotBounds.y + scrollAmount, 0))); |
|
1455 |
} |
|
1456 |
else { |
|
1457 |
// Dot isn't visible, choose the top or the bottom |
|
1458 |
// for the new location. |
|
1459 |
if (direction == -1) { |
|
1460 |
newIndex = target.viewToModel(new Point( |
|
1461 |
x, newVis.y)); |
|
1462 |
} |
|
1463 |
else { |
|
1464 |
newIndex = target.viewToModel(new Point( |
|
1465 |
x, newVis.y + visible.height)); |
|
1466 |
} |
|
1467 |
} |
|
1468 |
newIndex = constrainOffset(target, newIndex); |
|
1469 |
if (newIndex != selectedIndex) { |
|
1470 |
// Make sure the new visible location contains |
|
1471 |
// the location of dot, otherwise Caret will |
|
1472 |
// cause an additional scroll. |
|
4968
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1473 |
int newY = getAdjustedY(target, newVis, newIndex); |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1474 |
|
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1475 |
if (direction == -1 && newY <= initialY || direction == 1 && newY >= initialY) { |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1476 |
// Change index and correct newVis.y only if won't cause scrolling upward |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1477 |
newVis.y = newY; |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1478 |
|
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1479 |
if (select) { |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1480 |
target.moveCaretPosition(newIndex); |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1481 |
} else { |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1482 |
target.setCaretPosition(newIndex); |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1483 |
} |
2 | 1484 |
} |
1485 |
} |
|
1486 |
} catch (BadLocationException ble) { } |
|
1487 |
} else { |
|
1488 |
newVis.y = constrainY(target, |
|
1489 |
initialY + scrollAmount, visible.height); |
|
1490 |
} |
|
1491 |
if (magicPosition != null) { |
|
1492 |
caret.setMagicCaretPosition(magicPosition); |
|
1493 |
} |
|
1494 |
target.scrollRectToVisible(newVis); |
|
1495 |
} |
|
1496 |
} |
|
1497 |
||
1498 |
/** |
|
1499 |
* Makes sure <code>y</code> is a valid location in |
|
1500 |
* <code>target</code>. |
|
1501 |
*/ |
|
1502 |
private int constrainY(JTextComponent target, int y, int vis) { |
|
1503 |
if (y < 0) { |
|
1504 |
y = 0; |
|
1505 |
} |
|
1506 |
else if (y + vis > target.getHeight()) { |
|
1507 |
y = Math.max(0, target.getHeight() - vis); |
|
1508 |
} |
|
1509 |
return y; |
|
1510 |
} |
|
1511 |
||
1512 |
/** |
|
1513 |
* Ensures that <code>offset</code> is a valid offset into the |
|
1514 |
* model for <code>text</code>. |
|
1515 |
*/ |
|
1516 |
private int constrainOffset(JTextComponent text, int offset) { |
|
1517 |
Document doc = text.getDocument(); |
|
1518 |
||
1519 |
if ((offset != 0) && (offset > doc.getLength())) { |
|
1520 |
offset = doc.getLength(); |
|
1521 |
} |
|
1522 |
if (offset < 0) { |
|
1523 |
offset = 0; |
|
1524 |
} |
|
1525 |
return offset; |
|
1526 |
} |
|
1527 |
||
1528 |
/** |
|
4968
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1529 |
* Returns adjustsed {@code y} position that indicates the location to scroll to |
2 | 1530 |
* after selecting <code>index</code>. |
1531 |
*/ |
|
4968
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1532 |
private int getAdjustedY(JTextComponent text, Rectangle visible, int index) { |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1533 |
int result = visible.y; |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1534 |
|
2 | 1535 |
try { |
1536 |
Rectangle dotBounds = text.modelToView(index); |
|
1537 |
||
4968
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1538 |
if (dotBounds.y < visible.y) { |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1539 |
result = dotBounds.y; |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1540 |
} else { |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1541 |
if ((dotBounds.y > visible.y + visible.height) || |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1542 |
(dotBounds.y + dotBounds.height > visible.y + visible.height)) { |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1543 |
result = dotBounds.y + dotBounds.height - visible.height; |
2 | 1544 |
} |
1545 |
} |
|
4968
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1546 |
} catch (BadLocationException ble) { |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1547 |
} |
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1548 |
|
517b279d7f2b
6917744: JScrollPane Page Up/Down keys do not handle correctly html tables with different cells contents
rupashka
parents:
2
diff
changeset
|
1549 |
return result; |
2 | 1550 |
} |
1551 |
||
1552 |
/** |
|
1553 |
* Adjusts the Rectangle to contain the bounds of the character at |
|
1554 |
* <code>index</code> in response to a page up. |
|
1555 |
*/ |
|
1556 |
private boolean select; |
|
1557 |
||
1558 |
/** |
|
1559 |
* Direction to scroll, 1 is down, -1 is up. |
|
1560 |
*/ |
|
1561 |
private int direction; |
|
1562 |
} |
|
1563 |
||
1564 |
||
1565 |
/** |
|
1566 |
* Pages one view to the left or right. |
|
1567 |
*/ |
|
1568 |
static class PageAction extends TextAction { |
|
1569 |
||
1570 |
/** Create this object with the appropriate identifier. */ |
|
1571 |
public PageAction(String nm, boolean left, boolean select) { |
|
1572 |
super(nm); |
|
1573 |
this.select = select; |
|
1574 |
this.left = left; |
|
1575 |
} |
|
1576 |
||
1577 |
/** The operation to perform when this action is triggered. */ |
|
1578 |
public void actionPerformed(ActionEvent e) { |
|
1579 |
JTextComponent target = getTextComponent(e); |
|
1580 |
if (target != null) { |
|
1581 |
int selectedIndex; |
|
1582 |
Rectangle visible = new Rectangle(); |
|
1583 |
target.computeVisibleRect(visible); |
|
1584 |
if (left) { |
|
1585 |
visible.x = Math.max(0, visible.x - visible.width); |
|
1586 |
} |
|
1587 |
else { |
|
1588 |
visible.x += visible.width; |
|
1589 |
} |
|
1590 |
||
1591 |
selectedIndex = target.getCaretPosition(); |
|
1592 |
if(selectedIndex != -1) { |
|
1593 |
if (left) { |
|
1594 |
selectedIndex = target.viewToModel |
|
1595 |
(new Point(visible.x, visible.y)); |
|
1596 |
} |
|
1597 |
else { |
|
1598 |
selectedIndex = target.viewToModel |
|
1599 |
(new Point(visible.x + visible.width - 1, |
|
1600 |
visible.y + visible.height - 1)); |
|
1601 |
} |
|
1602 |
Document doc = target.getDocument(); |
|
1603 |
if ((selectedIndex != 0) && |
|
1604 |
(selectedIndex > (doc.getLength()-1))) { |
|
1605 |
selectedIndex = doc.getLength()-1; |
|
1606 |
} |
|
1607 |
else if(selectedIndex < 0) { |
|
1608 |
selectedIndex = 0; |
|
1609 |
} |
|
1610 |
if (select) |
|
1611 |
target.moveCaretPosition(selectedIndex); |
|
1612 |
else |
|
1613 |
target.setCaretPosition(selectedIndex); |
|
1614 |
} |
|
1615 |
} |
|
1616 |
} |
|
1617 |
||
1618 |
private boolean select; |
|
1619 |
private boolean left; |
|
1620 |
} |
|
1621 |
||
1622 |
static class DumpModelAction extends TextAction { |
|
1623 |
||
1624 |
DumpModelAction() { |
|
1625 |
super("dump-model"); |
|
1626 |
} |
|
1627 |
||
1628 |
public void actionPerformed(ActionEvent e) { |
|
1629 |
JTextComponent target = getTextComponent(e); |
|
1630 |
if (target != null) { |
|
1631 |
Document d = target.getDocument(); |
|
1632 |
if (d instanceof AbstractDocument) { |
|
1633 |
((AbstractDocument) d).dump(System.err); |
|
1634 |
} |
|
1635 |
} |
|
1636 |
} |
|
1637 |
} |
|
1638 |
||
1639 |
/* |
|
1640 |
* Action to move the selection by way of the |
|
1641 |
* getNextVisualPositionFrom method. Constructor indicates direction |
|
1642 |
* to use. |
|
1643 |
*/ |
|
1644 |
static class NextVisualPositionAction extends TextAction { |
|
1645 |
||
1646 |
/** |
|
1647 |
* Create this action with the appropriate identifier. |
|
1648 |
* @param nm the name of the action, Action.NAME. |
|
1649 |
* @param select whether to extend the selection when |
|
1650 |
* changing the caret position. |
|
1651 |
*/ |
|
1652 |
NextVisualPositionAction(String nm, boolean select, int direction) { |
|
1653 |
super(nm); |
|
1654 |
this.select = select; |
|
1655 |
this.direction = direction; |
|
1656 |
} |
|
1657 |
||
1658 |
/** The operation to perform when this action is triggered. */ |
|
1659 |
public void actionPerformed(ActionEvent e) { |
|
1660 |
JTextComponent target = getTextComponent(e); |
|
1661 |
if (target != null) { |
|
1662 |
Caret caret = target.getCaret(); |
|
1663 |
DefaultCaret bidiCaret = (caret instanceof DefaultCaret) ? |
|
1664 |
(DefaultCaret)caret : null; |
|
1665 |
int dot = caret.getDot(); |
|
1666 |
Position.Bias[] bias = new Position.Bias[1]; |
|
1667 |
Point magicPosition = caret.getMagicCaretPosition(); |
|
1668 |
||
1669 |
try { |
|
1670 |
if(magicPosition == null && |
|
1671 |
(direction == SwingConstants.NORTH || |
|
1672 |
direction == SwingConstants.SOUTH)) { |
|
1673 |
Rectangle r = (bidiCaret != null) ? |
|
1674 |
target.getUI().modelToView(target, dot, |
|
1675 |
bidiCaret.getDotBias()) : |
|
1676 |
target.modelToView(dot); |
|
1677 |
magicPosition = new Point(r.x, r.y); |
|
1678 |
} |
|
1679 |
||
1680 |
NavigationFilter filter = target.getNavigationFilter(); |
|
1681 |
||
1682 |
if (filter != null) { |
|
1683 |
dot = filter.getNextVisualPositionFrom |
|
1684 |
(target, dot, (bidiCaret != null) ? |
|
1685 |
bidiCaret.getDotBias() : |
|
1686 |
Position.Bias.Forward, direction, bias); |
|
1687 |
} |
|
1688 |
else { |
|
1689 |
dot = target.getUI().getNextVisualPositionFrom |
|
1690 |
(target, dot, (bidiCaret != null) ? |
|
1691 |
bidiCaret.getDotBias() : |
|
1692 |
Position.Bias.Forward, direction, bias); |
|
1693 |
} |
|
1694 |
if(bias[0] == null) { |
|
1695 |
bias[0] = Position.Bias.Forward; |
|
1696 |
} |
|
1697 |
if(bidiCaret != null) { |
|
1698 |
if (select) { |
|
1699 |
bidiCaret.moveDot(dot, bias[0]); |
|
1700 |
} else { |
|
1701 |
bidiCaret.setDot(dot, bias[0]); |
|
1702 |
} |
|
1703 |
} |
|
1704 |
else { |
|
1705 |
if (select) { |
|
1706 |
caret.moveDot(dot); |
|
1707 |
} else { |
|
1708 |
caret.setDot(dot); |
|
1709 |
} |
|
1710 |
} |
|
1711 |
if(magicPosition != null && |
|
1712 |
(direction == SwingConstants.NORTH || |
|
1713 |
direction == SwingConstants.SOUTH)) { |
|
1714 |
target.getCaret().setMagicCaretPosition(magicPosition); |
|
1715 |
} |
|
1716 |
} catch (BadLocationException ex) { |
|
1717 |
} |
|
1718 |
} |
|
1719 |
} |
|
1720 |
||
1721 |
private boolean select; |
|
1722 |
private int direction; |
|
1723 |
} |
|
1724 |
||
1725 |
/* |
|
1726 |
* Position the caret to the beginning of the word. |
|
1727 |
* @see DefaultEditorKit#beginWordAction |
|
1728 |
* @see DefaultEditorKit#selectBeginWordAction |
|
1729 |
* @see DefaultEditorKit#getActions |
|
1730 |
*/ |
|
1731 |
static class BeginWordAction extends TextAction { |
|
1732 |
||
1733 |
/** |
|
1734 |
* Create this action with the appropriate identifier. |
|
1735 |
* @param nm the name of the action, Action.NAME. |
|
1736 |
* @param select whether to extend the selection when |
|
1737 |
* changing the caret position. |
|
1738 |
*/ |
|
1739 |
BeginWordAction(String nm, boolean select) { |
|
1740 |
super(nm); |
|
1741 |
this.select = select; |
|
1742 |
} |
|
1743 |
||
1744 |
/** The operation to perform when this action is triggered. */ |
|
1745 |
public void actionPerformed(ActionEvent e) { |
|
1746 |
JTextComponent target = getTextComponent(e); |
|
1747 |
if (target != null) { |
|
1748 |
try { |
|
1749 |
int offs = target.getCaretPosition(); |
|
1750 |
int begOffs = Utilities.getWordStart(target, offs); |
|
1751 |
if (select) { |
|
1752 |
target.moveCaretPosition(begOffs); |
|
1753 |
} else { |
|
1754 |
target.setCaretPosition(begOffs); |
|
1755 |
} |
|
1756 |
} catch (BadLocationException bl) { |
|
1757 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1758 |
} |
|
1759 |
} |
|
1760 |
} |
|
1761 |
||
1762 |
private boolean select; |
|
1763 |
} |
|
1764 |
||
1765 |
/* |
|
1766 |
* Position the caret to the end of the word. |
|
1767 |
* @see DefaultEditorKit#endWordAction |
|
1768 |
* @see DefaultEditorKit#selectEndWordAction |
|
1769 |
* @see DefaultEditorKit#getActions |
|
1770 |
*/ |
|
1771 |
static class EndWordAction extends TextAction { |
|
1772 |
||
1773 |
/** |
|
1774 |
* Create this action with the appropriate identifier. |
|
1775 |
* @param nm the name of the action, Action.NAME. |
|
1776 |
* @param select whether to extend the selection when |
|
1777 |
* changing the caret position. |
|
1778 |
*/ |
|
1779 |
EndWordAction(String nm, boolean select) { |
|
1780 |
super(nm); |
|
1781 |
this.select = select; |
|
1782 |
} |
|
1783 |
||
1784 |
/** The operation to perform when this action is triggered. */ |
|
1785 |
public void actionPerformed(ActionEvent e) { |
|
1786 |
JTextComponent target = getTextComponent(e); |
|
1787 |
if (target != null) { |
|
1788 |
try { |
|
1789 |
int offs = target.getCaretPosition(); |
|
1790 |
int endOffs = Utilities.getWordEnd(target, offs); |
|
1791 |
if (select) { |
|
1792 |
target.moveCaretPosition(endOffs); |
|
1793 |
} else { |
|
1794 |
target.setCaretPosition(endOffs); |
|
1795 |
} |
|
1796 |
} catch (BadLocationException bl) { |
|
1797 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1798 |
} |
|
1799 |
} |
|
1800 |
} |
|
1801 |
||
1802 |
private boolean select; |
|
1803 |
} |
|
1804 |
||
1805 |
/* |
|
1806 |
* Position the caret to the beginning of the previous word. |
|
1807 |
* @see DefaultEditorKit#previousWordAction |
|
1808 |
* @see DefaultEditorKit#selectPreviousWordAction |
|
1809 |
* @see DefaultEditorKit#getActions |
|
1810 |
*/ |
|
1811 |
static class PreviousWordAction extends TextAction { |
|
1812 |
||
1813 |
/** |
|
1814 |
* Create this action with the appropriate identifier. |
|
1815 |
* @param nm the name of the action, Action.NAME. |
|
1816 |
* @param select whether to extend the selection when |
|
1817 |
* changing the caret position. |
|
1818 |
*/ |
|
1819 |
PreviousWordAction(String nm, boolean select) { |
|
1820 |
super(nm); |
|
1821 |
this.select = select; |
|
1822 |
} |
|
1823 |
||
1824 |
/** The operation to perform when this action is triggered. */ |
|
1825 |
public void actionPerformed(ActionEvent e) { |
|
1826 |
JTextComponent target = getTextComponent(e); |
|
1827 |
if (target != null) { |
|
1828 |
int offs = target.getCaretPosition(); |
|
1829 |
boolean failed = false; |
|
1830 |
try { |
|
1831 |
Element curPara = |
|
1832 |
Utilities.getParagraphElement(target, offs); |
|
1833 |
offs = Utilities.getPreviousWord(target, offs); |
|
1834 |
if(offs < curPara.getStartOffset()) { |
|
1835 |
// we should first move to the end of the |
|
1836 |
// previous paragraph (bug #4278839) |
|
1837 |
offs = Utilities.getParagraphElement(target, offs). |
|
1838 |
getEndOffset() - 1; |
|
1839 |
} |
|
1840 |
} catch (BadLocationException bl) { |
|
1841 |
if (offs != 0) { |
|
1842 |
offs = 0; |
|
1843 |
} |
|
1844 |
else { |
|
1845 |
failed = true; |
|
1846 |
} |
|
1847 |
} |
|
1848 |
if (!failed) { |
|
1849 |
if (select) { |
|
1850 |
target.moveCaretPosition(offs); |
|
1851 |
} else { |
|
1852 |
target.setCaretPosition(offs); |
|
1853 |
} |
|
1854 |
} |
|
1855 |
else { |
|
1856 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1857 |
} |
|
1858 |
} |
|
1859 |
} |
|
1860 |
||
1861 |
private boolean select; |
|
1862 |
} |
|
1863 |
||
1864 |
/* |
|
1865 |
* Position the caret to the next of the word. |
|
1866 |
* @see DefaultEditorKit#nextWordAction |
|
1867 |
* @see DefaultEditorKit#selectNextWordAction |
|
1868 |
* @see DefaultEditorKit#getActions |
|
1869 |
*/ |
|
1870 |
static class NextWordAction extends TextAction { |
|
1871 |
||
1872 |
/** |
|
1873 |
* Create this action with the appropriate identifier. |
|
1874 |
* @param nm the name of the action, Action.NAME. |
|
1875 |
* @param select whether to extend the selection when |
|
1876 |
* changing the caret position. |
|
1877 |
*/ |
|
1878 |
NextWordAction(String nm, boolean select) { |
|
1879 |
super(nm); |
|
1880 |
this.select = select; |
|
1881 |
} |
|
1882 |
||
1883 |
/** The operation to perform when this action is triggered. */ |
|
1884 |
public void actionPerformed(ActionEvent e) { |
|
1885 |
JTextComponent target = getTextComponent(e); |
|
1886 |
if (target != null) { |
|
1887 |
int offs = target.getCaretPosition(); |
|
1888 |
boolean failed = false; |
|
1889 |
int oldOffs = offs; |
|
1890 |
Element curPara = |
|
1891 |
Utilities.getParagraphElement(target, offs); |
|
1892 |
try { |
|
1893 |
offs = Utilities.getNextWord(target, offs); |
|
1894 |
if(offs >= curPara.getEndOffset() && |
|
1895 |
oldOffs != curPara.getEndOffset() - 1) { |
|
1896 |
// we should first move to the end of current |
|
1897 |
// paragraph (bug #4278839) |
|
1898 |
offs = curPara.getEndOffset() - 1; |
|
1899 |
} |
|
1900 |
} catch (BadLocationException bl) { |
|
1901 |
int end = target.getDocument().getLength(); |
|
1902 |
if (offs != end) { |
|
1903 |
if(oldOffs != curPara.getEndOffset() - 1) { |
|
1904 |
offs = curPara.getEndOffset() - 1; |
|
1905 |
} else { |
|
1906 |
offs = end; |
|
1907 |
} |
|
1908 |
} |
|
1909 |
else { |
|
1910 |
failed = true; |
|
1911 |
} |
|
1912 |
} |
|
1913 |
if (!failed) { |
|
1914 |
if (select) { |
|
1915 |
target.moveCaretPosition(offs); |
|
1916 |
} else { |
|
1917 |
target.setCaretPosition(offs); |
|
1918 |
} |
|
1919 |
} |
|
1920 |
else { |
|
1921 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1922 |
} |
|
1923 |
} |
|
1924 |
} |
|
1925 |
||
1926 |
private boolean select; |
|
1927 |
} |
|
1928 |
||
1929 |
/* |
|
1930 |
* Position the caret to the beginning of the line. |
|
1931 |
* @see DefaultEditorKit#beginLineAction |
|
1932 |
* @see DefaultEditorKit#selectBeginLineAction |
|
1933 |
* @see DefaultEditorKit#getActions |
|
1934 |
*/ |
|
1935 |
static class BeginLineAction extends TextAction { |
|
1936 |
||
1937 |
/** |
|
1938 |
* Create this action with the appropriate identifier. |
|
1939 |
* @param nm the name of the action, Action.NAME. |
|
1940 |
* @param select whether to extend the selection when |
|
1941 |
* changing the caret position. |
|
1942 |
*/ |
|
1943 |
BeginLineAction(String nm, boolean select) { |
|
1944 |
super(nm); |
|
1945 |
this.select = select; |
|
1946 |
} |
|
1947 |
||
1948 |
/** The operation to perform when this action is triggered. */ |
|
1949 |
public void actionPerformed(ActionEvent e) { |
|
1950 |
JTextComponent target = getTextComponent(e); |
|
1951 |
if (target != null) { |
|
1952 |
try { |
|
1953 |
int offs = target.getCaretPosition(); |
|
1954 |
int begOffs = Utilities.getRowStart(target, offs); |
|
1955 |
if (select) { |
|
1956 |
target.moveCaretPosition(begOffs); |
|
1957 |
} else { |
|
1958 |
target.setCaretPosition(begOffs); |
|
1959 |
} |
|
1960 |
} catch (BadLocationException bl) { |
|
1961 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
1962 |
} |
|
1963 |
} |
|
1964 |
} |
|
1965 |
||
1966 |
private boolean select; |
|
1967 |
} |
|
1968 |
||
1969 |
/* |
|
1970 |
* Position the caret to the end of the line. |
|
1971 |
* @see DefaultEditorKit#endLineAction |
|
1972 |
* @see DefaultEditorKit#selectEndLineAction |
|
1973 |
* @see DefaultEditorKit#getActions |
|
1974 |
*/ |
|
1975 |
static class EndLineAction extends TextAction { |
|
1976 |
||
1977 |
/** |
|
1978 |
* Create this action with the appropriate identifier. |
|
1979 |
* @param nm the name of the action, Action.NAME. |
|
1980 |
* @param select whether to extend the selection when |
|
1981 |
* changing the caret position. |
|
1982 |
*/ |
|
1983 |
EndLineAction(String nm, boolean select) { |
|
1984 |
super(nm); |
|
1985 |
this.select = select; |
|
1986 |
} |
|
1987 |
||
1988 |
/** The operation to perform when this action is triggered. */ |
|
1989 |
public void actionPerformed(ActionEvent e) { |
|
1990 |
JTextComponent target = getTextComponent(e); |
|
1991 |
if (target != null) { |
|
1992 |
try { |
|
1993 |
int offs = target.getCaretPosition(); |
|
1994 |
int endOffs = Utilities.getRowEnd(target, offs); |
|
1995 |
if (select) { |
|
1996 |
target.moveCaretPosition(endOffs); |
|
1997 |
} else { |
|
1998 |
target.setCaretPosition(endOffs); |
|
1999 |
} |
|
2000 |
} catch (BadLocationException bl) { |
|
2001 |
UIManager.getLookAndFeel().provideErrorFeedback(target); |
|
2002 |
} |
|
2003 |
} |
|
2004 |
} |
|
2005 |
||
2006 |
private boolean select; |
|
2007 |
} |
|
2008 |
||
2009 |
/* |
|
2010 |
* Position the caret to the beginning of the paragraph. |
|
2011 |
* @see DefaultEditorKit#beginParagraphAction |
|
2012 |
* @see DefaultEditorKit#selectBeginParagraphAction |
|
2013 |
* @see DefaultEditorKit#getActions |
|
2014 |
*/ |
|
2015 |
static class BeginParagraphAction extends TextAction { |
|
2016 |
||
2017 |
/** |
|
2018 |
* Create this action with the appropriate identifier. |
|
2019 |
* @param nm the name of the action, Action.NAME. |
|
2020 |
* @param select whether to extend the selection when |
|
2021 |
* changing the caret position. |
|
2022 |
*/ |
|
2023 |
BeginParagraphAction(String nm, boolean select) { |
|
2024 |
super(nm); |
|
2025 |
this.select = select; |
|
2026 |
} |
|
2027 |
||
2028 |
/** The operation to perform when this action is triggered. */ |
|
2029 |
public void actionPerformed(ActionEvent e) { |
|
2030 |
JTextComponent target = getTextComponent(e); |
|
2031 |
if (target != null) { |
|
2032 |
int offs = target.getCaretPosition(); |
|
2033 |
Element elem = Utilities.getParagraphElement(target, offs); |
|
2034 |
offs = elem.getStartOffset(); |
|
2035 |
if (select) { |
|
2036 |
target.moveCaretPosition(offs); |
|
2037 |
} else { |
|
2038 |
target.setCaretPosition(offs); |
|
2039 |
} |
|
2040 |
} |
|
2041 |
} |
|
2042 |
||
2043 |
private boolean select; |
|
2044 |
} |
|
2045 |
||
2046 |
/* |
|
2047 |
* Position the caret to the end of the paragraph. |
|
2048 |
* @see DefaultEditorKit#endParagraphAction |
|
2049 |
* @see DefaultEditorKit#selectEndParagraphAction |
|
2050 |
* @see DefaultEditorKit#getActions |
|
2051 |
*/ |
|
2052 |
static class EndParagraphAction extends TextAction { |
|
2053 |
||
2054 |
/** |
|
2055 |
* Create this action with the appropriate identifier. |
|
2056 |
* @param nm the name of the action, Action.NAME. |
|
2057 |
* @param select whether to extend the selection when |
|
2058 |
* changing the caret position. |
|
2059 |
*/ |
|
2060 |
EndParagraphAction(String nm, boolean select) { |
|
2061 |
super(nm); |
|
2062 |
this.select = select; |
|
2063 |
} |
|
2064 |
||
2065 |
/** The operation to perform when this action is triggered. */ |
|
2066 |
public void actionPerformed(ActionEvent e) { |
|
2067 |
JTextComponent target = getTextComponent(e); |
|
2068 |
if (target != null) { |
|
2069 |
int offs = target.getCaretPosition(); |
|
2070 |
Element elem = Utilities.getParagraphElement(target, offs); |
|
2071 |
offs = Math.min(target.getDocument().getLength(), |
|
2072 |
elem.getEndOffset()); |
|
2073 |
if (select) { |
|
2074 |
target.moveCaretPosition(offs); |
|
2075 |
} else { |
|
2076 |
target.setCaretPosition(offs); |
|
2077 |
} |
|
2078 |
} |
|
2079 |
} |
|
2080 |
||
2081 |
private boolean select; |
|
2082 |
} |
|
2083 |
||
2084 |
/* |
|
2085 |
* Move the caret to the beginning of the document. |
|
2086 |
* @see DefaultEditorKit#beginAction |
|
2087 |
* @see DefaultEditorKit#getActions |
|
2088 |
*/ |
|
2089 |
static class BeginAction extends TextAction { |
|
2090 |
||
2091 |
/* Create this object with the appropriate identifier. */ |
|
2092 |
BeginAction(String nm, boolean select) { |
|
2093 |
super(nm); |
|
2094 |
this.select = select; |
|
2095 |
} |
|
2096 |
||
2097 |
/** The operation to perform when this action is triggered. */ |
|
2098 |
public void actionPerformed(ActionEvent e) { |
|
2099 |
JTextComponent target = getTextComponent(e); |
|
2100 |
if (target != null) { |
|
2101 |
if (select) { |
|
2102 |
target.moveCaretPosition(0); |
|
2103 |
} else { |
|
2104 |
target.setCaretPosition(0); |
|
2105 |
} |
|
2106 |
} |
|
2107 |
} |
|
2108 |
||
2109 |
private boolean select; |
|
2110 |
} |
|
2111 |
||
2112 |
/* |
|
2113 |
* Move the caret to the end of the document. |
|
2114 |
* @see DefaultEditorKit#endAction |
|
2115 |
* @see DefaultEditorKit#getActions |
|
2116 |
*/ |
|
2117 |
static class EndAction extends TextAction { |
|
2118 |
||
2119 |
/* Create this object with the appropriate identifier. */ |
|
2120 |
EndAction(String nm, boolean select) { |
|
2121 |
super(nm); |
|
2122 |
this.select = select; |
|
2123 |
} |
|
2124 |
||
2125 |
/** The operation to perform when this action is triggered. */ |
|
2126 |
public void actionPerformed(ActionEvent e) { |
|
2127 |
JTextComponent target = getTextComponent(e); |
|
2128 |
if (target != null) { |
|
2129 |
Document doc = target.getDocument(); |
|
2130 |
int dot = doc.getLength(); |
|
2131 |
if (select) { |
|
2132 |
target.moveCaretPosition(dot); |
|
2133 |
} else { |
|
2134 |
target.setCaretPosition(dot); |
|
2135 |
} |
|
2136 |
} |
|
2137 |
} |
|
2138 |
||
2139 |
private boolean select; |
|
2140 |
} |
|
2141 |
||
2142 |
/* |
|
2143 |
* Select the word around the caret |
|
2144 |
* @see DefaultEditorKit#endAction |
|
2145 |
* @see DefaultEditorKit#getActions |
|
2146 |
*/ |
|
2147 |
static class SelectWordAction extends TextAction { |
|
2148 |
||
2149 |
/** |
|
2150 |
* Create this action with the appropriate identifier. |
|
2151 |
* @param nm the name of the action, Action.NAME. |
|
2152 |
* @param select whether to extend the selection when |
|
2153 |
* changing the caret position. |
|
2154 |
*/ |
|
2155 |
SelectWordAction() { |
|
2156 |
super(selectWordAction); |
|
2157 |
start = new BeginWordAction("pigdog", false); |
|
2158 |
end = new EndWordAction("pigdog", true); |
|
2159 |
} |
|
2160 |
||
2161 |
/** The operation to perform when this action is triggered. */ |
|
2162 |
public void actionPerformed(ActionEvent e) { |
|
2163 |
start.actionPerformed(e); |
|
2164 |
end.actionPerformed(e); |
|
2165 |
} |
|
2166 |
||
2167 |
private Action start; |
|
2168 |
private Action end; |
|
2169 |
} |
|
2170 |
||
2171 |
/* |
|
2172 |
* Select the line around the caret |
|
2173 |
* @see DefaultEditorKit#endAction |
|
2174 |
* @see DefaultEditorKit#getActions |
|
2175 |
*/ |
|
2176 |
static class SelectLineAction extends TextAction { |
|
2177 |
||
2178 |
/** |
|
2179 |
* Create this action with the appropriate identifier. |
|
2180 |
* @param nm the name of the action, Action.NAME. |
|
2181 |
* @param select whether to extend the selection when |
|
2182 |
* changing the caret position. |
|
2183 |
*/ |
|
2184 |
SelectLineAction() { |
|
2185 |
super(selectLineAction); |
|
2186 |
start = new BeginLineAction("pigdog", false); |
|
2187 |
end = new EndLineAction("pigdog", true); |
|
2188 |
} |
|
2189 |
||
2190 |
/** The operation to perform when this action is triggered. */ |
|
2191 |
public void actionPerformed(ActionEvent e) { |
|
2192 |
start.actionPerformed(e); |
|
2193 |
end.actionPerformed(e); |
|
2194 |
} |
|
2195 |
||
2196 |
private Action start; |
|
2197 |
private Action end; |
|
2198 |
} |
|
2199 |
||
2200 |
/* |
|
2201 |
* Select the paragraph around the caret |
|
2202 |
* @see DefaultEditorKit#endAction |
|
2203 |
* @see DefaultEditorKit#getActions |
|
2204 |
*/ |
|
2205 |
static class SelectParagraphAction extends TextAction { |
|
2206 |
||
2207 |
/** |
|
2208 |
* Create this action with the appropriate identifier. |
|
2209 |
* @param nm the name of the action, Action.NAME. |
|
2210 |
* @param select whether to extend the selection when |
|
2211 |
* changing the caret position. |
|
2212 |
*/ |
|
2213 |
SelectParagraphAction() { |
|
2214 |
super(selectParagraphAction); |
|
2215 |
start = new BeginParagraphAction("pigdog", false); |
|
2216 |
end = new EndParagraphAction("pigdog", true); |
|
2217 |
} |
|
2218 |
||
2219 |
/** The operation to perform when this action is triggered. */ |
|
2220 |
public void actionPerformed(ActionEvent e) { |
|
2221 |
start.actionPerformed(e); |
|
2222 |
end.actionPerformed(e); |
|
2223 |
} |
|
2224 |
||
2225 |
private Action start; |
|
2226 |
private Action end; |
|
2227 |
} |
|
2228 |
||
2229 |
/* |
|
2230 |
* Select the entire document |
|
2231 |
* @see DefaultEditorKit#endAction |
|
2232 |
* @see DefaultEditorKit#getActions |
|
2233 |
*/ |
|
2234 |
static class SelectAllAction extends TextAction { |
|
2235 |
||
2236 |
/** |
|
2237 |
* Create this action with the appropriate identifier. |
|
2238 |
* @param nm the name of the action, Action.NAME. |
|
2239 |
* @param select whether to extend the selection when |
|
2240 |
* changing the caret position. |
|
2241 |
*/ |
|
2242 |
SelectAllAction() { |
|
2243 |
super(selectAllAction); |
|
2244 |
} |
|
2245 |
||
2246 |
/** The operation to perform when this action is triggered. */ |
|
2247 |
public void actionPerformed(ActionEvent e) { |
|
2248 |
JTextComponent target = getTextComponent(e); |
|
2249 |
if (target != null) { |
|
2250 |
Document doc = target.getDocument(); |
|
2251 |
target.setCaretPosition(0); |
|
2252 |
target.moveCaretPosition(doc.getLength()); |
|
2253 |
} |
|
2254 |
} |
|
2255 |
||
2256 |
} |
|
2257 |
||
2258 |
/* |
|
2259 |
* Remove the selection, if any. |
|
2260 |
* @see DefaultEditorKit#unselectAction |
|
2261 |
* @see DefaultEditorKit#getActions |
|
2262 |
*/ |
|
2263 |
static class UnselectAction extends TextAction { |
|
2264 |
||
2265 |
/** |
|
2266 |
* Create this action with the appropriate identifier. |
|
2267 |
*/ |
|
2268 |
UnselectAction() { |
|
2269 |
super(unselectAction); |
|
2270 |
} |
|
2271 |
||
2272 |
/** The operation to perform when this action is triggered. */ |
|
2273 |
public void actionPerformed(ActionEvent e) { |
|
2274 |
JTextComponent target = getTextComponent(e); |
|
2275 |
if (target != null) { |
|
2276 |
target.setCaretPosition(target.getCaretPosition()); |
|
2277 |
} |
|
2278 |
} |
|
2279 |
||
2280 |
} |
|
2281 |
||
2282 |
/* |
|
2283 |
* Toggles the ComponentOrientation of the text component. |
|
2284 |
* @see DefaultEditorKit#toggleComponentOrientationAction |
|
2285 |
* @see DefaultEditorKit#getActions |
|
2286 |
*/ |
|
2287 |
static class ToggleComponentOrientationAction extends TextAction { |
|
2288 |
||
2289 |
/** |
|
2290 |
* Create this action with the appropriate identifier. |
|
2291 |
*/ |
|
2292 |
ToggleComponentOrientationAction() { |
|
2293 |
super(toggleComponentOrientationAction); |
|
2294 |
} |
|
2295 |
||
2296 |
/** The operation to perform when this action is triggered. */ |
|
2297 |
public void actionPerformed(ActionEvent e) { |
|
2298 |
JTextComponent target = getTextComponent(e); |
|
2299 |
if (target != null) { |
|
2300 |
ComponentOrientation last = target.getComponentOrientation(); |
|
2301 |
ComponentOrientation next; |
|
2302 |
if( last == ComponentOrientation.RIGHT_TO_LEFT ) |
|
2303 |
next = ComponentOrientation.LEFT_TO_RIGHT; |
|
2304 |
else |
|
2305 |
next = ComponentOrientation.RIGHT_TO_LEFT; |
|
2306 |
target.setComponentOrientation(next); |
|
2307 |
target.repaint(); |
|
2308 |
} |
|
2309 |
} |
|
2310 |
} |
|
2311 |
||
2312 |
} |