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: 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: import ;
90:
91: public abstract class JTextComponent extends JComponent
92: implements Scrollable, Accessible
93: {
94:
100: public class AccessibleJTextComponent extends AccessibleJComponent implements
101: AccessibleText, CaretListener, DocumentListener, AccessibleAction,
102: AccessibleEditableText
103: {
104: private static final long serialVersionUID = 7664188944091413696L;
105:
106:
109: private int caretDot;
110:
111:
114: public AccessibleJTextComponent()
115: {
116: super();
117: JTextComponent.this.addCaretListener(this);
118: caretDot = getCaretPosition();
119: }
120:
121:
127: public int getCaretPosition()
128: {
129: return JTextComponent.this.getCaretPosition();
130: }
131:
132:
138: public String getSelectedText()
139: {
140: return JTextComponent.this.getSelectedText();
141: }
142:
143:
153: public int getSelectionStart()
154: {
155: if (getSelectedText() == null
156: || (JTextComponent.this.getText().equals("")))
157: return 0;
158: return JTextComponent.this.getSelectionStart();
159: }
160:
161:
171: public int getSelectionEnd()
172: {
173: return JTextComponent.this.getSelectionEnd();
174: }
175:
176:
182: public void caretUpdate(CaretEvent e)
183: {
184: int dot = e.getDot();
185: int mark = e.getMark();
186: if (caretDot != dot)
187: {
188: firePropertyChange(ACCESSIBLE_CARET_PROPERTY, new Integer(caretDot),
189: new Integer(dot));
190: caretDot = dot;
191: }
192: if (mark != dot)
193: {
194: firePropertyChange(ACCESSIBLE_SELECTION_PROPERTY, null,
195: getSelectedText());
196: }
197: }
198:
199:
204: public AccessibleStateSet getAccessibleStateSet()
205: {
206: AccessibleStateSet state = super.getAccessibleStateSet();
207: if (isEditable())
208: state.add(AccessibleState.EDITABLE);
209: return state;
210: }
211:
212:
219: public AccessibleRole getAccessibleRole()
220: {
221: return AccessibleRole.TEXT;
222: }
223:
224:
230: public AccessibleEditableText getAccessibleEditableText()
231: {
232: return this;
233: }
234:
235:
243: public AccessibleText getAccessibleText()
244: {
245: return this;
246: }
247:
248:
255: public void insertUpdate(DocumentEvent e)
256: {
257: firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null,
258: new Integer(e.getOffset()));
259: }
260:
261:
268: public void removeUpdate(DocumentEvent e)
269: {
270: firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null,
271: new Integer(e.getOffset()));
272: }
273:
274:
281: public void changedUpdate(DocumentEvent e)
282: {
283: firePropertyChange(ACCESSIBLE_TEXT_PROPERTY, null,
284: new Integer(e.getOffset()));
285: }
286:
287:
296: public int getIndexAtPoint(Point p)
297: {
298: return viewToModel(p);
299: }
300:
301:
311: public Rectangle getCharacterBounds(int index)
312: {
313:
314:
315: Rectangle bounds = null;
316: if (index >= 0 && index < doc.getLength() - 1)
317: {
318: if (doc instanceof AbstractDocument)
319: ((AbstractDocument) doc).readLock();
320: try
321: {
322: TextUI ui = getUI();
323: if (ui != null)
324: {
325:
326: Rectangle rect = new Rectangle();
327: Insets insets = getInsets();
328: rect.x = insets.left;
329: rect.y = insets.top;
330: rect.width = getWidth() - insets.left - insets.right;
331: rect.height = getHeight() - insets.top - insets.bottom;
332: View rootView = ui.getRootView(JTextComponent.this);
333: if (rootView != null)
334: {
335: rootView.setSize(rect.width, rect.height);
336: Shape s = rootView.modelToView(index,
337: Position.Bias.Forward,
338: index + 1,
339: Position.Bias.Backward,
340: rect);
341: if (s != null)
342: bounds = s.getBounds();
343: }
344: }
345: }
346: catch (BadLocationException ex)
347: {
348:
349: }
350: finally
351: {
352: if (doc instanceof AbstractDocument)
353: ((AbstractDocument) doc).readUnlock();
354: }
355: }
356: return bounds;
357: }
358:
359:
364: public int getCharCount()
365: {
366: return JTextComponent.this.getText().length();
367: }
368:
369:
377: public AttributeSet getCharacterAttribute(int index)
378: {
379: AttributeSet atts;
380: if (doc instanceof AbstractDocument)
381: ((AbstractDocument) doc).readLock();
382: try
383: {
384: Element el = doc.getDefaultRootElement();
385: while (! el.isLeaf())
386: {
387: int i = el.getElementIndex(index);
388: el = el.getElement(i);
389: }
390: atts = el.getAttributes();
391: }
392: finally
393: {
394: if (doc instanceof AbstractDocument)
395: ((AbstractDocument) doc).readUnlock();
396: }
397: return atts;
398: }
399:
400:
409: public String getAtIndex(int part, int index)
410: {
411: return getAtIndexImpl(part, index, 0);
412: }
413:
414:
423: public String getAfterIndex(int part, int index)
424: {
425: return getAtIndexImpl(part, index, 1);
426: }
427:
428:
437: public String getBeforeIndex(int part, int index)
438: {
439: return getAtIndexImpl(part, index, -1);
440: }
441:
442:
451: private String getAtIndexImpl(int part, int index, int dir)
452: {
453: String ret = null;
454: if (doc instanceof AbstractDocument)
455: ((AbstractDocument) doc).readLock();
456: try
457: {
458: BreakIterator iter = null;
459: switch (part)
460: {
461: case CHARACTER:
462: iter = BreakIterator.getCharacterInstance(getLocale());
463: break;
464: case WORD:
465: iter = BreakIterator.getWordInstance(getLocale());
466: break;
467: case SENTENCE:
468: iter = BreakIterator.getSentenceInstance(getLocale());
469: break;
470: default:
471: break;
472: }
473: String text = doc.getText(0, doc.getLength() - 1);
474: iter.setText(text);
475: int start = index;
476: int end = index;
477: switch (dir)
478: {
479: case 0:
480: if (iter.isBoundary(index))
481: {
482: start = index;
483: end = iter.following(index);
484: }
485: else
486: {
487: start = iter.preceding(index);
488: end = iter.next();
489: }
490: break;
491: case 1:
492: start = iter.following(index);
493: end = iter.next();
494: break;
495: case -1:
496: end = iter.preceding(index);
497: start = iter.previous();
498: break;
499: default:
500: assert false;
501: }
502: ret = text.substring(start, end);
503: }
504: catch (BadLocationException ex)
505: {
506:
507: }
508: finally
509: {
510: if (doc instanceof AbstractDocument)
511: ((AbstractDocument) doc).readUnlock();
512: }
513: return ret;
514: }
515:
516:
522: public int getAccessibleActionCount()
523: {
524: return getActions().length;
525: }
526:
527:
535: public String getAccessibleActionDescription(int i)
536: {
537: String desc = null;
538: Action[] actions = getActions();
539: if (i >= 0 && i < actions.length)
540: desc = (String) actions[i].getValue(Action.NAME);
541: return desc;
542: }
543:
544:
552: public boolean doAccessibleAction(int i)
553: {
554: boolean ret = false;
555: Action[] actions = getActions();
556: if (i >= 0 && i < actions.length)
557: {
558: ActionEvent ev = new ActionEvent(JTextComponent.this,
559: ActionEvent.ACTION_PERFORMED, null);
560: actions[i].actionPerformed(ev);
561: ret = true;
562: }
563: return ret;
564: }
565:
566:
571: public void setTextContents(String s)
572: {
573: setText(s);
574: }
575:
576:
582: public void insertTextAtIndex(int index, String s)
583: {
584: try
585: {
586: doc.insertString(index, s, null);
587: }
588: catch (BadLocationException ex)
589: {
590:
591: ex.printStackTrace();
592: }
593: }
594:
595:
601: public String getTextRange(int start, int end)
602: {
603: try
604: {
605: return JTextComponent.this.getText(start, end - start);
606: }
607: catch (BadLocationException ble)
608: {
609: return "";
610: }
611: }
612:
613:
619: public void delete(int start, int end)
620: {
621: replaceText(start, end, "");
622: }
623:
624:
631: public void cut(int start, int end)
632: {
633: JTextComponent.this.select(start, end);
634: JTextComponent.this.cut();
635: }
636:
637:
642: public void paste(int start)
643: {
644: JTextComponent.this.setCaretPosition(start);
645: JTextComponent.this.paste();
646: }
647:
648:
656: public void replaceText(int start, int end, String s)
657: {
658: JTextComponent.this.select(start, end);
659: JTextComponent.this.replaceSelection(s);
660: }
661:
662:
668: public void selectText(int start, int end)
669: {
670: JTextComponent.this.select(start, end);
671: }
672:
673:
680: public void setAttributes(int start, int end, AttributeSet s)
681: {
682: if (doc instanceof StyledDocument)
683: {
684: StyledDocument sdoc = (StyledDocument) doc;
685: sdoc.setCharacterAttributes(start, end - start, s, true);
686: }
687: }
688: }
689:
690: public static class KeyBinding
691: {
692: public KeyStroke key;
693: public String actionName;
694:
695:
701: public KeyBinding(KeyStroke key, String actionName)
702: {
703: this.key = key;
704: this.actionName = actionName;
705: }
706: }
707:
708:
738:
739: private class KeymapWrapper extends InputMap
740: {
741: Keymap map;
742:
743: public KeymapWrapper(Keymap k)
744: {
745: map = k;
746: }
747:
748: public int size()
749: {
750: return map.getBoundKeyStrokes().length + super.size();
751: }
752:
753: public Object get(KeyStroke ks)
754: {
755: Action mapped = null;
756: Keymap m = map;
757: while(mapped == null && m != null)
758: {
759: mapped = m.getAction(ks);
760: if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED)
761: mapped = m.getDefaultAction();
762: if (mapped == null)
763: m = m.getResolveParent();
764: }
765:
766: if (mapped == null)
767: return super.get(ks);
768: else
769: return mapped;
770: }
771:
772: public KeyStroke[] keys()
773: {
774: KeyStroke[] superKeys = super.keys();
775: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
776: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
777: for (int i = 0; i < superKeys.length; ++i)
778: bothKeys[i] = superKeys[i];
779: for (int i = 0; i < mapKeys.length; ++i)
780: bothKeys[i + superKeys.length] = mapKeys[i];
781: return bothKeys;
782: }
783:
784: public KeyStroke[] allKeys()
785: {
786: KeyStroke[] superKeys = super.allKeys();
787: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
788: int skl = 0;
789: int mkl = 0;
790: if (superKeys != null)
791: skl = superKeys.length;
792: if (mapKeys != null)
793: mkl = mapKeys.length;
794: KeyStroke[] bothKeys = new KeyStroke[skl + mkl];
795: for (int i = 0; i < skl; ++i)
796: bothKeys[i] = superKeys[i];
797: for (int i = 0; i < mkl; ++i)
798: bothKeys[i + skl] = mapKeys[i];
799: return bothKeys;
800: }
801: }
802:
803: private class KeymapActionMap extends ActionMap
804: {
805: Keymap map;
806:
807: public KeymapActionMap(Keymap k)
808: {
809: map = k;
810: }
811:
812: public Action get(Object cmd)
813: {
814: if (cmd instanceof Action)
815: return (Action) cmd;
816: else
817: return super.get(cmd);
818: }
819:
820: public int size()
821: {
822: return map.getBoundKeyStrokes().length + super.size();
823: }
824:
825: public Object[] keys()
826: {
827: Object[] superKeys = super.keys();
828: Object[] mapKeys = map.getBoundKeyStrokes();
829: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
830: for (int i = 0; i < superKeys.length; ++i)
831: bothKeys[i] = superKeys[i];
832: for (int i = 0; i < mapKeys.length; ++i)
833: bothKeys[i + superKeys.length] = mapKeys[i];
834: return bothKeys;
835: }
836:
837: public Object[] allKeys()
838: {
839: Object[] superKeys = super.allKeys();
840: Object[] mapKeys = map.getBoundKeyStrokes();
841: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
842: for (int i = 0; i < superKeys.length; ++i)
843: bothKeys[i] = superKeys[i];
844: for (int i = 0; i < mapKeys.length; ++i)
845: bothKeys[i + superKeys.length] = mapKeys[i];
846: return bothKeys;
847: }
848:
849: }
850:
851: static class DefaultKeymap implements Keymap
852: {
853: String name;
854: Keymap parent;
855: Hashtable map;
856: Action defaultAction;
857:
858: public DefaultKeymap(String name)
859: {
860: this.name = name;
861: this.map = new Hashtable();
862: }
863:
864: public void addActionForKeyStroke(KeyStroke key, Action a)
865: {
866: map.put(key, a);
867: }
868:
869:
878: public Action getAction(KeyStroke key)
879: {
880: if (map.containsKey(key))
881: return (Action) map.get(key);
882: else if (parent != null)
883: return parent.getAction(key);
884: else
885: return null;
886: }
887:
888: public Action[] getBoundActions()
889: {
890: Action [] ret = new Action[map.size()];
891: Enumeration e = map.elements();
892: int i = 0;
893: while (e.hasMoreElements())
894: {
895: ret[i++] = (Action) e.nextElement();
896: }
897: return ret;
898: }
899:
900: public KeyStroke[] getBoundKeyStrokes()
901: {
902: KeyStroke [] ret = new KeyStroke[map.size()];
903: Enumeration e = map.keys();
904: int i = 0;
905: while (e.hasMoreElements())
906: {
907: ret[i++] = (KeyStroke) e.nextElement();
908: }
909: return ret;
910: }
911:
912: public Action getDefaultAction()
913: {
914: return defaultAction;
915: }
916:
917: public KeyStroke[] getKeyStrokesForAction(Action a)
918: {
919: int i = 0;
920: Enumeration e = map.keys();
921: while (e.hasMoreElements())
922: {
923: if (map.get(e.nextElement()).equals(a))
924: ++i;
925: }
926: KeyStroke [] ret = new KeyStroke[i];
927: i = 0;
928: e = map.keys();
929: while (e.hasMoreElements())
930: {
931: KeyStroke k = (KeyStroke) e.nextElement();
932: if (map.get(k).equals(a))
933: ret[i++] = k;
934: }
935: return ret;
936: }
937:
938: public String getName()
939: {
940: return name;
941: }
942:
943: public Keymap getResolveParent()
944: {
945: return parent;
946: }
947:
948: public boolean isLocallyDefined(KeyStroke key)
949: {
950: return map.containsKey(key);
951: }
952:
953: public void removeBindings()
954: {
955: map.clear();
956: }
957:
958: public void removeKeyStrokeBinding(KeyStroke key)
959: {
960: map.remove(key);
961: }
962:
963: public void setDefaultAction(Action a)
964: {
965: defaultAction = a;
966: }
967:
968: public void setResolveParent(Keymap p)
969: {
970: parent = p;
971: }
972: }
973:
974: class DefaultTransferHandler extends TransferHandler
975: {
976: public boolean canImport(JComponent component, DataFlavor[] flavors)
977: {
978: JTextComponent textComponent = (JTextComponent) component;
979:
980: if (! (textComponent.isEnabled()
981: && textComponent.isEditable()
982: && flavors != null))
983: return false;
984:
985: for (int i = 0; i < flavors.length; ++i)
986: if (flavors[i].equals(DataFlavor.stringFlavor))
987: return true;
988:
989: return false;
990: }
991:
992: public void exportToClipboard(JComponent component, Clipboard clipboard,
993: int action)
994: {
995: JTextComponent textComponent = (JTextComponent) component;
996: int start = textComponent.getSelectionStart();
997: int end = textComponent.getSelectionEnd();
998:
999: if (start == end)
1000: return;
1001:
1002: try
1003: {
1004:
1005: String data = textComponent.getDocument().getText(start, end);
1006: StringSelection selection = new StringSelection(data);
1007: clipboard.setContents(selection, null);
1008:
1009:
1010: if (action == MOVE)
1011: doc.remove(start, end - start);
1012: }
1013: catch (BadLocationException e)
1014: {
1015:
1016: }
1017: }
1018:
1019: public int getSourceActions()
1020: {
1021: return NONE;
1022: }
1023:
1024: public boolean importData(JComponent component, Transferable transferable)
1025: {
1026: DataFlavor flavor = null;
1027: DataFlavor[] flavors = transferable.getTransferDataFlavors();
1028:
1029: if (flavors == null)
1030: return false;
1031:
1032: for (int i = 0; i < flavors.length; ++i)
1033: if (flavors[i].equals(DataFlavor.stringFlavor))
1034: flavor = flavors[i];
1035:
1036: if (flavor == null)
1037: return false;
1038:
1039: try
1040: {
1041: JTextComponent textComponent = (JTextComponent) component;
1042: String data = (String) transferable.getTransferData(flavor);
1043: textComponent.replaceSelection(data);
1044: return true;
1045: }
1046: catch (IOException e)
1047: {
1048:
1049: }
1050: catch (UnsupportedFlavorException e)
1051: {
1052:
1053: }
1054:
1055: return false;
1056: }
1057: }
1058:
1059: private static final long serialVersionUID = -8796518220218978795L;
1060:
1061: public static final String DEFAULT_KEYMAP = "default";
1062: public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
1063:
1064: private static DefaultTransferHandler defaultTransferHandler;
1065: private static Hashtable keymaps = new Hashtable();
1066: private Keymap keymap;
1067: private char focusAccelerator = '\0';
1068: private NavigationFilter navigationFilter;
1069:
1070:
1082: public static Keymap getKeymap(String n)
1083: {
1084: return (Keymap) keymaps.get(n);
1085: }
1086:
1087:
1098: public static Keymap removeKeymap(String n)
1099: {
1100: Keymap km = (Keymap) keymaps.get(n);
1101: keymaps.remove(n);
1102: return km;
1103: }
1104:
1105:
1121: public static Keymap addKeymap(String n, Keymap parent)
1122: {
1123: Keymap k = new DefaultKeymap(n);
1124: k.setResolveParent(parent);
1125: if (n != null)
1126: keymaps.put(n, k);
1127: return k;
1128: }
1129:
1130:
1138: public Keymap getKeymap()
1139: {
1140: return keymap;
1141: }
1142:
1143:
1152: public void setKeymap(Keymap k)
1153: {
1154:
1155:
1156:
1157:
1158:
1159:
1160:
1161:
1162:
1163:
1164:
1165: KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k));
1166: InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED);
1167: if (childInputMap == null)
1168: setInputMap(JComponent.WHEN_FOCUSED, kw);
1169: else
1170: {
1171: while (childInputMap.getParent() != null
1172: && !(childInputMap.getParent() instanceof KeymapWrapper)
1173: && !(childInputMap.getParent() instanceof InputMapUIResource))
1174: childInputMap = childInputMap.getParent();
1175:
1176:
1177: if (childInputMap.getParent() == null)
1178: childInputMap.setParent(kw);
1179:
1180:
1181:
1182: else if (childInputMap.getParent() instanceof KeymapWrapper)
1183: {
1184: if (kw == null)
1185: childInputMap.setParent(childInputMap.getParent().getParent());
1186: else
1187: {
1188: kw.setParent(childInputMap.getParent().getParent());
1189: childInputMap.setParent(kw);
1190: }
1191: }
1192:
1193:
1194:
1195: else if (childInputMap.getParent() instanceof InputMapUIResource)
1196: {
1197: if (kw != null)
1198: {
1199: kw.setParent(childInputMap.getParent());
1200: childInputMap.setParent(kw);
1201: }
1202: }
1203: }
1204:
1205:
1206:
1207: KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k));
1208: ActionMap childActionMap = getActionMap();
1209: if (childActionMap == null)
1210: setActionMap(kam);
1211: else
1212: {
1213: while (childActionMap.getParent() != null
1214: && !(childActionMap.getParent() instanceof KeymapActionMap)
1215: && !(childActionMap.getParent() instanceof ActionMapUIResource))
1216: childActionMap = childActionMap.getParent();
1217:
1218:
1219: if (childActionMap.getParent() == null)
1220: childActionMap.setParent(kam);
1221:
1222:
1223:
1224: else if (childActionMap.getParent() instanceof KeymapActionMap)
1225: {
1226: if (kam == null)
1227: childActionMap.setParent(childActionMap.getParent().getParent());
1228: else
1229: {
1230: kam.setParent(childActionMap.getParent().getParent());
1231: childActionMap.setParent(kam);
1232: }
1233: }
1234:
1235:
1236:
1237: else if (childActionMap.getParent() instanceof ActionMapUIResource)
1238: {
1239: if (kam != null)
1240: {
1241: kam.setParent(childActionMap.getParent());
1242: childActionMap.setParent(kam);
1243: }
1244: }
1245: }
1246:
1247:
1248:
1249: Keymap old = keymap;
1250: keymap = k;
1251: firePropertyChange("keymap", old, k);
1252: }
1253:
1254:
1270: public static void loadKeymap(Keymap map,
1271: JTextComponent.KeyBinding[] bindings,
1272: Action[] actions)
1273: {
1274: Hashtable acts = new Hashtable(actions.length);
1275: for (int i = 0; i < actions.length; ++i)
1276: acts.put(actions[i].getValue(Action.NAME), actions[i]);
1277: for (int i = 0; i < bindings.length; ++i)
1278: if (acts.containsKey(bindings[i].actionName))
1279: map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName));
1280: }
1281:
1282:
1295: public Action[] getActions()
1296: {
1297: return getUI().getEditorKit(this).getActions();
1298: }
1299:
1300:
1301: Document doc;
1302: Caret caret;
1303: boolean editable;
1304:
1305: private Highlighter highlighter;
1306: private Color caretColor;
1307: private Color disabledTextColor;
1308: private Color selectedTextColor;
1309: private Color selectionColor;
1310: private Insets margin;
1311: private boolean dragEnabled;
1312:
1313:
1316: public JTextComponent()
1317: {
1318: Keymap defkeymap = getKeymap(DEFAULT_KEYMAP);
1319: if (defkeymap == null)
1320: {
1321: defkeymap = addKeymap(DEFAULT_KEYMAP, null);
1322: defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction());
1323: }
1324:
1325: setFocusable(true);
1326: setEditable(true);
1327: enableEvents(AWTEvent.KEY_EVENT_MASK);
1328: setOpaque(true);
1329: updateUI();
1330: }
1331:
1332: public void setDocument(Document newDoc)
1333: {
1334: Document oldDoc = doc;
1335: try
1336: {
1337: if (oldDoc instanceof AbstractDocument)
1338: ((AbstractDocument) oldDoc).readLock();
1339:
1340: doc = newDoc;
1341: firePropertyChange("document", oldDoc, newDoc);
1342: }
1343: finally
1344: {
1345: if (oldDoc instanceof AbstractDocument)
1346: ((AbstractDocument) oldDoc).readUnlock();
1347: }
1348: revalidate();
1349: repaint();
1350: }
1351:
1352: public Document getDocument()
1353: {
1354: return doc;
1355: }
1356:
1357:
1362: public AccessibleContext getAccessibleContext()
1363: {
1364: return new AccessibleJTextComponent();
1365: }
1366:
1367: public void setMargin(Insets m)
1368: {
1369: margin = m;
1370: }
1371:
1372: public Insets getMargin()
1373: {
1374: return margin;
1375: }
1376:
1377: public void setText(String text)
1378: {
1379: try
1380: {
1381: if (doc instanceof AbstractDocument)
1382: ((AbstractDocument) doc).replace(0, doc.getLength(), text, null);
1383: else
1384: {
1385: doc.remove(0, doc.getLength());
1386: doc.insertString(0, text, null);
1387: }
1388: }
1389: catch (BadLocationException e)
1390: {
1391:
1392: throw (InternalError) new InternalError().initCause(e);
1393: }
1394: }
1395:
1396:
1403: public String getText()
1404: {
1405: if (doc == null)
1406: return null;
1407:
1408: try
1409: {
1410: return doc.getText(0, doc.getLength());
1411: }
1412: catch (BadLocationException e)
1413: {
1414:
1415: return "";
1416: }
1417: }
1418:
1419:
1429: public String getText(int offset, int length)
1430: throws BadLocationException
1431: {
1432: return getDocument().getText(offset, length);
1433: }
1434:
1435:
1442: public String getSelectedText()
1443: {
1444: int start = getSelectionStart();
1445: int offset = getSelectionEnd() - start;
1446:
1447: if (offset <= 0)
1448: return null;
1449:
1450: try
1451: {
1452: return doc.getText(start, offset);
1453: }
1454: catch (BadLocationException e)
1455: {
1456:
1457: return null;
1458: }
1459: }
1460:
1461:
1467: public String getUIClassID()
1468: {
1469: return "TextComponentUI";
1470: }
1471:
1472:
1475: protected String paramString()
1476: {
1477:
1478: return super.paramString();
1479: }
1480:
1481:
1486: public TextUI getUI()
1487: {
1488: return (TextUI) ui;
1489: }
1490:
1491:
1496: public void setUI(TextUI newUI)
1497: {
1498: super.setUI(newUI);
1499: }
1500:
1501:
1505: public void updateUI()
1506: {
1507: setUI((TextUI) UIManager.getUI(this));
1508: }
1509:
1510: public Dimension getPreferredScrollableViewportSize()
1511: {
1512: return getPreferredSize();
1513: }
1514:
1515: public int getScrollableUnitIncrement(Rectangle visible, int orientation,
1516: int direction)
1517: {
1518:
1519: if (orientation == SwingConstants.HORIZONTAL)
1520: return visible.width / 10;
1521: else if (orientation == SwingConstants.VERTICAL)
1522: return visible.height / 10;
1523: else
1524: throw new IllegalArgumentException("orientation must be either "
1525: + "javax.swing.SwingConstants.VERTICAL "
1526: + "or "
1527: + "javax.swing.SwingConstants.HORIZONTAL"
1528: );
1529: }
1530:
1531: public int getScrollableBlockIncrement(Rectangle visible, int orientation,
1532: int direction)
1533: {
1534:
1535: if (orientation == SwingConstants.HORIZONTAL)
1536: return visible.width;
1537: else if (orientation == SwingConstants.VERTICAL)
1538: return visible.height;
1539: else
1540: throw new IllegalArgumentException("orientation must be either "
1541: + "javax.swing.SwingConstants.VERTICAL "
1542: + "or "
1543: + "javax.swing.SwingConstants.HORIZONTAL"
1544: );
1545: }
1546:
1547:
1552: public boolean isEditable()
1553: {
1554: return editable;
1555: }
1556:
1557:
1562: public void setEditable(boolean newValue)
1563: {
1564: if (editable == newValue)
1565: return;
1566:
1567: boolean oldValue = editable;
1568: editable = newValue;
1569: firePropertyChange("editable", oldValue, newValue);
1570: }
1571:
1572:
1577: public Caret getCaret()
1578: {
1579: return caret;
1580: }
1581:
1582:
1587: public void setCaret(Caret newCaret)
1588: {
1589: if (caret != null)
1590: caret.deinstall(this);
1591:
1592: Caret oldCaret = caret;
1593: caret = newCaret;
1594:
1595: if (caret != null)
1596: caret.install(this);
1597:
1598: firePropertyChange("caret", oldCaret, newCaret);
1599: }
1600:
1601: public Color getCaretColor()
1602: {
1603: return caretColor;
1604: }
1605:
1606: public void setCaretColor(Color newColor)
1607: {
1608: Color oldCaretColor = caretColor;
1609: caretColor = newColor;
1610: firePropertyChange("caretColor", oldCaretColor, newColor);
1611: }
1612:
1613: public Color getDisabledTextColor()
1614: {
1615: return disabledTextColor;
1616: }
1617:
1618: public void setDisabledTextColor(Color newColor)
1619: {
1620: Color oldColor = disabledTextColor;
1621: disabledTextColor = newColor;
1622: firePropertyChange("disabledTextColor", oldColor, newColor);
1623: }
1624:
1625: public Color getSelectedTextColor()
1626: {
1627: return selectedTextColor;
1628: }
1629:
1630: public void setSelectedTextColor(Color newColor)
1631: {
1632: Color oldColor = selectedTextColor;
1633: selectedTextColor = newColor;
1634: firePropertyChange("selectedTextColor", oldColor, newColor);
1635: }
1636:
1637: public Color getSelectionColor()
1638: {
1639: return selectionColor;
1640: }
1641:
1642: public void setSelectionColor(Color newColor)
1643: {
1644: Color oldColor = selectionColor;
1645: selectionColor = newColor;
1646: firePropertyChange("selectionColor", oldColor, newColor);
1647: }
1648:
1649:
1654: public int getCaretPosition()
1655: {
1656: return caret.getDot();
1657: }
1658:
1659:
1664: public void setCaretPosition(int position)
1665: {
1666: if (doc == null)
1667: return;
1668:
1669: if (position < 0 || position > doc.getLength())
1670: throw new IllegalArgumentException();
1671:
1672: caret.setDot(position);
1673: }
1674:
1675:
1679: public void moveCaretPosition(int position)
1680: {
1681: if (doc == null)
1682: return;
1683:
1684: if (position < 0 || position > doc.getLength())
1685: throw new IllegalArgumentException();
1686:
1687: caret.moveDot(position);
1688: }
1689:
1690: public Highlighter getHighlighter()
1691: {
1692: return highlighter;
1693: }
1694:
1695: public void setHighlighter(Highlighter newHighlighter)
1696: {
1697: if (highlighter != null)
1698: highlighter.deinstall(this);
1699:
1700: Highlighter oldHighlighter = highlighter;
1701: highlighter = newHighlighter;
1702:
1703: if (highlighter != null)
1704: highlighter.install(this);
1705:
1706: firePropertyChange("highlighter", oldHighlighter, newHighlighter);
1707: }
1708:
1709:
1714: public int getSelectionStart()
1715: {
1716: return Math.min(caret.getDot(), caret.getMark());
1717: }
1718:
1719:
1724: public void setSelectionStart(int start)
1725: {
1726: select(start, getSelectionEnd());
1727: }
1728:
1729:
1734: public int getSelectionEnd()
1735: {
1736: return Math.max(caret.getDot(), caret.getMark());
1737: }
1738:
1739:
1744: public void setSelectionEnd(int end)
1745: {
1746: select(getSelectionStart(), end);
1747: }
1748:
1749:
1755: public void select(int start, int end)
1756: {
1757: int length = doc.getLength();
1758:
1759: start = Math.max(start, 0);
1760: start = Math.min(start, length);
1761:
1762: end = Math.max(end, start);
1763: end = Math.min(end, length);
1764:
1765: setCaretPosition(start);
1766: moveCaretPosition(end);
1767: }
1768:
1769:
1772: public void selectAll()
1773: {
1774: select(0, doc.getLength());
1775: }
1776:
1777: public synchronized void replaceSelection(String content)
1778: {
1779: int dot = caret.getDot();
1780: int mark = caret.getMark();
1781:
1782:
1783: if (content == null)
1784: {
1785: caret.setDot(dot);
1786: return;
1787: }
1788:
1789: try
1790: {
1791: int start = getSelectionStart();
1792: int end = getSelectionEnd();
1793:
1794:
1795: if (dot != mark)
1796: doc.remove(start, end - start);
1797:
1798:
1799: doc.insertString(start, content, null);
1800:
1801:
1802: dot = start + content.length();
1803: setCaretPosition(dot);
1804:
1805:
1806: caret.setMagicCaretPosition(modelToView(dot).getLocation());
1807: }
1808: catch (BadLocationException e)
1809: {
1810:
1811: }
1812: }
1813:
1814: public boolean getScrollableTracksViewportHeight()
1815: {
1816: if (getParent() instanceof JViewport)
1817: return getParent().getHeight() > getPreferredSize().height;
1818:
1819: return false;
1820: }
1821:
1822: public boolean getScrollableTracksViewportWidth()
1823: {
1824: boolean res = false;
1825: Container c = getParent();
1826: if (c instanceof JViewport)
1827: res = ((JViewport) c).getExtentSize().width > getPreferredSize().width;
1828:
1829: return res;
1830: }
1831:
1832:
1837: public void addCaretListener(CaretListener listener)
1838: {
1839: listenerList.add(CaretListener.class, listener);
1840: }
1841:
1842:
1847: public void removeCaretListener(CaretListener listener)
1848: {
1849: listenerList.remove(CaretListener.class, listener);
1850: }
1851:
1852:
1857: public CaretListener[] getCaretListeners()
1858: {
1859: return (CaretListener[]) getListeners(CaretListener.class);
1860: }
1861:
1862:
1868: protected void fireCaretUpdate(CaretEvent event)
1869: {
1870: CaretListener[] listeners = getCaretListeners();
1871:
1872: for (int index = 0; index < listeners.length; ++index)
1873: listeners[index].caretUpdate(event);
1874: }
1875:
1876:
1881: public void addInputMethodListener(InputMethodListener listener)
1882: {
1883: listenerList.add(InputMethodListener.class, listener);
1884: }
1885:
1886:
1891: public void removeInputMethodListener(InputMethodListener listener)
1892: {
1893: listenerList.remove(InputMethodListener.class, listener);
1894: }
1895:
1896:
1901: public InputMethodListener[] getInputMethodListeners()
1902: {
1903: return (InputMethodListener[]) getListeners(InputMethodListener.class);
1904: }
1905:
1906: public Rectangle modelToView(int position) throws BadLocationException
1907: {
1908: return getUI().modelToView(this, position);
1909: }
1910:
1911: public boolean getDragEnabled()
1912: {
1913: return dragEnabled;
1914: }
1915:
1916: public void setDragEnabled(boolean enabled)
1917: {
1918: dragEnabled = enabled;
1919: }
1920:
1921: public int viewToModel(Point pt)
1922: {
1923: return getUI().viewToModel(this, pt);
1924: }
1925:
1926: public void copy()
1927: {
1928: if (isEnabled())
1929: doTransferAction("copy", TransferHandler.getCopyAction());
1930: }
1931:
1932: public void cut()
1933: {
1934: if (editable && isEnabled())
1935: doTransferAction("cut", TransferHandler.getCutAction());
1936: }
1937:
1938: public void paste()
1939: {
1940: if (editable && isEnabled())
1941: doTransferAction("paste", TransferHandler.getPasteAction());
1942: }
1943:
1944: private void doTransferAction(String name, Action action)
1945: {
1946:
1947: if (getTransferHandler() == null)
1948: {
1949: if (defaultTransferHandler == null)
1950: defaultTransferHandler = new DefaultTransferHandler();
1951:
1952: setTransferHandler(defaultTransferHandler);
1953: }
1954:
1955:
1956: ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1957: action.getValue(Action.NAME).toString());
1958: action.actionPerformed(event);
1959: }
1960:
1961: public void setFocusAccelerator(char newKey)
1962: {
1963: if (focusAccelerator == newKey)
1964: return;
1965:
1966: char oldKey = focusAccelerator;
1967: focusAccelerator = newKey;
1968: firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);
1969: }
1970:
1971: public char getFocusAccelerator()
1972: {
1973: return focusAccelerator;
1974: }
1975:
1976:
1979: public NavigationFilter getNavigationFilter()
1980: {
1981: return navigationFilter;
1982: }
1983:
1984:
1987: public void setNavigationFilter(NavigationFilter filter)
1988: {
1989: navigationFilter = filter;
1990: }
1991:
1992:
2009: public void read(Reader input, Object streamDescription)
2010: throws IOException
2011: {
2012: if (streamDescription != null)
2013: {
2014: Document d = getDocument();
2015: if (d != null)
2016: d.putProperty(Document.StreamDescriptionProperty, streamDescription);
2017: }
2018:
2019: StringBuffer b = new StringBuffer();
2020: int c;
2021:
2022:
2023: while ((c = input.read()) >= 0)
2024: b.append((char) c);
2025:
2026: setText(b.toString());
2027: }
2028:
2029:
2037: public void write(Writer output)
2038: throws IOException
2039: {
2040: output.write(getText());
2041: }
2042:
2043:
2053: public String getToolTipText(MouseEvent ev)
2054: {
2055: return getUI().getToolTipText(this, ev.getPoint());
2056: }
2057: }