1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
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 | * HTMLEditor kit that replaces the source of images in the document
|
---|
28 | * with resources loaded from the classpath.
|
---|
29 | * @author teknopaul
|
---|
30 | */
|
---|
31 | public class ClasspathHTMLEditorKit extends HTMLEditorKit {
|
---|
32 |
|
---|
33 | public ViewFactory getViewFactory(){
|
---|
34 |
|
---|
35 | return new HTMLEditorKit.HTMLFactory(){
|
---|
36 |
|
---|
37 | public View create(Element elem){
|
---|
38 | if(! elem.getName().equals("img")){
|
---|
39 | return super.create(elem);
|
---|
40 | }
|
---|
41 | return new ImageView(elem){
|
---|
42 | public URL getImageURL() {
|
---|
43 | String src = (String)getElement().getAttributes().
|
---|
44 | getAttribute(HTML.Attribute.SRC);
|
---|
45 | return TextPageRenderer.class.getResource(src);
|
---|
46 | }
|
---|
47 | };
|
---|
48 | }
|
---|
49 |
|
---|
50 | };
|
---|
51 |
|
---|
52 | }
|
---|
53 |
|
---|
54 | }
|
---|