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: import ;
66: import ;
67: import ;
68:
69:
92: public class JEditorPane extends JTextComponent
93: {
94:
99: protected class AccessibleJEditorPane extends AccessibleJTextComponent
100: {
101:
102:
105: protected AccessibleJEditorPane()
106: {
107: super();
108: }
109:
110:
117: public String getAccessibleDescription()
118: {
119: String descr = super.getAccessibleDescription();
120: if (descr == null)
121: return getContentType();
122: else
123: return descr;
124: }
125:
126:
131: public AccessibleStateSet getAccessibleStateSet()
132: {
133: AccessibleStateSet state = super.getAccessibleStateSet();
134:
135: return state;
136: }
137: }
138:
139:
145: protected class AccessibleJEditorPaneHTML extends AccessibleJEditorPane
146: {
147:
154: public AccessibleText getAccessibleText()
155: {
156: return new JEditorPaneAccessibleHypertextSupport();
157: }
158: }
159:
160:
166: protected class JEditorPaneAccessibleHypertextSupport
167: extends AccessibleJEditorPane implements AccessibleHypertext
168: {
169:
170:
175: public class HTMLLink extends AccessibleHyperlink
176: {
177:
178:
181: Element element;
182:
183:
188: public HTMLLink(Element el)
189: {
190: this.element = el;
191: }
192:
193:
201: public boolean isValid()
202: {
203:
204:
205:
206:
207: HTMLDocument doc = (HTMLDocument) getDocument();
208: return doc.getCharacterElement(element.getStartOffset()) == element;
209: }
210:
211:
219: public int getAccessibleActionCount()
220: {
221:
222: return 1;
223: }
224:
225:
232: public boolean doAccessibleAction(int i)
233: {
234: String href = (String) element.getAttributes().getAttribute("href");
235: HTMLDocument doc = (HTMLDocument) getDocument();
236: try
237: {
238: URL url = new URL(doc.getBase(), href);
239: setPage(url);
240: String desc = doc.getText(element.getStartOffset(),
241: element.getEndOffset() - element.getStartOffset());
242: HyperlinkEvent ev =
243: new HyperlinkEvent(JEditorPane.this,
244: HyperlinkEvent.EventType.ACTIVATED, url, desc,
245: element);
246: fireHyperlinkUpdate(ev);
247: return true;
248: }
249: catch (Exception ex)
250: {
251: return false;
252: }
253: }
254:
255:
264: public String getAccessibleActionDescription(int i)
265: {
266: HTMLDocument doc = (HTMLDocument) getDocument();
267: try
268: {
269: return doc.getText(element.getStartOffset(),
270: element.getEndOffset() - element.getStartOffset());
271: }
272: catch (BadLocationException ex)
273: {
274: throw (AssertionError)
275: new AssertionError("BadLocationException must not be thrown "
276: + "here.")
277: .initCause(ex);
278: }
279: }
280:
281:
290: public Object getAccessibleActionObject(int i)
291: {
292: String href = (String) element.getAttributes().getAttribute("href");
293: HTMLDocument doc = (HTMLDocument) getDocument();
294: try
295: {
296: URL url = new URL(doc.getBase(), href);
297: return url;
298: }
299: catch (MalformedURLException ex)
300: {
301: return null;
302: }
303: }
304:
305:
313: public Object getAccessibleActionAnchor(int i)
314: {
315:
316: return getAccessibleActionDescription(i);
317: }
318:
319:
324: public int getStartIndex()
325: {
326: return element.getStartOffset();
327: }
328:
329:
334: public int getEndIndex()
335: {
336: return element.getEndOffset();
337: }
338:
339: }
340:
341:
346: public int getLinkCount()
347: {
348: HTMLDocument doc = (HTMLDocument) getDocument();
349: HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
350: int count = 0;
351: while (linkIter.isValid())
352: {
353: count++;
354: linkIter.next();
355: }
356: return count;
357: }
358:
359:
369: public AccessibleHyperlink getLink(int i)
370: {
371: HTMLDocument doc = (HTMLDocument) getDocument();
372: HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
373: int count = 0;
374: while (linkIter.isValid())
375: {
376: count++;
377: if (count == i)
378: break;
379: linkIter.next();
380: }
381: if (linkIter.isValid())
382: {
383: int offset = linkIter.getStartOffset();
384:
385:
386:
387: Element el = doc.getCharacterElement(offset);
388: HTMLLink link = new HTMLLink(el);
389: return link;
390: }
391: else
392: return null;
393: }
394:
395:
406: public int getLinkIndex(int c)
407: {
408: HTMLDocument doc = (HTMLDocument) getDocument();
409: HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
410: int count = 0;
411: while (linkIter.isValid())
412: {
413: if (linkIter.getStartOffset() <= c && linkIter.getEndOffset() > c)
414: break;
415: count++;
416: linkIter.next();
417: }
418: if (linkIter.isValid())
419: return count;
420: else
421: return -1;
422: }
423:
424:
434: public String getLinkText(int i)
435: {
436: HTMLDocument doc = (HTMLDocument) getDocument();
437: HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A);
438: int count = 0;
439: while (linkIter.isValid())
440: {
441: count++;
442: if (count == i)
443: break;
444: linkIter.next();
445: }
446: if (linkIter.isValid())
447: {
448: int offset = linkIter.getStartOffset();
449:
450:
451:
452: Element el = doc.getCharacterElement(offset);
453: try
454: {
455: String text = doc.getText(el.getStartOffset(),
456: el.getEndOffset() - el.getStartOffset());
457: return text;
458: }
459: catch (BadLocationException ex)
460: {
461: throw (AssertionError)
462: new AssertionError("BadLocationException must not be thrown "
463: + "here.")
464: .initCause(ex);
465: }
466: }
467: else
468: return null;
469: }
470: }
471:
472:
478: private static class PlainEditorKit extends DefaultEditorKit
479: {
480:
481:
484: public ViewFactory getViewFactory()
485: {
486: return new ViewFactory()
487: {
488: public View create(Element el)
489: {
490: return new WrappedPlainView(el);
491: }
492: };
493: }
494: }
495:
496: private static final long serialVersionUID = 3140472492599046285L;
497:
498: private URL page;
499: private EditorKit editorKit;
500:
501: boolean focus_root;
502:
503: public JEditorPane()
504: {
505: setEditorKit(createDefaultEditorKit());
506: }
507:
508: public JEditorPane(String url) throws IOException
509: {
510: this(new URL(url));
511: }
512:
513: public JEditorPane(String type, String text)
514: {
515: setEditorKit(createEditorKitForContentType(type));
516: setText(text);
517: }
518:
519: public JEditorPane(URL url) throws IOException
520: {
521: this();
522: setPage(url);
523: }
524:
525: protected EditorKit createDefaultEditorKit()
526: {
527: return new PlainEditorKit();
528: }
529:
530: public static EditorKit createEditorKitForContentType(String type)
531: {
532: return new PlainEditorKit();
533: }
534:
535:
540: public void fireHyperlinkUpdate(HyperlinkEvent event)
541: {
542: HyperlinkListener[] listeners = getHyperlinkListeners();
543:
544: for (int index = 0; index < listeners.length; ++index)
545: listeners[index].hyperlinkUpdate(event);
546: }
547:
548:
553: public AccessibleContext getAccessibleContext()
554: {
555: if (accessibleContext == null)
556: {
557: if (getEditorKit() instanceof HTMLEditorKit)
558: accessibleContext = new AccessibleJEditorPaneHTML();
559: else
560: accessibleContext = new AccessibleJEditorPane();
561: }
562: return accessibleContext;
563: }
564:
565: public final String getContentType()
566: {
567: return getEditorKit().getContentType();
568: }
569:
570:
574: public EditorKit getEditorKit()
575: {
576: if (editorKit == null)
577: setEditorKit(createDefaultEditorKit());
578: return editorKit;
579: }
580:
581: public static String getEditorKitClassNameForContentType(String type)
582: {
583: return "text/plain";
584: }
585:
586: public EditorKit getEditorKitForContentType(String type)
587: {
588: return editorKit;
589: }
590:
591:
594: public Dimension getPreferredSize()
595: {
596: return super.getPreferredSize();
597: }
598:
599: public boolean getScrollableTracksViewportHeight()
600: {
601:
604: return isValid();
605: }
606:
607: public boolean getScrollableTracksViewportWidth()
608: {
609:
612: return isValid();
613: }
614:
615: public URL getPage()
616: {
617: return page;
618: }
619:
620: protected InputStream getStream(URL page)
621: throws IOException
622: {
623: return page.openStream();
624: }
625:
626: public String getText()
627: {
628: return super.getText();
629: }
630:
631: public String getUIClassID()
632: {
633: return "EditorPaneUI";
634: }
635:
636: public boolean isFocusCycleRoot()
637: {
638: return focus_root;
639: }
640:
641: protected String paramString()
642: {
643: return "JEditorPane";
644: }
645:
646:
649: public void read(InputStream in, Object desc) throws IOException
650: {
651: EditorKit kit = getEditorKit();
652: if (kit instanceof HTMLEditorKit && desc instanceof HTMLDocument)
653: {
654: Document doc = (Document) desc;
655: try
656: {
657: kit.read(in, doc, 0);
658: }
659: catch (BadLocationException ex)
660: {
661: assert false : "BadLocationException must not be thrown here.";
662: }
663: }
664: else
665: {
666: Reader inRead = new InputStreamReader(in);
667: super.read(inRead, desc);
668: }
669: }
670:
671:
674: public static void registerEditorKitForContentType(String type,
675: String classname)
676: {
677:
678: }
679:
680:
683: public static void registerEditorKitForContentType(String type,
684: String classname,
685: ClassLoader loader)
686: {
687:
688: }
689:
690:
694: public void replaceSelection(String content)
695: {
696:
697: }
698:
699:
703: public void scrollToReference(String reference)
704: {
705:
706: }
707:
708: public final void setContentType(String type)
709: {
710: if (editorKit != null
711: && editorKit.getContentType().equals(type))
712: return;
713:
714: EditorKit kit = getEditorKitForContentType(type);
715:
716: if (kit != null)
717: setEditorKit(kit);
718: }
719:
720: public void setEditorKit(EditorKit newValue)
721: {
722: if (editorKit == newValue)
723: return;
724:
725: if (editorKit != null)
726: editorKit.deinstall(this);
727:
728: EditorKit oldValue = editorKit;
729: editorKit = newValue;
730:
731: if (editorKit != null)
732: {
733: editorKit.install(this);
734: setDocument(editorKit.createDefaultDocument());
735: }
736:
737: firePropertyChange("editorKit", oldValue, newValue);
738: invalidate();
739: repaint();
740:
741: accessibleContext = null;
742: }
743:
744: public void setEditorKitForContentType(String type, EditorKit k)
745: {
746:
747: }
748:
749:
752: public void setPage(String url) throws IOException
753: {
754: setPage(new URL(url));
755: }
756:
757:
760: public void setPage(URL page) throws IOException
761: {
762: if (page == null)
763: throw new IOException("invalid url");
764:
765: try
766: {
767: this.page = page;
768: getEditorKit().read(page.openStream(), getDocument(), 0);
769: }
770: catch (BadLocationException e)
771: {
772:
773: }
774: }
775:
776: public void setText(String t)
777: {
778: super.setText(t);
779: }
780:
781:
786: public void addHyperlinkListener(HyperlinkListener listener)
787: {
788: listenerList.add(HyperlinkListener.class, listener);
789: }
790:
791:
796: public void removeHyperlinkListener(HyperlinkListener listener)
797: {
798: listenerList.remove(HyperlinkListener.class, listener);
799: }
800:
801:
808: public HyperlinkListener[] getHyperlinkListeners()
809: {
810: return (HyperlinkListener[]) getListeners(HyperlinkListener.class);
811: }
812: }