1:
37:
38:
39: package ;
40:
41: import ;
42:
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: import ;
63:
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: import ;
87: import ;
88:
89: public abstract class JTextComponent extends JComponent
90: implements Scrollable, Accessible
91: {
92:
97: public class AccessibleJTextComponent extends AccessibleJComponent implements
98: AccessibleText, CaretListener, DocumentListener, AccessibleAction,
99: AccessibleEditableText
100: {
101: private static final long serialVersionUID = 7664188944091413696L;
102:
103:
104: int dot = 0;
105:
106:
107: JTextComponent textComp = JTextComponent.this;
108:
109:
113: public AccessibleJTextComponent()
114: {
115: super();
116: textComp.addCaretListener(this);
117: }
118:
119:
126: public int getCaretPosition()
127: {
128: dot = textComp.getCaretPosition();
129: return dot;
130: }
131:
132:
137: public String getSelectedText()
138: {
139: return textComp.getSelectedText();
140: }
141:
142:
149: public int getSelectionStart()
150: {
151: if (getSelectedText() == null || (textComp.getText().equals("")))
152: return 0;
153: return textComp.getSelectionStart();
154: }
155:
156:
164: public int getSelectionEnd()
165: {
166: if (getSelectedText() == null || (textComp.getText().equals("")))
167: return 0;
168: return textComp.getSelectionEnd();
169: }
170:
171:
180: public void caretUpdate(CaretEvent e)
181: throws NotImplementedException
182: {
183:
184: dot = e.getDot();
185: }
186:
187:
192: public AccessibleStateSet getAccessibleStateSet()
193: throws NotImplementedException
194: {
195: AccessibleStateSet state = super.getAccessibleStateSet();
196:
197: return state;
198: }
199:
200:
207: public AccessibleRole getAccessibleRole()
208: {
209: return AccessibleRole.TEXT;
210: }
211:
212:
217: public AccessibleEditableText getAccessibleEditableText()
218: {
219: return this;
220: }
221:
222:
232: public AccessibleText getAccessibleText()
233: {
234: return this;
235: }
236:
237:
243: public void insertUpdate(DocumentEvent e)
244: throws NotImplementedException
245: {
246:
247: }
248:
249:
255: public void removeUpdate(DocumentEvent e)
256: throws NotImplementedException
257: {
258:
259: }
260:
261:
267: public void changedUpdate(DocumentEvent e)
268: throws NotImplementedException
269: {
270:
271: }
272:
273:
280: public int getIndexAtPoint(Point p)
281: throws NotImplementedException
282: {
283: return 0;
284: }
285:
286:
299: public Rectangle getCharacterBounds(int index)
300: throws NotImplementedException
301: {
302: return null;
303: }
304:
305:
310: public int getCharCount()
311: {
312: return textComp.getText().length();
313: }
314:
315:
322: public AttributeSet getCharacterAttribute(int index)
323: throws NotImplementedException
324: {
325: return null;
326: }
327:
328:
336: public String getAtIndex(int part, int index)
337: throws NotImplementedException
338: {
339: return null;
340: }
341:
342:
350: public String getAfterIndex(int part, int index)
351: throws NotImplementedException
352: {
353: return null;
354: }
355:
356:
364: public String getBeforeIndex(int part, int index)
365: throws NotImplementedException
366: {
367: return null;
368: }
369:
370:
376: public int getAccessibleActionCount()
377: throws NotImplementedException
378: {
379: return 0;
380: }
381:
382:
389: public String getAccessibleActionDescription(int i)
390: throws NotImplementedException
391: {
392:
393: return super.getAccessibleDescription();
394: }
395:
396:
402: public boolean doAccessibleAction(int i)
403: throws NotImplementedException
404: {
405: return false;
406: }
407:
408:
413: public void setTextContents(String s)
414: throws NotImplementedException
415: {
416:
417: }
418:
419:
425: public void insertTextAtIndex(int index, String s)
426: throws NotImplementedException
427: {
428: replaceText(index, index, s);
429: }
430:
431:
437: public String getTextRange(int start, int end)
438: {
439: try
440: {
441: return textComp.getText(start, end - start);
442: }
443: catch (BadLocationException ble)
444: {
445: return "";
446: }
447: }
448:
449:
455: public void delete(int start, int end)
456: {
457: replaceText(start, end, "");
458: }
459:
460:
466: public void cut(int start, int end)
467: {
468: textComp.select(start, end);
469: textComp.cut();
470: }
471:
472:
477: public void paste(int start)
478: {
479: textComp.setCaretPosition(start);
480: textComp.paste();
481: }
482:
483:
490: public void replaceText(int start, int end, String s)
491: {
492: textComp.select(start, end);
493: textComp.replaceSelection(s);
494: }
495:
496:
502: public void selectText(int start, int end)
503: {
504: textComp.select(start, end);
505: }
506:
507:
514: public void setAttributes(int start, int end, AttributeSet s)
515: throws NotImplementedException
516: {
517:
518: }
519: }
520:
521: public static class KeyBinding
522: {
523: public KeyStroke key;
524: public String actionName;
525:
526:
532: public KeyBinding(KeyStroke key, String actionName)
533: {
534: this.key = key;
535: this.actionName = actionName;
536: }
537: }
538:
539:
569:
570: private class KeymapWrapper extends InputMap
571: {
572: Keymap map;
573:
574: public KeymapWrapper(Keymap k)
575: {
576: map = k;
577: }
578:
579: public int size()
580: {
581: return map.getBoundKeyStrokes().length + super.size();
582: }
583:
584: public Object get(KeyStroke ks)
585: {
586: Action mapped = null;
587: Keymap m = map;
588: while(mapped == null && m != null)
589: {
590: mapped = m.getAction(ks);
591: if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED)
592: mapped = m.getDefaultAction();
593: if (mapped == null)
594: m = m.getResolveParent();
595: }
596:
597: if (mapped == null)
598: return super.get(ks);
599: else
600: return mapped;
601: }
602:
603: public KeyStroke[] keys()
604: {
605: KeyStroke[] superKeys = super.keys();
606: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
607: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
608: for (int i = 0; i < superKeys.length; ++i)
609: bothKeys[i] = superKeys[i];
610: for (int i = 0; i < mapKeys.length; ++i)
611: bothKeys[i + superKeys.length] = mapKeys[i];
612: return bothKeys;
613: }
614:
615: public KeyStroke[] allKeys()
616: {
617: KeyStroke[] superKeys = super.allKeys();
618: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
619: int skl = 0;
620: int mkl = 0;
621: if (superKeys != null)
622: skl = superKeys.length;
623: if (mapKeys != null)
624: mkl = mapKeys.length;
625: KeyStroke[] bothKeys = new KeyStroke[skl + mkl];
626: for (int i = 0; i < skl; ++i)
627: bothKeys[i] = superKeys[i];
628: for (int i = 0; i < mkl; ++i)
629: bothKeys[i + skl] = mapKeys[i];
630: return bothKeys;
631: }
632: }
633:
634: private class KeymapActionMap extends ActionMap
635: {
636: Keymap map;
637:
638: public KeymapActionMap(Keymap k)
639: {
640: map = k;
641: }
642:
643: public Action get(Object cmd)
644: {
645: if (cmd instanceof Action)
646: return (Action) cmd;
647: else
648: return super.get(cmd);
649: }
650:
651: public int size()
652: {
653: return map.getBoundKeyStrokes().length + super.size();
654: }
655:
656: public Object[] keys()
657: {
658: Object[] superKeys = super.keys();
659: Object[] mapKeys = map.getBoundKeyStrokes();
660: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
661: for (int i = 0; i < superKeys.length; ++i)
662: bothKeys[i] = superKeys[i];
663: for (int i = 0; i < mapKeys.length; ++i)
664: bothKeys[i + superKeys.length] = mapKeys[i];
665: return bothKeys;
666: }
667:
668: public Object[] allKeys()
669: {
670: Object[] superKeys = super.allKeys();
671: Object[] mapKeys = map.getBoundKeyStrokes();
672: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
673: for (int i = 0; i < superKeys.length; ++i)
674: bothKeys[i] = superKeys[i];
675: for (int i = 0; i < mapKeys.length; ++i)
676: bothKeys[i + superKeys.length] = mapKeys[i];
677: return bothKeys;
678: }
679:
680: }
681:
682: static class DefaultKeymap implements Keymap
683: {
684: String name;
685: Keymap parent;
686: Hashtable map;
687: Action defaultAction;
688:
689: public DefaultKeymap(String name)
690: {
691: this.name = name;
692: this.map = new Hashtable();
693: }
694:
695: public void addActionForKeyStroke(KeyStroke key, Action a)
696: {
697: map.put(key, a);
698: }
699:
700:
709: public Action getAction(KeyStroke key)
710: {
711: if (map.containsKey(key))
712: return (Action) map.get(key);
713: else if (parent != null)
714: return parent.getAction(key);
715: else
716: return null;
717: }
718:
719: public Action[] getBoundActions()
720: {
721: Action [] ret = new Action[map.size()];
722: Enumeration e = map.elements();
723: int i = 0;
724: while (e.hasMoreElements())
725: {
726: ret[i++] = (Action) e.nextElement();
727: }
728: return ret;
729: }
730:
731: public KeyStroke[] getBoundKeyStrokes()
732: {
733: KeyStroke [] ret = new KeyStroke[map.size()];
734: Enumeration e = map.keys();
735: int i = 0;
736: while (e.hasMoreElements())
737: {
738: ret[i++] = (KeyStroke) e.nextElement();
739: }
740: return ret;
741: }
742:
743: public Action getDefaultAction()
744: {
745: return defaultAction;
746: }
747:
748: public KeyStroke[] getKeyStrokesForAction(Action a)
749: {
750: int i = 0;
751: Enumeration e = map.keys();
752: while (e.hasMoreElements())
753: {
754: if (map.get(e.nextElement()).equals(a))
755: ++i;
756: }
757: KeyStroke [] ret = new KeyStroke[i];
758: i = 0;
759: e = map.keys();
760: while (e.hasMoreElements())
761: {
762: KeyStroke k = (KeyStroke) e.nextElement();
763: if (map.get(k).equals(a))
764: ret[i++] = k;
765: }
766: return ret;
767: }
768:
769: public String getName()
770: {
771: return name;
772: }
773:
774: public Keymap getResolveParent()
775: {
776: return parent;
777: }
778:
779: public boolean isLocallyDefined(KeyStroke key)
780: {
781: return map.containsKey(key);
782: }
783:
784: public void removeBindings()
785: {
786: map.clear();
787: }
788:
789: public void removeKeyStrokeBinding(KeyStroke key)
790: {
791: map.remove(key);
792: }
793:
794: public void setDefaultAction(Action a)
795: {
796: defaultAction = a;
797: }
798:
799: public void setResolveParent(Keymap p)
800: {
801: parent = p;
802: }
803: }
804:
805: class DefaultTransferHandler extends TransferHandler
806: {
807: public boolean canImport(JComponent component, DataFlavor[] flavors)
808: {
809: JTextComponent textComponent = (JTextComponent) component;
810:
811: if (! (textComponent.isEnabled()
812: && textComponent.isEditable()
813: && flavors != null))
814: return false;
815:
816: for (int i = 0; i < flavors.length; ++i)
817: if (flavors[i].equals(DataFlavor.stringFlavor))
818: return true;
819:
820: return false;
821: }
822:
823: public void exportToClipboard(JComponent component, Clipboard clipboard,
824: int action)
825: {
826: JTextComponent textComponent = (JTextComponent) component;
827: int start = textComponent.getSelectionStart();
828: int end = textComponent.getSelectionEnd();
829:
830: if (start == end)
831: return;
832:
833: try
834: {
835:
836: String data = textComponent.getDocument().getText(start, end);
837: StringSelection selection = new StringSelection(data);
838: clipboard.setContents(selection, null);
839:
840:
841: if (action == MOVE)
842: doc.remove(start, end - start);
843: }
844: catch (BadLocationException e)
845: {
846:
847: }
848: }
849:
850: public int getSourceActions()
851: {
852: return NONE;
853: }
854:
855: public boolean importData(JComponent component, Transferable transferable)
856: {
857: DataFlavor flavor = null;
858: DataFlavor[] flavors = transferable.getTransferDataFlavors();
859:
860: if (flavors == null)
861: return false;
862:
863: for (int i = 0; i < flavors.length; ++i)
864: if (flavors[i].equals(DataFlavor.stringFlavor))
865: flavor = flavors[i];
866:
867: if (flavor == null)
868: return false;
869:
870: try
871: {
872: JTextComponent textComponent = (JTextComponent) component;
873: String data = (String) transferable.getTransferData(flavor);
874: textComponent.replaceSelection(data);
875: return true;
876: }
877: catch (IOException e)
878: {
879:
880: }
881: catch (UnsupportedFlavorException e)
882: {
883:
884: }
885:
886: return false;
887: }
888: }
889:
890: private static final long serialVersionUID = -8796518220218978795L;
891:
892: public static final String DEFAULT_KEYMAP = "default";
893: public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
894:
895: private static DefaultTransferHandler defaultTransferHandler;
896: private static Hashtable keymaps = new Hashtable();
897: private Keymap keymap;
898: private char focusAccelerator = '\0';
899: private NavigationFilter navigationFilter;
900:
901:
913: public static Keymap getKeymap(String n)
914: {
915: return (Keymap) keymaps.get(n);
916: }
917:
918:
929: public static Keymap removeKeymap(String n)
930: {
931: Keymap km = (Keymap) keymaps.get(n);
932: keymaps.remove(n);
933: return km;
934: }
935:
936:
952: public static Keymap addKeymap(String n, Keymap parent)
953: {
954: Keymap k = new DefaultKeymap(n);
955: k.setResolveParent(parent);
956: if (n != null)
957: keymaps.put(n, k);
958: return k;
959: }
960:
961:
969: public Keymap getKeymap()
970: {
971: return keymap;
972: }
973:
974:
983: public void setKeymap(Keymap k)
984: {
985:
986:
987:
988:
989:
990:
991:
992:
993:
994:
995:
996: KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k));
997: InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED);
998: if (childInputMap == null)
999: setInputMap(JComponent.WHEN_FOCUSED, kw);
1000: else
1001: {
1002: while (childInputMap.getParent() != null
1003: && !(childInputMap.getParent() instanceof KeymapWrapper)
1004: && !(childInputMap.getParent() instanceof InputMapUIResource))
1005: childInputMap = childInputMap.getParent();
1006:
1007:
1008: if (childInputMap.getParent() == null)
1009: childInputMap.setParent(kw);
1010:
1011:
1012:
1013: else if (childInputMap.getParent() instanceof KeymapWrapper)
1014: {
1015: if (kw == null)
1016: childInputMap.setParent(childInputMap.getParent().getParent());
1017: else
1018: {
1019: kw.setParent(childInputMap.getParent().getParent());
1020: childInputMap.setParent(kw);
1021: }
1022: }
1023:
1024:
1025:
1026: else if (childInputMap.getParent() instanceof InputMapUIResource)
1027: {
1028: if (kw != null)
1029: {
1030: kw.setParent(childInputMap.getParent());
1031: childInputMap.setParent(kw);
1032: }
1033: }
1034: }
1035:
1036:
1037:
1038: KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k));
1039: ActionMap childActionMap = getActionMap();
1040: if (childActionMap == null)
1041: setActionMap(kam);
1042: else
1043: {
1044: while (childActionMap.getParent() != null
1045: && !(childActionMap.getParent() instanceof KeymapActionMap)
1046: && !(childActionMap.getParent() instanceof ActionMapUIResource))
1047: childActionMap = childActionMap.getParent();
1048:
1049:
1050: if (childActionMap.getParent() == null)
1051: childActionMap.setParent(kam);
1052:
1053:
1054:
1055: else if (childActionMap.getParent() instanceof KeymapActionMap)
1056: {
1057: if (kam == null)
1058: childActionMap.setParent(childActionMap.getParent().getParent());
1059: else
1060: {
1061: kam.setParent(childActionMap.getParent().getParent());
1062: childActionMap.setParent(kam);
1063: }
1064: }
1065:
1066:
1067:
1068: else if (childActionMap.getParent() instanceof ActionMapUIResource)
1069: {
1070: if (kam != null)
1071: {
1072: kam.setParent(childActionMap.getParent());
1073: childActionMap.setParent(kam);
1074: }
1075: }
1076: }
1077:
1078:
1079:
1080: Keymap old = keymap;
1081: keymap = k;
1082: firePropertyChange("keymap", old, k);
1083: }
1084:
1085:
1101: public static void loadKeymap(Keymap map,
1102: JTextComponent.KeyBinding[] bindings,
1103: Action[] actions)
1104: {
1105: Hashtable acts = new Hashtable(actions.length);
1106: for (int i = 0; i < actions.length; ++i)
1107: acts.put(actions[i].getValue(Action.NAME), actions[i]);
1108: for (int i = 0; i < bindings.length; ++i)
1109: if (acts.containsKey(bindings[i].actionName))
1110: map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName));
1111: }
1112:
1113:
1126: public Action[] getActions()
1127: {
1128: return getUI().getEditorKit(this).getActions();
1129: }
1130:
1131:
1132: Document doc;
1133: Caret caret;
1134: boolean editable;
1135:
1136: private Highlighter highlighter;
1137: private Color caretColor;
1138: private Color disabledTextColor;
1139: private Color selectedTextColor;
1140: private Color selectionColor;
1141: private Insets margin;
1142: private boolean dragEnabled;
1143:
1144:
1147: public JTextComponent()
1148: {
1149: Keymap defkeymap = getKeymap(DEFAULT_KEYMAP);
1150: if (defkeymap == null)
1151: {
1152: defkeymap = addKeymap(DEFAULT_KEYMAP, null);
1153: defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction());
1154: }
1155:
1156: setFocusable(true);
1157: setEditable(true);
1158: enableEvents(AWTEvent.KEY_EVENT_MASK);
1159: setOpaque(true);
1160: updateUI();
1161: }
1162:
1163: public void setDocument(Document newDoc)
1164: {
1165: Document oldDoc = doc;
1166: doc = newDoc;
1167: firePropertyChange("document", oldDoc, newDoc);
1168: revalidate();
1169: repaint();
1170: }
1171:
1172: public Document getDocument()
1173: {
1174: return doc;
1175: }
1176:
1177:
1182: public AccessibleContext getAccessibleContext()
1183: {
1184: return new AccessibleJTextComponent();
1185: }
1186:
1187: public void setMargin(Insets m)
1188: {
1189: margin = m;
1190: }
1191:
1192: public Insets getMargin()
1193: {
1194: return margin;
1195: }
1196:
1197: public void setText(String text)
1198: {
1199: try
1200: {
1201: if (doc instanceof AbstractDocument)
1202: ((AbstractDocument) doc).replace(0, doc.getLength(), text, null);
1203: else
1204: {
1205: doc.remove(0, doc.getLength());
1206: doc.insertString(0, text, null);
1207: }
1208: }
1209: catch (BadLocationException e)
1210: {
1211:
1212: throw (InternalError) new InternalError().initCause(e);
1213: }
1214: }
1215:
1216:
1223: public String getText()
1224: {
1225: if (doc == null)
1226: return null;
1227:
1228: try
1229: {
1230: return doc.getText(0, doc.getLength());
1231: }
1232: catch (BadLocationException e)
1233: {
1234:
1235: return "";
1236: }
1237: }
1238:
1239:
1249: public String getText(int offset, int length)
1250: throws BadLocationException
1251: {
1252: return getDocument().getText(offset, length);
1253: }
1254:
1255:
1262: public String getSelectedText()
1263: {
1264: int start = getSelectionStart();
1265: int offset = getSelectionEnd() - start;
1266:
1267: if (offset <= 0)
1268: return null;
1269:
1270: try
1271: {
1272: return doc.getText(start, offset);
1273: }
1274: catch (BadLocationException e)
1275: {
1276:
1277: return null;
1278: }
1279: }
1280:
1281:
1287: public String getUIClassID()
1288: {
1289: return "TextComponentUI";
1290: }
1291:
1292:
1295: protected String paramString()
1296: {
1297:
1298: return super.paramString();
1299: }
1300:
1301:
1306: public TextUI getUI()
1307: {
1308: return (TextUI) ui;
1309: }
1310:
1311:
1316: public void setUI(TextUI newUI)
1317: {
1318: super.setUI(newUI);
1319: }
1320:
1321:
1325: public void updateUI()
1326: {
1327: setUI((TextUI) UIManager.getUI(this));
1328: }
1329:
1330: public Dimension getPreferredScrollableViewportSize()
1331: {
1332: return getPreferredSize();
1333: }
1334:
1335: public int getScrollableUnitIncrement(Rectangle visible, int orientation,
1336: int direction)
1337: {
1338:
1339: if (orientation == SwingConstants.HORIZONTAL)
1340: return visible.width / 10;
1341: else if (orientation == SwingConstants.VERTICAL)
1342: return visible.height / 10;
1343: else
1344: throw new IllegalArgumentException("orientation must be either "
1345: + "javax.swing.SwingConstants.VERTICAL "
1346: + "or "
1347: + "javax.swing.SwingConstants.HORIZONTAL"
1348: );
1349: }
1350:
1351: public int getScrollableBlockIncrement(Rectangle visible, int orientation,
1352: int direction)
1353: {
1354:
1355: if (orientation == SwingConstants.HORIZONTAL)
1356: return visible.width;
1357: else if (orientation == SwingConstants.VERTICAL)
1358: return visible.height;
1359: else
1360: throw new IllegalArgumentException("orientation must be either "
1361: + "javax.swing.SwingConstants.VERTICAL "
1362: + "or "
1363: + "javax.swing.SwingConstants.HORIZONTAL"
1364: );
1365: }
1366:
1367:
1372: public boolean isEditable()
1373: {
1374: return editable;
1375: }
1376:
1377:
1382: public void setEditable(boolean newValue)
1383: {
1384: if (editable == newValue)
1385: return;
1386:
1387: boolean oldValue = editable;
1388: editable = newValue;
1389: firePropertyChange("editable", oldValue, newValue);
1390: }
1391:
1392:
1397: public Caret getCaret()
1398: {
1399: return caret;
1400: }
1401:
1402:
1407: public void setCaret(Caret newCaret)
1408: {
1409: if (caret != null)
1410: caret.deinstall(this);
1411:
1412: Caret oldCaret = caret;
1413: caret = newCaret;
1414:
1415: if (caret != null)
1416: caret.install(this);
1417:
1418: firePropertyChange("caret", oldCaret, newCaret);
1419: }
1420:
1421: public Color getCaretColor()
1422: {
1423: return caretColor;
1424: }
1425:
1426: public void setCaretColor(Color newColor)
1427: {
1428: Color oldCaretColor = caretColor;
1429: caretColor = newColor;
1430: firePropertyChange("caretColor", oldCaretColor, newColor);
1431: }
1432:
1433: public Color getDisabledTextColor()
1434: {
1435: return disabledTextColor;
1436: }
1437:
1438: public void setDisabledTextColor(Color newColor)
1439: {
1440: Color oldColor = disabledTextColor;
1441: disabledTextColor = newColor;
1442: firePropertyChange("disabledTextColor", oldColor, newColor);
1443: }
1444:
1445: public Color getSelectedTextColor()
1446: {
1447: return selectedTextColor;
1448: }
1449:
1450: public void setSelectedTextColor(Color newColor)
1451: {
1452: Color oldColor = selectedTextColor;
1453: selectedTextColor = newColor;
1454: firePropertyChange("selectedTextColor", oldColor, newColor);
1455: }
1456:
1457: public Color getSelectionColor()
1458: {
1459: return selectionColor;
1460: }
1461:
1462: public void setSelectionColor(Color newColor)
1463: {
1464: Color oldColor = selectionColor;
1465: selectionColor = newColor;
1466: firePropertyChange("selectionColor", oldColor, newColor);
1467: }
1468:
1469:
1474: public int getCaretPosition()
1475: {
1476: return caret.getDot();
1477: }
1478:
1479:
1484: public void setCaretPosition(int position)
1485: {
1486: if (doc == null)
1487: return;
1488:
1489: if (position < 0 || position > doc.getLength())
1490: throw new IllegalArgumentException();
1491:
1492: caret.setDot(position);
1493: }
1494:
1495:
1499: public void moveCaretPosition(int position)
1500: {
1501: if (doc == null)
1502: return;
1503:
1504: if (position < 0 || position > doc.getLength())
1505: throw new IllegalArgumentException();
1506:
1507: caret.moveDot(position);
1508: }
1509:
1510: public Highlighter getHighlighter()
1511: {
1512: return highlighter;
1513: }
1514:
1515: public void setHighlighter(Highlighter newHighlighter)
1516: {
1517: if (highlighter != null)
1518: highlighter.deinstall(this);
1519:
1520: Highlighter oldHighlighter = highlighter;
1521: highlighter = newHighlighter;
1522:
1523: if (highlighter != null)
1524: highlighter.install(this);
1525:
1526: firePropertyChange("highlighter", oldHighlighter, newHighlighter);
1527: }
1528:
1529:
1534: public int getSelectionStart()
1535: {
1536: return Math.min(caret.getDot(), caret.getMark());
1537: }
1538:
1539:
1544: public void setSelectionStart(int start)
1545: {
1546: select(start, getSelectionEnd());
1547: }
1548:
1549:
1554: public int getSelectionEnd()
1555: {
1556: return Math.max(caret.getDot(), caret.getMark());
1557: }
1558:
1559:
1564: public void setSelectionEnd(int end)
1565: {
1566: select(getSelectionStart(), end);
1567: }
1568:
1569:
1575: public void select(int start, int end)
1576: {
1577: int length = doc.getLength();
1578:
1579: start = Math.max(start, 0);
1580: start = Math.min(start, length);
1581:
1582: end = Math.max(end, start);
1583: end = Math.min(end, length);
1584:
1585: setCaretPosition(start);
1586: moveCaretPosition(end);
1587: }
1588:
1589:
1592: public void selectAll()
1593: {
1594: select(0, doc.getLength());
1595: }
1596:
1597: public synchronized void replaceSelection(String content)
1598: {
1599: int dot = caret.getDot();
1600: int mark = caret.getMark();
1601:
1602:
1603: if (content == null)
1604: {
1605: caret.setDot(dot);
1606: return;
1607: }
1608:
1609: try
1610: {
1611: int start = getSelectionStart();
1612: int end = getSelectionEnd();
1613:
1614:
1615: if (dot != mark)
1616: doc.remove(start, end - start);
1617:
1618:
1619: doc.insertString(start, content, null);
1620:
1621:
1622: dot = start + content.length();
1623: setCaretPosition(dot);
1624:
1625:
1626: caret.setMagicCaretPosition(modelToView(dot).getLocation());
1627: }
1628: catch (BadLocationException e)
1629: {
1630:
1631: }
1632: }
1633:
1634: public boolean getScrollableTracksViewportHeight()
1635: {
1636: if (getParent() instanceof JViewport)
1637: return getParent().getHeight() > getPreferredSize().height;
1638:
1639: return false;
1640: }
1641:
1642: public boolean getScrollableTracksViewportWidth()
1643: {
1644: if (getParent() instanceof JViewport)
1645: return getParent().getWidth() > getPreferredSize().width;
1646:
1647: return false;
1648: }
1649:
1650:
1655: public void addCaretListener(CaretListener listener)
1656: {
1657: listenerList.add(CaretListener.class, listener);
1658: }
1659:
1660:
1665: public void removeCaretListener(CaretListener listener)
1666: {
1667: listenerList.remove(CaretListener.class, listener);
1668: }
1669:
1670:
1675: public CaretListener[] getCaretListeners()
1676: {
1677: return (CaretListener[]) getListeners(CaretListener.class);
1678: }
1679:
1680:
1686: protected void fireCaretUpdate(CaretEvent event)
1687: {
1688: CaretListener[] listeners = getCaretListeners();
1689:
1690: for (int index = 0; index < listeners.length; ++index)
1691: listeners[index].caretUpdate(event);
1692: }
1693:
1694:
1699: public void addInputMethodListener(InputMethodListener listener)
1700: {
1701: listenerList.add(InputMethodListener.class, listener);
1702: }
1703:
1704:
1709: public void removeInputMethodListener(InputMethodListener listener)
1710: {
1711: listenerList.remove(InputMethodListener.class, listener);
1712: }
1713:
1714:
1719: public InputMethodListener[] getInputMethodListeners()
1720: {
1721: return (InputMethodListener[]) getListeners(InputMethodListener.class);
1722: }
1723:
1724: public Rectangle modelToView(int position) throws BadLocationException
1725: {
1726: return getUI().modelToView(this, position);
1727: }
1728:
1729: public boolean getDragEnabled()
1730: {
1731: return dragEnabled;
1732: }
1733:
1734: public void setDragEnabled(boolean enabled)
1735: {
1736: dragEnabled = enabled;
1737: }
1738:
1739: public int viewToModel(Point pt)
1740: {
1741: return getUI().viewToModel(this, pt);
1742: }
1743:
1744: public void copy()
1745: {
1746: if (isEnabled())
1747: doTransferAction("copy", TransferHandler.getCopyAction());
1748: }
1749:
1750: public void cut()
1751: {
1752: if (editable && isEnabled())
1753: doTransferAction("cut", TransferHandler.getCutAction());
1754: }
1755:
1756: public void paste()
1757: {
1758: if (editable && isEnabled())
1759: doTransferAction("paste", TransferHandler.getPasteAction());
1760: }
1761:
1762: private void doTransferAction(String name, Action action)
1763: {
1764:
1765: if (getTransferHandler() == null)
1766: {
1767: if (defaultTransferHandler == null)
1768: defaultTransferHandler = new DefaultTransferHandler();
1769:
1770: setTransferHandler(defaultTransferHandler);
1771: }
1772:
1773:
1774: ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1775: action.getValue(Action.NAME).toString());
1776: action.actionPerformed(event);
1777: }
1778:
1779: public void setFocusAccelerator(char newKey)
1780: {
1781: if (focusAccelerator == newKey)
1782: return;
1783:
1784: char oldKey = focusAccelerator;
1785: focusAccelerator = newKey;
1786: firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);
1787: }
1788:
1789: public char getFocusAccelerator()
1790: {
1791: return focusAccelerator;
1792: }
1793:
1794:
1797: public NavigationFilter getNavigationFilter()
1798: {
1799: return navigationFilter;
1800: }
1801:
1802:
1805: public void setNavigationFilter(NavigationFilter filter)
1806: {
1807: navigationFilter = filter;
1808: }
1809:
1810:
1827: public void read(Reader input, Object streamDescription)
1828: throws IOException
1829: {
1830: if (streamDescription != null)
1831: {
1832: Document d = getDocument();
1833: if (d != null)
1834: d.putProperty(Document.StreamDescriptionProperty, streamDescription);
1835: }
1836:
1837: StringBuffer b = new StringBuffer();
1838: int c;
1839:
1840:
1841: while ((c = input.read()) >= 0)
1842: b.append((char) c);
1843:
1844: setText(b.toString());
1845: }
1846:
1847:
1855: public void write(Writer output)
1856: throws IOException
1857: {
1858: output.write(getText());
1859: }
1860:
1861:
1871: public String getToolTipText(MouseEvent ev)
1872: {
1873: return getUI().getToolTipText(this, ev.getPoint());
1874: }
1875: }