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.awt.Color;
020: import java.awt.Dimension;
021: import java.awt.Font;
022: import java.awt.GridBagLayout;
023: import java.awt.Insets;
024: import java.util.ArrayList;
025:
026: import javax.swing.BorderFactory;
027: import javax.swing.JComponent;
028: import javax.swing.JLabel;
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.JTextArea;
032: import javax.swing.JTextPane;
033: import javax.swing.text.JTextComponent;
034: import javax.swing.text.html.HTMLEditorKit;
035:
036: import org.tp23.antinstaller.ValidationException;
037: import org.tp23.antinstaller.input.CommentOutput;
038: import org.tp23.antinstaller.input.OutputField;
039: import org.tp23.antinstaller.page.SimpleInputPage;
040: import org.tp23.antinstaller.renderer.RendererFactory;
041: import org.tp23.gui.GBCF;
042:
043: /**
044: *
045: * <p>This PageRenderer just renders a list of SwingInputRenderers using
046: * a BoxLayout </p>
047: * <p> </p>
048: * <p>Copyright: Copyright (c) 2004</p>
049: * <p>Company: tp23</p>
050: * @author Paul Hinds
051: * @version $Id: SimpleInputPageRenderer.java,v 1.6 2006/12/27 21:46:35 teknopaul Exp $
052: */
053: public class SimpleInputPageRenderer extends SwingPageRenderer {
054:
055: //Prevent displayText and explanatoryText being rendered using different fonts
056: private static final Font defaultFont = new JLabel().getFont();
057:
058: private JPanel contentPanel = new JPanel();
059:
060: private GridBagLayout gridLayout = new GridBagLayout();
061: private GBCF cf = new GBCF(); // GridBagConstraintsFactory
062: private boolean overflow = false;
063: // used in overflow
064: private JScrollPane scroller = null;
065:
066: private ArrayList renderers = new ArrayList();
067:
068: public SimpleInputPageRenderer() {
069: }
070:
071: public boolean validateFields() throws ValidationException {
072: OutputField[] fields = page.getOutputField();
073: for (int i = 0; i < fields.length; i++) {
074: if (!fields[i].validate(ctx)) {
075: SwingOutputFieldRenderer renderer = (SwingOutputFieldRenderer) renderers
076: .get(i);
077: renderer.renderError();
078: return false;
079: }
080: }
081: return true;
082: }
083:
084: public void updateInputFields() {
085: for (int i = 0; i < renderers.size(); i++) {
086: ((SwingOutputFieldRenderer) renderers.get(i))
087: .updateInputField();
088: }
089: }
090:
091: public void updateDefaultValues() {
092: for (int i = 0; i < renderers.size(); i++) {
093: ((SwingOutputFieldRenderer) renderers.get(i))
094: .updateDefaultValue();
095: }
096: }
097:
098: public void instanceInit() throws Exception {
099: overflow = ((SimpleInputPage) page).isOverflow();
100: if (overflow) {
101: //WARNING this causes flickering in the UI
102: contentPanel.setMaximumSize(new Dimension(
103: SizeConstants.PAGE_WIDTH - 50,
104: SizeConstants.PAGE_HEIGHT));
105: scroller = new JScrollPane();
106: scroller
107: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
108: scroller
109: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
110: dataPanel.add(scroller, BorderLayout.CENTER);
111: scroller.getViewport().add(contentPanel);
112: scroller.setOpaque(false);
113: ((JComponent) scroller.getViewport()).setOpaque(false);
114:
115: scroller.setBorder(BorderFactory.createEmptyBorder());
116: } else {
117: dataPanel.add(contentPanel, BorderLayout.CENTER);
118: }
119: contentPanel.setBorder(BorderFactory.createEmptyBorder(
120: SizeConstants.TOP_INDENT, 4, 4, 4));
121:
122: OutputField[] fields = page.getOutputField();
123: contentPanel.setDoubleBuffered(true);
124: contentPanel.setOpaque(false);
125: contentPanel.setLayout(gridLayout);
126: int row = 0;
127: for (int i = 0; i < fields.length; i++) {
128: SwingOutputFieldRenderer renderer = RendererFactory
129: .getSwingRenderer(fields[i]);
130: String text = fields[i].getExplanatoryText();
131: if (text != null) {
132: JTextComponent area = null;
133: if (text.startsWith("<html>")) {
134: area = new DisplayTextPane(contentPanel
135: .getBackground(), contentPanel
136: .getForeground());
137: area.setIgnoreRepaint(true);
138: area.setFont(defaultFont);
139: // HTMLEditorKit kit = new HTMLEditorKit();
140: // Document doc = new HTMLDocument();
141: // kit.read(new StringReader(text), doc, 0);
142: // area.setDocument(doc);
143: JLabel defaults = new JLabel();
144: HTMLEditorKit classpathKit = new ClasspathHTMLEditorKit();
145: ((DisplayTextPane) area).setEditorKit(classpathKit);
146:
147: String rule = "body{font-family:"
148: + defaults.getFont().getFamily()
149: + ";font-size:"
150: + defaults.getFont().getSize() + "}";
151: classpathKit.getStyleSheet().addRule(rule);
152: area.setText(text);
153: } else {
154: area = new DisplayTextArea(contentPanel
155: .getBackground(), contentPanel
156: .getForeground());
157: area.setIgnoreRepaint(true);
158: area.setFont(defaultFont);
159: area.setText(text);
160: }
161: contentPanel.add(area, cf.getSpan(row++));
162: if (fields[i] instanceof CommentOutput) {
163: CommentOutputRenderer crenderer = (CommentOutputRenderer) renderer;
164: crenderer.setExplanatoryTextField(area);
165: if (fields[i].getDisplayText() == null) {
166: continue;
167: }
168: }
169: }
170: renderer.setOutputField(fields[i]);
171: renderer.setInstallerContext(ctx);
172: renderer.initComponent(contentPanel);
173: row = renderer.addSelf(contentPanel, cf, row, overflow);
174: renderers.add(renderer);
175: }
176: JPanel glue = new JPanel();
177: glue.setOpaque(false);
178: contentPanel.add(glue, cf.getVertGlue(row++));
179: }
180: }
181:
182: /**
183: * A JTextArea that is not editable and looks like a JLabel but uses
184: * JTextAreas ability to wrap. Also has a fixed prefered width;
185: * @author Paul Hinds
186: * @version $Id: SimpleInputPageRenderer.java,v 1.6 2006/12/27 21:46:35 teknopaul Exp $
187: */
188: class DisplayTextArea extends JTextArea {
189: DisplayTextArea(Color back, Color fore) {
190: super ();
191: this .setBackground(back);
192: this .setForeground(fore);
193: this .setLineWrap(true);
194: this .setWrapStyleWord(true);
195: this .setEditable(false);
196: this .setSelectionColor(back);
197: this .setSelectionColor(back);
198: this .setMargin(new Insets(5, 0, 0, 0));
199: this .setOpaque(false);
200: }
201:
202: public Dimension getPreferredSize() {
203: Dimension pref = super .getPreferredSize();
204: return new Dimension(SizeConstants.PAGE_WIDTH - 40, pref.height);
205: }
206:
207: public Dimension getMinimumSize() {
208: Dimension pref = super .getMinimumSize();
209: return new Dimension(SizeConstants.PAGE_WIDTH - 40, pref.height);
210: }
211: }
212:
213: class DisplayTextPane extends JTextPane {
214: DisplayTextPane(Color back, Color fore) {
215: super ();
216: this .setBackground(back);
217: this .setForeground(fore);
218: this .setEditable(false);
219: this .setSelectionColor(back);
220: this .setSelectionColor(back);
221: this .setMargin(new Insets(5, 0, 0, 0));
222: this .setOpaque(false);
223: }
224:
225: public Dimension getPreferredSize() {
226: Dimension pref = super .getPreferredSize();
227: return new Dimension(SizeConstants.PAGE_WIDTH - 40, pref.height);
228: }
229:
230: public Dimension getMinimumSize() {
231: Dimension pref = super .getMinimumSize();
232: return new Dimension(SizeConstants.PAGE_WIDTH - 40, pref.height);
233: }
234: }
|