01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.tp23.antinstaller.renderer.swing;
17:
18: import java.net.URL;
19:
20: import javax.swing.text.Element;
21: import javax.swing.text.View;
22: import javax.swing.text.ViewFactory;
23: import javax.swing.text.html.HTML;
24: import javax.swing.text.html.HTMLEditorKit;
25: import javax.swing.text.html.ImageView;
26:
27: /**
28: * HTMLEditor kit that replaces the source of images in the document
29: * with resources loaded from the classpath.
30: * @author teknopaul
31: */
32: public class ClasspathHTMLEditorKit extends HTMLEditorKit {
33:
34: public ViewFactory getViewFactory() {
35:
36: return new HTMLEditorKit.HTMLFactory() {
37:
38: public View create(Element elem) {
39: if (!elem.getName().equals("img")) {
40: return super .create(elem);
41: }
42: return new ImageView(elem) {
43: public URL getImageURL() {
44: String src = (String) getElement()
45: .getAttributes().getAttribute(
46: HTML.Attribute.SRC);
47: return TextPageRenderer.class.getResource(src);
48: }
49: };
50: }
51:
52: };
53:
54: }
55:
56: }
|