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.awt.Color;
20import java.awt.Dimension;
21import java.awt.Font;
22import java.awt.GridBagLayout;
23import java.awt.Insets;
24import java.util.ArrayList;
25
26import javax.swing.BorderFactory;
27import javax.swing.JLabel;
28import javax.swing.JPanel;
29import javax.swing.JScrollPane;
30import javax.swing.JTextArea;
31
32import org.tp23.antinstaller.ValidationException;
33import org.tp23.antinstaller.input.CommentOutput;
34import org.tp23.antinstaller.input.OutputField;
35import org.tp23.antinstaller.page.SimpleInputPage;
36import org.tp23.antinstaller.renderer.RendererFactory;
37import org.tp23.gui.GBCF;
38
39/**
40 *
41 * <p>This PageRenderer just renders a list of SwingInputRenderers using
42* a BoxLayout </p>
43 * <p> </p>
44 * <p>Copyright: Copyright (c) 2004</p>
45 * <p>Company: tp23</p>
46 * @author Paul Hinds
47 * @version $Id: SimpleInputPageRenderer.java,v 1.6 2006/12/27 21:46:35 teknopaul Exp $
48 */
49public class SimpleInputPageRenderer
50    extends SwingPageRenderer {
51
52     //Prevent displayText and explanatoryText being rendered using different fonts
53    private static final Font defaultFont = new JLabel().getFont();
54
55    private JPanel contentPanel = new JPanel();
56    
57    private GridBagLayout gridLayout = new GridBagLayout();
58    private GBCF cf = new GBCF(); // GridBagConstraintsFactory
59    private boolean overflow = false;
60    // used in overflow
61    JScrollPane scroller = null;
62    
63
64    private ArrayList renderers = new ArrayList();
65
66    public SimpleInputPageRenderer(){
67    }
68        
69    public boolean validateFields() throws ValidationException {
70        OutputField[] fields = page.getOutputField();
71        for (int i = 0; i < fields.length; i++) {
72            if(!fields[i].validate(ctx)){
73                SwingOutputFieldRenderer renderer = (SwingOutputFieldRenderer)renderers.get(i);
74                renderer.renderError();
75                return false;
76            }
77        }
78        return true;
79    }
80    public void updateInputFields(){
81        for (int i = 0; i < renderers.size(); i++) {
82            ((SwingOutputFieldRenderer)renderers.get(i)).updateInputField();
83        }
84    }
85
86    public void updateDefaultValues(){
87        for (int i = 0; i < renderers.size(); i++) {
88            ((SwingOutputFieldRenderer)renderers.get(i)).updateDefaultValue();
89        }
90    }
91
92    public void instanceInit() throws Exception {
93        overflow = ((SimpleInputPage)page).isOverflow();
94        if(overflow){
95            //WARNING this causes flickering in the UI 
96            contentPanel.setMaximumSize(new Dimension(SizeConstants.PAGE_WIDTH - 50, SizeConstants.PAGE_HEIGHT));
97            scroller = new JScrollPane();
98            scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
99            scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
00            scroller.setBorder(BorderFactory.createCompoundBorder(
01                            BorderFactory.createEmptyBorder(4,4,4,4),
02                            BorderFactory.createEtchedBorder()              
03                            ));
04            add(scroller, BorderLayout.CENTER);
05            scroller.getViewport().add(contentPanel);
06            //contentPanel.setBackground(Color.red);
07        }
08        else {
09            this.add(contentPanel, BorderLayout.CENTER);
10            contentPanel.setBorder(BorderFactory.createEmptyBorder(SizeConstants.TOP_INDENT, 4, 4, 4));
11        }
12
13        OutputField[] fields = page.getOutputField();
14        contentPanel.setDoubleBuffered(true);
15        contentPanel.setLayout(gridLayout);
16        int row = 0;
17        for (int i = 0; i < fields.length; i++) {
18            SwingOutputFieldRenderer renderer = RendererFactory.getSwingRenderer(fields[i]);
19            String text = fields[i].getExplanatoryText();
20            if(fields[i].getExplanatoryText() != null){
21                JTextArea area = new DisplayTextArea(contentPanel.getBackground(), contentPanel.getForeground());
22                area.setIgnoreRepaint(true);
23                area.setFont( defaultFont );
24                area.setText(text);
25                contentPanel.add(area, cf.getSpan(row++));
26                if(fields[i] instanceof CommentOutput){
27                    CommentOutputRenderer crenderer = (CommentOutputRenderer)renderer;
28                    crenderer.setExplanatoryTextField(area);
29                    if(fields[i].getDisplayText() == null){
30                        continue;
31                    }
32                }
33            }
34            renderer.setOutputField(fields[i]);
35            renderer.setInstallerContext(ctx);
36            renderer.initComponent(contentPanel);
37            row = renderer.addSelf(contentPanel, cf, row, overflow);
38            renderers.add(renderer);
39        }
40        contentPanel.add(new JPanel(), cf.getVertGlue(row++));
41    }
42}
43/**
44 * A JTextArea that is not editable and looks like a JLabel but uses 
45 * JTextAreas ability to wrap.  Also has a fixed prefered width; 
46 * @author Paul Hinds
47 * @version $Id: SimpleInputPageRenderer.java,v 1.6 2006/12/27 21:46:35 teknopaul Exp $
48 */
49class DisplayTextArea extends JTextArea{
50    DisplayTextArea(Color back, Color fore){
51        super();
52        this.setBackground(back);
53        this.setForeground(fore);
54        this.setLineWrap(true);
55        this.setWrapStyleWord(true);
56        this.setEditable(false);
57        this.setSelectionColor(back);
58        this.setSelectionColor(back);
59        this.setMargin(new Insets(5,0,0,0));
60    }
61
62    public Dimension getPreferredSize(){
63        Dimension pref = super.getPreferredSize();
64        return new Dimension(SizeConstants.PAGE_WIDTH - 40, pref.height);
65    }
66    public Dimension getMinimumSize(){
67        Dimension pref = super.getMinimumSize();
68        return new Dimension(SizeConstants.PAGE_WIDTH - 40, pref.height);
69    }
70}
71