tentative merge of EKIT_0_9H and custom patches
[old-projects.git] / ekit / com / hexidec / ekit / component / ExtendedHTMLEditorKit.java
CommitLineData
c2da4d40
JL
1/*
2GNU Lesser General Public License
3
4ExtendedHTMLEditorKit
b6b7ed68 5Copyright (C) 2001 Frits Jalvingh, Jerry Pommer & Howard Kistler
c2da4d40
JL
6
7This library is free software; you can redistribute it and/or
8modify it under the terms of the GNU Lesser General Public
9License as published by the Free Software Foundation; either
10version 2.1 of the License, or (at your option) any later version.
11
12This library is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public
18License along with this library; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*/
21
22package com.hexidec.ekit.component;
23
b6b7ed68 24import javax.swing.text.Document;
c2da4d40
JL
25import javax.swing.text.Element;
26import javax.swing.text.StyleConstants;
27import javax.swing.text.View;
28import javax.swing.text.ViewFactory;
29import javax.swing.text.html.HTML;
30import javax.swing.text.html.HTMLEditorKit;
b6b7ed68 31import javax.swing.text.html.StyleSheet;
c2da4d40 32
c2da4d40 33import com.hexidec.ekit.component.ExtendedHTMLDocument;
c2da4d40
JL
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
44public class ExtendedHTMLEditorKit extends HTMLEditorKit
45{
c47fa64d
JL
46 private String BaseUrl = "";
47
c2da4d40
JL
48 /** Constructor
49 */
50 public ExtendedHTMLEditorKit()
51 {
52 }
c47fa64d
JL
53
54 public ExtendedHTMLEditorKit(String baseurl)
55 {
56 BaseUrl = baseurl;
57 }
c2da4d40
JL
58
59 /** Method for returning a ViewFactory which handles the image rendering.
60 */
61 public ViewFactory getViewFactory()
62 {
c47fa64d 63 return new HTMLFactoryExtended(BaseUrl);
c2da4d40
JL
64 }
65
b6b7ed68
JL
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;
c2da4d40
JL
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 {
c47fa64d
JL
85 private String BaseUrl;
86
c2da4d40
JL
87 /** Constructor
88 */
c47fa64d 89 public HTMLFactoryExtended(String baseurl)
c2da4d40 90 {
c47fa64d 91 BaseUrl = baseurl;
c2da4d40
JL
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 {
c47fa64d 105 return new RelativeImageView(elem,BaseUrl);
c2da4d40
JL
106 }
107 }
108 return super.create(elem);
109 }
110 }
111}