KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > yagga > miniinstaller > gui > InstallFrame


1 /*
2  * This file is part of MiniInstaller, a self installer builder for Java
3  * Copyright (C) 2002 Walter Gamba
4  * mailto:walter@yagga.net
5  * http://www.yagga.net/java/miniinstaller
6  *
7  * MiniInstaller is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * MiniInstaller is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * As the time of writing, the GNU General Public Licene can be
22  * found at http://www.gnu.org/licenses/gpl.txt
23  *
24  */

25
26 package net.yagga.miniinstaller.gui;
27
28 import net.yagga.miniinstaller.*;
29 import net.yagga.util.Ut;
30 import net.yagga.util.ResourceMgr;
31 import java.awt.*;
32 import javax.swing.*;
33
34 import javax.swing.plaf.metal.*;
35 import javax.swing.plaf.basic.*;
36 import javax.swing.plaf.multi.*;
37
38
39 /**
40  * Main Frame for installer.
41  * This frame handles communication with MiniInstaller class,
42  * and dispatch every call to specialized Panels.<BR/>
43  * It keeps custom colors, data, and dimension.. <br/>
44  * The actual interface is built the first time the Frame is shown, and NOT
45  * at constructor time!
46  * @author Walter Gamba
47  * @version 1.0
48  */

49 public class InstallFrame extends JFrame implements CanFwdListener, FinalExecuter{
50
51     //classical definition of various Pluggable Look & Feel
52
private String JavaDoc mac = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
53     private String JavaDoc metal = "javax.swing.plaf.metal.MetalLookAndFeel";
54     private String JavaDoc motif = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
55     private String JavaDoc windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
56
57   BorderLayout borderLayout1 = new BorderLayout();
58
59   NavPanel navPanel=null;
60   TitlePanel titlePanel=null;
61   StepPanel stepPanel=null;
62
63   private static final int DEF_X=750;
64   private static final int DEF_Y=300;
65
66   private String JavaDoc title;
67     private String JavaDoc panelTitle;
68
69   public static Dimension panelDim=new Dimension(DEF_X,DEF_Y);
70   public static Color BK_COL=SystemColor.control;
71   public static Color FG_COL=Color.black;
72     public static Color TIT_COL=Color.black;
73     public static Color TITLE_COL=Color.black;
74     public static Color STEP_COL=Color.black;
75     public static Color BTN_COL=SystemColor.controlText;
76
77
78   /**
79     parser constants
80   */

81   private final static String JavaDoc COL_BG="BK";
82   private final static String JavaDoc COL_TITLE="TITLE";
83   private final static String JavaDoc COL_SMALL_TITLE="SMALL_TITLE";
84   private final static String JavaDoc COL_TEXT="TEXT";
85   private final static String JavaDoc COL_STEP="STEP";
86     private final static String JavaDoc COL_BTN="BTN";
87
88
89     private final static String JavaDoc FONT_TITLE="TITLE";
90     private final static String JavaDoc FONT_STEP="STEP";
91     private final static String JavaDoc FONT_TEXT="TEXT";
92     private final static String JavaDoc FONT_SMALL_TITLE="SMALL_TITLE";
93
94     /** allows refresh only when interface is built */
95     private boolean canRefresh=false;
96     /** internal: store if user wants to execute shell command on exit */
97     private boolean executeShell=false;
98     /** store visibility for step panel */
99     private boolean stepVisible=true;
100
101   private MiniInstaller installer;
102
103     /** to be used by inner classes */
104     private InstallFrame thisFrame=null;
105
106
107   public InstallFrame(MiniInstaller mi, String JavaDoc title) {
108         super(title);
109     installer=mi;
110     this.title=title;
111         GuiProperties.init();
112         this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
113
114       this.addWindowListener(new java.awt.event.WindowAdapter JavaDoc(){
115             public void windowClosing(java.awt.event.WindowEvent JavaDoc e){
116                 exit();
117           }
118       });
119         thisFrame=this;
120   }
121
122
123     public void setVisible(boolean vis){
124         canRefresh=vis;
125         if(!vis)
126             super.setVisible(false);
127         else{
128             super.setVisible(false);
129             rebuild();
130             super.setVisible(true);
131         }
132     }
133
134     /**
135      * Forces a rebuild of everything..
136      */

137     public void rebuild(){
138         try{
139           jbInit();
140             //refresh();
141
}catch(Exception JavaDoc e){
142         }
143     }
144
145   private void jbInit() throws Exception JavaDoc {
146         JPanel pane=new JPanel();
147
148       this.setContentPane(pane);
149         pane.setDoubleBuffered(true);
150       //this.setFont(new java.awt.Font("Dialog", 2, 12));
151
this.setResizable(false);
152     this.setTitle(title);
153     this.setResizable(true);
154     this.getContentPane().setLayout(borderLayout1);
155     //
156
navPanel=new NavPanel(this);
157
158     this.getContentPane().add(navPanel,BorderLayout.SOUTH);
159     titlePanel=new TitlePanel(panelTitle);
160
161
162     this.getContentPane().add(titlePanel,BorderLayout.NORTH);
163     addPanel(new VoidPanel());
164
165     stepPanel=new StepPanel(installer.getSteps());
166         if(stepVisible){
167           this.getContentPane().add(stepPanel,BorderLayout.WEST);
168         }
169     this.setIconImage(ResourceMgr.retrieveImageIcon("img/miniinst_icon.gif").getImage());
170     this.pack();
171         this.doLayout();
172     stepPanel.resetStep();
173         //refresh();
174
}
175
176     /**
177      * Changes the look and feel of the installer
178      * @param laf the look and feel: at the moment it can be one among:<BR>
179      * <PRE>
180      * windows
181      * metal
182      * motif
183      * mac
184      * </PRE>
185      * @author Walter Gamba
186      */

187     public void setLookAndFeel(String JavaDoc laf){
188         String JavaDoc lafClass=null;
189         if(laf.equals("windows"))
190             lafClass=windows;
191         else if(laf.equals("metal"))
192             lafClass=metal;
193         else if(laf.equals("motif"))
194             lafClass=motif;
195         else if(laf.equals("mac"))
196             lafClass=mac;
197         else
198           Ut.infoln("Error Unsupported look and feel:'"+laf+"'");
199
200         try{
201             UIManager.setLookAndFeel(lafClass);
202             SwingUtilities.invokeLater(updateUI);
203         }catch(Exception JavaDoc ulafe){
204             Ut.infoln("Error UnsuppLAF:"+lafClass+"="+ulafe);
205         }
206     }
207
208     public void updateDims(Dimension d){
209         panelDim=d;
210         if(canRefresh)
211           refresh();
212     }
213
214   public void setCol(String JavaDoc what, int r, int g, int b){
215     if(what.equals(COL_BG))
216       BK_COL=new Color(r,g,b);
217     else if(what.equals(COL_TITLE))
218       TITLE_COL=new Color(r,g,b);
219     else if(what.equals(COL_TEXT))
220       FG_COL=new Color(r,g,b);
221     else if(what.equals(COL_SMALL_TITLE))
222       TIT_COL=new Color(r,g,b);
223     else if(what.equals(COL_STEP))
224       STEP_COL=new Color(r,g,b);
225         else if(what.equals(COL_BTN))
226       BTN_COL=new Color(r,g,b);
227         if(canRefresh)
228           refresh();
229   }
230
231     public void setFont(String JavaDoc font, String JavaDoc face, int style, int size){
232         if(font.equals(FONT_TITLE))
233             GuiProperties.setTitleFont(face,style,size);
234         else if(font.equals(FONT_TEXT))
235             GuiProperties.setTextFont(face,style,size);
236         else if(font.equals(FONT_SMALL_TITLE))
237             GuiProperties.setSmallTitleFont(face,style,size);
238         else if(font.equals(FONT_STEP))
239             GuiProperties.setStepFont(face,style,size);
240         else
241             Ut.error("Unknown font to set:'"+font+"'");
242     }
243
244   public void setName(String JavaDoc n){
245     if(titlePanel!=null)
246             titlePanel.setTitle(n);
247         panelTitle=n;
248   }
249
250   public void doStep(){
251         if(stepPanel!=null)
252       stepPanel.incStep();
253   }
254   public void revertStep(){
255     stepPanel.decStep();
256   }
257   public void revertStep(int to){
258     stepPanel.goStep(to);
259   }
260   public void changeTitle(String JavaDoc t){
261     title=t;
262     setTitle(title);
263   }
264   public void refresh(){
265     if(panel!=null){
266       panel.refresh();
267         }
268     navPanel.refresh();
269     titlePanel.refresh();
270     stepPanel.refresh();
271     this.pack();
272   }
273
274   /**
275    * called by MiniInstaller
276    */

277   InstallPanel panel=null;
278   InputPanel input=null;
279   UnzipPanel unzip=null;
280
281   public void doFinal(String JavaDoc title,String JavaDoc msg, String JavaDoc shellCommand, String JavaDoc shellCaption){
282         reset();
283       if(shellCaption==null && shellCommand!=null)
284         shellCaption="Execute "+shellCommand;
285     addPanel(new FinalPanel(this,title,msg,shellCaption));
286     navPanel.makeFinalPage();
287   }
288
289   public void doWrite(String JavaDoc title,String JavaDoc msg){
290     doWrite(title,msg,true);
291   }
292
293   public void doWrite(String JavaDoc title,String JavaDoc msg,boolean go){
294     reset();
295     addPanel(new WritePanel(title,msg,null));
296     if(go)
297       enableAllButtons();
298     else
299       enableAllButtonsButFwd();
300   }
301
302   public void doStart(String JavaDoc title,String JavaDoc msg,boolean isImage){
303     reset();
304
305         if(isImage)
306       addPanel(new WritePanel(title,null,msg));
307     else
308       addPanel(new WritePanel(title,msg,null));
309
310     enableAllButtonsButBk();
311   }
312
313     public void doAlert(String JavaDoc msg,boolean block){
314     reset();
315     addPanel(new WritePanel(msg,WritePanel.ICON_ALERT));
316     if(!block)
317       enableAllButtons();
318     else
319       enableAllButtonsButFwd();
320   }
321
322     public void setStepVisible(boolean show){
323         if(stepPanel!=null){
324             Ut.infoln("Hide/show panel :"+show);
325             stepPanel.setVisible(show);
326         }
327         stepVisible=show;
328         if(canRefresh){
329                 if(stepVisible){
330           this.getContentPane().add(stepPanel,BorderLayout.WEST);
331                 }
332
333         }
334     }
335
336   public void doDisplay(String JavaDoc msg){
337     reset();
338     addPanel(new DisplayPanel(msg));
339     enableAllButtons();
340   }
341
342   public void doShow(String JavaDoc imgFile){
343     reset();
344     //ImageIcon img=new ImageIcon(imgFile);
345
//ImageIcon img=ResourceMgr.retrieveImageIcon(imgFile);
346
addPanel(new WritePanel(null,imgFile));
347     enableAllButtons();
348   }
349
350   public void doUnzip(String JavaDoc title, String JavaDoc zipf, String JavaDoc ddir){
351     reset();
352     unzip=new UnzipPanel(title,zipf,ddir,this);
353     addPanel(unzip);
354     enableAllButtonsButFwd();
355   }
356
357   public void setProgress(int i){
358     unzip.setProgress(i);
359     //if(debug)
360
// Ut.info(" "+i);
361
}
362
363   public void doInput(String JavaDoc title, String JavaDoc msg, String JavaDoc defValue ){
364     reset();
365     input=new InputPanel(title,msg,defValue,false,false,this);
366     addPanel(input);
367     if(defValue.equals(""))
368       enableAllButtonsButFwd();
369   }
370   /**
371    * inputs files or Directories
372    * @param file if true looks for a file, if false, for a directory
373    */

374   public void doInputFile(String JavaDoc title, String JavaDoc msg, String JavaDoc defValue, boolean file){
375     reset();
376     input=new InputPanel(title,msg,defValue,true,file,this);
377     addPanel(input);
378     if(defValue.equals(""))
379       enableAllButtonsButFwd();
380   }
381
382
383   public String JavaDoc getInput(){
384     return input.getValue();
385   }
386
387   /**
388     utils
389   */

390   private void reset(){
391     input=null;
392     unzip=null;
393   }
394
395   private void enableAllButtons(){
396     navPanel.enableButtons(true,true,true);
397   }
398   private void enableAllButtonsButFwd(){
399     navPanel.enableButtons(true,true,false);
400   }
401   private void enableAllButtonsButBk(){
402     navPanel.enableButtons(true,false,true);
403   }
404
405   private void addPanel(InstallPanel p){
406
407     if(panel!=null)
408       this.getContentPane().remove((JPanel)panel);
409     this.getContentPane().add((JPanel)p,BorderLayout.CENTER);
410         panel=p;
411
412
413         /*//debug delle dimensioni
414         if(stepPanel!=null)
415           Ut.infoln("DIm step panel="+stepPanel.getPreferredSize()+","+stepPanel.getMinimumSize());
416         if(navPanel!=null)
417           Ut.infoln("DIm nav panel="+navPanel.getPreferredSize()+","+navPanel.getMinimumSize());
418         if(titlePanel!=null)
419           Ut.infoln("DIm titl panel="+titlePanel.getPreferredSize()+","+titlePanel.getMinimumSize());
420         if(p!=null)
421           Ut.infoln("DIm main panel="+((JPanel)p).getPreferredSize()+","+((JPanel)p).getMinimumSize());
422         */

423
424     this.pack();
425         this.doLayout();
426   }
427   /**
428     called by nav panel
429   */

430   public void exit(){
431         //Ut.infoln("Last step? "+installer.isLastStep());
432
if(installer.isLastStep())
433             installer.exit();
434         else{
435
436             if(_showOptionDialog(navPanel, "Are you sure you want to exit "+Version.name+"?",
437               "Exiting Installer",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE, null, null, null)
438                     ==JOptionPane.YES_OPTION){
439
440                     this.dispose();
441                     installer.exit();
442                     }
443             }
444   }
445
446         /**
447          * Recursive sets opaque false and font and colors for components
448          * @param jc
449          */

450         static void deepSet(JComponent jc, boolean opaque){
451             if(opaque)
452                 jc.setOpaque(false);
453             else
454                 jc.setBackground(BK_COL);
455
456             jc.setFont(GuiProperties.textFont);
457             if(jc instanceof JButton)
458                 jc.setForeground(BTN_COL);
459             else
460               jc.setForeground(FG_COL);
461             int n=jc.getComponentCount();
462             for(int i=0;i<n;i++){
463               Component c=jc.getComponent(i);
464                 if(c instanceof JComponent)
465                     deepSet((JComponent)c,opaque);
466             }
467         }
468
469         /**
470          * Copied from JDK's JOptionPane method of same name
471          * @see javax.swing.JOptionPane
472          */

473     private int _showOptionDialog(Component parentComponent, Object JavaDoc message,
474                                        String JavaDoc title, int optionType,
475                                        int messageType, Icon icon,
476                                        Object JavaDoc[] options, Object JavaDoc initialValue) {
477         JOptionPane pane = new JOptionPane(message, messageType,
478                                                        optionType, icon,
479                                                        options, initialValue);
480
481                 pane.setBackground(this.BK_COL);
482                 pane.setOpaque(true);
483         pane.setInitialValue(initialValue);
484
485                 //sets every inside componnts TRANSPARENT
486
deepSet(pane,true);
487
488         JDialog dialog = pane.createDialog(parentComponent, title);
489                 dialog.setBackground(BK_COL);
490                 dialog.getContentPane().setBackground(BK_COL);
491                 dialog.invalidate();
492
493         pane.selectInitialValue();
494
495         dialog.show();
496
497         Object JavaDoc selectedValue = pane.getValue();
498
499         if(selectedValue == null)
500             return JOptionPane.CLOSED_OPTION;
501         if(options == null) {
502             if(selectedValue instanceof Integer JavaDoc)
503                 return ((Integer JavaDoc)selectedValue).intValue();
504             return JOptionPane.CLOSED_OPTION;
505         }
506         for(int counter = 0, maxCounter = options.length;
507             counter < maxCounter; counter++) {
508             if(options[counter].equals(selectedValue))
509                 return counter;
510         }
511         return JOptionPane.CLOSED_OPTION;
512     }
513
514   public void bk(){
515     if(installer.isLastStep()){
516             installer.back();
517             //revert only if this was final page..
518
navPanel.revertFinalPage();
519         }
520         else
521             installer.back();
522
523         //don't refactor this line
524
//-> installer.back();
525
//because if you do back, then
526
//the return value of installer.isLastStep is modified..
527
}
528
529   public void fwd(){
530     installer.goOn();
531   }
532   /**
533     canFwdListener
534   */

535   public void canFwd(){
536     enableAllButtons();
537   }
538   /**
539    * FinalExecuter interface.
540      * Called by FinalPanel to notify user has chosen to execute, or not.. shell
541      * command on exit
542      * @param execute if true, command (if any) is executed on exit of the interface..
543    */

544    public void executeShellOnExit(boolean execute){
545       //close frame
546
executeShell=execute;
547    }
548
549      public boolean shouldExecuteShell(){
550         return executeShell;
551      }
552
553     /************************/
554     Runnable JavaDoc updateUI = new Runnable JavaDoc() {
555       public void run() {
556              SwingUtilities.updateComponentTreeUI(thisFrame);
557         }
558     };
559 }
560
561
Popular Tags