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 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *     
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package com.izforge.izpack.panels;
021
022import java.awt.GridBagConstraints;
023import java.awt.GridBagLayout;
024import java.awt.Insets;
025import java.awt.event.ActionEvent;
026import java.awt.event.ActionListener;
027import java.net.URL;
028
029import javax.swing.ButtonGroup;
030import javax.swing.JEditorPane;
031import javax.swing.JLabel;
032import javax.swing.JRadioButton;
033import javax.swing.JScrollPane;
034import javax.swing.event.HyperlinkEvent;
035import javax.swing.event.HyperlinkListener;
036
037import com.izforge.izpack.gui.LabelFactory;
038import com.izforge.izpack.installer.InstallData;
039import com.izforge.izpack.installer.InstallerFrame;
040import com.izforge.izpack.installer.IzPanel;
041import com.izforge.izpack.installer.ResourceManager;
042
043/**
044 * The IzPack HTML license panel.
045 * 
046 * @author Julien Ponge
047 */
048public class HTMLLicencePanel extends IzPanel implements HyperlinkListener, ActionListener
049{
050
051    /**
052     * 
053     */
054    private static final long serialVersionUID = 3256728385458746416L;
055
056    /** The layout. */
057    private GridBagLayout layout;
058
059    /** The layout constraints. */
060    private GridBagConstraints gbConstraints;
061
062    /** The info label. */
063    private JLabel infoLabel;
064
065    /** The text area. */
066    private JEditorPane textArea;
067
068    /** The radio buttons. */
069    private JRadioButton yesRadio, noRadio;
070
071    /**
072     * The constructor.
073     * 
074     * @param idata The installation data.
075     * @param parent Description of the Parameter
076     */
077    public HTMLLicencePanel(InstallerFrame parent, InstallData idata)
078    {
079        super(parent, idata);
080
081        // We initialize our layout
082        layout = new GridBagLayout();
083        gbConstraints = new GridBagConstraints();
084        setLayout(layout);
085
086        // We load the licence
087        loadLicence();
088
089        // We put our components
090
091        infoLabel = LabelFactory.create(parent.langpack.getString("LicencePanel.info"),
092                parent.icons.getImageIcon("history"), JLabel.TRAILING);
093        parent.buildConstraints(gbConstraints, 0, 0, 2, 1, 1.0, 0.0);
094        gbConstraints.insets = new Insets(5, 5, 5, 5);
095        gbConstraints.fill = GridBagConstraints.NONE;
096        gbConstraints.anchor = GridBagConstraints.WEST;
097        layout.addLayoutComponent(infoLabel, gbConstraints);
098        add(infoLabel);
099
100        try
101        {
102            textArea = new JEditorPane();
103            textArea.setEditable(false);
104            textArea.addHyperlinkListener(this);
105            JScrollPane scroller = new JScrollPane(textArea);
106            textArea.setPage(loadLicence());
107            parent.buildConstraints(gbConstraints, 0, 1, 2, 1, 1.0, 1.0);
108            gbConstraints.anchor = GridBagConstraints.CENTER;
109            gbConstraints.fill = GridBagConstraints.BOTH;
110            layout.addLayoutComponent(scroller, gbConstraints);
111            add(scroller);
112        }
113        catch (Exception err)
114        {
115            err.printStackTrace();
116        }
117
118        ButtonGroup group = new ButtonGroup();
119
120        yesRadio = new JRadioButton(parent.langpack.getString("LicencePanel.agree"), false);
121        group.add(yesRadio);
122        parent.buildConstraints(gbConstraints, 0, 2, 1, 1, 1.0, 0.0);
123        gbConstraints.anchor = GridBagConstraints.WEST;
124        gbConstraints.fill = GridBagConstraints.NONE;
125        layout.addLayoutComponent(yesRadio, gbConstraints);
126        add(yesRadio);
127        yesRadio.addActionListener(this);
128
129        noRadio = new JRadioButton(parent.langpack.getString("LicencePanel.notagree"), true);
130        group.add(noRadio);
131        parent.buildConstraints(gbConstraints, 0, 3, 1, 1, 1.0, 0.0);
132        gbConstraints.anchor = GridBagConstraints.WEST;
133        gbConstraints.fill = GridBagConstraints.NONE;
134        gbConstraints.insets = new Insets(0, 5, 5, 5);
135        layout.addLayoutComponent(noRadio, gbConstraints);
136        add(noRadio);
137        noRadio.addActionListener(this);
138        setInitialFocus(textArea);
139    }
140
141    /**
142     * Loads the license text.
143     * 
144     * @return The license text URL.
145     */
146    private URL loadLicence()
147    {
148        String resNamePrifix = "HTMLLicencePanel.licence";
149        try
150        {
151            return ResourceManager.getInstance().getURL(resNamePrifix);
152        }
153        catch (Exception ex)
154        {
155            ex.printStackTrace();
156        }
157        return null;
158    }
159
160    /**
161     * Actions-handling method (here it launches the installation).
162     * 
163     * @param e The event.
164     */
165    public void actionPerformed(ActionEvent e)
166    {
167        if (yesRadio.isSelected())
168            parent.unlockNextButton();
169        else
170            parent.lockNextButton();
171    }
172
173    /**
174     * Indicates wether the panel has been validated or not.
175     * 
176     * @return true if the user agrees with the license, false otherwise.
177     */
178    public boolean isValidated()
179    {
180        if (noRadio.isSelected())
181        {
182            parent.exit();
183            return false;
184        }
185        else
186            return (yesRadio.isSelected());
187    }
188
189    /**
190     * Hyperlink events handler.
191     * 
192     * @param e The event.
193     */
194    public void hyperlinkUpdate(HyperlinkEvent e)
195    {
196        try
197        {
198            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
199                textArea.setPage(e.getURL());
200        }
201        catch (Exception err)
202        {}
203    }
204
205    /** Called when the panel becomes active. */
206    public void panelActivate()
207    {
208        if (!yesRadio.isSelected()) parent.lockNextButton();
209    }
210}