001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.swing;
017:
018: import java.awt.BorderLayout;
019: import java.io.BufferedReader;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022:
023: import javax.swing.BorderFactory;
024: import javax.swing.JLabel;
025: import javax.swing.JScrollPane;
026: import javax.swing.JTextPane;
027: import javax.swing.text.html.HTMLEditorKit;
028:
029: import org.tp23.antinstaller.ValidationException;
030: import org.tp23.antinstaller.page.TextPage;
031: import org.tp23.antinstaller.runtime.ConfigurationException;
032:
033: /**
034: * A page containing a text file's contents, may be HTML in swing.
035: * The HTML supported is the standard Swing subset of HTML3.2 so
036: * it really just adds a bit of formatting and looks pretty bad.
037: * The page is also parsed and property references in the document
038: * are converted to the runtime values.
039: * e.g. ${java.user.name} would be replaced with the current user in the HTML text.
040: *
041: * Both the html page and embeded images are loaded from the classpath so
042: * can be packaged in the jar.
043: *
044: * The default font and background are determined by
045: * the LAF.
046: * @author teknopaul
047: *
048: */
049: public class TextPageRenderer extends SwingPageRenderer {
050:
051: private JTextPane textPane = new JTextPane();
052: private StringBuffer buffer = new StringBuffer();
053:
054: public TextPageRenderer() {
055: textPane.setOpaque(false);
056: }
057:
058: public boolean validateFields() throws ValidationException {
059: return true;
060: }
061:
062: public void instanceInit() throws Exception {
063: final String resource = ((TextPage) page).getHtmlResource();
064: InputStream in = this .getClass().getResourceAsStream(resource);
065: if (in == null) {
066: throw new ConfigurationException(
067: "Html page resource is missing:" + resource);
068: }
069: BufferedReader br = new BufferedReader(
070: new InputStreamReader(in));
071: String read = null;
072: while ((read = br.readLine()) != null) {
073: buffer.append(read);
074: }
075: // as per FindBugs
076: br.close();
077:
078: JLabel defaults = new JLabel();
079: textPane.setBackground(defaults.getBackground());
080: textPane.setEditable(false);
081: textPane.setContentType("text/html");
082: HTMLEditorKit classpathKit = new ClasspathHTMLEditorKit();
083: textPane.setEditorKit(classpathKit);
084: textPane.setAutoscrolls(true);
085:
086: String rule = "body{font-family:"
087: + defaults.getFont().getFamily() + ";font-size:"
088: + defaults.getFont().getSize() + "}";
089: classpathKit.getStyleSheet().addRule(rule);
090: textPane.setBorder(BorderFactory.createEmptyBorder());
091:
092: JScrollPane scroller = new JScrollPane();
093: scroller
094: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
095: scroller
096: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
097: // scroller.setBorder(BorderFactory.createCompoundBorder(
098: // BorderFactory.createEmptyBorder(4, 4, 4, 4),
099: // BorderFactory.createEtchedBorder()
100: // ));
101: scroller.setBorder(BorderFactory.createEmptyBorder());
102: add(scroller, BorderLayout.CENTER);
103: scroller.getViewport().add(textPane);
104: dataPanel.add(scroller, BorderLayout.CENTER);
105: }
106:
107: public void updateInputFields() {
108: }
109:
110: /**
111: * updateDefaultValues
112: */
113: public void updateDefaultValues() {
114: // parse property references
115: String parsedHtml = ctx.getInstaller().getResultContainer()
116: .getDefaultValue(buffer.toString());
117: textPane.setText(parsedHtml);
118: textPane.setCaretPosition(0);
119: }
120: }
|