KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > ImgPacksPanel


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2004 Volker Friedritz
8  * Copyright 2002 Marcus Wolschon
9  * Copyright 2002 Jan Blok
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */

23
24 package com.izforge.izpack.panels;
25
26 import java.awt.Component JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.GridBagConstraints JavaDoc;
29 import java.awt.GridBagLayout JavaDoc;
30 import java.awt.Insets JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.Box JavaDoc;
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.JLabel JavaDoc;
39 import javax.swing.JScrollPane JavaDoc;
40 import javax.swing.event.ListSelectionEvent JavaDoc;
41
42 import com.izforge.izpack.installer.InstallData;
43 import com.izforge.izpack.installer.InstallerFrame;
44 import com.izforge.izpack.installer.ResourceManager;
45 import com.izforge.izpack.util.IoHelper;
46
47 /**
48  * The ImgPacks panel class. Allows the packages selection with a small picture displayed for every
49  * pack. This new version combines the old PacksPanel and the ImgPacksPanel so that the positive
50  * characteristics of both are combined. This class handles only the layout and some related stuff.
51  * Common stuff are handled by the base class.
52  *
53  * @author Julien Ponge
54  * @author Volker Friedritz
55  * @author Klaus Bartz
56  */

57 public class ImgPacksPanel extends PacksPanelBase
58 {
59
60     /**
61      *
62      */

63     private static final long serialVersionUID = 3977858492633659444L;
64
65     /** The images to display. */
66     private ArrayList JavaDoc images;
67
68     /** The img label. */
69     private JLabel JavaDoc imgLabel;
70
71     /**
72      * The constructor.
73      *
74      * @param parent The parent window.
75      * @param idata The installation data.
76      */

77     public ImgPacksPanel(InstallerFrame parent, InstallData idata)
78     {
79         super(parent, idata);
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see com.izforge.izpack.panels.PacksPanelBase#createNormalLayout()
86      */

87     protected void createNormalLayout()
88     {
89         preLoadImages();
90         GridBagLayout JavaDoc layout = new GridBagLayout JavaDoc();
91         GridBagConstraints JavaDoc gbConstraints = new GridBagConstraints JavaDoc();
92         setLayout(layout);
93
94         // Create constraint for first component as standard constraint.
95
parent.buildConstraints(gbConstraints, 0, 0, 1, 1, 0.25, 0.0);
96         gbConstraints.insets = new Insets JavaDoc(5, 5, 5, 5);
97         gbConstraints.anchor = GridBagConstraints.WEST;
98         // Create the info label.
99
createLabel("PacksPanel.info", "preferences", layout, gbConstraints);
100
101         // Create the snap label.
102
parent.buildConstraints(gbConstraints, 1, 0, 1, 1, 0.50, 0.0);
103         createLabel("ImgPacksPanel.snap", "tip", layout, gbConstraints);
104
105         // Create packs table with a scroller.
106
tableScroller = new JScrollPane JavaDoc();
107         parent.buildConstraints(gbConstraints, 0, 1, 1, 2, 0.50, 0.0);
108         gbConstraints.fill = GridBagConstraints.BOTH;
109         packsTable = createPacksTable(250, tableScroller, layout, gbConstraints);
110
111         // Create the image label with a scroller.
112
imgLabel = new JLabel JavaDoc((ImageIcon JavaDoc) images.get(0));
113         JScrollPane JavaDoc imgScroller = new JScrollPane JavaDoc(imgLabel);
114         imgScroller.setPreferredSize(getPreferredSizeFromImages());
115         parent.buildConstraints(gbConstraints, 1, 1, 1, 1, 0.5, 1.0);
116         layout.addLayoutComponent(imgScroller, gbConstraints);
117         add(imgScroller);
118
119         // Create a vertical strut.
120

121         Component JavaDoc strut = Box.createVerticalStrut(20);
122         parent.buildConstraints(gbConstraints, 1, 2, 1, 3, 0.0, 0.0);
123         layout.addLayoutComponent(strut, gbConstraints);
124         add(strut);
125
126         // Create the dependency area with a scroller.
127
if (dependenciesExist)
128         {
129             JScrollPane JavaDoc depScroller = new JScrollPane JavaDoc();
130             depScroller.setPreferredSize(new Dimension JavaDoc(250, 40));
131             parent.buildConstraints(gbConstraints, 0, 3, 1, 1, 0.50, 0.50);
132             dependencyArea = createTextArea("ImgPacksPanel.dependencyList", depScroller, layout,
133                     gbConstraints);
134         }
135
136         // Create the description area with a scroller.
137
JScrollPane JavaDoc descriptionScroller = new JScrollPane JavaDoc();
138         descriptionScroller.setPreferredSize(new Dimension JavaDoc(200, 60));
139         descriptionScroller.setBorder(BorderFactory.createEmptyBorder());
140
141         parent.buildConstraints(gbConstraints, 1, 3, 1, 1, 0.50, 0.50);
142         descriptionArea = createTextArea("PacksPanel.description", descriptionScroller, layout,
143                 gbConstraints);
144         // Create the tip label.
145
parent.buildConstraints(gbConstraints, 0, 4, 2, 1, 0.0, 0.0);
146         createLabel("PacksPanel.tip", "tip", layout, gbConstraints);
147         // Create the space label.
148
parent.buildConstraints(gbConstraints, 0, 5, 2, 1, 0.0, 0.0);
149         spaceLabel = createPanelWithLabel("PacksPanel.space", layout, gbConstraints);
150         if (IoHelper.supported("getFreeSpace"))
151         { // Create the free space label only if free space is supported.
152
parent.buildConstraints(gbConstraints, 0, 6, 2, 1, 0.0, 0.0);
153             freeSpaceLabel = createPanelWithLabel("PacksPanel.freespace", layout, gbConstraints);
154         }
155
156     }
157
158     /** Pre-loads the images. */
159     private void preLoadImages()
160     {
161         int size = idata.availablePacks.size();
162         images = new ArrayList JavaDoc(size);
163         for (int i = 0; i < size; i++)
164             try
165             {
166                 URL JavaDoc url = ResourceManager.getInstance().getURL("ImgPacksPanel.img." + i);
167                 ImageIcon JavaDoc img = new ImageIcon JavaDoc(url);
168                 images.add(img);
169             }
170             catch (Exception JavaDoc err)
171             {
172                 err.printStackTrace();
173             }
174     }
175
176     /**
177      * Try to find a good preferredSize for imgScroller by checking all loaded images' width and
178      * height.
179      */

180     private Dimension JavaDoc getPreferredSizeFromImages()
181     {
182         int maxWidth = 80;
183         int maxHeight = 60;
184         ImageIcon JavaDoc icon;
185
186         for (Iterator JavaDoc it = images.iterator(); it.hasNext();)
187         {
188             icon = (ImageIcon JavaDoc) it.next();
189             maxWidth = Math.max(maxWidth, icon.getIconWidth());
190             maxHeight = Math.max(maxHeight, icon.getIconHeight());
191         }
192
193         maxWidth = Math.min(maxWidth + 20, idata.guiPrefs.width - 150);
194         maxHeight = Math.min(maxHeight + 20, idata.guiPrefs.height - 150);
195
196         return new Dimension JavaDoc(maxWidth, maxHeight);
197     }
198
199     /*
200      * (non-Javadoc)
201      *
202      * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
203      */

204     public void valueChanged(ListSelectionEvent JavaDoc e)
205     {
206         super.valueChanged(e);
207         int i = packsTable.getSelectedRow();
208         if (i >= 0) imgLabel.setIcon((ImageIcon JavaDoc) images.get(i));
209
210     }
211
212 }
213
Popular Tags