1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54: import ;
55:
56:
64: public class DefaultEditorKit extends EditorKit
65: {
66: static class SelectionPreviousWordAction
67: extends TextAction
68: {
69: SelectionPreviousWordAction()
70: {
71: super(selectionPreviousWordAction);
72: }
73:
74: public void actionPerformed(ActionEvent event)
75: {
76: try
77: {
78: JTextComponent t = getTextComponent(event);
79:
80: if (t != null)
81: {
82: int offs = Utilities.getPreviousWord(t, t.getCaretPosition());
83:
84: Caret c = t.getCaret();
85: c.moveDot(offs);
86: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
87: }
88: }
89: catch(BadLocationException ble)
90: {
91:
92: }
93: }
94: }
95:
96: static class SelectionNextWordAction
97: extends TextAction
98: {
99: SelectionNextWordAction()
100: {
101: super(selectionNextWordAction);
102: }
103:
104: public void actionPerformed(ActionEvent event)
105: {
106: try
107: {
108: JTextComponent t = getTextComponent(event);
109:
110: if (t != null)
111: {
112: int offs = Utilities.getNextWord(t, t.getCaretPosition());
113:
114: Caret c = t.getCaret();
115: c.moveDot(offs);
116: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
117: }
118: }
119: catch(BadLocationException ble)
120: {
121:
122: }
123: }
124: }
125:
126: static class PreviousWordAction
127: extends TextAction
128: {
129: PreviousWordAction()
130: {
131: super(previousWordAction);
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.getPreviousWord(t, t.getCaretPosition());
143:
144: Caret c = t.getCaret();
145: c.setDot(offs);
146: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
147: }
148: }
149: catch(BadLocationException ble)
150: {
151:
152: }
153: }
154: }
155:
156: static class NextWordAction
157: extends TextAction
158: {
159: NextWordAction()
160: {
161: super(nextWordAction);
162: }
163:
164: public void actionPerformed(ActionEvent event)
165: {
166: try
167: {
168: JTextComponent t = getTextComponent(event);
169:
170: if (t != null)
171: {
172: int offs = Utilities.getNextWord(t, t.getCaretPosition());
173:
174: Caret c = t.getCaret();
175: c.setDot(offs);
176: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
177: }
178: }
179: catch(BadLocationException ble)
180: {
181:
182: }
183: }
184: }
185:
186: static class SelectAllAction
187: extends TextAction
188: {
189: SelectAllAction()
190: {
191: super(selectAllAction);
192: }
193:
194: public void actionPerformed(ActionEvent event)
195: {
196: JTextComponent t = getTextComponent(event);
197: int offs = t.getDocument().getLength();
198: Caret c = t.getCaret();
199: c.setDot(0);
200: c.moveDot(offs);
201:
202: try
203: {
204: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
205: }
206: catch(BadLocationException ble)
207: {
208:
209: }
210: }
211: }
212:
213: static class SelectionBeginAction
214: extends TextAction
215: {
216: SelectionBeginAction()
217: {
218: super(selectionBeginAction);
219: }
220:
221: public void actionPerformed(ActionEvent event)
222: {
223: JTextComponent t = getTextComponent(event);
224: Caret c = t.getCaret();
225: c.moveDot(0);
226: try
227: {
228: c.setMagicCaretPosition(t.modelToView(0).getLocation());
229: }
230: catch(BadLocationException ble)
231: {
232:
233: }
234: }
235: }
236:
237: static class SelectionEndAction
238: extends TextAction
239: {
240: SelectionEndAction()
241: {
242: super(selectionEndAction);
243: }
244:
245: public void actionPerformed(ActionEvent event)
246: {
247: JTextComponent t = getTextComponent(event);
248: int offs = t.getDocument().getLength();
249: Caret c = t.getCaret();
250: c.moveDot(offs);
251: try
252: {
253: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
254: }
255: catch(BadLocationException ble)
256: {
257:
258: }
259: }
260: }
261:
262: static class SelectionEndLineAction
263: extends TextAction
264: {
265: SelectionEndLineAction()
266: {
267: super(selectionEndLineAction);
268: }
269:
270: public void actionPerformed(ActionEvent event)
271: {
272: JTextComponent t = getTextComponent(event);
273: try
274: {
275: Point p = t.modelToView(t.getCaret().getDot()).getLocation();
276: int cur = t.getCaretPosition();
277: int y = p.y;
278: int length = t.getDocument().getLength();
279: while (y == p.y && cur < length)
280: y = t.modelToView(++cur).getLocation().y;
281: if (cur != length)
282: cur--;
283:
284: Caret c = t.getCaret();
285: c.moveDot(cur);
286: c.setMagicCaretPosition(t.modelToView(cur).getLocation());
287: }
288: catch (BadLocationException ble)
289: {
290:
291: }
292: }
293: }
294:
295: static class SelectionBeginLineAction
296: extends TextAction
297: {
298: SelectionBeginLineAction()
299: {
300: super(selectionBeginLineAction);
301: }
302:
303: public void actionPerformed(ActionEvent event)
304: {
305: JTextComponent t = getTextComponent(event);
306:
307: try
308: {
309:
310:
311: Point p = t.modelToView(t.getCaret().getDot()).getLocation();
312:
313: int cur = t.getCaretPosition();
314: int y = p.y;
315:
316: while (y == p.y && cur > 0)
317: y = t.modelToView(--cur).getLocation().y;
318: if (cur != 0)
319: cur++;
320:
321: Caret c = t.getCaret();
322: c.moveDot(cur);
323: c.setMagicCaretPosition(t.modelToView(cur).getLocation());
324: }
325: catch (BadLocationException ble)
326: {
327:
328: }
329: }
330: }
331:
332: static class SelectionDownAction
333: extends TextAction
334: {
335: SelectionDownAction()
336: {
337: super(selectionDownAction);
338: }
339:
340: public void actionPerformed(ActionEvent event)
341: {
342: JTextComponent t = getTextComponent(event);
343: try
344: {
345: if (t != null)
346: {
347: Caret c = t.getCaret();
348:
349:
350: Point mcp = c.getMagicCaretPosition();
351: int x = (mcp != null) ? mcp.x : 0;
352: int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x);
353:
354: if (pos > -1)
355: t.moveCaretPosition(pos);
356: }
357: }
358: catch(BadLocationException ble)
359: {
360:
361: }
362: }
363: }
364:
365: static class SelectionUpAction
366: extends TextAction
367: {
368: SelectionUpAction()
369: {
370: super(selectionUpAction);
371: }
372:
373: public void actionPerformed(ActionEvent event)
374: {
375: JTextComponent t = getTextComponent(event);
376: try
377: {
378: if (t != null)
379: {
380: Caret c = t.getCaret();
381:
382:
383: Point mcp = c.getMagicCaretPosition();
384: int x = (mcp != null) ? mcp.x : 0;
385: int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x);
386:
387: if (pos > -1)
388: t.moveCaretPosition(pos);
389: }
390: }
391: catch(BadLocationException ble)
392: {
393:
394: }
395: }
396: }
397:
398: static class SelectionForwardAction
399: extends TextAction
400: {
401: SelectionForwardAction()
402: {
403: super(selectionForwardAction);
404: }
405:
406: public void actionPerformed(ActionEvent event)
407: {
408: JTextComponent t = getTextComponent(event);
409: if (t != null)
410: {
411: int offs = t.getCaretPosition() + 1;
412:
413: if(offs <= t.getDocument().getLength())
414: {
415: Caret c = t.getCaret();
416: c.moveDot(offs);
417: try
418: {
419: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
420: }
421: catch(BadLocationException ble)
422: {
423:
424: }
425: }
426: }
427: }
428: }
429:
430: static class SelectionBackwardAction
431: extends TextAction
432: {
433: SelectionBackwardAction()
434: {
435: super(selectionBackwardAction);
436: }
437:
438: public void actionPerformed(ActionEvent event)
439: {
440: JTextComponent t = getTextComponent(event);
441: if (t != null)
442: {
443: int offs = t.getCaretPosition() - 1;
444:
445: if(offs >= 0)
446: {
447: Caret c = t.getCaret();
448: c.moveDot(offs);
449: try
450: {
451: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
452: }
453: catch(BadLocationException ble)
454: {
455:
456: }
457: }
458: }
459: }
460: }
461:
462: static class DownAction
463: extends TextAction
464: {
465: DownAction()
466: {
467: super(downAction);
468: }
469:
470: public void actionPerformed(ActionEvent event)
471: {
472: JTextComponent t = getTextComponent(event);
473: try
474: {
475: if (t != null)
476: {
477: Caret c = t.getCaret();
478:
479:
480: Point mcp = c.getMagicCaretPosition();
481: int x = (mcp != null) ? mcp.x : 0;
482: int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x);
483:
484: if (pos > -1)
485: t.setCaretPosition(pos);
486: }
487: }
488: catch(BadLocationException ble)
489: {
490:
491: }
492: }
493: }
494:
495: static class UpAction
496: extends TextAction
497: {
498: UpAction()
499: {
500: super(upAction);
501: }
502:
503: public void actionPerformed(ActionEvent event)
504: {
505: JTextComponent t = getTextComponent(event);
506: try
507: {
508: if (t != null)
509: {
510: Caret c = t.getCaret();
511:
512:
513: Point mcp = c.getMagicCaretPosition();
514: int x = (mcp != null) ? mcp.x : 0;
515: int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x);
516:
517: if (pos > -1)
518: t.setCaretPosition(pos);
519: }
520: }
521: catch(BadLocationException ble)
522: {
523:
524: }
525: }
526: }
527:
528: static class ForwardAction
529: extends TextAction
530: {
531: ForwardAction()
532: {
533: super(forwardAction);
534: }
535:
536: public void actionPerformed(ActionEvent event)
537: {
538: JTextComponent t = getTextComponent(event);
539: if (t != null)
540: {
541: int offs = t.getCaretPosition() + 1;
542: if (offs <= t.getDocument().getLength())
543: {
544: Caret c = t.getCaret();
545: c.setDot(offs);
546:
547: try
548: {
549: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
550: }
551: catch (BadLocationException ble)
552: {
553:
554: }
555: }
556: }
557:
558: }
559: }
560:
561: static class BackwardAction
562: extends TextAction
563: {
564: BackwardAction()
565: {
566: super(backwardAction);
567: }
568:
569: public void actionPerformed(ActionEvent event)
570: {
571: JTextComponent t = getTextComponent(event);
572: if (t != null)
573: {
574: int offs = t.getCaretPosition() - 1;
575: if (offs >= 0)
576: {
577: Caret c = t.getCaret();
578: c.setDot(offs);
579:
580: try
581: {
582: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
583: }
584: catch (BadLocationException ble)
585: {
586:
587: }
588: }
589: }
590: }
591: }
592:
593: static class DeletePrevCharAction
594: extends TextAction
595: {
596: DeletePrevCharAction()
597: {
598: super(deletePrevCharAction);
599: }
600:
601: public void actionPerformed(ActionEvent event)
602: {
603: JTextComponent t = getTextComponent(event);
604: if (t != null)
605: {
606: try
607: {
608: int pos = t.getSelectionStart();
609: int len = t.getSelectionEnd() - pos;
610:
611: if (len > 0)
612: t.getDocument().remove(pos, len);
613: else if (pos > 0)
614: {
615: pos--;
616: t.getDocument().remove(pos, 1);
617: Caret c = t.getCaret();
618: c.setDot(pos);
619: c.setMagicCaretPosition(t.modelToView(pos).getLocation());
620: }
621: }
622: catch (BadLocationException e)
623: {
624:
625: }
626: }
627: }
628: }
629:
630: static class DeleteNextCharAction
631: extends TextAction
632: {
633: DeleteNextCharAction()
634: {
635: super(deleteNextCharAction);
636: }
637:
638: public void actionPerformed(ActionEvent event)
639: {
640: JTextComponent t = getTextComponent(event);
641: if (t != null)
642: {
643: try
644: {
645: int pos = t.getSelectionStart();
646: int len = t.getSelectionEnd() - pos;
647:
648: if (len > 0)
649: t.getDocument().remove(pos, len);
650: else if (pos < t.getDocument().getLength())
651: t.getDocument().remove(pos, 1);
652:
653: Caret c = t.getCaret();
654: c.setDot(pos);
655: c.setMagicCaretPosition(t.modelToView(pos).getLocation());
656: }
657: catch (BadLocationException e)
658: {
659:
660: }
661: }
662: }
663: }
664:
665: static class EndLineAction
666: extends TextAction
667: {
668: EndLineAction()
669: {
670: super(endLineAction);
671: }
672:
673: public void actionPerformed(ActionEvent event)
674: {
675: JTextComponent t = getTextComponent(event);
676: try
677: {
678: int offs = Utilities.getRowEnd(t, t.getCaretPosition());
679:
680: if (offs > -1)
681: {
682: Caret c = t.getCaret();
683: c.setDot(offs);
684: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
685: }
686: }
687: catch (BadLocationException ble)
688: {
689:
690: }
691: }
692: }
693:
694: static class BeginLineAction
695: extends TextAction
696: {
697: BeginLineAction()
698: {
699: super(beginLineAction);
700: }
701:
702: public void actionPerformed(ActionEvent event)
703: {
704: JTextComponent t = getTextComponent(event);
705: try
706: {
707: int offs = Utilities.getRowStart(t, t.getCaretPosition());
708:
709: if (offs > -1)
710: {
711: Caret c = t.getCaret();
712: c.setDot(offs);
713: c.setMagicCaretPosition(t.modelToView(offs).getLocation());
714: }
715: }
716: catch (BadLocationException ble)
717: {
718:
719: }
720: }
721: }
722:
723:
728: public static class BeepAction extends TextAction
729: {
730:
733: public BeepAction()
734: {
735: super(beepAction);
736: }
737:
738:
743: public void actionPerformed(ActionEvent event)
744: {
745: Toolkit.getDefaultToolkit().beep();
746: }
747: }
748:
749:
756: public static class CopyAction extends TextAction
757: {
758:
759:
762: public CopyAction()
763: {
764: super(copyAction);
765: }
766:
767:
772: public void actionPerformed(ActionEvent event)
773: {
774: getTextComponent(event).copy();
775: }
776: }
777:
778:
779:
787: public static class CutAction extends TextAction
788: {
789:
790:
793: public CutAction()
794: {
795: super(cutAction);
796: }
797:
798:
803: public void actionPerformed(ActionEvent event)
804: {
805: getTextComponent(event).cut();
806: }
807: }
808:
809:
816: public static class PasteAction extends TextAction
817: {
818:
819:
822: public PasteAction()
823: {
824: super(pasteAction);
825: }
826:
827:
832: public void actionPerformed(ActionEvent event)
833: {
834: getTextComponent(event).paste();
835: }
836: }
837:
838:
848: public static class DefaultKeyTypedAction
849: extends TextAction
850: {
851:
852:
855: public DefaultKeyTypedAction()
856: {
857: super(defaultKeyTypedAction);
858: }
859:
860:
865: public void actionPerformed(ActionEvent event)
866: {
867:
868:
869:
870: char c = event.getActionCommand().charAt(0);
871: if (Character.isISOControl(c))
872: return;
873:
874: JTextComponent t = getTextComponent(event);
875: if (t != null && t.isEnabled() && t.isEditable())
876: t.replaceSelection(event.getActionCommand());
877: }
878: }
879:
880:
885: public static class InsertBreakAction extends TextAction
886: {
887:
888:
891: public InsertBreakAction()
892: {
893: super(insertBreakAction);
894: }
895:
896:
901: public void actionPerformed(ActionEvent event)
902: {
903: JTextComponent t = getTextComponent(event);
904: t.replaceSelection("\n");
905: }
906: }
907:
908:
912:
913:
914: public static class InsertContentAction extends TextAction
915: {
916:
917:
920: public InsertContentAction()
921: {
922: super(insertContentAction);
923: }
924:
925:
930: public void actionPerformed(ActionEvent event)
931: {
932:
933:
934: }
935: }
936:
937:
940: public static class InsertTabAction extends TextAction
941: {
942:
943:
946: public InsertTabAction()
947: {
948: super(insertTabAction);
949: }
950:
951:
956: public void actionPerformed(ActionEvent event)
957: {
958: JTextComponent t = getTextComponent(event);
959: t.replaceSelection("\t");
960: }
961: }
962:
963:
966: private static final long serialVersionUID = 9017245433028523428L;
967:
968:
974: public static final String backwardAction = "caret-backward";
975:
976:
981: public static final String beepAction = "beep";
982:
983:
989: public static final String beginAction = "caret-begin";
990:
991:
997: public static final String beginLineAction = "caret-begin-line";
998:
999:
1005: public static final String beginParagraphAction = "caret-begin-paragraph";
1006:
1007:
1013: public static final String beginWordAction = "caret-begin-word";
1014:
1015:
1021: public static final String copyAction = "copy-to-clipboard";
1022:
1023:
1029: public static final String cutAction = "cut-to-clipboard";
1030:
1031:
1037: public static final String defaultKeyTypedAction = "default-typed";
1038:
1039:
1045: public static final String deleteNextCharAction = "delete-next";
1046:
1047:
1053: public static final String deletePrevCharAction = "delete-previous";
1054:
1055:
1060: public static final String downAction = "caret-down";
1061:
1062:
1068: public static final String endAction = "caret-end";
1069:
1070:
1076: public static final String endLineAction = "caret-end-line";
1077:
1078:
1082: public static final String EndOfLineStringProperty = "__EndOfLine__";
1083:
1084:
1090: public static final String endParagraphAction = "caret-end-paragraph";
1091:
1092:
1098: public static final String endWordAction = "caret-end-word";
1099:
1100:
1106: public static final String forwardAction = "caret-forward";
1107:
1108:
1113: public static final String insertBreakAction = "insert-break";
1114:
1115:
1120: public static final String insertContentAction = "insert-content";
1121:
1122:
1127: public static final String insertTabAction = "insert-tab";
1128:
1129:
1135: public static final String nextWordAction = "caret-next-word";
1136:
1137:
1142: public static final String pageDownAction = "page-down";
1143:
1144:
1149: public static final String pageUpAction = "page-up";
1150:
1151:
1157: public static final String pasteAction = "paste-from-clipboard";
1158:
1159:
1165: public static final String previousWordAction = "caret-previous-word";
1166:
1167:
1173: public static final String readOnlyAction = "set-read-only";
1174:
1175:
1180: public static final String selectAllAction = "select-all";
1181:
1182:
1188: public static final String selectionBackwardAction = "selection-backward";
1189:
1190:
1196: public static final String selectionBeginAction = "selection-begin";
1197:
1198:
1204: public static final String selectionBeginLineAction = "selection-begin-line";
1205:
1206:
1212: public static final String selectionBeginParagraphAction =
1213: "selection-begin-paragraph";
1214:
1215:
1221: public static final String selectionBeginWordAction = "selection-begin-word";
1222:
1223:
1229: public static final String selectionDownAction = "selection-down";
1230:
1231:
1237: public static final String selectionEndAction = "selection-end";
1238:
1239:
1245: public static final String selectionEndLineAction = "selection-end-line";
1246:
1247:
1253: public static final String selectionEndParagraphAction =
1254: "selection-end-paragraph";
1255:
1256:
1262: public static final String selectionEndWordAction = "selection-end-word";
1263:
1264:
1270: public static final String selectionForwardAction = "selection-forward";
1271:
1272:
1278: public static final String selectionNextWordAction = "selection-next-word";
1279:
1280:
1286: public static final String selectionPreviousWordAction =
1287: "selection-previous-word";
1288:
1289:
1295: public static final String selectionUpAction = "selection-up";
1296:
1297:
1303: public static final String selectLineAction = "select-line";
1304:
1305:
1311: public static final String selectParagraphAction = "select-paragraph";
1312:
1313:
1319: public static final String selectWordAction = "select-word";
1320:
1321:
1326: public static final String upAction = "caret-up";
1327:
1328:
1334: public static final String writableAction = "set-writable";
1335:
1336:
1339: public DefaultEditorKit()
1340: {
1341:
1342: }
1343:
1344:
1348:
1349:
1350: private static Action[] defaultActions =
1351: new Action[] {
1352:
1353: new BeepAction(),
1354: new CopyAction(),
1355: new CutAction(),
1356: new DefaultKeyTypedAction(),
1357: new InsertBreakAction(),
1358: new InsertContentAction(),
1359: new InsertTabAction(),
1360: new PasteAction(),
1361:
1362:
1363: new DeleteNextCharAction(),
1364: new DeletePrevCharAction(),
1365:
1366: new BeginLineAction(),
1367: new SelectionBeginLineAction(),
1368:
1369: new EndLineAction(),
1370: new SelectionEndLineAction(),
1371:
1372: new BackwardAction(),
1373: new SelectionBackwardAction(),
1374:
1375: new ForwardAction(),
1376: new SelectionForwardAction(),
1377:
1378: new UpAction(),
1379: new SelectionUpAction(),
1380:
1381: new DownAction(),
1382: new SelectionDownAction(),
1383:
1384: new NextWordAction(),
1385: new SelectionNextWordAction(),
1386:
1387: new PreviousWordAction(),
1388: new SelectionPreviousWordAction(),
1389:
1390: new SelectionBeginAction(),
1391: new SelectionEndAction(),
1392: new SelectAllAction(),
1393: };
1394:
1395:
1401: public Caret createCaret()
1402: {
1403: return new DefaultCaret();
1404: }
1405:
1406:
1413: public Document createDefaultDocument()
1414: {
1415: return new PlainDocument();
1416: }
1417:
1418:
1423: public Action[] getActions()
1424: {
1425: return defaultActions;
1426: }
1427:
1428:
1435: public String getContentType()
1436: {
1437: return "text/plain";
1438: }
1439:
1440:
1450: public ViewFactory getViewFactory()
1451: {
1452: return null;
1453: }
1454:
1455:
1468: public void read(InputStream in, Document document, int offset)
1469: throws BadLocationException, IOException
1470: {
1471: read(new InputStreamReader(in), document, offset);
1472: }
1473:
1474:
1487: public void read(Reader in, Document document, int offset)
1488: throws BadLocationException, IOException
1489: {
1490: BufferedReader reader = new BufferedReader(in);
1491:
1492: String line;
1493: StringBuffer content = new StringBuffer();
1494:
1495: while ((line = reader.readLine()) != null)
1496: {
1497: content.append(line);
1498: content.append("\n");
1499: }
1500:
1501: document.insertString(offset, content.substring(0, content.length() - 1),
1502: SimpleAttributeSet.EMPTY);
1503: }
1504:
1505:
1521: public void write(OutputStream out, Document document, int offset, int len)
1522: throws BadLocationException, IOException
1523: {
1524: write(new OutputStreamWriter(out), document, offset, len);
1525: }
1526:
1527:
1542: public void write(Writer out, Document document, int offset, int len)
1543: throws BadLocationException, IOException
1544: {
1545:
1546: if (offset < 0 || offset > document.getLength())
1547: throw new BadLocationException("Tried to write to invalid location",
1548: offset);
1549:
1550:
1551: if (offset + len > document.getLength())
1552: len = document.getLength() - offset;
1553:
1554: out.write(document.getText(offset, len));
1555: }
1556: }