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 2004 Volker Friedritz
008 * Copyright 2002 Marcus Wolschon
009 * Copyright 2002 Jan Blok
010 * 
011 * Licensed under the Apache License, Version 2.0 (the "License");
012 * you may not use this file except in compliance with the License.
013 * You may obtain a copy of the License at
014 * 
015 *     http://www.apache.org/licenses/LICENSE-2.0
016 *     
017 * Unless required by applicable law or agreed to in writing, software
018 * distributed under the License is distributed on an "AS IS" BASIS,
019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 * See the License for the specific language governing permissions and
021 * limitations under the License.
022 */
023
024package com.izforge.izpack.panels;
025
026import java.awt.Component;
027import java.awt.Dimension;
028import java.awt.GridBagConstraints;
029import java.awt.GridBagLayout;
030import java.awt.Insets;
031import java.net.URL;
032import java.util.ArrayList;
033import java.util.Iterator;
034
035import javax.swing.BorderFactory;
036import javax.swing.Box;
037import javax.swing.ImageIcon;
038import javax.swing.JLabel;
039import javax.swing.JScrollPane;
040import javax.swing.event.ListSelectionEvent;
041
042import com.izforge.izpack.installer.InstallData;
043import com.izforge.izpack.installer.InstallerFrame;
044import com.izforge.izpack.installer.ResourceManager;
045import com.izforge.izpack.util.IoHelper;
046
047/**
048 * The ImgPacks panel class. Allows the packages selection with a small picture displayed for every
049 * pack. This new version combines the old PacksPanel and the ImgPacksPanel so that the positive
050 * characteristics of both are combined. This class handles only the layout and some related stuff.
051 * Common stuff are handled by the base class.
052 * 
053 * @author Julien Ponge
054 * @author Volker Friedritz
055 * @author Klaus Bartz
056 */
057public class ImgPacksPanel extends PacksPanelBase
058{
059
060    /**
061     * 
062     */
063    private static final long serialVersionUID = 3977858492633659444L;
064
065    /** The images to display. */
066    private ArrayList images;
067
068    /** The description scroll. */
069    private JScrollPane descriptionScroller;
070
071    /** The img label. */
072    private JLabel imgLabel;
073
074    /** The layout. */
075    private GridBagLayout layout;
076
077    /** The layout constraints. */
078    private GridBagConstraints gbConstraints;
079
080    /**
081     * The constructor.
082     * 
083     * @param parent The parent window.
084     * @param idata The installation data.
085     */
086    public ImgPacksPanel(InstallerFrame parent, InstallData idata)
087    {
088        super(parent, idata);
089    }
090
091    /*
092     * (non-Javadoc)
093     * 
094     * @see com.izforge.izpack.panels.PacksPanelBase#createNormalLayout()
095     */
096    protected void createNormalLayout()
097    {
098        preLoadImages();
099        layout = new GridBagLayout();
100        gbConstraints = new GridBagConstraints();
101        setLayout(layout);
102
103        // Create constraint for first component as standard constraint.
104        parent.buildConstraints(gbConstraints, 0, 0, 1, 1, 0.25, 0.0);
105        gbConstraints.insets = new Insets(5, 5, 5, 5);
106        gbConstraints.anchor = GridBagConstraints.WEST;
107        // Create the info label.
108        createLabel("PacksPanel.info", "preferences", layout, gbConstraints);
109
110        // Create the snap label.
111        parent.buildConstraints(gbConstraints, 1, 0, 1, 1, 0.50, 0.0);
112        createLabel("ImgPacksPanel.snap", "tip", layout, gbConstraints);
113
114        // Create packs table with a scroller.
115        tableScroller = new JScrollPane();
116        parent.buildConstraints(gbConstraints, 0, 1, 1, 2, 0.50, 0.0);
117        gbConstraints.fill = GridBagConstraints.BOTH;
118        packsTable = createPacksTable(250, tableScroller, layout, gbConstraints);
119
120        // Create the image label with a scroller.
121        imgLabel = new JLabel((ImageIcon) images.get(0));
122        JScrollPane imgScroller = new JScrollPane(imgLabel);
123        imgScroller.setPreferredSize(getPreferredSizeFromImages());
124        parent.buildConstraints(gbConstraints, 1, 1, 1, 1, 0.5, 1.0);
125        layout.addLayoutComponent(imgScroller, gbConstraints);
126        add(imgScroller);
127
128        // Create a vertical strut.
129
130        Component strut = Box.createVerticalStrut(20);
131        parent.buildConstraints(gbConstraints, 1, 2, 1, 3, 0.0, 0.0);
132        layout.addLayoutComponent(strut, gbConstraints);
133        add(strut);
134
135        // Create the dependency area with a scroller.
136        if (dependenciesExist)
137        {
138            JScrollPane depScroller = new JScrollPane();
139            depScroller.setPreferredSize(new Dimension(250, 40));
140            parent.buildConstraints(gbConstraints, 0, 3, 1, 1, 0.50, 0.50);
141            dependencyArea = createTextArea("ImgPacksPanel.dependencyList", depScroller, layout,
142                    gbConstraints);
143        }
144
145        // Create the description area with a scroller.
146        descriptionScroller = new JScrollPane();
147        descriptionScroller.setPreferredSize(new Dimension(200, 60));
148        descriptionScroller.setBorder(BorderFactory.createEmptyBorder());
149
150        parent.buildConstraints(gbConstraints, 1, 3, 1, 1, 0.50, 0.50);
151        descriptionArea = createTextArea("PacksPanel.description", descriptionScroller, layout,
152                gbConstraints);
153        // Create the tip label.
154        parent.buildConstraints(gbConstraints, 0, 4, 2, 1, 0.0, 0.0);
155        createLabel("PacksPanel.tip", "tip", layout, gbConstraints);
156        // Create the space label.
157        parent.buildConstraints(gbConstraints, 0, 5, 2, 1, 0.0, 0.0);
158        spaceLabel = createPanelWithLabel("PacksPanel.space", layout, gbConstraints);
159        if (IoHelper.supported("getFreeSpace"))
160        { // Create the free space label only if free space is supported.
161            parent.buildConstraints(gbConstraints, 0, 6, 2, 1, 0.0, 0.0);
162            freeSpaceLabel = createPanelWithLabel("PacksPanel.freespace", layout, gbConstraints);
163        }
164
165    }
166
167    /** Pre-loads the images. */
168    private void preLoadImages()
169    {
170        int size = idata.availablePacks.size();
171        images = new ArrayList(size);
172        for (int i = 0; i < size; i++)
173            try
174            {
175                URL url = ResourceManager.getInstance().getURL("ImgPacksPanel.img." + i);
176                ImageIcon img = new ImageIcon(url);
177                images.add(img);
178            }
179            catch (Exception err)
180            {
181                err.printStackTrace();
182            }
183    }
184
185    /**
186     * Try to find a good preferredSize for imgScroller by checking all loaded images' width and
187     * height.
188     */
189    private Dimension getPreferredSizeFromImages()
190    {
191        int maxWidth = 80, maxHeight = 60;
192        ImageIcon icon;
193
194        for (Iterator it = images.iterator(); it.hasNext();)
195        {
196            icon = (ImageIcon) it.next();
197            maxWidth = Math.max(maxWidth, icon.getIconWidth());
198            maxHeight = Math.max(maxHeight, icon.getIconHeight());
199        }
200
201        maxWidth = Math.min(maxWidth + 20, idata.guiPrefs.width - 150);
202        maxHeight = Math.min(maxHeight + 20, idata.guiPrefs.height - 150);
203
204        return new Dimension(maxWidth, maxHeight);
205    }
206
207    /*
208     * (non-Javadoc)
209     * 
210     * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
211     */
212    public void valueChanged(ListSelectionEvent e)
213    {
214        super.valueChanged(e);
215        int i = packsTable.getSelectedRow();
216        if (i >= 0) imgLabel.setIcon((ImageIcon) images.get(i));
217
218    }
219
220}