1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56:
62: public class TextComponent extends Component
63: implements Serializable, Accessible
64: {
65:
66: private static final long serialVersionUID = -2214773872412987419L;
67:
68:
72: boolean editable;
73:
74:
78: int selectionStart;
79:
80:
84: int selectionEnd;
85:
86:
90: String text;
91:
92:
95: protected transient TextListener textListener;
96:
97: protected class AccessibleAWTTextComponent
98: extends AccessibleAWTComponent
99: implements AccessibleText, TextListener
100: {
101: private static final long serialVersionUID = 3631432373506317811L;
102:
103:
104:
105: public AccessibleAWTTextComponent()
106: {
107: TextComponent.this.addTextListener(this);
108: }
109:
110: public AccessibleRole getAccessibleRole()
111: {
112: return AccessibleRole.TEXT;
113: }
114:
115: public AccessibleStateSet getAccessibleStateSet()
116: {
117:
118:
119: AccessibleStateSet ss = super.getAccessibleStateSet();
120: if (editable)
121: ss.add(AccessibleState.EDITABLE);
122: return ss;
123: }
124:
125: public AccessibleText getAccessibleText()
126: {
127: return this;
128: }
129:
130:
133: public int getIndexAtPoint(Point point)
134: {
135: return TextComponent.this.getIndexAtPoint(point);
136: }
137:
138:
141: public Rectangle getCharacterBounds(int index)
142: {
143: return TextComponent.this.getCharacterBounds(index);
144: }
145:
146:
149: public int getCharCount()
150: {
151: return text.length();
152: }
153:
154:
157: public int getCaretPosition()
158: {
159: return TextComponent.this.getCaretPosition();
160: }
161:
162:
165: public String getAtIndex(int part, int index)
166: {
167: if (index < 0 || index >= text.length())
168: return null;
169: BreakIterator it = null;
170: switch (part)
171: {
172: case CHARACTER:
173: return text.substring(index, index + 1);
174: case WORD:
175: it = BreakIterator.getWordInstance();
176: break;
177: case SENTENCE:
178: it = BreakIterator.getSentenceInstance();
179: break;
180: default:
181: return null;
182: }
183: it.setText(text);
184: int start = index;
185: if (!it.isBoundary(index))
186: start = it.preceding(index);
187: int end = it.following(index);
188: if (end == -1)
189: return text.substring(index);
190: else
191: return text.substring(index, end);
192: }
193:
194:
197: public String getAfterIndex(int part, int index) {
198: if (index < 0 || index >= text.length())
199: return null;
200: BreakIterator it = null;
201: switch (part)
202: {
203: case CHARACTER:
204: return text.substring(index, index + 1);
205: case WORD:
206: it = BreakIterator.getWordInstance();
207: break;
208: case SENTENCE:
209: it = BreakIterator.getSentenceInstance();
210: break;
211: default:
212: return null;
213: }
214: it.setText(text);
215: int start = index;
216: if (!it.isBoundary(index))
217: start = it.following(index);
218:
219:
220: if (start == -1)
221: return null;
222: int end = it.following(start);
223: if (end == -1)
224: return text.substring(index);
225: else
226: return text.substring(index, end);
227: }
228:
229:
232: public String getBeforeIndex(int part, int index)
233: {
234: if (index < 1 || index >= text.length())
235: return null;
236: BreakIterator it = null;
237: switch (part)
238: {
239: case CHARACTER:
240: return text.substring(index - 1, index);
241: case WORD:
242: it = BreakIterator.getWordInstance();
243: break;
244: case SENTENCE:
245: it = BreakIterator.getSentenceInstance();
246: break;
247: default:
248: return null;
249: }
250: it.setText(text);
251: int end = index;
252: if (!it.isBoundary(index))
253: end = it.preceding(index);
254:
255:
256: if (end == -1)
257: return null;
258: int start = it.preceding(end);
259: if (start == -1)
260: return text.substring(0, end);
261: else
262: return text.substring(start, end);
263: }
264:
265:
268: public AttributeSet getCharacterAttribute(int index)
269: {
270:
271: return null;
272: }
273:
274:
277: public int getSelectionStart() {
278:
279: return selectionStart;
280: }
281:
282:
285: public int getSelectionEnd()
286: {
287: return selectionEnd;
288: }
289:
290:
293: public String getSelectedText()
294: {
295: if (selectionEnd - selectionStart > 0)
296: return text.substring(selectionStart, selectionEnd);
297: else
298: return null;
299: }
300:
301:
304: public void textValueChanged(TextEvent event)
305: {
306:
307:
308: }
309:
310: }
311:
312:
313: TextComponent(String text)
314: {
315: if (text == null)
316: this.text = "";
317: else
318: this.text = text;
319:
320: this.editable = true;
321: }
322:
323:
324:
329: public synchronized String getText()
330: {
331: TextComponentPeer tcp = (TextComponentPeer) getPeer();
332: if (tcp != null)
333: text = tcp.getText();
334:
335: return(text);
336: }
337:
338:
343: public synchronized void setText(String text)
344: {
345: if (text == null)
346: text = "";
347:
348: this.text = text;
349:
350: TextComponentPeer tcp = (TextComponentPeer) getPeer();
351: if (tcp != null)
352: tcp.setText(text);
353: setCaretPosition(0);
354: }
355:
356:
361: public synchronized String getSelectedText()
362: {
363: String alltext = getText();
364: int start = getSelectionStart();
365: int end = getSelectionEnd();
366:
367: return(alltext.substring(start, end));
368: }
369:
370:
376: public synchronized int getSelectionStart()
377: {
378: TextComponentPeer tcp = (TextComponentPeer) getPeer();
379: if (tcp != null)
380: selectionStart = tcp.getSelectionStart();
381:
382: return(selectionStart);
383: }
384:
385:
392: public synchronized void setSelectionStart(int selectionStart)
393: {
394: select(selectionStart,
395: (getSelectionEnd() < selectionStart)
396: ? selectionStart : getSelectionEnd());
397: }
398:
399:
405: public synchronized int getSelectionEnd()
406: {
407: TextComponentPeer tcp = (TextComponentPeer) getPeer();
408: if (tcp != null)
409: selectionEnd = tcp.getSelectionEnd();
410:
411: return(selectionEnd);
412: }
413:
414:
421: public synchronized void setSelectionEnd(int selectionEnd)
422: {
423: select(getSelectionStart(), selectionEnd);
424: }
425:
426:
434: public synchronized void select(int selectionStart, int selectionEnd)
435: {
436: if (selectionStart < 0)
437: selectionStart = 0;
438:
439: if (selectionStart > getText().length())
440: selectionStart = text.length();
441:
442: if (selectionEnd > text.length())
443: selectionEnd = text.length();
444:
445: if (selectionStart > selectionEnd)
446: selectionStart = selectionEnd;
447:
448: this.selectionStart = selectionStart;
449: this.selectionEnd = selectionEnd;
450:
451: TextComponentPeer tcp = (TextComponentPeer) getPeer();
452: if (tcp != null)
453: tcp.select(selectionStart, selectionEnd);
454: }
455:
456:
459: public synchronized void selectAll()
460: {
461: select(0, getText().length());
462: }
463:
464:
469: public synchronized int getCaretPosition()
470: {
471: TextComponentPeer tcp = (TextComponentPeer) getPeer();
472: if (tcp != null)
473: return(tcp.getCaretPosition());
474: else
475: return(0);
476: }
477:
478:
488: public synchronized void setCaretPosition(int caretPosition)
489: {
490: if (caretPosition < 0)
491: throw new IllegalArgumentException();
492:
493: TextComponentPeer tcp = (TextComponentPeer) getPeer();
494: if (tcp != null)
495: tcp.setCaretPosition(caretPosition);
496: }
497:
498:
504: public boolean isEditable()
505: {
506: return(editable);
507: }
508:
509:
515: public synchronized void setEditable(boolean editable)
516: {
517: this.editable = editable;
518:
519: TextComponentPeer tcp = (TextComponentPeer) getPeer();
520: if (tcp != null)
521: tcp.setEditable(editable);
522: }
523:
524:
527: public void removeNotify()
528: {
529: super.removeNotify();
530: }
531:
532:
538: public synchronized void addTextListener(TextListener listener)
539: {
540: textListener = AWTEventMulticaster.add(textListener, listener);
541:
542: enableEvents(AWTEvent.TEXT_EVENT_MASK);
543: }
544:
545:
551: public synchronized void removeTextListener(TextListener listener)
552: {
553: textListener = AWTEventMulticaster.remove(textListener, listener);
554: }
555:
556:
563: protected void processEvent(AWTEvent event)
564: {
565: if (event instanceof TextEvent)
566: processTextEvent((TextEvent)event);
567: else
568: super.processEvent(event);
569: }
570:
571:
580: protected void processTextEvent(TextEvent event)
581: {
582: if (textListener != null)
583: textListener.textValueChanged(event);
584: }
585:
586: void dispatchEventImpl(AWTEvent e)
587: {
588: if (e.id <= TextEvent.TEXT_LAST
589: && e.id >= TextEvent.TEXT_FIRST
590: && (textListener != null
591: || (eventMask & AWTEvent.TEXT_EVENT_MASK) != 0))
592: processEvent(e);
593: else
594: super.dispatchEventImpl(e);
595: }
596:
597:
602: protected String paramString()
603: {
604: return(getClass().getName() + "(text=" + getText() + ")");
605: }
606:
607:
615: public <T extends EventListener> T[] getListeners(Class<T> listenerType)
616: {
617: if (listenerType == TextListener.class)
618: return AWTEventMulticaster.getListeners(textListener, listenerType);
619:
620: return super.getListeners(listenerType);
621: }
622:
623:
626: public TextListener[] getTextListeners()
627: {
628: return (TextListener[]) getListeners(TextListener.class);
629: }
630:
631:
637: public AccessibleContext getAccessibleContext()
638: {
639:
640: if (accessibleContext == null)
641: accessibleContext = new AccessibleAWTTextComponent();
642: return accessibleContext;
643: }
644:
645:
646:
647:
648:
649: synchronized int getIndexAtPoint(Point p)
650: {
651: TextComponentPeer tcp = (TextComponentPeer) getPeer();
652: if (tcp != null)
653: return tcp.getIndexAtPoint(p.x, p.y);
654: return -1;
655: }
656:
657: synchronized Rectangle getCharacterBounds(int i)
658: {
659: TextComponentPeer tcp = (TextComponentPeer) getPeer();
660: if (tcp != null)
661: return tcp.getCharacterBounds(i);
662: return null;
663: }
664:
665:
671: static boolean ignoreOldMouseEvents()
672: {
673: return true;
674: }
675:
676: }