1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65:
66:
89: public class JEditorPane extends JTextComponent
90: {
91:
96: protected class AccessibleJEditorPane extends AccessibleJTextComponent
97: {
98:
99:
102: protected AccessibleJEditorPane()
103: {
104: super();
105: }
106:
107:
114: public String getAccessibleDescription()
115: {
116: String descr = super.getAccessibleDescription();
117: if (descr == null)
118: return getContentType();
119: else
120: return descr;
121: }
122:
123:
128: public AccessibleStateSet getAccessibleStateSet()
129: {
130: AccessibleStateSet state = super.getAccessibleStateSet();
131:
132: return state;
133: }
134: }
135:
136:
142: protected class AccessibleJEditorPaneHTML extends AccessibleJEditorPane
143: {
144:
151: public AccessibleText getAccessibleText()
152: {
153: return new JEditorPaneAccessibleHypertextSupport();
154: }
155: }
156:
157:
163: protected class JEditorPaneAccessibleHypertextSupport
164: extends AccessibleJEditorPane implements AccessibleHypertext
165: {
166:
167:
172: public class HTMLLink extends AccessibleHyperlink
173: {
174:
175:
178: Element element;
179:
180:
185: public HTMLLink(Element el)
186: {
187: this.element = el;
188: }
189:
190:
198: public boolean isValid()
199: {
200:
201:
202:
203:
204: HTMLDocument doc = (HTMLDocument) getDocument();
205: return doc.getCharacterElement(element.getStartOffset()) == element;
206: }
207:
208:
216: public int getAccessibleActionCount()
217: {
218:
219: return 1;
220: }
221:
222:
229: public boolean doAccessibleAction(int i)
230: {
231: String href = (String) element.getAttributes().getAttribute("href");
232: HTMLDocument doc = (HTMLDocument) getDocument();
233: try
234: {
235: URL url = new URL(doc.getBase(), href);
236: setPage(url);
237: String desc = doc.getText(element.getStartOffset(),
238: element.getEndOffset() - element.getStartOffset());
239: HyperlinkEvent ev =
240: new HyperlinkEvent(JEditorPane.this,
241: HyperlinkEvent.EventType.ACTIVATED, url, desc,
242: element);
243: fireHyperlinkUpdate(ev);
244: return true;
245: }
246: catch (Exception ex)
247: {
248: return false;
249: }
250: }
251:
252:
261: public String getAccessibleActionDescription(int i)
262: {
263: HTMLDocument doc = (HTMLDocument) getDocument();
264: try
265: {
266: return doc.getText(element.getStartOffset(),
267: element.getEndOffset() - element.getStartOffset());
268: }
269: catch (BadLocationException ex)
270: {
271: throw (AssertionError)
272: new AssertionError("BadLocationException must not be thrown "
273: + "here.")
274: .initCause(ex);
275: }
276: }
277:
278:
287: public Object getAccessibleActionObject(int i)
288: {
289: String href = (String) element.getAttributes().getAttribute("href");
290: HTMLDocument doc = (HTMLDocument) getDocument();
291: try
292: {
293: URL url = new URL(doc.getBase(), href);
294: return url;
295: }
296: catch (MalformedURLException ex)
297: {
298: return null;
299: }
300: }
301:
302:
310: public Object getAccessibleActionAnchor(int i)
311: {
312:
313: return getAccessibleActionDescription(i);
314: }
315:
316:
321: public int getStartIndex()
322: {
323: return element.getStartOffset();
324: }
325:
326:
331: public int getEndIndex()
332: {
333: return element.getEndOffset();
334: }
335:
336: }
337:
338:
343: public int getLinkCount()
344: {
345: HTMLDocument doc = (HTMLDocument) getDocument();
346: HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
347: int count = 0;
348: while (linkIter.isValid())
349: {
350: count++;
351: linkIter.next();
352: }
353: return count;
354: }
355:
356:
366: public AccessibleHyperlink getLink(int i)
367: {
368: HTMLDocument doc = (HTMLDocument) getDocument();
369: HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
370: int count = 0;
371: while (linkIter.isValid())
372: {
373: count++;
374: if (count == i)
375: break;
376: linkIter.next();
377: }
378: if (linkIter.isValid())
379: {
380: int offset = linkIter.getStartOffset();
381:
382:
383:
384: Element el = doc.getCharacterElement(offset);
385: HTMLLink link = new HTMLLink(el);
386: return link;
387: }
388: else
389: return null;
390: }
391:
392:
403: public int getLinkIndex(int c)
404: {
405: HTMLDocument doc = (HTMLDocument) getDocument();
406: HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
407: int count = 0;
408: while (linkIter.isValid())
409: {
410: if (linkIter.getStartOffset() <= c && linkIter.getEndOffset() > c)
411: break;
412: count++;
413: linkIter.next();
414: }
415: if (linkIter.isValid())
416: return count;
417: else
418: return -1;
419: }
420:
421:
431: public String getLinkText(int i)
432: {
433: HTMLDocument doc = (HTMLDocument) getDocument();
434: HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
435: int count = 0;
436: while (linkIter.isValid())
437: {
438: count++;
439: if (count == i)
440: break;
441: linkIter.next();
442: }
443: if (linkIter.isValid())
444: {
445: int offset = linkIter.getStartOffset();
446:
447:
448:
449: Element el = doc.getCharacterElement(offset);
450: try
451: {
452: String text = doc.getText(el.getStartOffset(),
453: el.getEndOffset() - el.getStartOffset());
454: return text;
455: }
456: catch (BadLocationException ex)
457: {
458: throw (AssertionError)
459: new AssertionError("BadLocationException must not be thrown "
460: + "here.")
461: .initCause(ex);
462: }
463: }
464: else
465: return null;
466: }
467: }
468:
469: private static final long serialVersionUID = 3140472492599046285L;
470:
471: private URL page;
472: private EditorKit editorKit;
473:
474: boolean focus_root;
475:
476: public JEditorPane()
477: {
478: setEditorKit(createDefaultEditorKit());
479: }
480:
481: public JEditorPane(String url) throws IOException
482: {
483: this(new URL(url));
484: }
485:
486: public JEditorPane(String type, String text)
487: {
488: setEditorKit(createEditorKitForContentType(type));
489: setText(text);
490: }
491:
492: public JEditorPane(URL url) throws IOException
493: {
494: this();
495: setPage(url);
496: }
497:
498: protected EditorKit createDefaultEditorKit()
499: {
500: return new DefaultEditorKit();
501: }
502:
503: public static EditorKit createEditorKitForContentType(String type)
504: {
505: return new DefaultEditorKit();
506: }
507:
508:
513: public void fireHyperlinkUpdate(HyperlinkEvent event)
514: {
515: HyperlinkListener[] listeners = getHyperlinkListeners();
516:
517: for (int index = 0; index < listeners.length; ++index)
518: listeners[index].hyperlinkUpdate(event);
519: }
520:
521:
526: public AccessibleContext getAccessibleContext()
527: {
528: if (accessibleContext == null)
529: {
530: if (getEditorKit() instanceof HTMLEditorKit)
531: accessibleContext = new AccessibleJEditorPaneHTML();
532: else
533: accessibleContext = new AccessibleJEditorPane();
534: }
535: return accessibleContext;
536: }
537:
538: public final String getContentType()
539: {
540: return getEditorKit().getContentType();
541: }
542:
543:
547: public EditorKit getEditorKit()
548: {
549: if (editorKit == null)
550: setEditorKit(createDefaultEditorKit());
551: return editorKit;
552: }
553:
554: public static String getEditorKitClassNameForContentType(String type)
555: {
556: return "text/plain";
557: }
558:
559: public EditorKit getEditorKitForContentType(String type)
560: {
561: return editorKit;
562: }
563:
564:
567: public Dimension getPreferredSize()
568: {
569: return super.getPreferredSize();
570: }
571:
572: public boolean getScrollableTracksViewportHeight()
573: {
574:
577: return isValid();
578: }
579:
580: public boolean getScrollableTracksViewportWidth()
581: {
582:
585: return isValid();
586: }
587:
588: public URL getPage()
589: {
590: return page;
591: }
592:
593: protected InputStream getStream(URL page)
594: throws IOException
595: {
596: return page.openStream();
597: }
598:
599: public String getText()
600: {
601: return super.getText();
602: }
603:
604: public String getUIClassID()
605: {
606: return "EditorPaneUI";
607: }
608:
609: public boolean isFocusCycleRoot()
610: {
611: return focus_root;
612: }
613:
614: protected String paramString()
615: {
616: return "JEditorPane";
617: }
618:
619:
622: public void read(InputStream in, Object desc) throws IOException
623: {
624: EditorKit kit = getEditorKit();
625: if (kit instanceof HTMLEditorKit && desc instanceof HTMLDocument)
626: {
627: Document doc = (Document) desc;
628: try
629: {
630: kit.read(in, doc, 0);
631: }
632: catch (BadLocationException ex)
633: {
634: assert false : "BadLocationException must not be thrown here.";
635: }
636: }
637: else
638: {
639: Reader inRead = new InputStreamReader(in);
640: super.read(inRead, desc);
641: }
642: }
643:
644:
647: public static void registerEditorKitForContentType(String type,
648: String classname)
649: {
650:
651: }
652:
653:
656: public static void registerEditorKitForContentType(String type,
657: String classname,
658: ClassLoader loader)
659: {
660:
661: }
662:
663:
667: public void replaceSelection(String content)
668: {
669:
670: }
671:
672:
676: public void scrollToReference(String reference)
677: {
678:
679: }
680:
681: public final void setContentType(String type)
682: {
683: if (editorKit != null
684: && editorKit.getContentType().equals(type))
685: return;
686:
687: EditorKit kit = getEditorKitForContentType(type);
688:
689: if (kit != null)
690: setEditorKit(kit);
691: }
692:
693: public void setEditorKit(EditorKit newValue)
694: {
695: if (editorKit == newValue)
696: return;
697:
698: if (editorKit != null)
699: editorKit.deinstall(this);
700:
701: EditorKit oldValue = editorKit;
702: editorKit = newValue;
703:
704: if (editorKit != null)
705: {
706: editorKit.install(this);
707: setDocument(editorKit.createDefaultDocument());
708: }
709:
710: firePropertyChange("editorKit", oldValue, newValue);
711: invalidate();
712: repaint();
713:
714: accessibleContext = null;
715: }
716:
717: public void setEditorKitForContentType(String type, EditorKit k)
718: {
719:
720: }
721:
722:
725: public void setPage(String url) throws IOException
726: {
727: setPage(new URL(url));
728: }
729:
730:
733: public void setPage(URL page) throws IOException
734: {
735: if (page == null)
736: throw new IOException("invalid url");
737:
738: try
739: {
740: this.page = page;
741: getEditorKit().read(page.openStream(), getDocument(), 0);
742: }
743: catch (BadLocationException e)
744: {
745:
746: }
747: }
748:
749: public void setText(String t)
750: {
751: super.setText(t);
752: }
753:
754:
759: public void addHyperlinkListener(HyperlinkListener listener)
760: {
761: listenerList.add(HyperlinkListener.class, listener);
762: }
763:
764:
769: public void removeHyperlinkListener(HyperlinkListener listener)
770: {
771: listenerList.remove(HyperlinkListener.class, listener);
772: }
773:
774:
781: public HyperlinkListener[] getHyperlinkListeners()
782: {
783: return (HyperlinkListener[]) getListeners(HyperlinkListener.class);
784: }
785: }