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.awt.BorderLayout;
|
---|
19 | import java.io.BufferedReader;
|
---|
20 | import java.io.InputStream;
|
---|
21 | import java.io.InputStreamReader;
|
---|
22 |
|
---|
23 | import javax.swing.BorderFactory;
|
---|
24 | import javax.swing.JLabel;
|
---|
25 | import javax.swing.JScrollPane;
|
---|
26 | import javax.swing.JTextPane;
|
---|
27 | import javax.swing.text.html.HTMLEditorKit;
|
---|
28 |
|
---|
29 | import org.tp23.antinstaller.ValidationException;
|
---|
30 | import org.tp23.antinstaller.page.TextPage;
|
---|
31 | import org.tp23.antinstaller.runtime.ConfigurationException;
|
---|
32 | /**
|
---|
33 | * A page containing a text file's contents, may be HTML in swing.
|
---|
34 | * The HTML supported is the standard Swing subset of HTML3.2 so
|
---|
35 | * it really just adds a bit of formatting and looks pretty bad.
|
---|
36 | * The page is also parsed and property references in the document
|
---|
37 | * are converted to the runtime values.
|
---|
38 | * e.g. ${java.user.name} would be replaced with the current user in the HTML text.
|
---|
39 | *
|
---|
40 | * Both the html page and embeded images are loaded from the classpath so
|
---|
41 | * can be packaged in the jar.
|
---|
42 | *
|
---|
43 | * The default font and background are determined by
|
---|
44 | * the LAF.
|
---|
45 | * @author teknopaul
|
---|
46 | *
|
---|
47 | */
|
---|
48 | public class TextPageRenderer extends SwingPageRenderer{
|
---|
49 |
|
---|
50 | private JTextPane textPane = new JTextPane();
|
---|
51 | private StringBuffer buffer = new StringBuffer();
|
---|
52 |
|
---|
53 | public TextPageRenderer() {
|
---|
54 | }
|
---|
55 |
|
---|
56 | public boolean validateFields()throws ValidationException{
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void reInstanceInit() {}
|
---|
61 |
|
---|
62 | public void instanceInit() throws Exception {
|
---|
63 | final String resource = ((TextPage)page).getHtmlResource();
|
---|
64 | InputStream in = this.getClass().getResourceAsStream(resource);
|
---|
65 | if(in == null){
|
---|
66 | throw new ConfigurationException("Html page resource is missing:" + resource);
|
---|
67 | }
|
---|
68 | BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
---|
69 | String read = null;
|
---|
70 | while ( (read = br.readLine()) != null) {
|
---|
71 | buffer.append(read);
|
---|
72 | }
|
---|
73 | // as per FindBugs
|
---|
74 | br.close();
|
---|
75 |
|
---|
76 | JLabel defaults = new JLabel();
|
---|
77 | textPane.setBackground(defaults.getBackground());
|
---|
78 | textPane.setEditable(false);
|
---|
79 | textPane.setContentType("text/html");
|
---|
80 | HTMLEditorKit classpathKit = new ClasspathHTMLEditorKit();
|
---|
81 | textPane.setEditorKit(classpathKit);
|
---|
82 | textPane.setAutoscrolls(true);
|
---|
83 |
|
---|
84 | String rule = "body{font-family:" + defaults.getFont().getFamily() +
|
---|
85 | ";font-size:" + defaults.getFont().getSize() + "}";
|
---|
86 | classpathKit.getStyleSheet().addRule(rule);
|
---|
87 | textPane.setBorder(BorderFactory.createEmptyBorder());
|
---|
88 |
|
---|
89 | JScrollPane scroller = new JScrollPane();
|
---|
90 | scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
---|
91 | scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
|
---|
92 | scroller.setBorder(BorderFactory.createCompoundBorder(
|
---|
93 | BorderFactory.createEmptyBorder(4, 4, 4, 4),
|
---|
94 | BorderFactory.createEtchedBorder()
|
---|
95 | ));
|
---|
96 | add(scroller, BorderLayout.CENTER);
|
---|
97 | scroller.getViewport().add(textPane);
|
---|
98 | this.add(scroller, BorderLayout.CENTER);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void updateInputFields(){
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * updateDefaultValues
|
---|
108 | */
|
---|
109 | public void updateDefaultValues() {
|
---|
110 | // parse property references
|
---|
111 | String parsedHtml = ctx.getInstaller().getResultContainer().getDefaultValue(buffer.toString());
|
---|
112 | textPane.setText(parsedHtml);
|
---|
113 | textPane.setCaretPosition(0);
|
---|
114 | }
|
---|
115 | }
|
---|