| 1 | /* |
| 2 | GNU Lesser General Public License |
| 3 | |
| 4 | ExtendedHTMLEditorKit |
| 5 | Copyright (C) 2001 Frits Jalvingh, Jerry Pommer & Howard Kistler |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | package com.hexidec.ekit.component; |
| 23 | |
| 24 | import javax.swing.text.Document; |
| 25 | import javax.swing.text.Element; |
| 26 | import javax.swing.text.StyleConstants; |
| 27 | import javax.swing.text.View; |
| 28 | import javax.swing.text.ViewFactory; |
| 29 | import javax.swing.text.html.HTML; |
| 30 | import javax.swing.text.html.HTMLEditorKit; |
| 31 | import javax.swing.text.html.StyleSheet; |
| 32 | |
| 33 | import com.hexidec.ekit.component.ExtendedHTMLDocument; |
| 34 | |
| 35 | /** |
| 36 | * This class extends HTMLEditorKit so that it can provide other renderer classes |
| 37 | * instead of the defaults. Most important is the part which renders relative |
| 38 | * image paths. |
| 39 | * |
| 40 | * @author <a href="mailto:jal@grimor.com">Frits Jalvingh</a> |
| 41 | * @version 1.0 |
| 42 | */ |
| 43 | |
| 44 | public class ExtendedHTMLEditorKit extends HTMLEditorKit |
| 45 | { |
| 46 | private String BaseUrl = ""; |
| 47 | |
| 48 | /** Constructor |
| 49 | */ |
| 50 | public ExtendedHTMLEditorKit() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | public ExtendedHTMLEditorKit(String baseurl) |
| 55 | { |
| 56 | BaseUrl = baseurl; |
| 57 | } |
| 58 | |
| 59 | /** Method for returning a ViewFactory which handles the image rendering. |
| 60 | */ |
| 61 | public ViewFactory getViewFactory() |
| 62 | { |
| 63 | return new HTMLFactoryExtended(BaseUrl); |
| 64 | } |
| 65 | |
| 66 | public Document createDefaultDocument() |
| 67 | { |
| 68 | StyleSheet styles = getStyleSheet(); |
| 69 | StyleSheet ss = new StyleSheet(); |
| 70 | ss.addStyleSheet(styles); |
| 71 | ExtendedHTMLDocument doc = new ExtendedHTMLDocument(ss); |
| 72 | doc.setParser(getParser()); |
| 73 | doc.setAsynchronousLoadPriority(4); |
| 74 | doc.setTokenThreshold(100); |
| 75 | return doc; |
| 76 | } |
| 77 | |
| 78 | /* Inner Classes --------------------------------------------- */ |
| 79 | |
| 80 | /** Class that replaces the default ViewFactory and supports |
| 81 | * the proper rendering of both URL-based and local images. |
| 82 | */ |
| 83 | public static class HTMLFactoryExtended extends HTMLFactory implements ViewFactory |
| 84 | { |
| 85 | private String BaseUrl; |
| 86 | |
| 87 | /** Constructor |
| 88 | */ |
| 89 | public HTMLFactoryExtended(String baseurl) |
| 90 | { |
| 91 | BaseUrl = baseurl; |
| 92 | } |
| 93 | |
| 94 | /** Method to handle IMG tags and |
| 95 | * invoke the image loader. |
| 96 | */ |
| 97 | public View create(Element elem) |
| 98 | { |
| 99 | Object obj = elem.getAttributes().getAttribute(StyleConstants.NameAttribute); |
| 100 | if(obj instanceof HTML.Tag) |
| 101 | { |
| 102 | HTML.Tag tagType = (HTML.Tag)obj; |
| 103 | if(tagType == HTML.Tag.IMG) |
| 104 | { |
| 105 | return new RelativeImageView(elem,BaseUrl); |
| 106 | } |
| 107 | } |
| 108 | return super.create(elem); |
| 109 | } |
| 110 | } |
| 111 | } |