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 */
16package org.tp23.antinstaller.renderer.swing;
17
18import java.awt.BorderLayout;
19import java.io.BufferedReader;
20import java.io.InputStream;
21import java.io.InputStreamReader;
22
23import javax.swing.BorderFactory;
24import javax.swing.JLabel;
25import javax.swing.JScrollPane;
26import javax.swing.JTextPane;
27import javax.swing.text.html.HTMLEditorKit;
28
29import org.tp23.antinstaller.ValidationException;
30import org.tp23.antinstaller.page.TextPage;
31import 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 */
48public 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 instanceInit() throws Exception {
61        final String resource = ((TextPage)page).getHtmlResource();
62        InputStream in = this.getClass().getResourceAsStream(resource);
63        if(in == null){
64            throw new ConfigurationException("Html page resource is missing:" + resource);
65        }
66        BufferedReader br = new BufferedReader(new InputStreamReader(in));
67        String read = null;
68        while ( (read = br.readLine()) != null) {
69            buffer.append(read);
70        }
71        // as per FindBugs 
72        br.close();
73        
74        JLabel defaults = new JLabel();
75        textPane.setBackground(defaults.getBackground());
76        textPane.setEditable(false);
77        textPane.setContentType("text/html");
78        HTMLEditorKit classpathKit = new ClasspathHTMLEditorKit();
79        textPane.setEditorKit(classpathKit);
80        textPane.setAutoscrolls(true);
81        
82        String rule = "body{font-family:" + defaults.getFont().getFamily() + 
83        ";font-size:" + defaults.getFont().getSize() + "}";
84        classpathKit.getStyleSheet().addRule(rule);
85        textPane.setBorder(BorderFactory.createEmptyBorder());
86
87        JScrollPane scroller = new JScrollPane();
88        scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
89        scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
90        scroller.setBorder(BorderFactory.createCompoundBorder(
91                        BorderFactory.createEmptyBorder(4, 4, 4, 4),
92                        BorderFactory.createEtchedBorder()              
93                        ));
94        add(scroller, BorderLayout.CENTER);
95        scroller.getViewport().add(textPane);
96        this.add(scroller, BorderLayout.CENTER);
97    }
98
99    public void updateInputFields(){
00    }
01
02
03
04    /**
05     * updateDefaultValues
06     */
07    public void updateDefaultValues() {
08        // parse property references
09        String parsedHtml = ctx.getInstaller().getResultContainer().getDefaultValue(buffer.toString());
10        textPane.setText(parsedHtml);
11        textPane.setCaretPosition(0);
12    }
13}
14