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:
85: public abstract class JTextComponent extends JComponent
86: implements Scrollable, Accessible
87: {
88:
91:
92: public class AccessibleJTextComponent extends AccessibleJComponent
93: implements AccessibleText, CaretListener, DocumentListener
94: {
95: private static final long serialVersionUID = 7664188944091413696L;
96:
97:
100: public AccessibleJTextComponent()
101: {
102:
103: }
104:
105:
109: public int getCaretPosition()
110: {
111: return 0;
112: }
113:
114:
118: public String getSelectedText()
119: {
120: return null;
121: }
122:
123:
127: public int getSelectionStart()
128: {
129: return 0;
130: }
131:
132:
136: public int getSelectionEnd()
137: {
138: return 0;
139: }
140:
141:
145: public void caretUpdate(CaretEvent value0)
146: {
147:
148: }
149:
150:
154: public AccessibleStateSet getAccessibleStateSet()
155: {
156: return null;
157: }
158:
159:
163: public AccessibleRole getAccessibleRole()
164: {
165: return null;
166: }
167:
168:
172: public AccessibleText getAccessibleText()
173: {
174: return null;
175: }
176:
177:
181: public void insertUpdate(DocumentEvent value0)
182: {
183:
184: }
185:
186:
190: public void removeUpdate(DocumentEvent value0)
191: {
192:
193: }
194:
195:
199: public void changedUpdate(DocumentEvent value0)
200: {
201:
202: }
203:
204:
209: public int getIndexAtPoint(Point value0)
210: {
211: return 0;
212: }
213:
214:
218: Rectangle getRootEditorRect()
219: {
220: return null;
221: }
222:
223:
228: public Rectangle getCharacterBounds(int value0)
229: {
230: return null;
231: }
232:
233:
237: public int getCharCount()
238: {
239: return 0;
240: }
241:
242:
247: public AttributeSet getCharacterAttribute(int value0)
248: {
249: return null;
250: }
251:
252:
258: public String getAtIndex(int value0, int value1)
259: {
260: return null;
261: }
262:
263:
269: public String getAfterIndex(int value0, int value1)
270: {
271: return null;
272: }
273:
274:
280: public String getBeforeIndex(int value0, int value1)
281: {
282: return null;
283: }
284: }
285:
286: public static class KeyBinding
287: {
288: public KeyStroke key;
289: public String actionName;
290:
291:
297: public KeyBinding(KeyStroke key, String actionName)
298: {
299: this.key = key;
300: this.actionName = actionName;
301: }
302: }
303:
304:
334:
335: private class KeymapWrapper extends InputMap
336: {
337: Keymap map;
338:
339: public KeymapWrapper(Keymap k)
340: {
341: map = k;
342: }
343:
344: public int size()
345: {
346: return map.getBoundKeyStrokes().length + super.size();
347: }
348:
349: public Object get(KeyStroke ks)
350: {
351: Action mapped = null;
352: Keymap m = map;
353: while(mapped == null && m != null)
354: {
355: mapped = m.getAction(ks);
356: if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED)
357: mapped = m.getDefaultAction();
358: if (mapped == null)
359: m = m.getResolveParent();
360: }
361:
362: if (mapped == null)
363: return super.get(ks);
364: else
365: return mapped;
366: }
367:
368: public KeyStroke[] keys()
369: {
370: KeyStroke[] superKeys = super.keys();
371: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
372: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
373: for (int i = 0; i < superKeys.length; ++i)
374: bothKeys[i] = superKeys[i];
375: for (int i = 0; i < mapKeys.length; ++i)
376: bothKeys[i + superKeys.length] = mapKeys[i];
377: return bothKeys;
378: }
379:
380: public KeyStroke[] allKeys()
381: {
382: KeyStroke[] superKeys = super.allKeys();
383: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
384: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
385: for (int i = 0; i < superKeys.length; ++i)
386: bothKeys[i] = superKeys[i];
387: for (int i = 0; i < mapKeys.length; ++i)
388: bothKeys[i + superKeys.length] = mapKeys[i];
389: return bothKeys;
390: }
391: }
392:
393: private class KeymapActionMap extends ActionMap
394: {
395: Keymap map;
396:
397: public KeymapActionMap(Keymap k)
398: {
399: map = k;
400: }
401:
402: public Action get(Object cmd)
403: {
404: if (cmd instanceof Action)
405: return (Action) cmd;
406: else
407: return super.get(cmd);
408: }
409:
410: public int size()
411: {
412: return map.getBoundKeyStrokes().length + super.size();
413: }
414:
415: public Object[] keys()
416: {
417: Object[] superKeys = super.keys();
418: Object[] mapKeys = map.getBoundKeyStrokes();
419: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
420: for (int i = 0; i < superKeys.length; ++i)
421: bothKeys[i] = superKeys[i];
422: for (int i = 0; i < mapKeys.length; ++i)
423: bothKeys[i + superKeys.length] = mapKeys[i];
424: return bothKeys;
425: }
426:
427: public Object[] allKeys()
428: {
429: Object[] superKeys = super.allKeys();
430: Object[] mapKeys = map.getBoundKeyStrokes();
431: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
432: for (int i = 0; i < superKeys.length; ++i)
433: bothKeys[i] = superKeys[i];
434: for (int i = 0; i < mapKeys.length; ++i)
435: bothKeys[i + superKeys.length] = mapKeys[i];
436: return bothKeys;
437: }
438:
439: }
440:
441: static class DefaultKeymap implements Keymap
442: {
443: String name;
444: Keymap parent;
445: Hashtable map;
446: Action defaultAction;
447:
448: public DefaultKeymap(String name)
449: {
450: this.name = name;
451: this.map = new Hashtable();
452: }
453:
454: public void addActionForKeyStroke(KeyStroke key, Action a)
455: {
456: map.put(key, a);
457: }
458:
459:
468: public Action getAction(KeyStroke key)
469: {
470: if (map.containsKey(key))
471: return (Action) map.get(key);
472: else if (parent != null)
473: return parent.getAction(key);
474: else
475: return null;
476: }
477:
478: public Action[] getBoundActions()
479: {
480: Action [] ret = new Action[map.size()];
481: Enumeration e = map.elements();
482: int i = 0;
483: while (e.hasMoreElements())
484: {
485: ret[i++] = (Action) e.nextElement();
486: }
487: return ret;
488: }
489:
490: public KeyStroke[] getBoundKeyStrokes()
491: {
492: KeyStroke [] ret = new KeyStroke[map.size()];
493: Enumeration e = map.keys();
494: int i = 0;
495: while (e.hasMoreElements())
496: {
497: ret[i++] = (KeyStroke) e.nextElement();
498: }
499: return ret;
500: }
501:
502: public Action getDefaultAction()
503: {
504: return defaultAction;
505: }
506:
507: public KeyStroke[] getKeyStrokesForAction(Action a)
508: {
509: int i = 0;
510: Enumeration e = map.keys();
511: while (e.hasMoreElements())
512: {
513: if (map.get(e.nextElement()).equals(a))
514: ++i;
515: }
516: KeyStroke [] ret = new KeyStroke[i];
517: i = 0;
518: e = map.keys();
519: while (e.hasMoreElements())
520: {
521: KeyStroke k = (KeyStroke) e.nextElement();
522: if (map.get(k).equals(a))
523: ret[i++] = k;
524: }
525: return ret;
526: }
527:
528: public String getName()
529: {
530: return name;
531: }
532:
533: public Keymap getResolveParent()
534: {
535: return parent;
536: }
537:
538: public boolean isLocallyDefined(KeyStroke key)
539: {
540: return map.containsKey(key);
541: }
542:
543: public void removeBindings()
544: {
545: map.clear();
546: }
547:
548: public void removeKeyStrokeBinding(KeyStroke key)
549: {
550: map.remove(key);
551: }
552:
553: public void setDefaultAction(Action a)
554: {
555: defaultAction = a;
556: }
557:
558: public void setResolveParent(Keymap p)
559: {
560: parent = p;
561: }
562: }
563:
564: class DefaultTransferHandler extends TransferHandler
565: {
566: public boolean canImport(JComponent component, DataFlavor[] flavors)
567: {
568: JTextComponent textComponent = (JTextComponent) component;
569:
570: if (! (textComponent.isEnabled()
571: && textComponent.isEditable()
572: && flavors != null))
573: return false;
574:
575: for (int i = 0; i < flavors.length; ++i)
576: if (flavors[i].equals(DataFlavor.stringFlavor))
577: return true;
578:
579: return false;
580: }
581:
582: public void exportToClipboard(JComponent component, Clipboard clipboard,
583: int action)
584: {
585: JTextComponent textComponent = (JTextComponent) component;
586: int start = textComponent.getSelectionStart();
587: int end = textComponent.getSelectionEnd();
588:
589: if (start == end)
590: return;
591:
592: try
593: {
594:
595: String data = textComponent.getDocument().getText(start, end);
596: StringSelection selection = new StringSelection(data);
597: clipboard.setContents(selection, null);
598:
599:
600: if (action == MOVE)
601: doc.remove(start, end - start);
602: }
603: catch (BadLocationException e)
604: {
605:
606: }
607: }
608:
609: public int getSourceActions()
610: {
611: return NONE;
612: }
613:
614: public boolean importData(JComponent component, Transferable transferable)
615: {
616: DataFlavor flavor = null;
617: DataFlavor[] flavors = transferable.getTransferDataFlavors();
618:
619: if (flavors == null)
620: return false;
621:
622: for (int i = 0; i < flavors.length; ++i)
623: if (flavors[i].equals(DataFlavor.stringFlavor))
624: flavor = flavors[i];
625:
626: if (flavor == null)
627: return false;
628:
629: try
630: {
631: JTextComponent textComponent = (JTextComponent) component;
632: String data = (String) transferable.getTransferData(flavor);
633: textComponent.replaceSelection(data);
634: return true;
635: }
636: catch (IOException e)
637: {
638:
639: }
640: catch (UnsupportedFlavorException e)
641: {
642:
643: }
644:
645: return false;
646: }
647: }
648:
649: private static final long serialVersionUID = -8796518220218978795L;
650:
651: public static final String DEFAULT_KEYMAP = "default";
652: public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
653:
654: private static DefaultTransferHandler defaultTransferHandler;
655: private static Hashtable keymaps = new Hashtable();
656: private Keymap keymap;
657: private char focusAccelerator = '\0';
658: private NavigationFilter navigationFilter;
659:
660:
672: public static Keymap getKeymap(String n)
673: {
674: return (Keymap) keymaps.get(n);
675: }
676:
677:
688: public static Keymap removeKeymap(String n)
689: {
690: Keymap km = (Keymap) keymaps.get(n);
691: keymaps.remove(n);
692: return km;
693: }
694:
695:
711: public static Keymap addKeymap(String n, Keymap parent)
712: {
713: Keymap k = new DefaultKeymap(n);
714: k.setResolveParent(parent);
715: if (n != null)
716: keymaps.put(n, k);
717: return k;
718: }
719:
720:
728: public Keymap getKeymap()
729: {
730: return keymap;
731: }
732:
733:
742: public void setKeymap(Keymap k)
743: {
744:
745:
746:
747:
748:
749:
750:
751:
752:
753:
754:
755: KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k));
756: InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED);
757: if (childInputMap == null)
758: setInputMap(JComponent.WHEN_FOCUSED, kw);
759: else
760: {
761: while (childInputMap.getParent() != null
762: && !(childInputMap.getParent() instanceof KeymapWrapper)
763: && !(childInputMap.getParent() instanceof InputMapUIResource))
764: childInputMap = childInputMap.getParent();
765:
766:
767: if (childInputMap.getParent() == null)
768: childInputMap.setParent(kw);
769:
770:
771:
772: else if (childInputMap.getParent() instanceof KeymapWrapper)
773: {
774: if (kw == null)
775: childInputMap.setParent(childInputMap.getParent().getParent());
776: else
777: {
778: kw.setParent(childInputMap.getParent().getParent());
779: childInputMap.setParent(kw);
780: }
781: }
782:
783:
784:
785: else if (childInputMap.getParent() instanceof InputMapUIResource)
786: {
787: if (kw != null)
788: {
789: kw.setParent(childInputMap.getParent());
790: childInputMap.setParent(kw);
791: }
792: }
793: }
794:
795:
796:
797: KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k));
798: ActionMap childActionMap = getActionMap();
799: if (childActionMap == null)
800: setActionMap(kam);
801: else
802: {
803: while (childActionMap.getParent() != null
804: && !(childActionMap.getParent() instanceof KeymapActionMap)
805: && !(childActionMap.getParent() instanceof ActionMapUIResource))
806: childActionMap = childActionMap.getParent();
807:
808:
809: if (childActionMap.getParent() == null)
810: childActionMap.setParent(kam);
811:
812:
813:
814: else if (childActionMap.getParent() instanceof KeymapActionMap)
815: {
816: if (kam == null)
817: childActionMap.setParent(childActionMap.getParent().getParent());
818: else
819: {
820: kam.setParent(childActionMap.getParent().getParent());
821: childActionMap.setParent(kam);
822: }
823: }
824:
825:
826:
827: else if (childActionMap.getParent() instanceof ActionMapUIResource)
828: {
829: if (kam != null)
830: {
831: kam.setParent(childActionMap.getParent());
832: childActionMap.setParent(kam);
833: }
834: }
835: }
836:
837:
838:
839: Keymap old = keymap;
840: keymap = k;
841: firePropertyChange("keymap", old, k);
842: }
843:
844:
860: public static void loadKeymap(Keymap map,
861: JTextComponent.KeyBinding[] bindings,
862: Action[] actions)
863: {
864: Hashtable acts = new Hashtable(actions.length);
865: for (int i = 0; i < actions.length; ++i)
866: acts.put(actions[i].getValue(Action.NAME), actions[i]);
867: for (int i = 0; i < bindings.length; ++i)
868: if (acts.containsKey(bindings[i].actionName))
869: map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName));
870: }
871:
872:
885: public Action[] getActions()
886: {
887: return getUI().getEditorKit(this).getActions();
888: }
889:
890:
891: Document doc;
892: Caret caret;
893: boolean editable;
894:
895: private Highlighter highlighter;
896: private Color caretColor;
897: private Color disabledTextColor;
898: private Color selectedTextColor;
899: private Color selectionColor;
900: private Insets margin;
901: private boolean dragEnabled;
902:
903:
906: public JTextComponent()
907: {
908: Keymap defkeymap = getKeymap(DEFAULT_KEYMAP);
909: boolean creatingKeymap = false;
910: if (defkeymap == null)
911: {
912: defkeymap = addKeymap(DEFAULT_KEYMAP, null);
913: defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction());
914: creatingKeymap = true;
915: }
916:
917: setFocusable(true);
918: setEditable(true);
919: enableEvents(AWTEvent.KEY_EVENT_MASK);
920: updateUI();
921:
922:
923: if (creatingKeymap)
924: loadKeymap(defkeymap,
925: new KeyBinding[] {
926: new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),
927: DefaultEditorKit.backwardAction),
928: new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),
929: DefaultEditorKit.forwardAction),
930: new KeyBinding(KeyStroke.getKeyStroke("typed \b"),
931: DefaultEditorKit.deletePrevCharAction),
932: new KeyBinding(KeyStroke.getKeyStroke("typed \u007f"),
933: DefaultEditorKit.deleteNextCharAction)
934: },
935: getActions());
936: }
937:
938: public void setDocument(Document newDoc)
939: {
940: Document oldDoc = doc;
941: doc = newDoc;
942: firePropertyChange("document", oldDoc, newDoc);
943: revalidate();
944: repaint();
945: }
946:
947: public Document getDocument()
948: {
949: return doc;
950: }
951:
952:
957: public AccessibleContext getAccessibleContext()
958: {
959: return null;
960: }
961:
962: public void setMargin(Insets m)
963: {
964: margin = m;
965: }
966:
967: public Insets getMargin()
968: {
969: return margin;
970: }
971:
972: public void setText(String text)
973: {
974: try
975: {
976: if (doc instanceof AbstractDocument)
977: ((AbstractDocument) doc).replace(0, doc.getLength(), text, null);
978: else
979: {
980: doc.remove(0, doc.getLength());
981: doc.insertString(0, text, null);
982: }
983: }
984: catch (BadLocationException e)
985: {
986:
987: }
988: }
989:
990:
997: public String getText()
998: {
999: if (doc == null)
1000: return null;
1001:
1002: try
1003: {
1004: return doc.getText(0, doc.getLength());
1005: }
1006: catch (BadLocationException e)
1007: {
1008:
1009: return "";
1010: }
1011: }
1012:
1013:
1023: public String getText(int offset, int length)
1024: throws BadLocationException
1025: {
1026: return getDocument().getText(offset, length);
1027: }
1028:
1029:
1036: public String getSelectedText()
1037: {
1038: try
1039: {
1040: return doc.getText(getSelectionStart(), getSelectionEnd());
1041: }
1042: catch (BadLocationException e)
1043: {
1044:
1045: return null;
1046: }
1047: }
1048:
1049:
1055: public String getUIClassID()
1056: {
1057: return "TextComponentUI";
1058: }
1059:
1060:
1063: protected String paramString()
1064: {
1065:
1066: return super.paramString();
1067: }
1068:
1069:
1074: public TextUI getUI()
1075: {
1076: return (TextUI) ui;
1077: }
1078:
1079:
1084: public void setUI(TextUI newUI)
1085: {
1086: super.setUI(newUI);
1087: }
1088:
1089:
1093: public void updateUI()
1094: {
1095: setUI((TextUI) UIManager.getUI(this));
1096: }
1097:
1098: public Dimension getPreferredScrollableViewportSize()
1099: {
1100: return getPreferredSize();
1101: }
1102:
1103: public int getScrollableUnitIncrement(Rectangle visible, int orientation,
1104: int direction)
1105: {
1106:
1107: if (orientation == SwingConstants.HORIZONTAL)
1108: return visible.width / 10;
1109: else if (orientation == SwingConstants.VERTICAL)
1110: return visible.height / 10;
1111: else
1112: throw new IllegalArgumentException("orientation must be either "
1113: + "javax.swing.SwingConstants.VERTICAL "
1114: + "or "
1115: + "javax.swing.SwingConstants.HORIZONTAL"
1116: );
1117: }
1118:
1119: public int getScrollableBlockIncrement(Rectangle visible, int orientation,
1120: int direction)
1121: {
1122:
1123: if (orientation == SwingConstants.HORIZONTAL)
1124: return visible.width;
1125: else if (orientation == SwingConstants.VERTICAL)
1126: return visible.height;
1127: else
1128: throw new IllegalArgumentException("orientation must be either "
1129: + "javax.swing.SwingConstants.VERTICAL "
1130: + "or "
1131: + "javax.swing.SwingConstants.HORIZONTAL"
1132: );
1133: }
1134:
1135:
1140: public boolean isEditable()
1141: {
1142: return editable;
1143: }
1144:
1145:
1150: public void setEditable(boolean newValue)
1151: {
1152: if (editable == newValue)
1153: return;
1154:
1155: boolean oldValue = editable;
1156: editable = newValue;
1157: firePropertyChange("editable", oldValue, newValue);
1158: }
1159:
1160:
1165: public Caret getCaret()
1166: {
1167: return caret;
1168: }
1169:
1170:
1175: public void setCaret(Caret newCaret)
1176: {
1177: if (caret != null)
1178: caret.deinstall(this);
1179:
1180: Caret oldCaret = caret;
1181: caret = newCaret;
1182:
1183: if (caret != null)
1184: caret.install(this);
1185:
1186: firePropertyChange("caret", oldCaret, newCaret);
1187: }
1188:
1189: public Color getCaretColor()
1190: {
1191: return caretColor;
1192: }
1193:
1194: public void setCaretColor(Color newColor)
1195: {
1196: Color oldCaretColor = caretColor;
1197: caretColor = newColor;
1198: firePropertyChange("caretColor", oldCaretColor, newColor);
1199: }
1200:
1201: public Color getDisabledTextColor()
1202: {
1203: return disabledTextColor;
1204: }
1205:
1206: public void setDisabledTextColor(Color newColor)
1207: {
1208: Color oldColor = disabledTextColor;
1209: disabledTextColor = newColor;
1210: firePropertyChange("disabledTextColor", oldColor, newColor);
1211: }
1212:
1213: public Color getSelectedTextColor()
1214: {
1215: return selectedTextColor;
1216: }
1217:
1218: public void setSelectedTextColor(Color newColor)
1219: {
1220: Color oldColor = selectedTextColor;
1221: selectedTextColor = newColor;
1222: firePropertyChange("selectedTextColor", oldColor, newColor);
1223: }
1224:
1225: public Color getSelectionColor()
1226: {
1227: return selectionColor;
1228: }
1229:
1230: public void setSelectionColor(Color newColor)
1231: {
1232: Color oldColor = selectionColor;
1233: selectionColor = newColor;
1234: firePropertyChange("selectionColor", oldColor, newColor);
1235: }
1236:
1237:
1242: public int getCaretPosition()
1243: {
1244: return caret.getDot();
1245: }
1246:
1247:
1252: public void setCaretPosition(int position)
1253: {
1254: if (doc == null)
1255: return;
1256:
1257: if (position < 0 || position > doc.getLength())
1258: throw new IllegalArgumentException();
1259:
1260: caret.setDot(position);
1261: }
1262:
1263:
1267: public void moveCaretPosition(int position)
1268: {
1269: if (doc == null)
1270: return;
1271:
1272: if (position < 0 || position > doc.getLength())
1273: throw new IllegalArgumentException();
1274:
1275: caret.moveDot(position);
1276: }
1277:
1278: public Highlighter getHighlighter()
1279: {
1280: return highlighter;
1281: }
1282:
1283: public void setHighlighter(Highlighter newHighlighter)
1284: {
1285: if (highlighter != null)
1286: highlighter.deinstall(this);
1287:
1288: Highlighter oldHighlighter = highlighter;
1289: highlighter = newHighlighter;
1290:
1291: if (highlighter != null)
1292: highlighter.install(this);
1293:
1294: firePropertyChange("highlighter", oldHighlighter, newHighlighter);
1295: }
1296:
1297:
1302: public int getSelectionStart()
1303: {
1304: return Math.min(caret.getDot(), caret.getMark());
1305: }
1306:
1307:
1312: public void setSelectionStart(int start)
1313: {
1314: select(start, getSelectionEnd());
1315: }
1316:
1317:
1322: public int getSelectionEnd()
1323: {
1324: return Math.max(caret.getDot(), caret.getMark());
1325: }
1326:
1327:
1332: public void setSelectionEnd(int end)
1333: {
1334: select(getSelectionStart(), end);
1335: }
1336:
1337:
1343: public void select(int start, int end)
1344: {
1345: int length = doc.getLength();
1346:
1347: start = Math.max(start, 0);
1348: start = Math.min(start, length);
1349:
1350: end = Math.max(end, start);
1351: end = Math.min(end, length);
1352:
1353: setCaretPosition(start);
1354: moveCaretPosition(end);
1355: }
1356:
1357:
1360: public void selectAll()
1361: {
1362: select(0, doc.getLength());
1363: }
1364:
1365: public synchronized void replaceSelection(String content)
1366: {
1367: int dot = caret.getDot();
1368: int mark = caret.getMark();
1369:
1370:
1371: if (content == null)
1372: {
1373: caret.setDot(dot);
1374: return;
1375: }
1376:
1377: try
1378: {
1379: int start = getSelectionStart();
1380: int end = getSelectionEnd();
1381:
1382:
1383: if (dot != mark)
1384: doc.remove(start, end - start);
1385:
1386:
1387: doc.insertString(start, content, null);
1388:
1389:
1390: setCaretPosition(start + content.length());
1391: }
1392: catch (BadLocationException e)
1393: {
1394:
1395: }
1396: }
1397:
1398: public boolean getScrollableTracksViewportHeight()
1399: {
1400: if (getParent() instanceof JViewport)
1401: return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
1402:
1403: return false;
1404: }
1405:
1406: public boolean getScrollableTracksViewportWidth()
1407: {
1408: if (getParent() instanceof JViewport)
1409: return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
1410:
1411: return false;
1412: }
1413:
1414:
1419: public void addCaretListener(CaretListener listener)
1420: {
1421: listenerList.add(CaretListener.class, listener);
1422: }
1423:
1424:
1429: public void removeCaretListener(CaretListener listener)
1430: {
1431: listenerList.remove(CaretListener.class, listener);
1432: }
1433:
1434:
1439: public CaretListener[] getCaretListeners()
1440: {
1441: return (CaretListener[]) getListeners(CaretListener.class);
1442: }
1443:
1444:
1450: protected void fireCaretUpdate(CaretEvent event)
1451: {
1452: CaretListener[] listeners = getCaretListeners();
1453:
1454: for (int index = 0; index < listeners.length; ++index)
1455: listeners[index].caretUpdate(event);
1456: }
1457:
1458:
1463: public void addInputMethodListener(InputMethodListener listener)
1464: {
1465: listenerList.add(InputMethodListener.class, listener);
1466: }
1467:
1468:
1473: public void removeInputMethodListener(InputMethodListener listener)
1474: {
1475: listenerList.remove(InputMethodListener.class, listener);
1476: }
1477:
1478:
1483: public InputMethodListener[] getInputMethodListeners()
1484: {
1485: return (InputMethodListener[]) getListeners(InputMethodListener.class);
1486: }
1487:
1488: public Rectangle modelToView(int position) throws BadLocationException
1489: {
1490: return getUI().modelToView(this, position);
1491: }
1492:
1493: public boolean getDragEnabled()
1494: {
1495: return dragEnabled;
1496: }
1497:
1498: public void setDragEnabled(boolean enabled)
1499: {
1500: dragEnabled = enabled;
1501: }
1502:
1503: public int viewToModel(Point pt)
1504: {
1505: return getUI().viewToModel(this, pt);
1506: }
1507:
1508: public void copy()
1509: {
1510: doTransferAction("copy", TransferHandler.getCopyAction());
1511: }
1512:
1513: public void cut()
1514: {
1515: doTransferAction("cut", TransferHandler.getCutAction());
1516: }
1517:
1518: public void paste()
1519: {
1520: doTransferAction("paste", TransferHandler.getPasteAction());
1521: }
1522:
1523: private void doTransferAction(String name, Action action)
1524: {
1525:
1526: if (getTransferHandler() == null)
1527: {
1528: if (defaultTransferHandler == null)
1529: defaultTransferHandler = new DefaultTransferHandler();
1530:
1531: setTransferHandler(defaultTransferHandler);
1532: }
1533:
1534:
1535: ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1536: action.getValue(Action.NAME).toString());
1537: action.actionPerformed(event);
1538: }
1539:
1540: public void setFocusAccelerator(char newKey)
1541: {
1542: if (focusAccelerator == newKey)
1543: return;
1544:
1545: char oldKey = focusAccelerator;
1546: focusAccelerator = newKey;
1547: firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);
1548: }
1549:
1550: public char getFocusAccelerator()
1551: {
1552: return focusAccelerator;
1553: }
1554:
1555:
1558: public NavigationFilter getNavigationFilter()
1559: {
1560: return navigationFilter;
1561: }
1562:
1563:
1566: public void setNavigationFilter(NavigationFilter filter)
1567: {
1568: navigationFilter = filter;
1569: }
1570:
1571:
1588: public void read(Reader input, Object streamDescription)
1589: throws IOException
1590: {
1591: if (streamDescription != null)
1592: {
1593: Document d = getDocument();
1594: if (d != null)
1595: d.putProperty(Document.StreamDescriptionProperty, streamDescription);
1596: }
1597:
1598: StringBuffer b = new StringBuffer();
1599: int c;
1600:
1601:
1602: while ((c = input.read()) >= 0)
1603: b.append((char) c);
1604:
1605: setText(b.toString());
1606: }
1607:
1608:
1616: public void write(Writer output)
1617: throws IOException
1618: {
1619: output.write(getText());
1620: }
1621:
1622:
1632: public String getToolTipText(MouseEvent ev)
1633: {
1634: return getUI().getToolTipText(this, ev.getPoint());
1635: }
1636: }