1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62:
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84: import ;
85: import ;
86:
87: public abstract class JTextComponent extends JComponent
88: implements Scrollable, Accessible
89: {
90:
93:
94: public class AccessibleJTextComponent extends AccessibleJComponent
95: implements AccessibleText, CaretListener, DocumentListener
96: {
97: private static final long serialVersionUID = 7664188944091413696L;
98:
99:
102: public AccessibleJTextComponent()
103: {
104:
105: }
106:
107:
111: public int getCaretPosition()
112: {
113: return 0;
114: }
115:
116:
120: public String getSelectedText()
121: {
122: return null;
123: }
124:
125:
129: public int getSelectionStart()
130: {
131: return 0;
132: }
133:
134:
138: public int getSelectionEnd()
139: {
140: return 0;
141: }
142:
143:
147: public void caretUpdate(CaretEvent value0)
148: {
149:
150: }
151:
152:
156: public AccessibleStateSet getAccessibleStateSet()
157: {
158: return null;
159: }
160:
161:
165: public AccessibleRole getAccessibleRole()
166: {
167: return null;
168: }
169:
170:
174: public AccessibleText getAccessibleText()
175: {
176: return null;
177: }
178:
179:
183: public void insertUpdate(DocumentEvent value0)
184: {
185:
186: }
187:
188:
192: public void removeUpdate(DocumentEvent value0)
193: {
194:
195: }
196:
197:
201: public void changedUpdate(DocumentEvent value0)
202: {
203:
204: }
205:
206:
211: public int getIndexAtPoint(Point value0)
212: {
213: return 0;
214: }
215:
216:
220: Rectangle getRootEditorRect()
221: {
222: return null;
223: }
224:
225:
230: public Rectangle getCharacterBounds(int value0)
231: {
232: return null;
233: }
234:
235:
239: public int getCharCount()
240: {
241: return 0;
242: }
243:
244:
249: public AttributeSet getCharacterAttribute(int value0)
250: {
251: return null;
252: }
253:
254:
260: public String getAtIndex(int value0, int value1)
261: {
262: return null;
263: }
264:
265:
271: public String getAfterIndex(int value0, int value1)
272: {
273: return null;
274: }
275:
276:
282: public String getBeforeIndex(int value0, int value1)
283: {
284: return null;
285: }
286: }
287:
288: public static class KeyBinding
289: {
290: public KeyStroke key;
291: public String actionName;
292:
293:
299: public KeyBinding(KeyStroke key, String actionName)
300: {
301: this.key = key;
302: this.actionName = actionName;
303: }
304: }
305:
306:
309: private class CaretBlinkTimer extends Timer implements ActionListener
310: {
311:
314: public CaretBlinkTimer()
315: {
316: super(1000, null);
317: addActionListener(this);
318: }
319:
320:
323: public void actionPerformed(ActionEvent ev)
324: {
325: Caret c = caret;
326: if (c != null)
327: c.setVisible(!c.isVisible());
328: }
329:
330:
333: public void update()
334: {
335: stop();
336: Caret c = caret;
337: if (c != null)
338: {
339: setDelay(c.getBlinkRate());
340: if (editable)
341: start();
342: else
343: c.setVisible(false);
344: }
345: }
346: }
347:
348:
378:
379: private class KeymapWrapper extends InputMap
380: {
381: Keymap map;
382:
383: public KeymapWrapper(Keymap k)
384: {
385: map = k;
386: }
387:
388: public int size()
389: {
390: return map.getBoundKeyStrokes().length + super.size();
391: }
392:
393: public Object get(KeyStroke ks)
394: {
395: Action mapped = null;
396: Keymap m = map;
397: while(mapped == null && m != null)
398: {
399: mapped = m.getAction(ks);
400: if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED)
401: mapped = m.getDefaultAction();
402: if (mapped == null)
403: m = m.getResolveParent();
404: }
405:
406: if (mapped == null)
407: return super.get(ks);
408: else
409: return mapped;
410: }
411:
412: public KeyStroke[] keys()
413: {
414: KeyStroke[] superKeys = super.keys();
415: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
416: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
417: for (int i = 0; i < superKeys.length; ++i)
418: bothKeys[i] = superKeys[i];
419: for (int i = 0; i < mapKeys.length; ++i)
420: bothKeys[i + superKeys.length] = mapKeys[i];
421: return bothKeys;
422: }
423:
424: public KeyStroke[] allKeys()
425: {
426: KeyStroke[] superKeys = super.allKeys();
427: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
428: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
429: for (int i = 0; i < superKeys.length; ++i)
430: bothKeys[i] = superKeys[i];
431: for (int i = 0; i < mapKeys.length; ++i)
432: bothKeys[i + superKeys.length] = mapKeys[i];
433: return bothKeys;
434: }
435: }
436:
437: private class KeymapActionMap extends ActionMap
438: {
439: Keymap map;
440:
441: public KeymapActionMap(Keymap k)
442: {
443: map = k;
444: }
445:
446: public Action get(Object cmd)
447: {
448: if (cmd instanceof Action)
449: return (Action) cmd;
450: else
451: return super.get(cmd);
452: }
453:
454: public int size()
455: {
456: return map.getBoundKeyStrokes().length + super.size();
457: }
458:
459: public Object[] keys()
460: {
461: Object[] superKeys = super.keys();
462: Object[] mapKeys = map.getBoundKeyStrokes();
463: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
464: for (int i = 0; i < superKeys.length; ++i)
465: bothKeys[i] = superKeys[i];
466: for (int i = 0; i < mapKeys.length; ++i)
467: bothKeys[i + superKeys.length] = mapKeys[i];
468: return bothKeys;
469: }
470:
471: public Object[] allKeys()
472: {
473: Object[] superKeys = super.allKeys();
474: Object[] mapKeys = map.getBoundKeyStrokes();
475: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
476: for (int i = 0; i < superKeys.length; ++i)
477: bothKeys[i] = superKeys[i];
478: for (int i = 0; i < mapKeys.length; ++i)
479: bothKeys[i + superKeys.length] = mapKeys[i];
480: return bothKeys;
481: }
482:
483: }
484:
485: static class DefaultKeymap implements Keymap
486: {
487: String name;
488: Keymap parent;
489: Hashtable map;
490: Action defaultAction;
491:
492: public DefaultKeymap(String name)
493: {
494: this.name = name;
495: this.map = new Hashtable();
496: }
497:
498: public void addActionForKeyStroke(KeyStroke key, Action a)
499: {
500: map.put(key, a);
501: }
502:
503:
512: public Action getAction(KeyStroke key)
513: {
514: if (map.containsKey(key))
515: return (Action) map.get(key);
516: else if (parent != null)
517: return parent.getAction(key);
518: else
519: return null;
520: }
521:
522: public Action[] getBoundActions()
523: {
524: Action [] ret = new Action[map.size()];
525: Enumeration e = map.elements();
526: int i = 0;
527: while (e.hasMoreElements())
528: {
529: ret[i++] = (Action) e.nextElement();
530: }
531: return ret;
532: }
533:
534: public KeyStroke[] getBoundKeyStrokes()
535: {
536: KeyStroke [] ret = new KeyStroke[map.size()];
537: Enumeration e = map.keys();
538: int i = 0;
539: while (e.hasMoreElements())
540: {
541: ret[i++] = (KeyStroke) e.nextElement();
542: }
543: return ret;
544: }
545:
546: public Action getDefaultAction()
547: {
548: return defaultAction;
549: }
550:
551: public KeyStroke[] getKeyStrokesForAction(Action a)
552: {
553: int i = 0;
554: Enumeration e = map.keys();
555: while (e.hasMoreElements())
556: {
557: if (map.get(e.nextElement()).equals(a))
558: ++i;
559: }
560: KeyStroke [] ret = new KeyStroke[i];
561: i = 0;
562: e = map.keys();
563: while (e.hasMoreElements())
564: {
565: KeyStroke k = (KeyStroke) e.nextElement();
566: if (map.get(k).equals(a))
567: ret[i++] = k;
568: }
569: return ret;
570: }
571:
572: public String getName()
573: {
574: return name;
575: }
576:
577: public Keymap getResolveParent()
578: {
579: return parent;
580: }
581:
582: public boolean isLocallyDefined(KeyStroke key)
583: {
584: return map.containsKey(key);
585: }
586:
587: public void removeBindings()
588: {
589: map.clear();
590: }
591:
592: public void removeKeyStrokeBinding(KeyStroke key)
593: {
594: map.remove(key);
595: }
596:
597: public void setDefaultAction(Action a)
598: {
599: defaultAction = a;
600: }
601:
602: public void setResolveParent(Keymap p)
603: {
604: parent = p;
605: }
606: }
607:
608: class DefaultTransferHandler extends TransferHandler
609: {
610: public boolean canImport(JComponent component, DataFlavor[] flavors)
611: {
612: JTextComponent textComponent = (JTextComponent) component;
613:
614: if (! (textComponent.isEnabled()
615: && textComponent.isEditable()
616: && flavors != null))
617: return false;
618:
619: for (int i = 0; i < flavors.length; ++i)
620: if (flavors[i].equals(DataFlavor.stringFlavor))
621: return true;
622:
623: return false;
624: }
625:
626: public void exportToClipboard(JComponent component, Clipboard clipboard,
627: int action)
628: {
629: JTextComponent textComponent = (JTextComponent) component;
630: int start = textComponent.getSelectionStart();
631: int end = textComponent.getSelectionEnd();
632:
633: if (start == end)
634: return;
635:
636: try
637: {
638:
639: String data = textComponent.getDocument().getText(start, end);
640: StringSelection selection = new StringSelection(data);
641: clipboard.setContents(selection, null);
642:
643:
644: if (action == MOVE)
645: doc.remove(start, end - start);
646: }
647: catch (BadLocationException e)
648: {
649:
650: }
651: }
652:
653: public int getSourceActions()
654: {
655: return NONE;
656: }
657:
658: public boolean importData(JComponent component, Transferable transferable)
659: {
660: DataFlavor flavor = null;
661: DataFlavor[] flavors = transferable.getTransferDataFlavors();
662:
663: if (flavors == null)
664: return false;
665:
666: for (int i = 0; i < flavors.length; ++i)
667: if (flavors[i].equals(DataFlavor.stringFlavor))
668: flavor = flavors[i];
669:
670: if (flavor == null)
671: return false;
672:
673: try
674: {
675: JTextComponent textComponent = (JTextComponent) component;
676: String data = (String) transferable.getTransferData(flavor);
677: textComponent.replaceSelection(data);
678: return true;
679: }
680: catch (IOException e)
681: {
682:
683: }
684: catch (UnsupportedFlavorException e)
685: {
686:
687: }
688:
689: return false;
690: }
691: }
692:
693: private static final long serialVersionUID = -8796518220218978795L;
694:
695: public static final String DEFAULT_KEYMAP = "default";
696: public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
697:
698: private static DefaultTransferHandler defaultTransferHandler;
699: private static Hashtable keymaps = new Hashtable();
700: private Keymap keymap;
701: private char focusAccelerator = '\0';
702: private NavigationFilter navigationFilter;
703:
704: private CaretBlinkTimer caretBlinkTimer;
705:
706:
718: public static Keymap getKeymap(String n)
719: {
720: return (Keymap) keymaps.get(n);
721: }
722:
723:
734: public static Keymap removeKeymap(String n)
735: {
736: Keymap km = (Keymap) keymaps.get(n);
737: keymaps.remove(n);
738: return km;
739: }
740:
741:
757: public static Keymap addKeymap(String n, Keymap parent)
758: {
759: Keymap k = new DefaultKeymap(n);
760: k.setResolveParent(parent);
761: if (n != null)
762: keymaps.put(n, k);
763: return k;
764: }
765:
766:
774: public Keymap getKeymap()
775: {
776: return keymap;
777: }
778:
779:
788: public void setKeymap(Keymap k)
789: {
790:
791:
792:
793:
794:
795:
796:
797:
798:
799:
800:
801: KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k));
802: InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED);
803: if (childInputMap == null)
804: setInputMap(JComponent.WHEN_FOCUSED, kw);
805: else
806: {
807: while (childInputMap.getParent() != null
808: && !(childInputMap.getParent() instanceof KeymapWrapper)
809: && !(childInputMap.getParent() instanceof InputMapUIResource))
810: childInputMap = childInputMap.getParent();
811:
812:
813: if (childInputMap.getParent() == null)
814: childInputMap.setParent(kw);
815:
816:
817:
818: else if (childInputMap.getParent() instanceof KeymapWrapper)
819: {
820: if (kw == null)
821: childInputMap.setParent(childInputMap.getParent().getParent());
822: else
823: {
824: kw.setParent(childInputMap.getParent().getParent());
825: childInputMap.setParent(kw);
826: }
827: }
828:
829:
830:
831: else if (childInputMap.getParent() instanceof InputMapUIResource)
832: {
833: if (kw != null)
834: {
835: kw.setParent(childInputMap.getParent());
836: childInputMap.setParent(kw);
837: }
838: }
839: }
840:
841:
842:
843: KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k));
844: ActionMap childActionMap = getActionMap();
845: if (childActionMap == null)
846: setActionMap(kam);
847: else
848: {
849: while (childActionMap.getParent() != null
850: && !(childActionMap.getParent() instanceof KeymapActionMap)
851: && !(childActionMap.getParent() instanceof ActionMapUIResource))
852: childActionMap = childActionMap.getParent();
853:
854:
855: if (childActionMap.getParent() == null)
856: childActionMap.setParent(kam);
857:
858:
859:
860: else if (childActionMap.getParent() instanceof KeymapActionMap)
861: {
862: if (kam == null)
863: childActionMap.setParent(childActionMap.getParent().getParent());
864: else
865: {
866: kam.setParent(childActionMap.getParent().getParent());
867: childActionMap.setParent(kam);
868: }
869: }
870:
871:
872:
873: else if (childActionMap.getParent() instanceof ActionMapUIResource)
874: {
875: if (kam != null)
876: {
877: kam.setParent(childActionMap.getParent());
878: childActionMap.setParent(kam);
879: }
880: }
881: }
882:
883:
884:
885: Keymap old = keymap;
886: keymap = k;
887: firePropertyChange("keymap", old, k);
888: }
889:
890:
906: public static void loadKeymap(Keymap map,
907: JTextComponent.KeyBinding[] bindings,
908: Action[] actions)
909: {
910: Hashtable acts = new Hashtable(actions.length);
911: for (int i = 0; i < actions.length; ++i)
912: acts.put(actions[i].getValue(Action.NAME), actions[i]);
913: for (int i = 0; i < bindings.length; ++i)
914: if (acts.containsKey(bindings[i].actionName))
915: map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName));
916: }
917:
918:
931: public Action[] getActions()
932: {
933: return getUI().getEditorKit(this).getActions();
934: }
935:
936:
937: Document doc;
938: Caret caret;
939: boolean editable;
940:
941: private Highlighter highlighter;
942: private Color caretColor;
943: private Color disabledTextColor;
944: private Color selectedTextColor;
945: private Color selectionColor;
946: private Insets margin;
947: private boolean dragEnabled;
948:
949:
952: public JTextComponent()
953: {
954: Keymap defkeymap = getKeymap(DEFAULT_KEYMAP);
955: boolean creatingKeymap = false;
956: if (defkeymap == null)
957: {
958: defkeymap = addKeymap(DEFAULT_KEYMAP, null);
959: defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction());
960: creatingKeymap = true;
961: }
962:
963: caretBlinkTimer = new CaretBlinkTimer();
964:
965: setFocusable(true);
966: setEditable(true);
967: enableEvents(AWTEvent.KEY_EVENT_MASK);
968: updateUI();
969:
970:
971: if (creatingKeymap)
972: loadKeymap(defkeymap,
973: new KeyBinding[] {
974: new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),
975: DefaultEditorKit.backwardAction),
976: new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),
977: DefaultEditorKit.forwardAction),
978: new KeyBinding(KeyStroke.getKeyStroke("typed \b"),
979: DefaultEditorKit.deletePrevCharAction),
980: new KeyBinding(KeyStroke.getKeyStroke("typed \u007f"),
981: DefaultEditorKit.deleteNextCharAction)
982: },
983: getActions());
984: }
985:
986: public void setDocument(Document newDoc)
987: {
988: Document oldDoc = doc;
989: doc = newDoc;
990: firePropertyChange("document", oldDoc, newDoc);
991: revalidate();
992: repaint();
993: }
994:
995: public Document getDocument()
996: {
997: return doc;
998: }
999:
1000:
1005: public AccessibleContext getAccessibleContext()
1006: {
1007: return null;
1008: }
1009:
1010: public void setMargin(Insets m)
1011: {
1012: margin = m;
1013: }
1014:
1015: public Insets getMargin()
1016: {
1017: return margin;
1018: }
1019:
1020: public void setText(String text)
1021: {
1022: try
1023: {
1024: if (doc instanceof AbstractDocument)
1025: ((AbstractDocument) doc).replace(0, doc.getLength(), text, null);
1026: else
1027: {
1028: doc.remove(0, doc.getLength());
1029: doc.insertString(0, text, null);
1030: }
1031: }
1032: catch (BadLocationException e)
1033: {
1034:
1035: }
1036: }
1037:
1038:
1045: public String getText()
1046: {
1047: if (doc == null)
1048: return null;
1049:
1050: try
1051: {
1052: return doc.getText(0, doc.getLength());
1053: }
1054: catch (BadLocationException e)
1055: {
1056:
1057: return "";
1058: }
1059: }
1060:
1061:
1071: public String getText(int offset, int length)
1072: throws BadLocationException
1073: {
1074: return getDocument().getText(offset, length);
1075: }
1076:
1077:
1084: public String getSelectedText()
1085: {
1086: try
1087: {
1088: return doc.getText(getSelectionStart(), getSelectionEnd());
1089: }
1090: catch (BadLocationException e)
1091: {
1092:
1093: return null;
1094: }
1095: }
1096:
1097:
1103: public String getUIClassID()
1104: {
1105: return "TextComponentUI";
1106: }
1107:
1108:
1111: protected String paramString()
1112: {
1113:
1114: return super.paramString();
1115: }
1116:
1117:
1122: public TextUI getUI()
1123: {
1124: return (TextUI) ui;
1125: }
1126:
1127:
1132: public void setUI(TextUI newUI)
1133: {
1134: super.setUI(newUI);
1135: }
1136:
1137:
1141: public void updateUI()
1142: {
1143: setUI((TextUI) UIManager.getUI(this));
1144: }
1145:
1146: public Dimension getPreferredScrollableViewportSize()
1147: {
1148: return getPreferredSize();
1149: }
1150:
1151: public int getScrollableUnitIncrement(Rectangle visible, int orientation,
1152: int direction)
1153: {
1154:
1155: if (orientation == SwingConstants.HORIZONTAL)
1156: return visible.width / 10;
1157: else if (orientation == SwingConstants.VERTICAL)
1158: return visible.height / 10;
1159: else
1160: throw new IllegalArgumentException("orientation must be either "
1161: + "javax.swing.SwingConstants.VERTICAL "
1162: + "or "
1163: + "javax.swing.SwingConstants.HORIZONTAL"
1164: );
1165: }
1166:
1167: public int getScrollableBlockIncrement(Rectangle visible, int orientation,
1168: int direction)
1169: {
1170:
1171: if (orientation == SwingConstants.HORIZONTAL)
1172: return visible.width;
1173: else if (orientation == SwingConstants.VERTICAL)
1174: return visible.height;
1175: else
1176: throw new IllegalArgumentException("orientation must be either "
1177: + "javax.swing.SwingConstants.VERTICAL "
1178: + "or "
1179: + "javax.swing.SwingConstants.HORIZONTAL"
1180: );
1181: }
1182:
1183:
1188: public boolean isEditable()
1189: {
1190: return editable;
1191: }
1192:
1193:
1198: public void setEditable(boolean newValue)
1199: {
1200: if (editable == newValue)
1201: return;
1202:
1203: if (newValue == true)
1204: caretBlinkTimer.start();
1205: else
1206: {
1207: caretBlinkTimer.stop();
1208: caret.setVisible(false);
1209: }
1210:
1211: boolean oldValue = editable;
1212: editable = newValue;
1213: firePropertyChange("editable", oldValue, newValue);
1214: }
1215:
1216:
1221: public Caret getCaret()
1222: {
1223: return caret;
1224: }
1225:
1226:
1231: public void setCaret(Caret newCaret)
1232: {
1233: if (caret != null)
1234: caret.deinstall(this);
1235:
1236: Caret oldCaret = caret;
1237: caret = newCaret;
1238:
1239: caretBlinkTimer.update();
1240:
1241: if (caret != null)
1242: caret.install(this);
1243:
1244: firePropertyChange("caret", oldCaret, newCaret);
1245: }
1246:
1247: public Color getCaretColor()
1248: {
1249: return caretColor;
1250: }
1251:
1252: public void setCaretColor(Color newColor)
1253: {
1254: Color oldCaretColor = caretColor;
1255: caretColor = newColor;
1256: firePropertyChange("caretColor", oldCaretColor, newColor);
1257: }
1258:
1259: public Color getDisabledTextColor()
1260: {
1261: return disabledTextColor;
1262: }
1263:
1264: public void setDisabledTextColor(Color newColor)
1265: {
1266: Color oldColor = disabledTextColor;
1267: disabledTextColor = newColor;
1268: firePropertyChange("disabledTextColor", oldColor, newColor);
1269: }
1270:
1271: public Color getSelectedTextColor()
1272: {
1273: return selectedTextColor;
1274: }
1275:
1276: public void setSelectedTextColor(Color newColor)
1277: {
1278: Color oldColor = selectedTextColor;
1279: selectedTextColor = newColor;
1280: firePropertyChange("selectedTextColor", oldColor, newColor);
1281: }
1282:
1283: public Color getSelectionColor()
1284: {
1285: return selectionColor;
1286: }
1287:
1288: public void setSelectionColor(Color newColor)
1289: {
1290: Color oldColor = selectionColor;
1291: selectionColor = newColor;
1292: firePropertyChange("selectionColor", oldColor, newColor);
1293: }
1294:
1295:
1300: public int getCaretPosition()
1301: {
1302: return caret.getDot();
1303: }
1304:
1305:
1310: public void setCaretPosition(int position)
1311: {
1312: if (doc == null)
1313: return;
1314:
1315: if (position < 0 || position > doc.getLength())
1316: throw new IllegalArgumentException();
1317:
1318: caret.setDot(position);
1319: }
1320:
1321:
1325: public void moveCaretPosition(int position)
1326: {
1327: if (doc == null)
1328: return;
1329:
1330: if (position < 0 || position > doc.getLength())
1331: throw new IllegalArgumentException();
1332:
1333: caret.moveDot(position);
1334: }
1335:
1336: public Highlighter getHighlighter()
1337: {
1338: return highlighter;
1339: }
1340:
1341: public void setHighlighter(Highlighter newHighlighter)
1342: {
1343: if (highlighter != null)
1344: highlighter.deinstall(this);
1345:
1346: Highlighter oldHighlighter = highlighter;
1347: highlighter = newHighlighter;
1348:
1349: if (highlighter != null)
1350: highlighter.install(this);
1351:
1352: firePropertyChange("highlighter", oldHighlighter, newHighlighter);
1353: }
1354:
1355:
1360: public int getSelectionStart()
1361: {
1362: return Math.min(caret.getDot(), caret.getMark());
1363: }
1364:
1365:
1370: public void setSelectionStart(int start)
1371: {
1372: select(start, getSelectionEnd());
1373: }
1374:
1375:
1380: public int getSelectionEnd()
1381: {
1382: return Math.max(caret.getDot(), caret.getMark());
1383: }
1384:
1385:
1390: public void setSelectionEnd(int end)
1391: {
1392: select(getSelectionStart(), end);
1393: }
1394:
1395:
1401: public void select(int start, int end)
1402: {
1403: int length = doc.getLength();
1404:
1405: start = Math.max(start, 0);
1406: start = Math.min(start, length);
1407:
1408: end = Math.max(end, 0);
1409: end = Math.min(end, length);
1410:
1411: setCaretPosition(start);
1412: moveCaretPosition(end);
1413: }
1414:
1415:
1418: public void selectAll()
1419: {
1420: select(0, doc.getLength());
1421: }
1422:
1423: public synchronized void replaceSelection(String content)
1424: {
1425: int dot = caret.getDot();
1426: int mark = caret.getMark();
1427:
1428:
1429: if (content == null)
1430: {
1431: caret.setDot(dot);
1432: return;
1433: }
1434:
1435: try
1436: {
1437: int start = getSelectionStart();
1438: int end = getSelectionEnd();
1439:
1440:
1441: if (dot != mark)
1442: doc.remove(start, end - start);
1443:
1444:
1445: doc.insertString(start, content, null);
1446:
1447:
1448: setCaretPosition(start + content.length());
1449: }
1450: catch (BadLocationException e)
1451: {
1452:
1453: }
1454: }
1455:
1456: public boolean getScrollableTracksViewportHeight()
1457: {
1458: if (getParent() instanceof JViewport)
1459: return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
1460:
1461: return false;
1462: }
1463:
1464: public boolean getScrollableTracksViewportWidth()
1465: {
1466: if (getParent() instanceof JViewport)
1467: return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
1468:
1469: return false;
1470: }
1471:
1472:
1477: public void addCaretListener(CaretListener listener)
1478: {
1479: listenerList.add(CaretListener.class, listener);
1480: }
1481:
1482:
1487: public void removeCaretListener(CaretListener listener)
1488: {
1489: listenerList.remove(CaretListener.class, listener);
1490: }
1491:
1492:
1497: public CaretListener[] getCaretListeners()
1498: {
1499: return (CaretListener[]) getListeners(CaretListener.class);
1500: }
1501:
1502:
1508: protected void fireCaretUpdate(CaretEvent event)
1509: {
1510: CaretListener[] listeners = getCaretListeners();
1511:
1512: for (int index = 0; index < listeners.length; ++index)
1513: listeners[index].caretUpdate(event);
1514: }
1515:
1516:
1521: public void addInputMethodListener(InputMethodListener listener)
1522: {
1523: listenerList.add(InputMethodListener.class, listener);
1524: }
1525:
1526:
1531: public void removeInputMethodListener(InputMethodListener listener)
1532: {
1533: listenerList.remove(InputMethodListener.class, listener);
1534: }
1535:
1536:
1541: public InputMethodListener[] getInputMethodListeners()
1542: {
1543: return (InputMethodListener[]) getListeners(InputMethodListener.class);
1544: }
1545:
1546: public Rectangle modelToView(int position) throws BadLocationException
1547: {
1548: return getUI().modelToView(this, position);
1549: }
1550:
1551: public boolean getDragEnabled()
1552: {
1553: return dragEnabled;
1554: }
1555:
1556: public void setDragEnabled(boolean enabled)
1557: {
1558: dragEnabled = enabled;
1559: }
1560:
1561: public int viewToModel(Point pt)
1562: {
1563: return getUI().viewToModel(this, pt);
1564: }
1565:
1566: public void copy()
1567: {
1568: doTransferAction("copy", TransferHandler.getCopyAction());
1569: }
1570:
1571: public void cut()
1572: {
1573: doTransferAction("cut", TransferHandler.getCutAction());
1574: }
1575:
1576: public void paste()
1577: {
1578: doTransferAction("paste", TransferHandler.getPasteAction());
1579: }
1580:
1581: private void doTransferAction(String name, Action action)
1582: {
1583:
1584: if (getTransferHandler() == null)
1585: {
1586: if (defaultTransferHandler == null)
1587: defaultTransferHandler = new DefaultTransferHandler();
1588:
1589: setTransferHandler(defaultTransferHandler);
1590: }
1591:
1592:
1593: ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1594: action.getValue(Action.NAME).toString());
1595: action.actionPerformed(event);
1596: }
1597:
1598: public void setFocusAccelerator(char newKey)
1599: {
1600: if (focusAccelerator == newKey)
1601: return;
1602:
1603: char oldKey = focusAccelerator;
1604: focusAccelerator = newKey;
1605: firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);
1606: }
1607:
1608: public char getFocusAccelerator()
1609: {
1610: return focusAccelerator;
1611: }
1612:
1613:
1616: public NavigationFilter getNavigationFilter()
1617: {
1618: return navigationFilter;
1619: }
1620:
1621:
1624: public void setNavigationFilter(NavigationFilter filter)
1625: {
1626: navigationFilter = filter;
1627: }
1628:
1629:
1646: public void read(Reader input, Object streamDescription)
1647: throws IOException
1648: {
1649: if (streamDescription != null)
1650: {
1651: Document d = getDocument();
1652: if (d != null)
1653: d.putProperty(Document.StreamDescriptionProperty, streamDescription);
1654: }
1655:
1656: StringBuffer b = new StringBuffer();
1657: int c;
1658:
1659:
1660: while ((c = input.read()) >= 0)
1661: b.append((char) c);
1662:
1663: setText(b.toString());
1664: }
1665:
1666:
1674: public void write(Writer output)
1675: throws IOException
1676: {
1677: output.write(getText());
1678: }
1679:
1680:
1690: public String getToolTipText(MouseEvent ev)
1691: {
1692: return getUI().getToolTipText(this, ev.getPoint());
1693: }
1694: }