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:
86: public abstract class JTextComponent extends JComponent
87: implements Scrollable, Accessible
88: {
89:
92: public class AccessibleJTextComponent extends AccessibleJComponent
93: implements AccessibleText, CaretListener, DocumentListener
94: {
95: private static final long serialVersionUID = 7664188944091413696L;
96:
97:
101: public AccessibleJTextComponent()
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:
307: private class CaretBlinkTimer
308: extends Timer
309: implements ActionListener
310: {
311:
314: public CaretBlinkTimer()
315: {
316: super(1000, null);
317: addActionListener(this);
318: }
319:
320:
323: public void actionPerformed(ActionEvent ev)
324: {
325: Caret c = caret;
326: if (c != null)
327: c.setVisible(!c.isVisible());
328: }
329:
330:
333: public void update()
334: {
335: stop();
336: Caret c = caret;
337: if (c != null)
338: {
339: setDelay(c.getBlinkRate());
340: if (editable)
341: start();
342: else
343: c.setVisible(false);
344: }
345: }
346: }
347:
348:
378:
379: private class KeymapWrapper extends InputMap
380: {
381: Keymap map;
382:
383: public KeymapWrapper(Keymap k)
384: {
385: map = k;
386: }
387:
388: public int size()
389: {
390: return map.getBoundKeyStrokes().length + super.size();
391: }
392:
393: public Object get(KeyStroke ks)
394: {
395: Action mapped = null;
396: Keymap m = map;
397: while(mapped == null && m != null)
398: {
399: mapped = m.getAction(ks);
400: if (mapped == null && ks.getKeyEventType() == KeyEvent.KEY_TYPED)
401: mapped = m.getDefaultAction();
402: if (mapped == null)
403: m = m.getResolveParent();
404: }
405:
406: if (mapped == null)
407: return super.get(ks);
408: else
409: return mapped;
410: }
411:
412: public KeyStroke[] keys()
413: {
414: KeyStroke[] superKeys = super.keys();
415: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
416: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
417: for (int i = 0; i < superKeys.length; ++i)
418: bothKeys[i] = superKeys[i];
419: for (int i = 0; i < mapKeys.length; ++i)
420: bothKeys[i + superKeys.length] = mapKeys[i];
421: return bothKeys;
422: }
423:
424: public KeyStroke[] allKeys()
425: {
426: KeyStroke[] superKeys = super.allKeys();
427: KeyStroke[] mapKeys = map.getBoundKeyStrokes();
428: KeyStroke[] bothKeys = new KeyStroke[superKeys.length + mapKeys.length];
429: for (int i = 0; i < superKeys.length; ++i)
430: bothKeys[i] = superKeys[i];
431: for (int i = 0; i < mapKeys.length; ++i)
432: bothKeys[i + superKeys.length] = mapKeys[i];
433: return bothKeys;
434: }
435: }
436:
437: private class KeymapActionMap extends ActionMap
438: {
439: Keymap map;
440:
441: public KeymapActionMap(Keymap k)
442: {
443: map = k;
444: }
445:
446: public Action get(Object cmd)
447: {
448: if (cmd instanceof Action)
449: return (Action) cmd;
450: else
451: return super.get(cmd);
452: }
453:
454: public int size()
455: {
456: return map.getBoundKeyStrokes().length + super.size();
457: }
458:
459: public Object[] keys()
460: {
461: Object[] superKeys = super.keys();
462: Object[] mapKeys = map.getBoundKeyStrokes();
463: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
464: for (int i = 0; i < superKeys.length; ++i)
465: bothKeys[i] = superKeys[i];
466: for (int i = 0; i < mapKeys.length; ++i)
467: bothKeys[i + superKeys.length] = mapKeys[i];
468: return bothKeys;
469: }
470:
471: public Object[] allKeys()
472: {
473: Object[] superKeys = super.allKeys();
474: Object[] mapKeys = map.getBoundKeyStrokes();
475: Object[] bothKeys = new Object[superKeys.length + mapKeys.length];
476: for (int i = 0; i < superKeys.length; ++i)
477: bothKeys[i] = superKeys[i];
478: for (int i = 0; i < mapKeys.length; ++i)
479: bothKeys[i + superKeys.length] = mapKeys[i];
480: return bothKeys;
481: }
482:
483: }
484:
485: static class DefaultKeymap implements Keymap
486: {
487: String name;
488: Keymap parent;
489: Hashtable map;
490: Action defaultAction;
491:
492: public DefaultKeymap(String name)
493: {
494: this.name = name;
495: this.map = new Hashtable();
496: }
497:
498: public void addActionForKeyStroke(KeyStroke key, Action a)
499: {
500: map.put(key, a);
501: }
502:
503:
512: public Action getAction(KeyStroke key)
513: {
514: if (map.containsKey(key))
515: return (Action) map.get(key);
516: else if (parent != null)
517: return parent.getAction(key);
518: else
519: return null;
520: }
521:
522: public Action[] getBoundActions()
523: {
524: Action [] ret = new Action[map.size()];
525: Enumeration e = map.elements();
526: int i = 0;
527: while (e.hasMoreElements())
528: {
529: ret[i++] = (Action) e.nextElement();
530: }
531: return ret;
532: }
533:
534: public KeyStroke[] getBoundKeyStrokes()
535: {
536: KeyStroke [] ret = new KeyStroke[map.size()];
537: Enumeration e = map.keys();
538: int i = 0;
539: while (e.hasMoreElements())
540: {
541: ret[i++] = (KeyStroke) e.nextElement();
542: }
543: return ret;
544: }
545:
546: public Action getDefaultAction()
547: {
548: return defaultAction;
549: }
550:
551: public KeyStroke[] getKeyStrokesForAction(Action a)
552: {
553: int i = 0;
554: Enumeration e = map.keys();
555: while (e.hasMoreElements())
556: {
557: if (map.get(e.nextElement()).equals(a))
558: ++i;
559: }
560: KeyStroke [] ret = new KeyStroke[i];
561: i = 0;
562: e = map.keys();
563: while (e.hasMoreElements())
564: {
565: KeyStroke k = (KeyStroke) e.nextElement();
566: if (map.get(k).equals(a))
567: ret[i++] = k;
568: }
569: return ret;
570: }
571:
572: public String getName()
573: {
574: return name;
575: }
576:
577: public Keymap getResolveParent()
578: {
579: return parent;
580: }
581:
582: public boolean isLocallyDefined(KeyStroke key)
583: {
584: return map.containsKey(key);
585: }
586:
587: public void removeBindings()
588: {
589: map.clear();
590: }
591:
592: public void removeKeyStrokeBinding(KeyStroke key)
593: {
594: map.remove(key);
595: }
596:
597: public void setDefaultAction(Action a)
598: {
599: defaultAction = a;
600: }
601:
602: public void setResolveParent(Keymap p)
603: {
604: parent = p;
605: }
606: }
607:
608: class DefaultTransferHandler
609: extends TransferHandler
610: {
611: public boolean canImport(JComponent component, DataFlavor[] flavors)
612: {
613: JTextComponent textComponent = (JTextComponent) component;
614:
615: if (! (textComponent.isEnabled()
616: && textComponent.isEditable()
617: && flavors != null))
618: return false;
619:
620: for (int i = 0; i < flavors.length; ++i)
621: if (flavors[i].equals(DataFlavor.stringFlavor))
622: return true;
623:
624: return false;
625: }
626:
627: public void exportToClipboard(JComponent component, Clipboard clipboard,
628: int action)
629: {
630: JTextComponent textComponent = (JTextComponent) component;
631: int start = textComponent.getSelectionStart();
632: int end = textComponent.getSelectionEnd();
633:
634: if (start == end)
635: return;
636:
637: try
638: {
639:
640: String data = textComponent.getDocument().getText(start, end);
641: StringSelection selection = new StringSelection(data);
642: clipboard.setContents(selection, null);
643:
644:
645: if (action == MOVE)
646: doc.remove(start, end - start);
647: }
648: catch (BadLocationException e)
649: {
650:
651: }
652: }
653:
654: public int getSourceActions()
655: {
656: return NONE;
657: }
658:
659: public boolean importData(JComponent component, Transferable transferable)
660: {
661: DataFlavor flavor = null;
662: DataFlavor[] flavors = transferable.getTransferDataFlavors();
663:
664: if (flavors == null)
665: return false;
666:
667: for (int i = 0; i < flavors.length; ++i)
668: if (flavors[i].equals(DataFlavor.stringFlavor))
669: flavor = flavors[i];
670:
671: if (flavor == null)
672: return false;
673:
674: try
675: {
676: JTextComponent textComponent = (JTextComponent) component;
677: String data = (String) transferable.getTransferData(flavor);
678: textComponent.replaceSelection(data);
679: return true;
680: }
681: catch (IOException e)
682: {
683:
684: }
685: catch (UnsupportedFlavorException e)
686: {
687:
688: }
689:
690: return false;
691: }
692: }
693:
694: private static final long serialVersionUID = -8796518220218978795L;
695:
696: public static final String DEFAULT_KEYMAP = "default";
697: public static final String FOCUS_ACCELERATOR_KEY = "focusAcceleratorKey";
698:
699: private static DefaultTransferHandler defaultTransferHandler;
700: private static Hashtable keymaps = new Hashtable();
701: private Keymap keymap;
702: private char focusAccelerator = '\0';
703: private NavigationFilter navigationFilter;
704:
705: private CaretBlinkTimer caretBlinkTimer;
706:
707:
719: public static Keymap getKeymap(String n)
720: {
721: return (Keymap) keymaps.get(n);
722: }
723:
724:
735: public static Keymap removeKeymap(String n)
736: {
737: Keymap km = (Keymap) keymaps.get(n);
738: keymaps.remove(n);
739: return km;
740: }
741:
742:
758: public static Keymap addKeymap(String n, Keymap parent)
759: {
760: Keymap k = new DefaultKeymap(n);
761: k.setResolveParent(parent);
762: if (n != null)
763: keymaps.put(n, k);
764: return k;
765: }
766:
767:
775: public Keymap getKeymap()
776: {
777: return keymap;
778: }
779:
780:
789: public void setKeymap(Keymap k)
790: {
791:
792:
793:
794:
795:
796:
797:
798:
799:
800:
801:
802: KeymapWrapper kw = (k == null ? null : new KeymapWrapper(k));
803: InputMap childInputMap = getInputMap(JComponent.WHEN_FOCUSED);
804: if (childInputMap == null)
805: setInputMap(JComponent.WHEN_FOCUSED, kw);
806: else
807: {
808: while (childInputMap.getParent() != null
809: && !(childInputMap.getParent() instanceof KeymapWrapper)
810: && !(childInputMap.getParent() instanceof InputMapUIResource))
811: childInputMap = childInputMap.getParent();
812:
813:
814: if (childInputMap.getParent() == null)
815: childInputMap.setParent(kw);
816:
817:
818:
819: else if (childInputMap.getParent() instanceof KeymapWrapper)
820: {
821: if (kw == null)
822: childInputMap.setParent(childInputMap.getParent().getParent());
823: else
824: {
825: kw.setParent(childInputMap.getParent().getParent());
826: childInputMap.setParent(kw);
827: }
828: }
829:
830:
831:
832: else if (childInputMap.getParent() instanceof InputMapUIResource)
833: {
834: if (kw != null)
835: {
836: kw.setParent(childInputMap.getParent());
837: childInputMap.setParent(kw);
838: }
839: }
840: }
841:
842:
843:
844: KeymapActionMap kam = (k == null ? null : new KeymapActionMap(k));
845: ActionMap childActionMap = getActionMap();
846: if (childActionMap == null)
847: setActionMap(kam);
848: else
849: {
850: while (childActionMap.getParent() != null
851: && !(childActionMap.getParent() instanceof KeymapActionMap)
852: && !(childActionMap.getParent() instanceof ActionMapUIResource))
853: childActionMap = childActionMap.getParent();
854:
855:
856: if (childActionMap.getParent() == null)
857: childActionMap.setParent(kam);
858:
859:
860:
861: else if (childActionMap.getParent() instanceof KeymapActionMap)
862: {
863: if (kam == null)
864: childActionMap.setParent(childActionMap.getParent().getParent());
865: else
866: {
867: kam.setParent(childActionMap.getParent().getParent());
868: childActionMap.setParent(kam);
869: }
870: }
871:
872:
873:
874: else if (childActionMap.getParent() instanceof ActionMapUIResource)
875: {
876: if (kam != null)
877: {
878: kam.setParent(childActionMap.getParent());
879: childActionMap.setParent(kam);
880: }
881: }
882: }
883:
884:
885:
886: Keymap old = keymap;
887: keymap = k;
888: firePropertyChange("keymap", old, k);
889: }
890:
891:
907: public static void loadKeymap(Keymap map,
908: JTextComponent.KeyBinding[] bindings,
909: Action[] actions)
910: {
911: Hashtable acts = new Hashtable(actions.length);
912: for (int i = 0; i < actions.length; ++i)
913: acts.put(actions[i].getValue(Action.NAME), actions[i]);
914: for (int i = 0; i < bindings.length; ++i)
915: if (acts.containsKey(bindings[i].actionName))
916: map.addActionForKeyStroke(bindings[i].key, (Action) acts.get(bindings[i].actionName));
917: }
918:
919:
932: public Action[] getActions()
933: {
934: return getUI().getEditorKit(this).getActions();
935: }
936:
937:
938: Document doc;
939: Caret caret;
940: boolean editable;
941:
942: private Highlighter highlighter;
943: private Color caretColor;
944: private Color disabledTextColor;
945: private Color selectedTextColor;
946: private Color selectionColor;
947: private Insets margin;
948: private boolean dragEnabled;
949:
950:
953: public JTextComponent()
954: {
955: Keymap defkeymap = getKeymap(DEFAULT_KEYMAP);
956: boolean creatingKeymap = false;
957: if (defkeymap == null)
958: {
959: defkeymap = addKeymap(DEFAULT_KEYMAP, null);
960: defkeymap.setDefaultAction(new DefaultEditorKit.DefaultKeyTypedAction());
961: creatingKeymap = true;
962: }
963:
964: caretBlinkTimer = new CaretBlinkTimer();
965:
966: setFocusable(true);
967: setEditable(true);
968: enableEvents(AWTEvent.KEY_EVENT_MASK);
969: updateUI();
970:
971:
972: if (creatingKeymap)
973: loadKeymap(defkeymap,
974: new KeyBinding[] {
975: new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),
976: DefaultEditorKit.backwardAction),
977: new KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),
978: DefaultEditorKit.forwardAction),
979: new KeyBinding(KeyStroke.getKeyStroke("typed \b"),
980: DefaultEditorKit.deletePrevCharAction),
981: new KeyBinding(KeyStroke.getKeyStroke("typed \u007f"),
982: DefaultEditorKit.deleteNextCharAction)
983: },
984: getActions());
985: }
986:
987: public void setDocument(Document newDoc)
988: {
989: Document oldDoc = doc;
990: doc = newDoc;
991: firePropertyChange("document", oldDoc, newDoc);
992: revalidate();
993: repaint();
994: }
995:
996: public Document getDocument()
997: {
998: return doc;
999: }
1000:
1001:
1006: public AccessibleContext getAccessibleContext()
1007: {
1008: return null;
1009: }
1010:
1011: public void setMargin(Insets m)
1012: {
1013: margin = m;
1014: }
1015:
1016: public Insets getMargin()
1017: {
1018: return margin;
1019: }
1020:
1021: public void setText(String text)
1022: {
1023: try
1024: {
1025: doc.remove(0, doc.getLength());
1026: doc.insertString(0, text, null);
1027: }
1028: catch (BadLocationException e)
1029: {
1030:
1031: }
1032: }
1033:
1034:
1041: public String getText()
1042: {
1043: if (doc == null)
1044: return null;
1045:
1046: try
1047: {
1048: return doc.getText(0, doc.getLength());
1049: }
1050: catch (BadLocationException e)
1051: {
1052:
1053: return "";
1054: }
1055: }
1056:
1057:
1067: public String getText(int offset, int length)
1068: throws BadLocationException
1069: {
1070: return getDocument().getText(offset, length);
1071: }
1072:
1073:
1080: public String getSelectedText()
1081: {
1082: try
1083: {
1084: return doc.getText(getSelectionStart(), getSelectionEnd());
1085: }
1086: catch (BadLocationException e)
1087: {
1088:
1089: return null;
1090: }
1091: }
1092:
1093:
1099: public String getUIClassID()
1100: {
1101: return "TextComponentUI";
1102: }
1103:
1104:
1107: protected String paramString()
1108: {
1109: return "JTextComponent";
1110: }
1111:
1112:
1117: public TextUI getUI()
1118: {
1119: return (TextUI) ui;
1120: }
1121:
1122:
1127: public void setUI(TextUI newUI)
1128: {
1129: super.setUI(newUI);
1130: }
1131:
1132:
1136: public void updateUI()
1137: {
1138: setUI((TextUI) UIManager.getUI(this));
1139: }
1140:
1141: public Dimension getPreferredScrollableViewportSize()
1142: {
1143: return getPreferredSize();
1144: }
1145:
1146: public int getScrollableUnitIncrement(Rectangle visible, int orientation,
1147: int direction)
1148: {
1149:
1150: if (orientation == SwingConstants.HORIZONTAL)
1151: return visible.width / 10;
1152: else if (orientation == SwingConstants.VERTICAL)
1153: return visible.height / 10;
1154: else
1155: throw new IllegalArgumentException("orientation must be either "
1156: + "javax.swing.SwingConstants.VERTICAL "
1157: + "or "
1158: + "javax.swing.SwingConstants.HORIZONTAL"
1159: );
1160: }
1161:
1162: public int getScrollableBlockIncrement(Rectangle visible, int orientation,
1163: int direction)
1164: {
1165:
1166: if (orientation == SwingConstants.HORIZONTAL)
1167: return visible.width;
1168: else if (orientation == SwingConstants.VERTICAL)
1169: return visible.height;
1170: else
1171: throw new IllegalArgumentException("orientation must be either "
1172: + "javax.swing.SwingConstants.VERTICAL "
1173: + "or "
1174: + "javax.swing.SwingConstants.HORIZONTAL"
1175: );
1176: }
1177:
1178:
1183: public boolean isEditable()
1184: {
1185: return editable;
1186: }
1187:
1188:
1193: public void setEditable(boolean newValue)
1194: {
1195: if (editable == newValue)
1196: return;
1197:
1198: if (newValue == true)
1199: caretBlinkTimer.start();
1200: else
1201: {
1202: caretBlinkTimer.stop();
1203: caret.setVisible(false);
1204: }
1205:
1206: boolean oldValue = editable;
1207: editable = newValue;
1208: firePropertyChange("editable", oldValue, newValue);
1209: }
1210:
1211:
1216: public Caret getCaret()
1217: {
1218: return caret;
1219: }
1220:
1221:
1226: public void setCaret(Caret newCaret)
1227: {
1228: if (caret != null)
1229: caret.deinstall(this);
1230:
1231: Caret oldCaret = caret;
1232: caret = newCaret;
1233:
1234: caretBlinkTimer.update();
1235:
1236: if (caret != null)
1237: caret.install(this);
1238:
1239: firePropertyChange("caret", oldCaret, newCaret);
1240: }
1241:
1242: public Color getCaretColor()
1243: {
1244: return caretColor;
1245: }
1246:
1247: public void setCaretColor(Color newColor)
1248: {
1249: Color oldCaretColor = caretColor;
1250: caretColor = newColor;
1251: firePropertyChange("caretColor", oldCaretColor, newColor);
1252: }
1253:
1254: public Color getDisabledTextColor()
1255: {
1256: return disabledTextColor;
1257: }
1258:
1259: public void setDisabledTextColor(Color newColor)
1260: {
1261: Color oldColor = disabledTextColor;
1262: disabledTextColor = newColor;
1263: firePropertyChange("disabledTextColor", oldColor, newColor);
1264: }
1265:
1266: public Color getSelectedTextColor()
1267: {
1268: return selectedTextColor;
1269: }
1270:
1271: public void setSelectedTextColor(Color newColor)
1272: {
1273: Color oldColor = selectedTextColor;
1274: selectedTextColor = newColor;
1275: firePropertyChange("selectedTextColor", oldColor, newColor);
1276: }
1277:
1278: public Color getSelectionColor()
1279: {
1280: return selectionColor;
1281: }
1282:
1283: public void setSelectionColor(Color newColor)
1284: {
1285: Color oldColor = selectionColor;
1286: selectionColor = newColor;
1287: firePropertyChange("selectionColor", oldColor, newColor);
1288: }
1289:
1290:
1295: public int getCaretPosition()
1296: {
1297: return caret.getDot();
1298: }
1299:
1300:
1305: public void setCaretPosition(int position)
1306: {
1307: if (doc == null)
1308: return;
1309:
1310: if (position < 0 || position > doc.getLength())
1311: throw new IllegalArgumentException();
1312:
1313: caret.setDot(position);
1314: }
1315:
1316:
1320: public void moveCaretPosition(int position)
1321: {
1322: if (doc == null)
1323: return;
1324:
1325: if (position < 0 || position > doc.getLength())
1326: throw new IllegalArgumentException();
1327:
1328: caret.moveDot(position);
1329: }
1330:
1331: public Highlighter getHighlighter()
1332: {
1333: return highlighter;
1334: }
1335:
1336: public void setHighlighter(Highlighter newHighlighter)
1337: {
1338: if (highlighter != null)
1339: highlighter.deinstall(this);
1340:
1341: Highlighter oldHighlighter = highlighter;
1342: highlighter = newHighlighter;
1343:
1344: if (highlighter != null)
1345: highlighter.install(this);
1346:
1347: firePropertyChange("highlighter", oldHighlighter, newHighlighter);
1348: }
1349:
1350:
1355: public int getSelectionStart()
1356: {
1357: return Math.min(caret.getDot(), caret.getMark());
1358: }
1359:
1360:
1365: public void setSelectionStart(int start)
1366: {
1367: select(start, getSelectionEnd());
1368: }
1369:
1370:
1375: public int getSelectionEnd()
1376: {
1377: return Math.max(caret.getDot(), caret.getMark());
1378: }
1379:
1380:
1385: public void setSelectionEnd(int end)
1386: {
1387: select(getSelectionStart(), end);
1388: }
1389:
1390:
1396: public void select(int start, int end)
1397: {
1398: int length = doc.getLength();
1399:
1400: start = Math.max(start, 0);
1401: start = Math.min(start, length);
1402:
1403: end = Math.max(end, 0);
1404: end = Math.min(end, length);
1405:
1406: setCaretPosition(start);
1407: moveCaretPosition(end);
1408: }
1409:
1410:
1413: public void selectAll()
1414: {
1415: select(0, doc.getLength());
1416: }
1417:
1418: public synchronized void replaceSelection(String content)
1419: {
1420: int dot = caret.getDot();
1421: int mark = caret.getMark();
1422:
1423:
1424: if (content == null)
1425: {
1426: caret.setDot(dot);
1427: return;
1428: }
1429:
1430: try
1431: {
1432: int start = getSelectionStart();
1433: int end = getSelectionEnd();
1434:
1435:
1436: if (dot != mark)
1437: doc.remove(start, end - start);
1438:
1439:
1440: doc.insertString(start, content, null);
1441:
1442:
1443: setCaretPosition(start + content.length());
1444: }
1445: catch (BadLocationException e)
1446: {
1447:
1448: }
1449: }
1450:
1451: public boolean getScrollableTracksViewportHeight()
1452: {
1453: if (getParent() instanceof JViewport)
1454: return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
1455:
1456: return false;
1457: }
1458:
1459: public boolean getScrollableTracksViewportWidth()
1460: {
1461: if (getParent() instanceof JViewport)
1462: return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
1463:
1464: return false;
1465: }
1466:
1467:
1472: public void addCaretListener(CaretListener listener)
1473: {
1474: listenerList.add(CaretListener.class, listener);
1475: }
1476:
1477:
1482: public void removeCaretListener(CaretListener listener)
1483: {
1484: listenerList.remove(CaretListener.class, listener);
1485: }
1486:
1487:
1492: public CaretListener[] getCaretListeners()
1493: {
1494: return (CaretListener[]) getListeners(CaretListener.class);
1495: }
1496:
1497:
1503: protected void fireCaretUpdate(CaretEvent event)
1504: {
1505: CaretListener[] listeners = getCaretListeners();
1506:
1507: for (int index = 0; index < listeners.length; ++index)
1508: listeners[index].caretUpdate(event);
1509: }
1510:
1511:
1516: public void addInputMethodListener(InputMethodListener listener)
1517: {
1518: listenerList.add(InputMethodListener.class, listener);
1519: }
1520:
1521:
1526: public void removeInputMethodListener(InputMethodListener listener)
1527: {
1528: listenerList.remove(InputMethodListener.class, listener);
1529: }
1530:
1531:
1536: public InputMethodListener[] getInputMethodListeners()
1537: {
1538: return (InputMethodListener[]) getListeners(InputMethodListener.class);
1539: }
1540:
1541: public Rectangle modelToView(int position) throws BadLocationException
1542: {
1543: return getUI().modelToView(this, position);
1544: }
1545:
1546: public boolean getDragEnabled()
1547: {
1548: return dragEnabled;
1549: }
1550:
1551: public void setDragEnabled(boolean enabled)
1552: {
1553: dragEnabled = enabled;
1554: }
1555:
1556: public int viewToModel(Point pt)
1557: {
1558: return getUI().viewToModel(this, pt);
1559: }
1560:
1561: public void copy()
1562: {
1563: doTransferAction("copy", TransferHandler.getCopyAction());
1564: }
1565:
1566: public void cut()
1567: {
1568: doTransferAction("cut", TransferHandler.getCutAction());
1569: }
1570:
1571: public void paste()
1572: {
1573: doTransferAction("paste", TransferHandler.getPasteAction());
1574: }
1575:
1576: private void doTransferAction(String name, Action action)
1577: {
1578:
1579: if (getTransferHandler() == null)
1580: {
1581: if (defaultTransferHandler == null)
1582: defaultTransferHandler = new DefaultTransferHandler();
1583:
1584: setTransferHandler(defaultTransferHandler);
1585: }
1586:
1587:
1588: ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
1589: action.getValue(Action.NAME).toString());
1590: action.actionPerformed(event);
1591: }
1592:
1593: public void setFocusAccelerator(char newKey)
1594: {
1595: if (focusAccelerator == newKey)
1596: return;
1597:
1598: char oldKey = focusAccelerator;
1599: focusAccelerator = newKey;
1600: firePropertyChange(FOCUS_ACCELERATOR_KEY, oldKey, newKey);
1601: }
1602:
1603: public char getFocusAccelerator()
1604: {
1605: return focusAccelerator;
1606: }
1607:
1608:
1611: public NavigationFilter getNavigationFilter()
1612: {
1613: return navigationFilter;
1614: }
1615:
1616:
1619: public void setNavigationFilter(NavigationFilter filter)
1620: {
1621: navigationFilter = filter;
1622: }
1623:
1624:
1641: public void read(Reader input, Object streamDescription)
1642: throws IOException
1643: {
1644: if (streamDescription != null)
1645: {
1646: Document d = getDocument();
1647: if (d != null)
1648: d.putProperty(Document.StreamDescriptionProperty, streamDescription);
1649: }
1650:
1651: StringBuffer b = new StringBuffer();
1652: int c;
1653:
1654:
1655: while ((c = input.read()) >= 0)
1656: b.append((char) c);
1657:
1658: setText(b.toString());
1659: }
1660:
1661:
1669: public void write(Writer output)
1670: throws IOException
1671: {
1672: output.write(getText());
1673: }
1674: }