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:
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84: import ;
85: import ;
86:
87: public abstract class JTextComponent extends JComponent
88: implements Scrollable, Accessible
89: {
90:
95: public class AccessibleJTextComponent extends AccessibleJComponent implements
96: AccessibleText, CaretListener, DocumentListener, AccessibleAction,
97: AccessibleEditableText
98: {
99: private static final long serialVersionUID = 7664188944091413696L;
100:
101:
102: int dot = 0;
103:
104:
105: JTextComponent textComp = JTextComponent.this;
106:
107:
111: public AccessibleJTextComponent()
112: {
113: super();
114: textComp.addCaretListener(this);
115: }
116:
117:
124: public int getCaretPosition()
125: {
126: dot = textComp.getCaretPosition();
127: return dot;
128: }
129:
130:
135: public String getSelectedText()
136: {
137: return textComp.getSelectedText();
138: }
139:
140:
147: public int getSelectionStart()
148: {
149: if (getSelectedText() == null || (textComp.getText().equals("")))
150: return 0;
151: return textComp.getSelectionStart();
152: }
153:
154:
162: public int getSelectionEnd()
163: {
164: if (getSelectedText() == null || (textComp.getText().equals("")))
165: return 0;
166: return textComp.getSelectionEnd();
167: }
168:
169:
178: public void caretUpdate(CaretEvent e)
179: {
180:
181: dot = e.getDot();
182: }
183:
184:
189: public AccessibleStateSet getAccessibleStateSet()
190: {
191: AccessibleStateSet state = super.getAccessibleStateSet();
192:
193: return state;
194: }
195:
196:
203: public AccessibleRole getAccessibleRole()
204: {
205: return AccessibleRole.TEXT;
206: }
207:
208:
213: public AccessibleEditableText getAccessibleEditableText()
214: {
215: return this;
216: }
217:
218:
228: public AccessibleText getAccessibleText()
229: {
230: return this;
231: }
232:
233:
239: public void insertUpdate(DocumentEvent e)
240: {
241:
242: }
243:
244:
250: public void removeUpdate(DocumentEvent e)
251: {
252:
253: }
254:
255:
261: public void changedUpdate(DocumentEvent e)
262: {
263:
264: }
265:
266:
273: public int getIndexAtPoint(Point p)
274: {
275: return 0;
276: }
277:
278:
291: public Rectangle getCharacterBounds(int index)
292: {
293: return null;
294: }
295:
296:
301: public int getCharCount()
302: {
303: return textComp.getText().length();
304: }
305:
306:
313: public AttributeSet getCharacterAttribute(int index)
314: {
315: return null;
316: }
317:
318:
326: public String getAtIndex(int part, int index)
327: {
328: return null;
329: }
330:
331:
339: public String getAfterIndex(int part, int index)
340: {
341: return null;
342: }
343:
344:
352: public String getBeforeIndex(int part, int index)
353: {
354: return null;
355: }
356:
357:
363: public int getAccessibleActionCount()
364: {
365: return 0;
366: }
367:
368:
376: public String getAccessibleActionDescription(int i)
377: {
378:
379: return super.getAccessibleDescription();
380: }
381:
382:
388: public boolean doAccessibleAction(int i)
389: {
390: return false;
391: }
392:
393:
398: public void setTextContents(String s)
399: {
400:
401: }
402:
403:
409: public void insertTextAtIndex(int index, String s)
410: {
411: replaceText(index, index, s);
412: }
413:
414:
420: public String getTextRange(int start, int end)
421: {
422: try
423: {
424: return textComp.getText(start, end - start);
425: }
426: catch (BadLocationException ble)
427: {
428: return "";
429: }
430: }
431:
432:
438: public void delete(int start, int end)
439: {
440: replaceText(start, end, "");
441: }
442:
443:
449: public void cut(int start, int end)
450: {
451: textComp.select(start, end);
452: textComp.cut();
453: }
454:
455:
460: public void paste(int start)
461: {
462: textComp.setCaretPosition(start);
463: textComp.paste();
464: }
465:
466:
473: public void replaceText(int start, int end, String s)
474: {
475: textComp.select(start, end);
476: textComp.replaceSelection(s);
477: }
478:
479:
485: public void selectText(int start, int end)
486: {
487: textComp.select(start, end);
488: }
489:
490:
497: public void setAttributes(int start, int end, AttributeSet s)
498: {
499:
500: }
501: }
502:
503: public static class KeyBinding
504: {
505: public KeyStroke key;
506: public String actionName;
507:
508:
514: public KeyBinding(KeyStroke key, String actionName)
515: {
516: this.key = key;
517: this.actionName = actionName;
518: }
519: }
520:
521:
551:
552: private class KeymapWrapper extends InputMap
553: {
554: Keymap map;
555:
556: public KeymapWrapper(Keymap k)
557: {
558: map = k;
559: }
560:
561: public int size()
562: {
563: return map.getBoundKeyStrokes().length + super.size();
564: }
565:
566: public Object get(KeyStroke ks)
567: {
568: Action mapped = null;
569: Keymap m = map;
570: while(mapped == null && m != null)
571: {
572: mapped = m.getAction(ks);
573: if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED)
574: mapped = m.getDefaultAction();
575: if (mapped == null)
576: m = m.getResolveParent();
577: }
578:
579: if (mapped == null)
580: return super.get(ks);
581: else
582: return mapped;
583: }
584:
585: public KeyStroke[] keys()
586: {
587: KeyStroke[] superKeys = super.keys();
588: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
589: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
590: for (int i = 0; i < superKeys.length; ++i)
591: bothKeys[i] = superKeys[i];
592: for (int i = 0; i < mapKeys.length; ++i)
593: bothKeys[i + superKeys.length] = mapKeys[i];
594: return bothKeys;
595: }
596:
597: public KeyStroke[] allKeys()
598: {
599: KeyStroke[] superKeys = super.allKeys();
600: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
601: int skl = 0;
602: int mkl = 0;
603: if (superKeys != null)
604: skl = superKeys.length;
605: if (mapKeys != null)
606: mkl = mapKeys.length;
607: KeyStroke[] bothKeys = new KeyStroke[skl + mkl];
608: for (int i = 0; i < skl; ++i)
609: bothKeys[i] = superKeys[i];
610: for (int i = 0; i < mkl; ++i)
611: bothKeys[i + skl] = mapKeys[i];
612: return bothKeys;
613: }
614: }
615:
616: private class KeymapActionMap extends ActionMap
617: {
618: Keymap map;
619:
620: public KeymapActionMap(Keymap k)
621: {
622: map = k;
623: }
624:
625: public Action get(Object cmd)
626: {
627: if (cmd instanceof Action)
628: return (Action) cmd;
629: else
630: return super.get(cmd);
631: }
632:
633: public int size()
634: {
635: return map.getBoundKeyStrokes().length + super.size();
636: }
637:
638: public Object[] keys()
639: {
640: Object[] superKeys = super.keys();
641: Object[] mapKeys = map.getBoundKeyStrokes();
642: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
643: for (int i = 0; i < superKeys.length; ++i)
644: bothKeys[i] = superKeys[i];
645: for (int i = 0; i < mapKeys.length; ++i)
646: bothKeys[i + superKeys.length] = mapKeys[i];
647: return bothKeys;
648: }
649:
650: public Object[] allKeys()
651: {
652: Object[] superKeys = super.allKeys();
653: Object[] mapKeys = map.getBoundKeyStrokes();
654: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
655: for (int i = 0; i < superKeys.length; ++i)
656: bothKeys[i] = superKeys[i];
657: for (int i = 0; i < mapKeys.length; ++i)
658: bothKeys[i + superKeys.length] = mapKeys[i];
659: return bothKeys;
660: }
661:
662: }
663:
664: static class DefaultKeymap implements Keymap
665: {
666: String name;
667: Keymap parent;
668: Hashtable map;
669: Action defaultAction;
670:
671: public DefaultKeymap(String name)
672: {
673: this.name = name;
674: this.map = new Hashtable();
675: }
676:
677: public void addActionForKeyStroke(KeyStroke key, Action a)
678: {
679: map.put(key, a);
680: }
681:
682:
691: public Action getAction(KeyStroke key)
692: {
693: if (map.containsKey(key))
694: return (Action) map.get(key);
695: else if (parent != null)
696: return parent.getAction(key);
697: else
698: return null;
699: }
700:
701: public Action[] getBoundActions()
702: {
703: Action [] ret = new Action[map.size()];
704: Enumeration e = map.elements();
705: int i = 0;
706: while (e.hasMoreElements())
707: {
708: ret[i++] = (Action) e.nextElement();
709: }
710: return ret;
711: }
712:
713: public KeyStroke[] getBoundKeyStrokes()
714: {
715: KeyStroke [] ret = new KeyStroke[map.size()];
716: Enumeration e = map.keys();
717: int i = 0;
718: while (e.hasMoreElements())
719: {
720: ret[i++] = (KeyStroke) e.nextElement();
721: }
722: return ret;
723: }
724:
725: public Action getDefaultAction()
726: {
727: return defaultAction;
728: }
729:
730: public KeyStroke[] getKeyStrokesForAction(Action a)
731: {
732: int i = 0;
733: Enumeration e = map.keys();
734: while (e.hasMoreElements())
735: {
736: if (map.get(e.nextElement()).equals(a))
737: ++i;
738: }
739: KeyStroke [] ret = new KeyStroke[i];
740: i = 0;
741: e = map.keys();
742: while (e.hasMoreElements())
743: {
744: KeyStroke k = (KeyStroke) e.nextElement();
745: if (map.get(k).equals(a))
746: ret[i++] = k;
747: }
748: return ret;
749: }
750:
751: public String getName()
752: {
753: return name;
754: }
755:
756: public Keymap getResolveParent()
757: {
758: return parent;
759: }
760:
761: public boolean isLocallyDefined(KeyStroke key)
762: {
763: return map.containsKey(key);
764: }
765:
766: public void removeBindings()
767: {
768: map.clear();
769: }
770:
771: public void removeKeyStrokeBinding(KeyStroke key)
772: {
773: map.remove(key);
774: }
775:
776: public void setDefaultAction(Action a)
777: {
778: defaultAction = a;
779: }
780:
781: public void setResolveParent(Keymap p)
782: {
783: parent = p;
784: }
785: }
786:
787: class DefaultTransferHandler extends TransferHandler
788: {
789: public boolean canImport(JComponent component, DataFlavor[] flavors)
790: {
791: JTextComponent textComponent = (JTextComponent) component;
792:
793: if (! (textComponent.isEnabled()
794: && textComponent.isEditable()
795: && flavors != null))
796: return false;
797:
798: for (int i = 0; i < flavors.length; ++i)
799: if (flavors[i].equals(DataFlavor.stringFlavor))
800: return true;
801:
802: return false;
803: }
804:
805: public void exportToClipboard(JComponent component, Clipboard clipboard,
806: int action)
807: {
808: JTextComponent textComponent = (JTextComponent) component;
809: int start = textComponent.getSelectionStart();
810: int end = textComponent.getSelectionEnd();
811:
812: if (start == end)
813: return;
814:
815: try
816: {
817:
818: String data = textComponent.getDocument().getText(start, end);
819: StringSelection selection = new StringSelection(data);
820: clipboard.setContents(selection, null);
821:
822:
823: if (action == MOVE)
824: doc.remove(start, end - start);
825: }
826: catch (BadLocationException e)
827: {
828:
829: }
830: }
831:
832: public int getSourceActions()
833: {
834: return NONE;
835: }
836:
837: public boolean importData(JComponent component, Transferable transferable)
838: {
839: DataFlavor flavor = null;
840: DataFlavor[] flavors = transferable.getTransferDataFlavors();
841:
842: if (flavors == null)
843: return false;
844:
845: for (int i = 0; i < flavors.length; ++i)
846: if (flavors[i].equals(DataFlavor.stringFlavor))
847: flavor = flavors[i];
848:
849: if (flavor == null)
850: return false;
851:
852: try
853: {
854: JTextComponent textComponent = (JTextComponent) component;
855: String data = (String) transferable.getTransferData(flavor);
856: textComponent.replaceSelection(data);
857: return true;
858: }
859: catch (IOException e)
860: {
861:
862: }
863: catch (UnsupportedFlavorException e)
864: {
865:
866: }
867:
868: return false;
869: }
870: }
871:
872: private static final long serialVersionUID = -8796518220218978795L;
873:
874: public static final String DEFAULT_KEYMAP = "default";
875: public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
876:
877: private static DefaultTransferHandler defaultTransferHandler;
878: private static Hashtable keymaps = new Hashtable();
879: private Keymap keymap;
880: private char focusAccelerator = '\0';
881: private NavigationFilter navigationFilter;
882:
883:
895: public static Keymap getKeymap(String n)
896: {
897: return (Keymap) keymaps.get(n);
898: }
899:
900:
911: public static Keymap removeKeymap(String n)
912: {
913: Keymap km = (Keymap) keymaps.get(n);
914: keymaps.remove(n);
915: return km;
916: }
917:
918:
934: public static Keymap addKeymap(String n, Keymap parent)
935: {
936: Keymap k = new DefaultKeymap(n);
937: k.setResolveParent(parent);
938: if (n != null)
939: keymaps.put(n, k);
940: return k;
941: }
942:
943:
951: public Keymap getKeymap()
952: {
953: return keymap;
954: }
955:
956:
965: public void setKeymap(Keymap k)
966: {
967:
968:
969:
970:
971:
972:
973:
974:
975:
976:
977:
978: KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k));
979: InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED);
980: if (childInputMap == null)
981: setInputMap(JComponent.WHEN_FOCUSED, kw);
982: else
983: {
984: while (childInputMap.getParent() != null
985: && !(childInputMap.getParent() instanceof KeymapWrapper)
986: && !(childInputMap.getParent() instanceof InputMapUIResource))
987: childInputMap = childInputMap.getParent();
988:
989:
990: if (childInputMap.getParent() == null)
991: childInputMap.setParent(kw);
992:
993:
994:
995: else if (childInputMap.getParent() instanceof KeymapWrapper)
996: {
997: if (kw == null)
998: childInputMap.setParent(childInputMap.getParent().getParent());
999: else
1000: {
1001: kw.setParent(childInputMap.getParent().getParent());
1002: childInputMap.setParent(kw);
1003: }
1004: }
1005:
1006:
1007:
1008: else if (childInputMap.getParent() instanceof InputMapUIResource)
1009: {
1010: if (kw != null)
1011: {
1012: kw.setParent(childInputMap.getParent());
1013: childInputMap.setParent(kw);
1014: }
1015: }
1016: }
1017:
1018:
1019:
1020: KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k));
1021: ActionMap childActionMap = getActionMap();
1022: if (childActionMap == null)
1023: setActionMap(kam);
1024: else
1025: {
1026: while (childActionMap.getParent() != null
1027: && !(childActionMap.getParent() instanceof KeymapActionMap)
1028: && !(childActionMap.getParent() instanceof ActionMapUIResource))
1029: childActionMap = childActionMap.getParent();
1030:
1031:
1032: if (childActionMap.getParent() == null)
1033: childActionMap.setParent(kam);
1034:
1035:
1036:
1037: else if (childActionMap.getParent() instanceof KeymapActionMap)
1038: {
1039: if (kam == null)
1040: childActionMap.setParent(childActionMap.getParent().getParent());
1041: else
1042: {
1043: kam.setParent(childActionMap.getParent().getParent());
1044: childActionMap.setParent(kam);
1045: }
1046: }
1047:
1048:
1049:
1050: else if (childActionMap.getParent() instanceof ActionMapUIResource)
1051: {
1052: if (kam != null)
1053: {
1054: kam.setParent(childActionMap.getParent());
1055: childActionMap.setParent(kam);
1056: }
1057: }
1058: }
1059:
1060:
1061:
1062: Keymap old = keymap;
1063: keymap = k;
1064: firePropertyChange("keymap", old, k);
1065: }
1066:
1067:
1083: public static void loadKeymap(Keymap map,
1084: JTextComponent.KeyBinding[] bindings,
1085: Action[] actions)
1086: {
1087: Hashtable acts = new Hashtable(actions.length);
1088: for (int i = 0; i < actions.length; ++i)
1089: acts.put(actions[i].getValue(Action.NAME), actions[i]);
1090: for (int i = 0; i < bindings.length; ++i)
1091: if (acts.containsKey(bindings[i].actionName))
1092: map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName));
1093: }
1094:
1095:
1108: public Action[] getActions()
1109: {
1110: return getUI().getEditorKit(this).getActions();
1111: }
1112:
1113:
1114: Document doc;
1115: Caret caret;
1116: boolean editable;
1117:
1118: private Highlighter highlighter;
1119: private Color caretColor;
1120: private Color disabledTextColor;
1121: private Color selectedTextColor;
1122: private Color selectionColor;
1123: private Insets margin;
1124: private boolean dragEnabled;
1125:
1126:
1129: public JTextComponent()
1130: {
1131: Keymap defkeymap = getKeymap(DEFAULT_KEYMAP);
1132: if (defkeymap == null)
1133: {
1134: defkeymap = addKeymap(DEFAULT_KEYMAP, null);
1135: defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction());
1136: }
1137:
1138: setFocusable(true);
1139: setEditable(true);
1140: enableEvents(AWTEvent.KEY_EVENT_MASK);
1141: setOpaque(true);
1142: updateUI();
1143: }
1144:
1145: public void setDocument(Document newDoc)
1146: {
1147: Document oldDoc = doc;
1148: doc = newDoc;
1149: firePropertyChange("document", oldDoc, newDoc);
1150: revalidate();
1151: repaint();
1152: }
1153:
1154: public Document getDocument()
1155: {
1156: return doc;
1157: }
1158:
1159:
1164: public AccessibleContext getAccessibleContext()
1165: {
1166: return new AccessibleJTextComponent();
1167: }
1168:
1169: public void setMargin(Insets m)
1170: {
1171: margin = m;
1172: }
1173:
1174: public Insets getMargin()
1175: {
1176: return margin;
1177: }
1178:
1179: public void setText(String text)
1180: {
1181: try
1182: {
1183: if (doc instanceof AbstractDocument)
1184: ((AbstractDocument) doc).replace(0, doc.getLength(), text, null);
1185: else
1186: {
1187: doc.remove(0, doc.getLength());
1188: doc.insertString(0, text, null);
1189: }
1190: }
1191: catch (BadLocationException e)
1192: {
1193:
1194: throw (InternalError) new InternalError().initCause(e);
1195: }
1196: }
1197:
1198:
1205: public String getText()
1206: {
1207: if (doc == null)
1208: return null;
1209:
1210: try
1211: {
1212: return doc.getText(0, doc.getLength());
1213: }
1214: catch (BadLocationException e)
1215: {
1216:
1217: return "";
1218: }
1219: }
1220:
1221:
1231: public String getText(int offset, int length)
1232: throws BadLocationException
1233: {
1234: return getDocument().getText(offset, length);
1235: }
1236:
1237:
1244: public String getSelectedText()
1245: {
1246: int start = getSelectionStart();
1247: int offset = getSelectionEnd() - start;
1248:
1249: if (offset <= 0)
1250: return null;
1251:
1252: try
1253: {
1254: return doc.getText(start, offset);
1255: }
1256: catch (BadLocationException e)
1257: {
1258:
1259: return null;
1260: }
1261: }
1262:
1263:
1269: public String getUIClassID()
1270: {
1271: return "TextComponentUI";
1272: }
1273:
1274:
1277: protected String paramString()
1278: {
1279:
1280: return super.paramString();
1281: }
1282:
1283:
1288: public TextUI getUI()
1289: {
1290: return (TextUI) ui;
1291: }
1292:
1293:
1298: public void setUI(TextUI newUI)
1299: {
1300: super.setUI(newUI);
1301: }
1302:
1303:
1307: public void updateUI()
1308: {
1309: setUI((TextUI) UIManager.getUI(this));
1310: }
1311:
1312: public Dimension getPreferredScrollableViewportSize()
1313: {
1314: return getPreferredSize();
1315: }
1316:
1317: public int getScrollableUnitIncrement(Rectangle visible, int orientation,
1318: int direction)
1319: {
1320:
1321: if (orientation == SwingConstants.HORIZONTAL)
1322: return visible.width / 10;
1323: else if (orientation == SwingConstants.VERTICAL)
1324: return visible.height / 10;
1325: else
1326: throw new IllegalArgumentException("orientation must be either "
1327: + "javax.swing.SwingConstants.VERTICAL "
1328: + "or "
1329: + "javax.swing.SwingConstants.HORIZONTAL"
1330: );
1331: }
1332:
1333: public int getScrollableBlockIncrement(Rectangle visible, int orientation,
1334: int direction)
1335: {
1336:
1337: if (orientation == SwingConstants.HORIZONTAL)
1338: return visible.width;
1339: else if (orientation == SwingConstants.VERTICAL)
1340: return visible.height;
1341: else
1342: throw new IllegalArgumentException("orientation must be either "
1343: + "javax.swing.SwingConstants.VERTICAL "
1344: + "or "
1345: + "javax.swing.SwingConstants.HORIZONTAL"
1346: );
1347: }
1348:
1349:
1354: public boolean isEditable()
1355: {
1356: return editable;
1357: }
1358:
1359:
1364: public void setEditable(boolean newValue)
1365: {
1366: if (editable == newValue)
1367: return;
1368:
1369: boolean oldValue = editable;
1370: editable = newValue;
1371: firePropertyChange("editable", oldValue, newValue);
1372: }
1373:
1374:
1379: public Caret getCaret()
1380: {
1381: return caret;
1382: }
1383:
1384:
1389: public void setCaret(Caret newCaret)
1390: {
1391: if (caret != null)
1392: caret.deinstall(this);
1393:
1394: Caret oldCaret = caret;
1395: caret = newCaret;
1396:
1397: if (caret != null)
1398: caret.install(this);
1399:
1400: firePropertyChange("caret", oldCaret, newCaret);
1401: }
1402:
1403: public Color getCaretColor()
1404: {
1405: return caretColor;
1406: }
1407:
1408: public void setCaretColor(Color newColor)
1409: {
1410: Color oldCaretColor = caretColor;
1411: caretColor = newColor;
1412: firePropertyChange("caretColor", oldCaretColor, newColor);
1413: }
1414:
1415: public Color getDisabledTextColor()
1416: {
1417: return disabledTextColor;
1418: }
1419:
1420: public void setDisabledTextColor(Color newColor)
1421: {
1422: Color oldColor = disabledTextColor;
1423: disabledTextColor = newColor;
1424: firePropertyChange("disabledTextColor", oldColor, newColor);
1425: }
1426:
1427: public Color getSelectedTextColor()
1428: {
1429: return selectedTextColor;
1430: }
1431:
1432: public void setSelectedTextColor(Color newColor)
1433: {
1434: Color oldColor = selectedTextColor;
1435: selectedTextColor = newColor;
1436: firePropertyChange("selectedTextColor", oldColor, newColor);
1437: }
1438:
1439: public Color getSelectionColor()
1440: {
1441: return selectionColor;
1442: }
1443:
1444: public void setSelectionColor(Color newColor)
1445: {
1446: Color oldColor = selectionColor;
1447: selectionColor = newColor;
1448: firePropertyChange("selectionColor", oldColor, newColor);
1449: }
1450:
1451:
1456: public int getCaretPosition()
1457: {
1458: return caret.getDot();
1459: }
1460:
1461:
1466: public void setCaretPosition(int position)
1467: {
1468: if (doc == null)
1469: return;
1470:
1471: if (position < 0 || position > doc.getLength())
1472: throw new IllegalArgumentException();
1473:
1474: caret.setDot(position);
1475: }
1476:
1477:
1481: public void moveCaretPosition(int position)
1482: {
1483: if (doc == null)
1484: return;
1485:
1486: if (position < 0 || position > doc.getLength())
1487: throw new IllegalArgumentException();
1488:
1489: caret.moveDot(position);
1490: }
1491:
1492: public Highlighter getHighlighter()
1493: {
1494: return highlighter;
1495: }
1496:
1497: public void setHighlighter(Highlighter newHighlighter)
1498: {
1499: if (highlighter != null)
1500: highlighter.deinstall(this);
1501:
1502: Highlighter oldHighlighter = highlighter;
1503: highlighter = newHighlighter;
1504:
1505: if (highlighter != null)
1506: highlighter.install(this);
1507:
1508: firePropertyChange("highlighter", oldHighlighter, newHighlighter);
1509: }
1510:
1511:
1516: public int getSelectionStart()
1517: {
1518: return Math.min(caret.getDot(), caret.getMark());
1519: }
1520:
1521:
1526: public void setSelectionStart(int start)
1527: {
1528: select(start, getSelectionEnd());
1529: }
1530:
1531:
1536: public int getSelectionEnd()
1537: {
1538: return Math.max(caret.getDot(), caret.getMark());
1539: }
1540:
1541:
1546: public void setSelectionEnd(int end)
1547: {
1548: select(getSelectionStart(), end);
1549: }
1550:
1551:
1557: public void select(int start, int end)
1558: {
1559: int length = doc.getLength();
1560:
1561: start = Math.max(start, 0);
1562: start = Math.min(start, length);
1563:
1564: end = Math.max(end, start);
1565: end = Math.min(end, length);
1566:
1567: setCaretPosition(start);
1568: moveCaretPosition(end);
1569: }
1570:
1571:
1574: public void selectAll()
1575: {
1576: select(0, doc.getLength());
1577: }
1578:
1579: public synchronized void replaceSelection(String content)
1580: {
1581: int dot = caret.getDot();
1582: int mark = caret.getMark();
1583:
1584:
1585: if (content == null)
1586: {
1587: caret.setDot(dot);
1588: return;
1589: }
1590:
1591: try
1592: {
1593: int start = getSelectionStart();
1594: int end = getSelectionEnd();
1595:
1596:
1597: if (dot != mark)
1598: doc.remove(start, end - start);
1599:
1600:
1601: doc.insertString(start, content, null);
1602:
1603:
1604: dot = start + content.length();
1605: setCaretPosition(dot);
1606:
1607:
1608: caret.setMagicCaretPosition(modelToView(dot).getLocation());
1609: }
1610: catch (BadLocationException e)
1611: {
1612:
1613: }
1614: }
1615:
1616: public boolean getScrollableTracksViewportHeight()
1617: {
1618: if (getParent() instanceof JViewport)
1619: return getParent().getHeight() > getPreferredSize().height;
1620:
1621: return false;
1622: }
1623:
1624: public boolean getScrollableTracksViewportWidth()
1625: {
1626: if (getParent() instanceof JViewport)
1627: return getParent().getWidth() > getPreferredSize().width;
1628:
1629: return false;
1630: }
1631:
1632:
1637: public void addCaretListener(CaretListener listener)
1638: {
1639: listenerList.add(CaretListener.class, listener);
1640: }
1641:
1642:
1647: public void removeCaretListener(CaretListener listener)
1648: {
1649: listenerList.remove(CaretListener.class, listener);
1650: }
1651:
1652:
1657: public CaretListener[] getCaretListeners()
1658: {
1659: return (CaretListener[]) getListeners(CaretListener.class);
1660: }
1661:
1662:
1668: protected void fireCaretUpdate(CaretEvent event)
1669: {
1670: CaretListener[] listeners = getCaretListeners();
1671:
1672: for (int index = 0; index < listeners.length; ++index)
1673: listeners[index].caretUpdate(event);
1674: }
1675:
1676:
1681: public void addInputMethodListener(InputMethodListener listener)
1682: {
1683: listenerList.add(InputMethodListener.class, listener);
1684: }
1685:
1686:
1691: public void removeInputMethodListener(InputMethodListener listener)
1692: {
1693: listenerList.remove(InputMethodListener.class, listener);
1694: }
1695:
1696:
1701: public InputMethodListener[] getInputMethodListeners()
1702: {
1703: return (InputMethodListener[]) getListeners(InputMethodListener.class);
1704: }
1705:
1706: public Rectangle modelToView(int position) throws BadLocationException
1707: {
1708: return getUI().modelToView(this, position);
1709: }
1710:
1711: public boolean getDragEnabled()
1712: {
1713: return dragEnabled;
1714: }
1715:
1716: public void setDragEnabled(boolean enabled)
1717: {
1718: dragEnabled = enabled;
1719: }
1720:
1721: public int viewToModel(Point pt)
1722: {
1723: return getUI().viewToModel(this, pt);
1724: }
1725:
1726: public void copy()
1727: {
1728: doTransferAction("copy", TransferHandler.getCopyAction());
1729: }
1730:
1731: public void cut()
1732: {
1733: doTransferAction("cut", TransferHandler.getCutAction());
1734: }
1735:
1736: public void paste()
1737: {
1738: doTransferAction("paste", TransferHandler.getPasteAction());
1739: }
1740:
1741: private void doTransferAction(String name, Action action)
1742: {
1743:
1744: if (getTransferHandler() == null)
1745: {
1746: if (defaultTransferHandler == null)
1747: defaultTransferHandler = new DefaultTransferHandler();
1748:
1749: setTransferHandler(defaultTransferHandler);
1750: }
1751:
1752:
1753: ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1754: action.getValue(Action.NAME).toString());
1755: action.actionPerformed(event);
1756: }
1757:
1758: public void setFocusAccelerator(char newKey)
1759: {
1760: if (focusAccelerator == newKey)
1761: return;
1762:
1763: char oldKey = focusAccelerator;
1764: focusAccelerator = newKey;
1765: firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);
1766: }
1767:
1768: public char getFocusAccelerator()
1769: {
1770: return focusAccelerator;
1771: }
1772:
1773:
1776: public NavigationFilter getNavigationFilter()
1777: {
1778: return navigationFilter;
1779: }
1780:
1781:
1784: public void setNavigationFilter(NavigationFilter filter)
1785: {
1786: navigationFilter = filter;
1787: }
1788:
1789:
1806: public void read(Reader input, Object streamDescription)
1807: throws IOException
1808: {
1809: if (streamDescription != null)
1810: {
1811: Document d = getDocument();
1812: if (d != null)
1813: d.putProperty(Document.StreamDescriptionProperty, streamDescription);
1814: }
1815:
1816: StringBuffer b = new StringBuffer();
1817: int c;
1818:
1819:
1820: while ((c = input.read()) >= 0)
1821: b.append((char) c);
1822:
1823: setText(b.toString());
1824: }
1825:
1826:
1834: public void write(Writer output)
1835: throws IOException
1836: {
1837: output.write(getText());
1838: }
1839:
1840:
1850: public String getToolTipText(MouseEvent ev)
1851: {
1852: return getUI().getToolTipText(this, ev.getPoint());
1853: }
1854: }