001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2001 Johannes Lehtinen 
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.panels;
023
024import java.awt.GridBagConstraints;
025import java.awt.GridBagLayout;
026import java.awt.Insets;
027
028import javax.swing.JLabel;
029import javax.swing.JScrollPane;
030import javax.swing.JTextArea;
031
032import com.izforge.izpack.gui.LabelFactory;
033import com.izforge.izpack.installer.InstallData;
034import com.izforge.izpack.installer.InstallerFrame;
035import com.izforge.izpack.installer.IzPanel;
036import com.izforge.izpack.installer.ResourceManager;
037import com.izforge.izpack.util.VariableSubstitutor;
038
039/**
040 * The XInfo panel class - shows some adaptative text (ie by parsing for some variables.
041 * 
042 * @author Julien Ponge
043 */
044public class XInfoPanel extends IzPanel
045{
046
047    /**
048     * 
049     */
050    private static final long serialVersionUID = 3257009856274970416L;
051
052    /** The layout. */
053    private GridBagLayout layout;
054
055    /** The layout constraints. */
056    private GridBagConstraints gbConstraints;
057
058    /** The info label. */
059    private JLabel infoLabel;
060
061    /** The text area. */
062    private JTextArea textArea;
063
064    /** The info to display. */
065    private String info;
066
067    /**
068     * The constructor.
069     * 
070     * @param parent The parent window.
071     * @param idata The installation data.
072     */
073    public XInfoPanel(InstallerFrame parent, InstallData idata)
074    {
075        super(parent, idata);
076
077        // We initialize our layout
078        layout = new GridBagLayout();
079        gbConstraints = new GridBagConstraints();
080        setLayout(layout);
081
082        // We add the components
083
084        infoLabel = LabelFactory.create(parent.langpack.getString("InfoPanel.info"), parent.icons
085                .getImageIcon("edit"), JLabel.TRAILING);
086        parent.buildConstraints(gbConstraints, 0, 0, 1, 1, 1.0, 0.0);
087        gbConstraints.insets = new Insets(5, 5, 5, 5);
088        gbConstraints.fill = GridBagConstraints.BOTH;
089        gbConstraints.anchor = GridBagConstraints.SOUTHWEST;
090        layout.addLayoutComponent(infoLabel, gbConstraints);
091        add(infoLabel);
092
093        textArea = new JTextArea();
094        textArea.setEditable(false);
095        JScrollPane scroller = new JScrollPane(textArea);
096        parent.buildConstraints(gbConstraints, 0, 1, 1, 1, 1.0, 0.9);
097        gbConstraints.anchor = GridBagConstraints.CENTER;
098        layout.addLayoutComponent(scroller, gbConstraints);
099        add(scroller);
100    }
101
102    /** Loads the info text. */
103    private void loadInfo()
104    {
105        try
106        {
107            // We read it
108            info = ResourceManager.getInstance().getTextResource("XInfoPanel.info");
109        }
110        catch (Exception err)
111        {
112            info = "Error : could not load the info text !";
113        }
114    }
115
116    /** Parses the text for special variables. */
117    private void parseText()
118    {
119        try
120        {
121            // Initialize the variable substitutor
122            VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables());
123
124            // Parses the info text
125            info = vs.substitute(info, null);
126        }
127        catch (Exception err)
128        {
129            err.printStackTrace();
130        }
131    }
132
133    /** Called when the panel becomes active. */
134    public void panelActivate()
135    {
136        // Text handling
137        loadInfo();
138        parseText();
139
140        // UI handling
141        textArea.setText(info);
142        textArea.setCaretPosition(0);
143    }
144
145    /**
146     * Indicates wether the panel has been validated or not.
147     * 
148     * @return Always true.
149     */
150    public boolean isValidated()
151    {
152        return true;
153    }
154}