1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55:
56:
65: public class DefaultEditorKit extends EditorKit
66: {
67: static class SelectionPreviousWordAction
68: extends TextAction
69: {
70: SelectionPreviousWordAction()
71: {
72: super(selectionPreviousWordAction);
73: }
74:
75: public void actionPerformed(ActionEvent event)
76: {
77: try
78: {
79: JTextComponent t = getTextComponent(event);
80:
81: if (t != null)
82: {
83: int offs = Utilities.getPreviousWord(t, t.getCaretPosition());
84:
85: Caret c = t.getCaret();
86: c.moveDot(offs);
87: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
88: }
89: }
90: catch(BadLocationException ble)
91: {
92:
93: }
94: }
95: }
96:
97: static class SelectionNextWordAction
98: extends TextAction
99: {
100: SelectionNextWordAction()
101: {
102: super(selectionNextWordAction);
103: }
104:
105: public void actionPerformed(ActionEvent event)
106: {
107: try
108: {
109: JTextComponent t = getTextComponent(event);
110:
111: if (t != null)
112: {
113: int offs = Utilities.getNextWord(t, t.getCaretPosition());
114:
115: Caret c = t.getCaret();
116: c.moveDot(offs);
117: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
118: }
119: }
120: catch(BadLocationException ble)
121: {
122:
123: }
124: }
125: }
126:
127: static class SelectionBeginWordAction extends TextAction
128: {
129: SelectionBeginWordAction()
130: {
131: super(selectionBeginWordAction);
132: }
133:
134: public void actionPerformed(ActionEvent event)
135: {
136: try
137: {
138: JTextComponent t = getTextComponent(event);
139:
140: if (t != null)
141: {
142: int offs = Utilities.getWordStart(t, t.getCaretPosition());
143:
144: Caret c = t.getCaret();
145: c.moveDot(offs);
146: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
147: }
148: }
149: catch(BadLocationException ble)
150: {
151:
152: }
153: }
154: }
155:
156: static class SelectionEndWordAction extends TextAction
157: {
158: SelectionEndWordAction()
159: {
160: super(selectionEndWordAction);
161: }
162:
163: public void actionPerformed(ActionEvent event)
164: {
165: try
166: {
167: JTextComponent t = getTextComponent(event);
168:
169: if (t != null)
170: {
171: int offs = Utilities.getWordEnd(t, t.getCaretPosition());
172:
173: Caret c = t.getCaret();
174: c.moveDot(offs);
175: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
176: }
177: }
178: catch(BadLocationException ble)
179: {
180:
181: }
182: }
183: }
184:
185: static class BeginWordAction extends TextAction
186: {
187: BeginWordAction()
188: {
189: super(beginWordAction);
190: }
191:
192: public void actionPerformed(ActionEvent event)
193: {
194: try
195: {
196: JTextComponent t = getTextComponent(event);
197:
198: if (t != null)
199: {
200: int offs = Utilities.getWordStart(t, t.getCaretPosition());
201:
202: Caret c = t.getCaret();
203: c.setDot(offs);
204: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
205: }
206: }
207: catch(BadLocationException ble)
208: {
209:
210: }
211: }
212: }
213:
214: static class EndWordAction extends TextAction
215: {
216: EndWordAction()
217: {
218: super(endWordAction);
219: }
220:
221: public void actionPerformed(ActionEvent event)
222: {
223: try
224: {
225: JTextComponent t = getTextComponent(event);
226:
227: if (t != null)
228: {
229: int offs = Utilities.getWordEnd(t, t.getCaretPosition());
230:
231: Caret c = t.getCaret();
232: c.setDot(offs);
233: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
234: }
235: }
236: catch(BadLocationException ble)
237: {
238:
239: }
240: }
241: }
242:
243: static class PreviousWordAction
244: extends TextAction
245: {
246: PreviousWordAction()
247: {
248: super(previousWordAction);
249: }
250:
251: public void actionPerformed(ActionEvent event)
252: {
253: try
254: {
255: JTextComponent t = getTextComponent(event);
256:
257: if (t != null)
258: {
259: int offs = Utilities.getPreviousWord(t, t.getCaretPosition());
260:
261: Caret c = t.getCaret();
262: c.setDot(offs);
263: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
264: }
265: }
266: catch(BadLocationException ble)
267: {
268:
269: }
270: }
271: }
272:
273: static class NextWordAction
274: extends TextAction
275: {
276: NextWordAction()
277: {
278: super(nextWordAction);
279: }
280:
281: public void actionPerformed(ActionEvent event)
282: {
283: try
284: {
285: JTextComponent t = getTextComponent(event);
286:
287: if (t != null)
288: {
289: int offs = Utilities.getNextWord(t, t.getCaretPosition());
290:
291: Caret c = t.getCaret();
292: c.setDot(offs);
293: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
294: }
295: }
296: catch(BadLocationException ble)
297: {
298:
299: }
300: }
301: }
302:
303: static class SelectAllAction
304: extends TextAction
305: {
306: SelectAllAction()
307: {
308: super(selectAllAction);
309: }
310:
311: public void actionPerformed(ActionEvent event)
312: {
313: JTextComponent t = getTextComponent(event);
314: if (t != null)
315: {
316: int offs = t.getDocument().getLength();
317: Caret c = t.getCaret();
318: c.setDot(0);
319: c.moveDot(offs);
320: try
321: {
322: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
323: }
324: catch(BadLocationException ble)
325: {
326:
327: }
328: }
329: }
330: }
331:
332: static class SelectionBeginAction
333: extends TextAction
334: {
335: SelectionBeginAction()
336: {
337: super(selectionBeginAction);
338: }
339:
340: public void actionPerformed(ActionEvent event)
341: {
342: JTextComponent t = getTextComponent(event);
343: if (t != null)
344: {
345: Caret c = t.getCaret();
346: c.moveDot(0);
347: try
348: {
349: c.setMagicCaretPosition(t.modelToView(0).getLocation());
350: }
351: catch(BadLocationException ble)
352: {
353:
354: }
355: }
356: }
357: }
358:
359: static class SelectionEndAction
360: extends TextAction
361: {
362: SelectionEndAction()
363: {
364: super(selectionEndAction);
365: }
366:
367: public void actionPerformed(ActionEvent event)
368: {
369: JTextComponent t = getTextComponent(event);
370: if (t != null)
371: {
372: int offs = t.getDocument().getLength();
373: Caret c = t.getCaret();
374: c.moveDot(offs);
375: try
376: {
377: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
378: }
379: catch(BadLocationException ble)
380: {
381:
382: }
383: }
384: }
385: }
386:
387: static class SelectionBeginLineAction
388: extends TextAction
389: {
390:
391: SelectionBeginLineAction()
392: {
393: super(selectionBeginLineAction);
394: }
395:
396: public void actionPerformed(ActionEvent event)
397: {
398: JTextComponent t = getTextComponent(event);
399: if (t != null)
400: {
401: Caret c = t.getCaret();
402: try
403: {
404: int offs = Utilities.getRowStart(t, c.getDot());
405: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
406: }
407: catch(BadLocationException ble)
408: {
409:
410: }
411: }
412: }
413: }
414:
415: static class SelectionEndLineAction
416: extends TextAction
417: {
418: SelectionEndLineAction()
419: {
420: super(selectionEndLineAction);
421: }
422:
423: public void actionPerformed(ActionEvent event)
424: {
425: JTextComponent t = getTextComponent(event);
426: if (t != null)
427: {
428: Caret c = t.getCaret();
429: try
430: {
431: int offs = Utilities.getRowEnd(t, c.getDot());
432: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
433: }
434: catch(BadLocationException ble)
435: {
436:
437: }
438: }
439: }
440: }
441:
442: static class SelectLineAction extends TextAction
443: {
444: SelectLineAction()
445: {
446: super(selectLineAction);
447: }
448:
449: public void actionPerformed(ActionEvent event)
450: {
451: JTextComponent t = getTextComponent(event);
452: if (t != null)
453: {
454: Caret c = t.getCaret();
455: try
456: {
457: int offs1 = Utilities.getRowStart(t, c.getDot());
458: int offs2 = Utilities.getRowEnd(t, c.getDot());
459: c.setDot(offs2);
460: c.moveDot(offs1);
461: c.setMagicCaretPosition(t.modelToView(offs2).getLocation());
462: }
463: catch(BadLocationException ble)
464: {
465:
466: }
467: }
468: }
469: }
470:
471: static class SelectWordAction extends TextAction
472: {
473: SelectWordAction()
474: {
475: super(selectWordAction);
476: }
477:
478: public void actionPerformed(ActionEvent event)
479: {
480: JTextComponent t = getTextComponent(event);
481: if (t != null)
482: {
483: Caret c = t.getCaret();
484: int dot = c.getDot();
485: try
486: {
487: int wordStart = Utilities.getWordStart(t, dot);
488:
489: if (dot == wordStart)
490: {
491:
492: c.setDot(wordStart);
493: c.moveDot(Utilities.getWordEnd(t, wordStart));
494: }
495: else
496: {
497:
498:
499: int nextWord = Utilities.getNextWord(t, dot);
500: int previousWord = Utilities.getPreviousWord(t, dot);
501: int previousWordEnd = Utilities.getWordEnd(t, previousWord);
502:
503:
504:
505: if (dot >= previousWordEnd && dot <= nextWord)
506: {
507: c.setDot(previousWordEnd);
508: c.moveDot(nextWord);
509: }
510: else
511: {
512:
513: c.setDot(previousWord);
514: c.moveDot(previousWordEnd);
515: }
516: }
517:
518:
519:
520: if (c.getDot() != dot)
521: c.setMagicCaretPosition(t.modelToView(c.getDot()).getLocation());
522: }
523: catch(BadLocationException ble)
524: {
525:
526: }
527: }
528: }
529: }
530:
531: static class SelectionDownAction
532: extends TextAction.VerticalMovementAction
533: {
534: SelectionDownAction()
535: {
536: super(selectionDownAction, SwingConstants.SOUTH);
537: }
538:
539: protected void actionPerformedImpl(Caret c, int offs)
540: {
541: c.moveDot(offs);
542: }
543:
544: }
545:
546: static class SelectionUpAction
547: extends TextAction.VerticalMovementAction
548: {
549: SelectionUpAction()
550: {
551: super(selectionUpAction, SwingConstants.NORTH);
552: }
553:
554: protected void actionPerformedImpl(Caret c, int offs)
555: {
556: c.moveDot(offs);
557: }
558:
559: }
560:
561: static class SelectionForwardAction
562: extends TextAction.HorizontalMovementAction
563: {
564: SelectionForwardAction()
565: {
566: super(selectionForwardAction, SwingConstants.EAST);
567: }
568:
569: protected void actionPerformedImpl(Caret c, int offs)
570: {
571: c.moveDot(offs);
572: }
573: }
574:
575: static class SelectionBackwardAction
576: extends TextAction.HorizontalMovementAction
577: {
578: SelectionBackwardAction()
579: {
580: super(selectionBackwardAction, SwingConstants.WEST);
581: }
582:
583: protected void actionPerformedImpl(Caret c, int offs)
584: {
585: c.moveDot(offs);
586: }
587: }
588:
589: static class DownAction
590: extends TextAction.VerticalMovementAction
591: {
592: DownAction()
593: {
594: super(downAction, SwingConstants.SOUTH);
595: }
596:
597: protected void actionPerformedImpl(Caret c, int offs)
598: {
599: c.setDot(offs);
600: }
601: }
602:
603: static class UpAction
604: extends TextAction.VerticalMovementAction
605: {
606: UpAction()
607: {
608: super(upAction, SwingConstants.NORTH);
609: }
610:
611: protected void actionPerformedImpl(Caret c, int offs)
612: {
613: c.setDot(offs);
614: }
615:
616: }
617:
618: static class ForwardAction
619: extends TextAction.HorizontalMovementAction
620: {
621: ForwardAction()
622: {
623: super(forwardAction, SwingConstants.EAST);
624: }
625:
626: protected void actionPerformedImpl(Caret c, int offs)
627: {
628: c.setDot(offs);
629: }
630:
631: }
632:
633: static class BackwardAction
634: extends TextAction.HorizontalMovementAction
635: {
636: BackwardAction()
637: {
638: super(backwardAction, SwingConstants.WEST);
639: }
640:
641: protected void actionPerformedImpl(Caret c, int offs)
642: {
643: c.setDot(offs);
644: }
645:
646: }
647:
648: static class DeletePrevCharAction
649: extends TextAction
650: {
651: DeletePrevCharAction()
652: {
653: super(deletePrevCharAction);
654: }
655:
656: public void actionPerformed(ActionEvent event)
657: {
658: JTextComponent t = getTextComponent(event);
659: if (t != null)
660: {
661: try
662: {
663: int pos = t.getSelectionStart();
664: int len = t.getSelectionEnd() - pos;
665:
666: if (len > 0)
667: t.getDocument().remove(pos, len);
668: else if (pos > 0)
669: {
670: pos--;
671: t.getDocument().remove(pos, 1);
672: Caret c = t.getCaret();
673: c.setDot(pos);
674: c.setMagicCaretPosition(t.modelToView(pos).getLocation());
675: }
676: }
677: catch (BadLocationException e)
678: {
679:
680: }
681: }
682: }
683: }
684:
685: static class DeleteNextCharAction
686: extends TextAction
687: {
688: DeleteNextCharAction()
689: {
690: super(deleteNextCharAction);
691: }
692:
693: public void actionPerformed(ActionEvent event)
694: {
695: JTextComponent t = getTextComponent(event);
696: if (t != null)
697: {
698: try
699: {
700: int pos = t.getSelectionStart();
701: int len = t.getSelectionEnd() - pos;
702:
703: if (len > 0)
704: t.getDocument().remove(pos, len);
705: else if (pos < t.getDocument().getLength())
706: t.getDocument().remove(pos, 1);
707:
708: Caret c = t.getCaret();
709: c.setDot(pos);
710: c.setMagicCaretPosition(t.modelToView(pos).getLocation());
711: }
712: catch (BadLocationException e)
713: {
714:
715: }
716: }
717: }
718: }
719:
720: static class EndLineAction
721: extends TextAction
722: {
723: EndLineAction()
724: {
725: super(endLineAction);
726: }
727:
728: public void actionPerformed(ActionEvent event)
729: {
730: JTextComponent t = getTextComponent(event);
731: if (t != null)
732: {
733: try
734: {
735: int offs = Utilities.getRowEnd(t, t.getCaretPosition());
736: if (offs > -1)
737: {
738: Caret c = t.getCaret();
739: c.setDot(offs);
740: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
741: }
742: }
743: catch (BadLocationException ble)
744: {
745:
746: }
747: }
748: }
749: }
750:
751: static class BeginLineAction
752: extends TextAction
753: {
754: BeginLineAction()
755: {
756: super(beginLineAction);
757: }
758:
759: public void actionPerformed(ActionEvent event)
760: {
761: JTextComponent t = getTextComponent(event);
762: if (t != null)
763: {
764: try
765: {
766: int offs = Utilities.getRowStart(t, t.getCaretPosition());
767: if (offs > -1)
768: {
769: Caret c = t.getCaret();
770: c.setDot(offs);
771: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
772: }
773: }
774: catch (BadLocationException ble)
775: {
776:
777: }
778: }
779: }
780: }
781:
782: static class BeginAction extends TextAction
783: {
784:
785: BeginAction()
786: {
787: super(beginAction);
788: }
789:
790: public void actionPerformed(ActionEvent event)
791: {
792: JTextComponent t = getTextComponent(event);
793: if (t != null)
794: {
795: Caret c = t.getCaret();
796: c.setDot(0);
797: try
798: {
799: c.setMagicCaretPosition(t.modelToView(0).getLocation());
800: }
801: catch(BadLocationException ble)
802: {
803:
804: }
805: }
806: }
807: }
808:
809: static class EndAction extends TextAction
810: {
811:
812: EndAction()
813: {
814: super(endAction);
815: }
816:
817: public void actionPerformed(ActionEvent event)
818: {
819: JTextComponent t = getTextComponent(event);
820: if (t != null)
821: {
822: int offs = t.getDocument().getLength();
823: Caret c = t.getCaret();
824: c.setDot(offs);
825: try
826: {
827: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
828: }
829: catch(BadLocationException ble)
830: {
831:
832: }
833: }
834: }
835: }
836:
837:
842: public static class BeepAction extends TextAction
843: {
844:
847: public BeepAction()
848: {
849: super(beepAction);
850: }
851:
852:
857: public void actionPerformed(ActionEvent event)
858: {
859: Toolkit.getDefaultToolkit().beep();
860: }
861: }
862:
863:
870: public static class CopyAction extends TextAction
871: {
872:
873:
876: public CopyAction()
877: {
878: super(copyAction);
879: }
880:
881:
886: public void actionPerformed(ActionEvent event)
887: {
888: JTextComponent target = getTextComponent(event);
889: if (target != null)
890: target.copy();
891: }
892: }
893:
894:
895:
903: public static class CutAction extends TextAction
904: {
905:
906:
909: public CutAction()
910: {
911: super(cutAction);
912: }
913:
914:
919: public void actionPerformed(ActionEvent event)
920: {
921: JTextComponent target = getTextComponent(event);
922: if (target != null)
923: target.cut();
924: }
925: }
926:
927:
934: public static class PasteAction extends TextAction
935: {
936:
937:
940: public PasteAction()
941: {
942: super(pasteAction);
943: }
944:
945: