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: import ;
64:
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: import ;
89:
90: public abstract class JTextComponent extends JComponent
91: implements Scrollable, Accessible
92: {
93:
99: public class AccessibleJTextComponent extends AccessibleJComponent implements
100: AccessibleText, CaretListener, DocumentListener, AccessibleAction,
101: AccessibleEditableText
102: {
103: private static final long serialVersionUID = 7664188944091413696L;
104:
105:
108: int dot = 0;
109:
110:
113: JTextComponent textComp = JTextComponent.this;
114:
115:
118: public AccessibleJTextComponent()
119: {
120: super();
121: textComp.addCaretListener(this);
122: }
123:
124:
130: public int getCaretPosition()
131: {
132: dot = textComp.getCaretPosition();
133: return dot;
134: }
135:
136:
142: public String getSelectedText()
143: {
144: return textComp.getSelectedText();
145: }
146:
147:
157: public int getSelectionStart()
158: {
159: if (getSelectedText() == null || (textComp.getText().equals("")))
160: return 0;
161: return textComp.getSelectionStart();
162: }
163:
164:
174: public int getSelectionEnd()
175: {
176: if (getSelectedText() == null || (textComp.getText().equals("")))
177: return 0;
178: return textComp.getSelectionEnd();
179: }
180:
181:
187: public void caretUpdate(CaretEvent e)
188: throws NotImplementedException
189: {
190:
191: dot = e.getDot();
192: }
193:
194:
199: public AccessibleStateSet getAccessibleStateSet()
200: throws NotImplementedException
201: {
202: AccessibleStateSet state = super.getAccessibleStateSet();
203:
204: return state;
205: }
206:
207:
214: public AccessibleRole getAccessibleRole()
215: {
216: return AccessibleRole.TEXT;
217: }
218:
219:
225: public AccessibleEditableText getAccessibleEditableText()
226: {
227: return this;
228: }
229:
230:
238: public AccessibleText getAccessibleText()
239: {
240: return this;
241: }
242:
243:
250: public void insertUpdate(DocumentEvent e)
251: throws NotImplementedException
252: {
253:
254: }
255:
256:
263: public void removeUpdate(DocumentEvent e)
264: throws NotImplementedException
265: {
266:
267: }
268:
269:
276: public void changedUpdate(DocumentEvent e)
277: throws NotImplementedException
278: {
279:
280: }
281:
282:
291: public int getIndexAtPoint(Point p)
292: throws NotImplementedException
293: {
294: return 0;
295: }
296:
297:
307: public Rectangle getCharacterBounds(int index)
308: throws NotImplementedException
309: {
310: return null;
311: }
312:
313:
318: public int getCharCount()
319: {
320: return textComp.getText().length();
321: }
322:
323:
331: public AttributeSet getCharacterAttribute(int index)
332: throws NotImplementedException
333: {
334: return null;
335: }
336:
337:
346: public String getAtIndex(int part, int index)
347: throws NotImplementedException
348: {
349: return null;
350: }
351:
352:
361: public String getAfterIndex(int part, int index)
362: throws NotImplementedException
363: {
364: return null;
365: }
366:
367:
376: public String getBeforeIndex(int part, int index)
377: throws NotImplementedException
378: {
379: return null;
380: }
381:
382:
388: public int getAccessibleActionCount()
389: throws NotImplementedException
390: {
391: return 0;
392: }
393:
394:
402: public String getAccessibleActionDescription(int i)
403: throws NotImplementedException
404: {
405:
406: return super.getAccessibleDescription();
407: }
408:
409:
417: public boolean doAccessibleAction(int i)
418: throws NotImplementedException
419: {
420: return false;
421: }
422:
423:
428: public void setTextContents(String s)
429: throws NotImplementedException
430: {
431:
432: }
433:
434:
440: public void insertTextAtIndex(int index, String s)
441: throws NotImplementedException
442: {
443: replaceText(index, index, s);
444: }
445:
446:
452: public String getTextRange(int start, int end)
453: {
454: try
455: {
456: return textComp.getText(start, end - start);
457: }
458: catch (BadLocationException ble)
459: {
460: return "";
461: }
462: }
463:
464:
470: public void delete(int start, int end)
471: {
472: replaceText(start, end, "");
473: }
474:
475:
482: public void cut(int start, int end)
483: {
484: textComp.select(start, end);
485: textComp.cut();
486: }
487:
488:
493: public void paste(int start)
494: {
495: textComp.setCaretPosition(start);
496: textComp.paste();
497: }
498:
499:
507: public void replaceText(int start, int end, String s)
508: {
509: textComp.select(start, end);
510: textComp.replaceSelection(s);
511: }
512:
513:
519: public void selectText(int start, int end)
520: {
521: textComp.select(start, end);
522: }
523:
524:
531: public void setAttributes(int start, int end, AttributeSet s)
532: throws NotImplementedException
533: {
534:
535: }
536: }
537:
538: public static class KeyBinding
539: {
540: public KeyStroke key;
541: public String actionName;
542:
543:
549: public KeyBinding(KeyStroke key, String actionName)
550: {
551: this.key = key;
552: this.actionName = actionName;
553: }
554: }
555:
556:
586:
587: private class KeymapWrapper extends InputMap
588: {
589: Keymap map;
590:
591: public KeymapWrapper(Keymap k)
592: {
593: map = k;
594: }
595:
596: public int size()
597: {
598: return map.getBoundKeyStrokes().length + super.size();
599: }
600:
601: public Object get(KeyStroke ks)
602: {
603: Action mapped = null;
604: Keymap m = map;
605: while(mapped == null && m != null)
606: {
607: mapped = m.getAction(ks);
608: if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED)
609: mapped = m.getDefaultAction();
610: if (mapped == null)
611: m = m.getResolveParent();
612: }
613:
614: if (mapped == null)
615: return super.get(ks);
616: else
617: return mapped;
618: }
619:
620: public KeyStroke[] keys()
621: {
622: KeyStroke[] superKeys = super.keys();
623: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
624: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
625: for (int i = 0; i < superKeys.length; ++i)
626: bothKeys[i] = superKeys[i];
627: for (int i = 0; i < mapKeys.length; ++i)
628: bothKeys[i + superKeys.length] = mapKeys[i];
629: return bothKeys;
630: }
631:
632: public KeyStroke[] allKeys()
633: {
634: KeyStroke[] superKeys = super.allKeys();
635: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
636: int skl = 0;
637: int mkl = 0;
638: if (superKeys != null)
639: skl = superKeys.length;
640: if (mapKeys != null)
641: mkl = mapKeys.length;
642: KeyStroke[] bothKeys = new KeyStroke[skl + mkl];
643: for (int i = 0; i < skl; ++i)
644: bothKeys[i] = superKeys[i];
645: for (int i = 0; i < mkl; ++i)
646: bothKeys[i + skl] = mapKeys[i];
647: return bothKeys;
648: }
649: }
650:
651: private class KeymapActionMap extends ActionMap
652: {
653: Keymap map;
654:
655: public KeymapActionMap(Keymap k)
656: {
657: map = k;
658: }
659:
660: public Action get(Object cmd)
661: {
662: if (cmd instanceof Action)
663: return (Action) cmd;
664: else
665: return super.get(cmd);
666: }
667:
668: public int size()
669: {
670: return map.getBoundKeyStrokes().length + super.size();
671: }
672:
673: public Object[] keys()
674: {
675: Object[] superKeys = super.keys();
676: Object[] mapKeys = map.getBoundKeyStrokes();
677: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
678: for (int i = 0; i < superKeys.length; ++i)
679: bothKeys[i] = superKeys[i];
680: for (int i = 0; i < mapKeys.length; ++i)
681: bothKeys[i + superKeys.length] = mapKeys[i];
682: return bothKeys;
683: }
684:
685: public Object[] allKeys()
686: {
687: Object[] superKeys = super.allKeys();
688: Object[] mapKeys = map.getBoundKeyStrokes();
689: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
690: for (int i = 0; i < superKeys.length; ++i)
691: bothKeys[i] = superKeys[i];
692: for (int i = 0; i < mapKeys.length; ++i)
693: bothKeys[i + superKeys.length] = mapKeys[i];
694: return bothKeys;
695: }
696:
697: }
698:
699: static class DefaultKeymap implements Keymap
700: {
701: String name;
702: Keymap parent;
703: Hashtable map;
704: Action defaultAction;
705:
706: public DefaultKeymap(String name)
707: {
708: this.name = name;
709: this.map = new Hashtable();
710: }
711:
712: public void addActionForKeyStroke(KeyStroke key, Action a)
713: {
714: map.put(key, a);
715: }
716:
717:
726: public Action getAction(KeyStroke key)
727: {
728: if (map.containsKey(key))
729: return (Action) map.get(key);
730: else if (parent != null)
731: return parent.getAction(key);
732: else
733: return null;
734: }
735:
736: public Action[] getBoundActions()
737: {
738: Action [] ret = new Action[map.size()];
739: Enumeration e = map.elements();
740: int i = 0;
741: while (e.hasMoreElements())
742: {
743: ret[i++] = (Action) e.nextElement();
744: }
745: return ret;
746: }
747:
748: public KeyStroke[] getBoundKeyStrokes()
749: {
750: KeyStroke [] ret = new KeyStroke[map.size()];
751: Enumeration e = map.keys();
752: int i = 0;
753: while (e.hasMoreElements())
754: {
755: ret[i++] = (KeyStroke) e.nextElement();
756: }
757: return ret;
758: }
759:
760: public Action getDefaultAction()
761: {
762: return defaultAction;
763: }
764:
765: public KeyStroke[] getKeyStrokesForAction(Action a)
766: {
767: int i = 0;
768: Enumeration e = map.keys();
769: while (e.hasMoreElements())
770: {
771: if (map.get(e.nextElement()).equals(a))
772: ++i;
773: }
774: KeyStroke [] ret = new KeyStroke[i];
775: i = 0;
776: e = map.keys();
777: while (e.hasMoreElements())
778: {
779: KeyStroke k = (KeyStroke) e.nextElement();
780: if (map.get(k).equals(a))
781: ret[i++] = k;
782: }
783: return ret;
784: }
785:
786: public String getName()
787: {
788: return name;
789: }
790:
791: public Keymap getResolveParent()
792: {
793: return parent;
794: }
795:
796: public boolean isLocallyDefined(KeyStroke key)
797: {
798: return map.containsKey(key);
799: }
800:
801: public void removeBindings()
802: {
803: map.clear();
804: }
805:
806: public void removeKeyStrokeBinding(KeyStroke key)
807: {
808: map.remove(key);
809: }
810:
811: public void setDefaultAction(Action a)
812: {
813: defaultAction = a;
814: }
815:
816: public void setResolveParent(Keymap p)
817: {
818: parent = p;
819: }
820: }
821:
822: class DefaultTransferHandler extends TransferHandler
823: {
824: public boolean canImport(JComponent component, DataFlavor[] flavors)
825: {
826: JTextComponent textComponent = (JTextComponent) component;
827:
828: if (! (textComponent.isEnabled()
829: && textComponent.isEditable()
830: && flavors != null))
831: return false;
832:
833: for (int i = 0; i < flavors.length; ++i)
834: if (flavors[i].equals(DataFlavor.stringFlavor))
835: return true;
836:
837: return false;
838: }
839:
840: public void exportToClipboard(JComponent component, Clipboard clipboard,
841: int action)
842: {
843: JTextComponent textComponent = (JTextComponent) component;
844: int start = textComponent.getSelectionStart();
845: int end = textComponent.getSelectionEnd();
846:
847: if (start == end)
848: return;
849:
850: try
851: {
852:
853: String data = textComponent.getDocument().getText(start, end);
854: StringSelection selection = new StringSelection(data);
855: clipboard.setContents(selection, null);
856:
857:
858: if (action == MOVE)
859: doc.remove(start, end - start);
860: }
861: catch (BadLocationException e)
862: {
863:
864: }
865: }
866:
867: public int getSourceActions()
868: {
869: return NONE;
870: }
871:
872: public boolean importData(JComponent component, Transferable transferable)
873: {
874: DataFlavor flavor = null;
875: DataFlavor[] flavors = transferable.getTransferDataFlavors();
876:
877: if (flavors == null)
878: return false;
879:
880: for (int i = 0; i < flavors.length; ++i)
881: if (flavors[i].equals(DataFlavor.stringFlavor))
882: flavor = flavors[i];
883:
884: if (flavor == null)
885: return false;
886:
887: try
888: {
889: JTextComponent textComponent = (JTextComponent) component;
890: String data = (String) transferable.getTransferData(flavor);
891: textComponent.replaceSelection(data);
892: return true;
893: }
894: catch (IOException e)
895: {
896:
897: }
898: catch (UnsupportedFlavorException e)
899: {
900:
901: }
902:
903: return false;
904: }
905: }
906:
907: private static final long serialVersionUID = -8796518220218978795L;
908:
909: public static final String DEFAULT_KEYMAP = "default";
910: public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
911:
912: private static DefaultTransferHandler defaultTransferHandler;
913: private static Hashtable keymaps = new Hashtable();
914: private Keymap keymap;
915: private char focusAccelerator = '\0';
916: private NavigationFilter navigationFilter;
917:
918:
930: public static Keymap getKeymap(String n)
931: {
932: return (Keymap) keymaps.get(n);
933: }
934:
935:
946: public static Keymap removeKeymap(String n)
947: {
948: Keymap km = (Keymap) keymaps.get(n);
949: keymaps.remove(n);
950: return km;
951: }
952:
953:
969: public static Keymap addKeymap(String n, Keymap parent)
970: {
971: Keymap k = new DefaultKeymap(n);
972: k.setResolveParent(parent);
973: if (n != null)
974: keymaps.put(n, k);
975: return k;
976: }
977:
978:
986: public Keymap getKeymap()
987: {
988: return keymap;
989: }
990:
991:
1000: public void setKeymap(Keymap k)
1001: {
1002:
1003:
1004:
1005:
1006:
1007:
1008:
1009:
1010:
1011:
1012:
1013: KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k));
1014: InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED);
1015: if (childInputMap == null)
1016: setInputMap(JComponent.WHEN_FOCUSED, kw);
1017: else
1018: {
1019: while (childInputMap.getParent() != null
1020: && !(childInputMap.getParent() instanceof KeymapWrapper)
1021: && !(childInputMap.getParent() instanceof InputMapUIResource))
1022: childInputMap = childInputMap.getParent();
1023:
1024:
1025: if (childInputMap.getParent() == null)
1026: childInputMap.setParent(kw);
1027:
1028:
1029:
1030: else if (childInputMap.getParent() instanceof KeymapWrapper)
1031: {
1032: if (kw == null)
1033: childInputMap.setParent(childInputMap.getParent().getParent());
1034: else
1035: {
1036: kw.setParent(childInputMap.getParent().getParent());
1037: childInputMap.setParent(kw);
1038: }
1039: }
1040:
1041:
1042:
1043: else if (childInputMap.getParent() instanceof InputMapUIResource)
1044: {
1045: if (kw != null)
1046: {
1047: kw.setParent(childInputMap.getParent());
1048: childInputMap.setParent(kw);
1049: }
1050: }
1051: }
1052:
1053:
1054:
1055: KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k));
1056: ActionMap childActionMap = getActionMap();
1057: if (childActionMap == null)
1058: setActionMap(kam);
1059: else
1060: {
1061: while (childActionMap.getParent() != null
1062: && !(childActionMap.getParent() instanceof KeymapActionMap)
1063: && !(childActionMap.getParent() instanceof ActionMapUIResource))
1064: childActionMap = childActionMap.getParent();
1065:
1066:
1067: if (childActionMap.getParent() == null)
1068: childActionMap.setParent(kam);
1069:
1070:
1071:
1072: else if (childActionMap.getParent() instanceof KeymapActionMap)
1073: {
1074: if (kam == null)
1075: childActionMap.setParent(childActionMap.getParent().getParent());
1076: else
1077: {
1078: kam.setParent(childActionMap.getParent().getParent());
1079: childActionMap.setParent(kam);
1080: }
1081: }
1082:
1083:
1084:
1085: else if (childActionMap.getParent() instanceof ActionMapUIResource)
1086: {
1087: if (kam != null)
1088: {
1089: kam.setParent(childActionMap.getParent());
1090: childActionMap.setParent(kam);
1091: }
1092: }
1093: }
1094:
1095:
1096:
1097: Keymap old = keymap;
1098: keymap = k;
1099: firePropertyChange("keymap", old, k);
1100: }
1101:
1102:
1118: public static void loadKeymap(Keymap map,
1119: JTextComponent.KeyBinding[] bindings,
1120: Action[] actions)
1121: {
1122: Hashtable acts = new Hashtable(actions.length);
1123: for (int i = 0; i < actions.length; ++i)
1124: acts.put(actions[i].getValue(Action.NAME), actions[i]);
1125: for (int i = 0; i < bindings.length; ++i)
1126: if (acts.containsKey(bindings[i].actionName))
1127: map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName));
1128: }
1129:
1130:
1143: public Action[] getActions()
1144: {
1145: return getUI().getEditorKit(this).getActions();
1146: }
1147:
1148:
1149: Document doc;
1150: Caret caret;
1151: boolean editable;
1152:
1153: private Highlighter highlighter;
1154: private Color caretColor;
1155: private Color disabledTextColor;
1156: private Color selectedTextColor;
1157: private Color selectionColor;
1158: private Insets margin;
1159: private boolean dragEnabled;
1160:
1161:
1164: public JTextComponent()
1165: {
1166: Keymap defkeymap = getKeymap(DEFAULT_KEYMAP);
1167: if (defkeymap == null)
1168: {
1169: defkeymap = addKeymap(DEFAULT_KEYMAP, null);
1170: defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction());
1171: }
1172:
1173: setFocusable(true);
1174: setEditable(true);
1175: enableEvents(AWTEvent.KEY_EVENT_MASK);
1176: setOpaque(true);
1177: updateUI();
1178: }
1179:
1180: public void setDocument(Document newDoc)
1181: {
1182: Document oldDoc = doc;
1183: try
1184: {
1185: if (oldDoc instanceof AbstractDocument)
1186: ((AbstractDocument) oldDoc).readLock();
1187:
1188: doc = newDoc;
1189: firePropertyChange("document", oldDoc, newDoc);
1190: }
1191: finally
1192: {
1193: if (oldDoc instanceof AbstractDocument)
1194: ((AbstractDocument) oldDoc).readUnlock();
1195: }
1196: revalidate();
1197: repaint();
1198: }
1199:
1200: public Document getDocument()
1201: {
1202: return doc;
1203: }
1204:
1205:
1210: public AccessibleContext getAccessibleContext()
1211: {
1212: return new AccessibleJTextComponent();
1213: }
1214:
1215: public void setMargin(Insets m)
1216: {
1217: margin = m;
1218: }
1219:
1220: public Insets getMargin()
1221: {
1222: return margin;
1223: }
1224:
1225: public void setText(String text)
1226: {
1227: try
1228: {
1229: if (doc instanceof AbstractDocument)
1230: ((AbstractDocument) doc).replace(0, doc.getLength(), text, null);
1231: else
1232: {
1233: doc.remove(0, doc.getLength());
1234: doc.insertString(0, text, null);
1235: }
1236: }
1237: catch (BadLocationException e)
1238: {
1239:
1240: throw (InternalError) new InternalError().initCause(e);
1241: }
1242: }
1243:
1244:
1251: public String getText()
1252: {
1253: if (doc == null)
1254: return null;
1255:
1256: try
1257: {
1258: return doc.getText(0, doc.getLength());
1259: }
1260: catch (BadLocationException e)
1261: {
1262:
1263: return "";
1264: }
1265: }
1266:
1267:
1277: public String getText(int offset, int length)
1278: throws BadLocationException
1279: {
1280: return getDocument().getText(offset, length);
1281: }
1282:
1283:
1290: public String getSelectedText()
1291: {
1292: int start = getSelectionStart();
1293: int offset = getSelectionEnd() - start;
1294:
1295: if (offset <= 0)
1296: return null;
1297:
1298: try
1299: {
1300: return doc.getText(start, offset);
1301: }
1302: catch (BadLocationException e)
1303: {
1304:
1305: return null;
1306: }
1307: }
1308:
1309:
1315: public String getUIClassID()
1316: {
1317: return "TextComponentUI";
1318: }
1319:
1320:
1323: protected String paramString()
1324: {
1325:
1326: return super.paramString();
1327: }
1328:
1329:
1334: public TextUI getUI()
1335: {
1336: return (TextUI) ui;
1337: }
1338:
1339:
1344: public void setUI(TextUI newUI)
1345: {
1346: super.setUI(newUI);
1347: }
1348:
1349:
1353: public void updateUI()
1354: {
1355: setUI((TextUI) UIManager.getUI(this));
1356: }
1357:
1358: public Dimension getPreferredScrollableViewportSize()
1359: {
1360: return getPreferredSize();
1361: }
1362:
1363: public int getScrollableUnitIncrement(Rectangle visible, int orientation,
1364: int direction)
1365: {
1366:
1367: if (orientation == SwingConstants.HORIZONTAL)
1368: return visible.width / 10;
1369: else if (orientation == SwingConstants.VERTICAL)
1370: return visible.height / 10;
1371: else
1372: throw new IllegalArgumentException("orientation must be either "
1373: + "javax.swing.SwingConstants.VERTICAL "
1374: + "or "
1375: + "javax.swing.SwingConstants.HORIZONTAL"
1376: );
1377: }
1378:
1379: public int getScrollableBlockIncrement(Rectangle visible, int orientation,
1380: int direction)
1381: {
1382:
1383: if (orientation == SwingConstants.HORIZONTAL)
1384: return visible.width;
1385: else if (orientation == SwingConstants.VERTICAL)
1386: return visible.height;
1387: else
1388: throw new IllegalArgumentException("orientation must be either "
1389: + "javax.swing.SwingConstants.VERTICAL "
1390: + "or "
1391: + "javax.swing.SwingConstants.HORIZONTAL"
1392: );
1393: }
1394:
1395:
1400: public boolean isEditable()
1401: {
1402: return editable;
1403: }
1404:
1405:
1410: public void setEditable(boolean newValue)
1411: {
1412: if (editable == newValue)
1413: return;
1414:
1415: boolean oldValue = editable;
1416: editable = newValue;
1417: firePropertyChange("editable", oldValue, newValue);
1418: }
1419:
1420:
1425: public Caret getCaret()
1426: {
1427: return caret;
1428: }
1429:
1430:
1435: public void setCaret(Caret newCaret)
1436: {
1437: if (caret != null)
1438: caret.deinstall(this);
1439:
1440: Caret oldCaret = caret;
1441: caret = newCaret;
1442:
1443: if (caret != null)
1444: caret.install(this);
1445:
1446: firePropertyChange("caret", oldCaret, newCaret);
1447: }
1448:
1449: public Color getCaretColor()
1450: {
1451: return caretColor;
1452: }
1453:
1454: public void setCaretColor(Color newColor)
1455: {
1456: Color oldCaretColor = caretColor;
1457: caretColor = newColor;
1458: firePropertyChange("caretColor", oldCaretColor, newColor);
1459: }
1460:
1461: public Color getDisabledTextColor()
1462: {
1463: return disabledTextColor;
1464: }
1465:
1466: public void setDisabledTextColor(Color newColor)
1467: {
1468: Color oldColor = disabledTextColor;
1469: disabledTextColor = newColor;
1470: firePropertyChange("disabledTextColor", oldColor, newColor);
1471: }
1472:
1473: public Color getSelectedTextColor()
1474: {
1475: return selectedTextColor;
1476: }
1477:
1478: public void setSelectedTextColor(Color newColor)
1479: {
1480: Color oldColor = selectedTextColor;
1481: selectedTextColor = newColor;
1482: firePropertyChange("selectedTextColor", oldColor, newColor);
1483: }
1484:
1485: public Color getSelectionColor()
1486: {
1487: return selectionColor;
1488: }
1489:
1490: public void setSelectionColor(Color newColor)
1491: {
1492: Color oldColor = selectionColor;
1493: selectionColor = newColor;
1494: firePropertyChange("selectionColor", oldColor, newColor);
1495: }
1496:
1497:
1502: public int getCaretPosition()
1503: {
1504: return caret.getDot();
1505: }
1506:
1507:
1512: public void setCaretPosition(int position)
1513: {
1514: if (doc == null)
1515: return;
1516:
1517: if (position < 0 || position > doc.getLength())
1518: throw new IllegalArgumentException();
1519:
1520: caret.setDot(position);
1521: }
1522:
1523:
1527: public void moveCaretPosition(int position)
1528: {
1529: if (doc == null)
1530: return;
1531:
1532: if (position < 0 || position > doc.getLength())
1533: throw new IllegalArgumentException();
1534:
1535: caret.moveDot(position);
1536: }
1537:
1538: public Highlighter getHighlighter()
1539: {
1540: return highlighter;
1541: }
1542:
1543: public void setHighlighter(Highlighter newHighlighter)
1544: {
1545: if (highlighter != null)
1546: highlighter.deinstall(this);
1547:
1548: Highlighter oldHighlighter = highlighter;
1549: highlighter = newHighlighter;
1550:
1551: if (highlighter != null)
1552: highlighter.install(this);
1553:
1554: firePropertyChange("highlighter", oldHighlighter, newHighlighter);
1555: }
1556:
1557:
1562: public int getSelectionStart()
1563: {
1564: return Math.min(caret.getDot(), caret.getMark());
1565: }
1566:
1567:
1572: public void setSelectionStart(int start)
1573: {
1574: select(start, getSelectionEnd());
1575: }
1576:
1577:
1582: public int getSelectionEnd()
1583: {
1584: return Math.max(caret.getDot(), caret.getMark());
1585: }
1586:
1587:
1592: public void setSelectionEnd(int end)
1593: {
1594: select(getSelectionStart(), end);
1595: }
1596:
1597:
1603: public void select(int start, int end)
1604: {
1605: int length = doc.getLength();
1606:
1607: start = Math.max(start, 0);
1608: start = Math.min(start, length);
1609:
1610: end = Math.max(end, start);
1611: end = Math.min(end, length);
1612:
1613: setCaretPosition(start);
1614: moveCaretPosition(end);
1615: }
1616:
1617:
1620: public void selectAll()
1621: {
1622: select(0, doc.getLength());
1623: }
1624:
1625: public synchronized void replaceSelection(String content)
1626: {
1627: int dot = caret.getDot();
1628: int mark = caret.getMark();
1629:
1630:
1631: if (content == null)
1632: {
1633: caret.setDot(dot);
1634: return;
1635: }
1636:
1637: try
1638: {
1639: int start = getSelectionStart();
1640: int end = getSelectionEnd();
1641:
1642:
1643: if (dot != mark)
1644: doc.remove(start, end - start);
1645:
1646:
1647: doc.insertString(start, content, null);
1648:
1649:
1650: dot = start + content.length();
1651: setCaretPosition(dot);
1652:
1653:
1654: caret.setMagicCaretPosition(modelToView(dot).getLocation());
1655: }
1656: catch (BadLocationException e)
1657: {
1658:
1659: }
1660: }
1661:
1662: public boolean getScrollableTracksViewportHeight()
1663: {
1664: if (getParent() instanceof JViewport)
1665: return getParent().getHeight() > getPreferredSize().height;
1666:
1667: return false;
1668: }
1669:
1670: public boolean getScrollableTracksViewportWidth()
1671: {
1672: boolean res = false;;
1673: Container c = getParent();
1674: if (c instanceof JViewport)
1675: res = ((JViewport) c).getExtentSize().width > getPreferredSize().width;
1676:
1677: return res;
1678: }
1679:
1680:
1685: public void addCaretListener(CaretListener listener)
1686: {
1687: listenerList.add(CaretListener.class, listener);
1688: }
1689:
1690:
1695: public void removeCaretListener(CaretListener listener)
1696: {
1697: listenerList.remove(CaretListener.class, listener);
1698: }
1699:
1700:
1705: public CaretListener[] getCaretListeners()
1706: {
1707: return (CaretListener[]) getListeners(CaretListener.class);
1708: }
1709:
1710:
1716: protected void fireCaretUpdate(CaretEvent event)
1717: {
1718: CaretListener[] listeners = getCaretListeners();
1719:
1720: for (int index = 0; index < listeners.length; ++index)
1721: listeners[index].caretUpdate(event);
1722: }
1723:
1724:
1729: public void addInputMethodListener(InputMethodListener listener)
1730: {
1731: listenerList.add(InputMethodListener.class, listener);
1732: }
1733:
1734:
1739: public void removeInputMethodListener(InputMethodListener listener)
1740: {
1741: listenerList.remove(InputMethodListener.class, listener);
1742: }
1743:
1744:
1749: public InputMethodListener[] getInputMethodListeners()
1750: {
1751: return (InputMethodListener[]) getListeners(InputMethodListener.class);
1752: }
1753:
1754: public Rectangle modelToView(int position) throws BadLocationException
1755: {
1756: return getUI().modelToView(this, position);
1757: }
1758:
1759: public boolean getDragEnabled()
1760: {
1761: return dragEnabled;
1762: }
1763:
1764: public void setDragEnabled(boolean enabled)
1765: {
1766: dragEnabled = enabled;
1767: }
1768:
1769: public int viewToModel(Point pt)
1770: {
1771: return getUI().viewToModel(this, pt);
1772: }
1773:
1774: public void copy()
1775: {
1776: if (isEnabled())
1777: doTransferAction("copy", TransferHandler.getCopyAction());
1778: }
1779:
1780: public void cut()
1781: {
1782: if (editable && isEnabled())
1783: doTransferAction("cut", TransferHandler.getCutAction());
1784: }
1785:
1786: public void paste()
1787: {
1788: if (editable && isEnabled())
1789: doTransferAction("paste", TransferHandler.getPasteAction());
1790: }
1791:
1792: private void doTransferAction(String name, Action action)
1793: {
1794:
1795: if (getTransferHandler() == null)
1796: {
1797: if (defaultTransferHandler == null)
1798: defaultTransferHandler = new DefaultTransferHandler();
1799:
1800: setTransferHandler(defaultTransferHandler);
1801: }
1802:
1803:
1804: ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1805: action.getValue(Action.NAME).toString());
1806: action.actionPerformed(event);
1807: }
1808:
1809: public void setFocusAccelerator(char newKey)
1810: {
1811: if (focusAccelerator == newKey)
1812: return;
1813:
1814: char oldKey = focusAccelerator;
1815: focusAccelerator = newKey;
1816: firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);
1817: }
1818:
1819: public char getFocusAccelerator()
1820: {
1821: return focusAccelerator;
1822: }
1823:
1824:
1827: public NavigationFilter getNavigationFilter()
1828: {
1829: return navigationFilter;
1830: }
1831:
1832:
1835: public void setNavigationFilter(NavigationFilter filter)
1836: {
1837: navigationFilter = filter;
1838: }
1839:
1840:
1857: public void read(Reader input, Object streamDescription)
1858: throws IOException
1859: {
1860: if (streamDescription != null)
1861: {
1862: Document d = getDocument();
1863: if (d != null)
1864: d.putProperty(Document.StreamDescriptionProperty, streamDescription);
1865: }
1866:
1867: StringBuffer b = new StringBuffer();
1868: int c;
1869:
1870:
1871: while ((c = input.read()) >= 0)
1872: b.append((char) c);
1873:
1874: setText(b.toString());
1875: }
1876:
1877:
1885: public void write(Writer output)
1886: throws IOException
1887: {
1888: output.write(getText());
1889: }
1890:
1891:
1901: public String getToolTipText(MouseEvent ev)
1902: {
1903: return getUI().getToolTipText(this, ev.getPoint());
1904: }
1905: }