KickJava   Java API By Example, From Geeks To Geeks.

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


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 2002 Marcus Wolschon
8  * Copyright 2002 Jan Blok
9  * Copyright 2004 Klaus Bartz
10  * Copyright 2007 Dennis Reil
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */

24
25 package com.izforge.izpack.panels;
26
27 import java.awt.Color JavaDoc;
28 import java.awt.Component JavaDoc;
29 import java.awt.Dimension JavaDoc;
30 import java.awt.GridBagConstraints JavaDoc;
31 import java.awt.GridBagLayout JavaDoc;
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.event.ActionListener JavaDoc;
34 import java.io.File JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import javax.swing.AbstractCellEditor JavaDoc;
42 import javax.swing.BorderFactory JavaDoc;
43 import javax.swing.Box JavaDoc;
44 import javax.swing.BoxLayout JavaDoc;
45 import javax.swing.JCheckBox JavaDoc;
46 import javax.swing.JLabel JavaDoc;
47 import javax.swing.JOptionPane JavaDoc;
48 import javax.swing.JPanel JavaDoc;
49 import javax.swing.JScrollPane JavaDoc;
50 import javax.swing.JTable JavaDoc;
51 import javax.swing.JTextArea JavaDoc;
52 import javax.swing.ListSelectionModel JavaDoc;
53 import javax.swing.border.Border JavaDoc;
54 import javax.swing.event.ListSelectionEvent JavaDoc;
55 import javax.swing.event.ListSelectionListener JavaDoc;
56 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
57 import javax.swing.table.TableCellEditor JavaDoc;
58 import javax.swing.table.TableCellRenderer JavaDoc;
59
60 import net.n3.nanoxml.XMLElement;
61
62 import com.izforge.izpack.LocaleDatabase;
63 import com.izforge.izpack.Pack;
64 import com.izforge.izpack.gui.LabelFactory;
65 import com.izforge.izpack.installer.InstallData;
66 import com.izforge.izpack.installer.InstallerFrame;
67 import com.izforge.izpack.installer.IzPanel;
68 import com.izforge.izpack.installer.ResourceManager;
69 import com.izforge.izpack.util.Debug;
70 import com.izforge.izpack.util.IoHelper;
71 import com.izforge.izpack.util.VariableSubstitutor;
72
73 /**
74  * The base class for Packs panels. It brings the common member and methods of the different packs
75  * panels together. This class handles the common logic of pack selection. The derived class should
76  * be create the layout and other specific actions. There are some helper methods to simplify layout
77  * creation in the derived class.
78  *
79  * @author Julien Ponge
80  * @author Klaus Bartz
81  * @author Dennis Reil
82  */

83 public abstract class PacksPanelBase extends IzPanel implements PacksPanelInterface,
84         ListSelectionListener JavaDoc
85 {
86
87     // Common used Swing fields
88
/**
89      * The free space label.
90      */

91     protected JLabel JavaDoc freeSpaceLabel;
92
93     /**
94      * The space label.
95      */

96     protected JLabel JavaDoc spaceLabel;
97
98     /**
99      * The tip label.
100      */

101     protected JTextArea JavaDoc descriptionArea;
102
103     /**
104      * The dependencies label.
105      */

106     protected JTextArea JavaDoc dependencyArea;
107
108     /**
109      * The packs table.
110      */

111     protected JTable JavaDoc packsTable;
112
113     /**
114      * The tablescroll.
115      */

116     protected JScrollPane JavaDoc tableScroller;
117
118     // Non-GUI fields
119
/**
120      * Map that connects names with pack objects
121      */

122     private Map JavaDoc names;
123
124     /**
125      * The bytes of the current pack.
126      */

127     protected int bytes = 0;
128
129     /**
130      * The free bytes of the current selected disk.
131      */

132     protected long freeBytes = 0;
133
134     /**
135      * Are there dependencies in the packs
136      */

137     protected boolean dependenciesExist = false;
138
139     /**
140      * The packs locale database.
141      */

142     private LocaleDatabase langpack = null;
143
144     /**
145      * The name of the XML file that specifies the panel langpack
146      */

147     private static final String JavaDoc LANG_FILE_NAME = "packsLang.xml";
148
149     /**
150      * The constructor.
151      *
152      * @param parent The parent window.
153      * @param idata The installation data.
154      */

155     public PacksPanelBase(InstallerFrame parent, InstallData idata)
156     {
157         super(parent, idata);
158         // Load langpack.
159
try
160         {
161             this.langpack = parent.langpack;
162             InputStream JavaDoc inputStream = ResourceManager.getInstance().getInputStream(LANG_FILE_NAME);
163             this.langpack.add(inputStream);
164         }
165         catch (Throwable JavaDoc exception)
166         {
167             Debug.trace(exception);
168         }
169         // init the map
170
computePacks(idata.availablePacks);
171
172         createNormalLayout();
173     }
174
175     /**
176      * The Implementation of this method should create the layout for the current class.
177      */

178     abstract protected void createNormalLayout();
179
180     /*
181      * (non-Javadoc)
182      *
183      * @see com.izforge.izpack.panels.PacksPanelInterface#getLangpack()
184      */

185     public LocaleDatabase getLangpack()
186     {
187         return (langpack);
188     }
189
190     /*
191      * (non-Javadoc)
192      *
193      * @see com.izforge.izpack.panels.PacksPanelInterface#getBytes()
194      */

195     public int getBytes()
196     {
197         return (bytes);
198     }
199
200     /*
201      * (non-Javadoc)
202      *
203      * @see com.izforge.izpack.panels.PacksPanelInterface#setBytes(int)
204      */

205     public void setBytes(int bytes)
206     {
207         this.bytes = bytes;
208     }
209
210     /*
211      * (non-Javadoc)
212      *
213      * @see com.izforge.izpack.panels.PacksPanelInterface#showSpaceRequired()
214      */

215     public void showSpaceRequired()
216     {
217         if (spaceLabel != null) spaceLabel.setText(Pack.toByteUnitsString(bytes));
218     }
219
220     /*
221      * (non-Javadoc)
222      *
223      * @see com.izforge.izpack.panels.PacksPanelInterface#showFreeSpace()
224      */

225     public void showFreeSpace()
226     {
227         if (IoHelper.supported("getFreeSpace") && freeSpaceLabel != null)
228         {
229             String JavaDoc msg = null;
230             freeBytes = IoHelper.getFreeSpace(IoHelper.existingParent(
231                     new File JavaDoc(idata.getInstallPath())).getAbsolutePath());
232             if (freeBytes < 0)
233                 msg = parent.langpack.getString("PacksPanel.notAscertainable");
234             else
235                 msg = Pack.toByteUnitsString(freeBytes);
236             freeSpaceLabel.setText(msg);
237         }
238     }
239
240     /**
241      * Indicates wether the panel has been validated or not.
242      *
243      * @return true if the needed space is less than the free space, else false
244      */

245     public boolean isValidated()
246     {
247         if (IoHelper.supported("getFreeSpace") && freeBytes >= 0 && freeBytes <= bytes)
248         {
249             JOptionPane.showMessageDialog(this, parent.langpack
250                     .getString("PacksPanel.notEnoughSpace"), parent.langpack
251                     .getString("installer.error"), JOptionPane.ERROR_MESSAGE);
252             return (false);
253         }
254         return (true);
255     }
256
257     /**
258      * Asks to make the XML panel data.
259      *
260      * @param panelRoot The XML tree to write the data in.
261      */

262     public void makeXMLData(XMLElement panelRoot)
263     {
264         new ImgPacksPanelAutomationHelper().makeXMLData(idata, panelRoot);
265     }
266
267     /*
268      * (non-Javadoc)
269      *
270      * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
271      */

272     public void valueChanged(ListSelectionEvent JavaDoc e)
273     {
274         VariableSubstitutor vs = new VariableSubstitutor(idata.getVariables());
275
276         int i = packsTable.getSelectedRow();
277         if (i < 0) return;
278         // Operations for the description
279
if (descriptionArea != null)
280         {
281             Pack pack = (Pack) idata.availablePacks.get(i);
282             String JavaDoc desc = "";
283             String JavaDoc key = pack.id + ".description";
284             if (langpack != null && pack.id != null && !"".equals(pack.id))
285             {
286                 desc = langpack.getString(key);
287             }
288             if ("".equals(desc) || key.equals(desc))
289             {
290                 desc = pack.description;
291             }
292             desc = vs.substitute(desc, null);
293             descriptionArea.setText(desc);
294         }
295         // Operation for the dependency listing
296
if (dependencyArea != null)
297         {
298             Pack pack = (Pack) idata.availablePacks.get(i);
299             List JavaDoc dep = pack.dependencies;
300             String JavaDoc list = "";
301             if (dep != null)
302             {
303                 list += (langpack == null) ? "Dependencies: " : langpack
304                         .getString("PacksPanel.dependencies");
305             }
306             for (int j = 0; dep != null && j < dep.size(); j++)
307             {
308                 String JavaDoc name = (String JavaDoc) dep.get(j);
309                 list += getI18NPackName((Pack) names.get(name));
310                 if (j != dep.size() - 1) list += ", ";
311             }
312
313             // add the list of the packs to be excluded
314
String JavaDoc excludeslist = (langpack == null) ? "Excludes: " : langpack
315                     .getString("PacksPanel.excludes");
316             int numexcludes = 0;
317             if (pack.excludeGroup != null)
318             {
319                 for (int q = 0; q < idata.availablePacks.size(); q++)
320                 {
321                     Pack otherpack = (Pack) idata.availablePacks.get(q);
322                     String JavaDoc exgroup = otherpack.excludeGroup;
323                     if (exgroup != null)
324                     {
325                         if (q != i && pack.excludeGroup.equals(exgroup))
326                         {
327
328                             excludeslist += getI18NPackName(otherpack) + ", ";
329                             numexcludes++;
330                         }
331                     }
332                 }
333             }
334             // concatenate
335
if (dep != null) excludeslist = " " + excludeslist;
336             if (numexcludes > 0) list += excludeslist;
337             if (list.endsWith(", ")) list = list.substring(0, list.length() - 2);
338
339             // and display the result
340
dependencyArea.setText(list);
341         }
342     }
343
344     /**
345      * This method tries to resolve the localized name of the given pack. If this is not possible,
346      * the name given in the installation description file in ELEMENT <pack> will be used.
347      *
348      * @param pack for which the name should be resolved
349      * @return localized name of the pack
350      */

351     private String JavaDoc getI18NPackName(Pack pack)
352     {
353         // Internationalization code
354
String JavaDoc packName = pack.name;
355         String JavaDoc key = pack.id;
356         if (langpack != null && pack.id != null && !"".equals(pack.id))
357         {
358             packName = langpack.getString(key);
359         }
360         if ("".equals(packName) || key.equals(packName))
361         {
362             packName = pack.name;
363         }
364         return (packName);
365     }
366
367     /**
368      * Layout helper method:<br>
369      * Creates an label with a message given by msgId and an icon given by the iconId. If layout and
370      * constraints are not null, the label will be added to layout with the given constraints. The
371      * label will be added to this object.
372      *
373      * @param msgId identifier for the IzPack langpack
374      * @param iconId identifier for the IzPack icons
375      * @param layout layout to be used
376      * @param constraints constraints to be used
377      * @return the created label
378      */

379     protected JLabel JavaDoc createLabel(String JavaDoc msgId, String JavaDoc iconId, GridBagLayout JavaDoc layout,
380             GridBagConstraints JavaDoc constraints)
381     {
382         JLabel JavaDoc label = LabelFactory.create(parent.langpack.getString(msgId), parent.icons
383                 .getImageIcon(iconId), TRAILING);
384         if (layout != null && constraints != null) layout.addLayoutComponent(label, constraints);
385         add(label);
386         return (label);
387     }
388
389     /**
390      * Creates a panel containing a anonymous label on the left with the message for the given msgId
391      * and a label on the right side with initial no text. The right label will be returned. If
392      * layout and constraints are not null, the label will be added to layout with the given
393      * constraints. The panel will be added to this object.
394      *
395      * @param msgId identifier for the IzPack langpack
396      * @param layout layout to be used
397      * @param constraints constraints to be used
398      * @return the created (right) label
399      */

400     protected JLabel JavaDoc createPanelWithLabel(String JavaDoc msgId, GridBagLayout JavaDoc layout,
401             GridBagConstraints JavaDoc constraints)
402     {
403         JPanel JavaDoc panel = new JPanel JavaDoc();
404         JLabel JavaDoc label = new JLabel JavaDoc();
405         if (label == null) label = new JLabel JavaDoc("");
406         panel.setAlignmentX(LEFT_ALIGNMENT);
407         panel.setLayout(new BoxLayout JavaDoc(panel, BoxLayout.X_AXIS));
408         panel.add(LabelFactory.create(parent.langpack.getString(msgId)));
409         panel.add(Box.createHorizontalGlue());
410         panel.add(label);
411         if (layout != null && constraints != null) layout.addLayoutComponent(panel, constraints);
412         add(panel);
413         return (label);
414     }
415
416     /**
417      * Creates a text area with standard settings and the title given by the msgId. If scroller is
418      * not null, the create text area will be added to the scroller and the scroller to this object,
419      * else the text area will be added directly to this object. If layout and constraints are not
420      * null, the text area or scroller will be added to layout with the given constraints. The text
421      * area will be returned.
422      *
423      * @param msgId identifier for the IzPack langpack
424      * @param scroller the scroller to be used
425      * @param layout layout to be used
426      * @param constraints constraints to be used
427      * @return the created text area
428      */

429     protected JTextArea JavaDoc createTextArea(String JavaDoc msgId, JScrollPane JavaDoc scroller, GridBagLayout JavaDoc layout,
430             GridBagConstraints JavaDoc constraints)
431     {
432         JTextArea JavaDoc area = new JTextArea JavaDoc();
433         // area.setMargin(new Insets(2, 2, 2, 2));
434
area.setAlignmentX(LEFT_ALIGNMENT);
435         area.setCaretPosition(0);
436         area.setEditable(false);
437         area.setEditable(false);
438         area.setOpaque(false);
439         area.setLineWrap(true);
440         area.setWrapStyleWord(true);
441         area.setBorder(BorderFactory.createTitledBorder(parent.langpack.getString(msgId)));
442         area.setFont(getControlTextFont());
443
444         if (layout != null && constraints != null)
445         {
446             if (scroller != null)
447             {
448                 layout.addLayoutComponent(scroller, constraints);
449             }
450             else
451                 layout.addLayoutComponent(area, constraints);
452         }
453         if (scroller != null)
454         {
455             scroller.setViewportView(area);
456             add(scroller);
457         }
458         else
459             add(area);
460         return (area);
461
462     }
463
464     /**
465      * Creates the table for the packs. All parameters are required. The table will be returned.
466      *
467      * @param width of the table
468      * @param scroller the scroller to be used
469      * @param layout layout to be used
470      * @param constraints constraints to be used
471      * @return the created table
472      */

473     protected JTable JavaDoc createPacksTable(int width, JScrollPane JavaDoc scroller, GridBagLayout JavaDoc layout,
474             GridBagConstraints JavaDoc constraints)
475     {
476
477         JTable JavaDoc table = new JTable JavaDoc();
478         table.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
479         table.setIntercellSpacing(new Dimension JavaDoc(0, 0));
480         table.setBackground(Color.white);
481         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
482         table.getSelectionModel().addListSelectionListener(this);
483         table.setShowGrid(false);
484         scroller.setViewportView(table);
485         scroller.setAlignmentX(LEFT_ALIGNMENT);
486         scroller.getViewport().setBackground(Color.white);
487         scroller.setPreferredSize(new Dimension JavaDoc(width, (idata.guiPrefs.height / 3 + 30)));
488
489         if (layout != null && constraints != null)
490             layout.addLayoutComponent(scroller, constraints);
491         add(scroller);
492         return (table);
493     }
494
495     /**
496      * Computes pack related data like the names or the dependencies state.
497      *
498      * @param packs
499      */

500     private void computePacks(List JavaDoc packs)
501     {
502         names = new HashMap JavaDoc();
503         dependenciesExist = false;
504         for (int i = 0; i < packs.size(); i++)
505         {
506             Pack pack = (Pack) packs.get(i);
507             names.put(pack.name, pack);
508             if (pack.dependencies != null || pack.excludeGroup != null) dependenciesExist = true;
509         }
510     }
511
512     /**
513      * Called when the panel becomes active. If a derived class implements this method also, it is
514      * recomanded to call this method with the super operator first.
515      */

516     public void panelActivate()
517     {
518         try
519         {
520             
521             packsTable.setModel(new PacksModel(this, idata, this.parent.getRules()));
522             CheckBoxEditorRenderer packSelectedRenderer = new CheckBoxEditorRenderer(false);
523             packsTable.getColumnModel().getColumn(0).setCellRenderer(packSelectedRenderer);
524             CheckBoxEditorRenderer packSelectedEditor = new CheckBoxEditorRenderer(true);
525             packsTable.getColumnModel().getColumn(0).setCellEditor(packSelectedEditor);
526             packsTable.getColumnModel().getColumn(0).setMaxWidth(40);
527             
528             //packsTable.getColumnModel().getColumn(1).setCellRenderer(renderer1);
529
packsTable.getColumnModel().getColumn(1).setCellRenderer(new PacksPanelTableCellRenderer());
530             PacksPanelTableCellRenderer renderer2 = new PacksPanelTableCellRenderer();
531             renderer2.setHorizontalAlignment(RIGHT);
532             packsTable.getColumnModel().getColumn(2).setCellRenderer(renderer2);
533             packsTable.getColumnModel().getColumn(2).setMaxWidth(100);
534
535             // remove header,so we don't need more strings
536
tableScroller.remove(packsTable.getTableHeader());
537             tableScroller.setColumnHeaderView(null);
538             tableScroller.setColumnHeader(null);
539
540             // set the JCheckBoxes to the currently selected panels. The
541
// selection might have changed in another panel
542
java.util.Iterator JavaDoc iter = idata.availablePacks.iterator();
543             bytes = 0;
544             while (iter.hasNext())
545             {
546                 Pack p = (Pack) iter.next();
547                 if (p.required)
548                 {
549                     bytes += p.nbytes;
550                     continue;
551                 }
552                 if (idata.selectedPacks.contains(p)) bytes += p.nbytes;
553             }
554         }
555         catch (Exception JavaDoc e)
556         {
557             e.printStackTrace();
558         }
559         showSpaceRequired();
560         showFreeSpace();
561     }
562
563     /*
564      * (non-Javadoc)
565      *
566      * @see com.izforge.izpack.installer.IzPanel#getSummaryBody()
567      */

568     public String JavaDoc getSummaryBody()
569     {
570         StringBuffer JavaDoc retval = new StringBuffer JavaDoc(256);
571         Iterator JavaDoc iter = idata.selectedPacks.iterator();
572         boolean first = true;
573         while (iter.hasNext())
574         {
575             if (!first)
576             {
577                 retval.append("<br>");
578             }
579             first = false;
580             Pack pack = (Pack) iter.next();
581             if (langpack != null && pack.id != null && !"".equals(pack.id))
582             {
583                 retval.append(langpack.getString(pack.id));
584             }
585             else
586                 retval.append(pack.name);
587         }
588         return (retval.toString());
589     }
590
591     static class CheckBoxEditorRenderer extends AbstractCellEditor JavaDoc implements TableCellRenderer JavaDoc,
592             TableCellEditor JavaDoc, ActionListener JavaDoc
593     {
594
595         /**
596          *
597          */

598         private static final long serialVersionUID = 4049072731222061879L;
599
600         private JCheckBox JavaDoc display;
601
602         /**
603          * Creates a check box renderer. If useAsEditor is set, an action listener will be added,
604          * else not.
605          *
606          * @param useAsEditor
607          */

608         public CheckBoxEditorRenderer(boolean useAsEditor)
609         {
610             display = new JCheckBox JavaDoc();
611             display.setHorizontalAlignment(CENTER);
612             if (useAsEditor) display.addActionListener(this);
613
614         }
615
616         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
617                 boolean isSelected, boolean hasFocus, int row, int column)
618         {
619             if (isSelected)
620             {
621                 display.setForeground(table.getSelectionForeground());
622                 display.setBackground(table.getSelectionBackground());
623             }
624             else
625             {
626                 display.setForeground(table.getForeground());
627                 display.setBackground(table.getBackground());
628             }
629             int state = ((Integer JavaDoc) value).intValue();
630             if (state == -2)
631             {
632                 // condition not fulfilled
633
display.setForeground(Color.GRAY);
634             }
635             display.setSelected((value != null && Math.abs(state) == 1));
636             display.setEnabled(state >= 0);
637             return display;
638         }
639
640         /**
641          * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable,
642          * Object, boolean, int, int)
643          */

644         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value,
645                 boolean isSelected, int row, int column)
646         {
647             return getTableCellRendererComponent(table, value, isSelected, false, row, column);
648         }
649
650         public Object JavaDoc getCellEditorValue()
651         {
652             return new Integer JavaDoc(display.isSelected() ? 1 : 0);
653         }
654
655         public void actionPerformed(ActionEvent JavaDoc e)
656         {
657             stopCellEditing();
658         }
659     }
660
661     static class PacksPanelTableCellRenderer extends DefaultTableCellRenderer JavaDoc
662     {
663
664         
665         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
666                 boolean isSelected, boolean hasFocus, int row, int column)
667         {
668             Component JavaDoc renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);
669             
670             int state = ((Integer JavaDoc) table.getModel().getValueAt(row, 0)).intValue();
671             if (state == -2)
672             {
673                 // condition not fulfilled
674
renderer.setForeground(Color.GRAY);
675                 if (isSelected){
676                     renderer.setBackground(table.getSelectionBackground());
677                 }
678                 else {
679                     renderer.setBackground(table.getBackground());
680                 }
681             }
682             else {
683                 if (isSelected){
684                     renderer.setForeground(table.getSelectionForeground());
685                     renderer.setBackground(table.getSelectionBackground());
686                 }
687                 else {
688                     renderer.setForeground(table.getForeground());
689                     renderer.setBackground(table.getBackground());
690                 }
691             }
692             
693             return renderer;
694         }
695
696     }
697
698 }
699
Popular Tags