added custom code
[old-projects.git] / ekit / com / hexidec / ekit / component / ExtendedHTMLEditorKit.java
CommitLineData
c2da4d40
JL
1/*
2GNU Lesser General Public License
3
4ExtendedHTMLEditorKit
5Copyright (C) 2001-2002 Frits Jalvingh & Howard Kistler
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
24import javax.swing.text.Element;
25import javax.swing.text.StyleConstants;
26import javax.swing.text.View;
27import javax.swing.text.ViewFactory;
28import javax.swing.text.html.HTML;
29import javax.swing.text.html.HTMLEditorKit;
30
31/* WACKY GERMAN CODE */
32import com.hexidec.ekit.component.ExtendedHTMLDocument;
33import javax.swing.text.html.StyleSheet;
34import javax.swing.text.Document;
35
36/**
37 * This class extends HTMLEditorKit so that it can provide other renderer classes
38 * instead of the defaults. Most important is the part which renders relative
39 * image paths.
40 *
41 * @author <a href="mailto:jal@grimor.com">Frits Jalvingh</a>
42 * @version 1.0
43 */
44
45public class ExtendedHTMLEditorKit extends HTMLEditorKit
46{
c47fa64d
JL
47 private String BaseUrl = "";
48
c2da4d40
JL
49 /** Constructor
50 */
51 public ExtendedHTMLEditorKit()
52 {
53 }
c47fa64d
JL
54
55 public ExtendedHTMLEditorKit(String baseurl)
56 {
57 BaseUrl = baseurl;
58 }
c2da4d40
JL
59
60 /** Method for returning a ViewFactory which handles the image rendering.
61 */
62 public ViewFactory getViewFactory()
63 {
c47fa64d 64 return new HTMLFactoryExtended(BaseUrl);
c2da4d40
JL
65 }
66
67/* WACKY GERMAN CODE */
68 public Document createDefaultDocument() {
69 StyleSheet styles = getStyleSheet();
70 StyleSheet ss = new StyleSheet();
71 ss.addStyleSheet(styles);
72 ExtendedHTMLDocument doc = new ExtendedHTMLDocument(ss);
73 doc.setParser(getParser());
74 doc.setAsynchronousLoadPriority(4);
75 doc.setTokenThreshold(100);
76 return doc;
77 }
78
79/* Inner Classes --------------------------------------------- */
80
81 /** Class that replaces the default ViewFactory and supports
82 * the proper rendering of both URL-based and local images.
83 */
84 public static class HTMLFactoryExtended extends HTMLFactory implements ViewFactory
85 {
c47fa64d
JL
86 private String BaseUrl;
87
c2da4d40
JL
88 /** Constructor
89 */
c47fa64d 90 public HTMLFactoryExtended(String baseurl)
c2da4d40 91 {
c47fa64d 92 BaseUrl = baseurl;
c2da4d40
JL
93 }
94
95 /** Method to handle IMG tags and
96 * invoke the image loader.
97 */
98 public View create(Element elem)
99 {
100 Object obj = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
101 if(obj instanceof HTML.Tag)
102 {
103 HTML.Tag tagType = (HTML.Tag)obj;
104 if(tagType == HTML.Tag.IMG)
105 {
c47fa64d 106 return new RelativeImageView(elem,BaseUrl);
c2da4d40
JL
107 }
108 }
109 return super.create(elem);
110 }
111 }
112}