KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > wminstaller > app > Installer


1 package de.webman.wminstaller.app;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.io.File JavaDoc;
7 import java.util.*;
8 import java.beans.*;
9
10 import de.webman.wminstaller.screens.*;
11
12 /**
13  * @author <a HREF="mailto:ulf@webman.de">Ulf Goldammer</a>, <a HREF="mailto:gregor@webman.de">Gregor Klinke</a>
14  * @version $Revision: 1.2 $
15  **/

16 public class Installer
17 {
18     /* $Id: Installer.java,v 1.2 2002/02/11 18:24:22 gregor Exp $ */
19
20     private JFrame f = new JFrame("Webman Setup und Installation");
21     private Container fcp = f.getContentPane();
22
23     private CardsPanel headarea = new CardsPanel( this);
24     private CardsPanel cardarea = new CardsPanel( this);
25     private CardsPanel naviarea = new CardsPanel( this);
26
27     private int lastCard = -1;
28     private int currentCard = 0;
29     
30     private File JavaDoc baseDir = null;
31     private File JavaDoc dataDir = null;
32     
33     private HashMap dict = new HashMap();
34     private PropertyChangeSupport pcl = null;
35
36
37
38     /* ----------------------------------------------------------------------
39        konstruktoren
40        ---------------------------------------------------------------------- */

41     public Installer() {
42         pcl = new PropertyChangeSupport(this);
43     }
44     
45     public Installer( File JavaDoc baseDir) {
46         this();
47         this.baseDir = baseDir;
48
49         try{
50             dataDir = new File JavaDoc(getCWD(baseDir), "data");
51             if (!isDirectory(dataDir))
52                 dataDir = null;
53         }
54         catch( SecurityException JavaDoc e) {
55             dataDir = null;
56         }
57     }
58     
59     public File JavaDoc getBaseDir() {
60         return baseDir;
61     }
62     
63     public File JavaDoc getDataDir() {
64         System.out.println("Datadir: " + dataDir);
65         return dataDir;
66     }
67     
68     /**
69      * returns the property dictionary for this installer. Screens should
70      * store their values into this dictionary, so that following (or
71      * preceding) screens may use them properly without accessing the
72      * screen objects directly.
73      **/

74     public HashMap getDictionary() {
75         return dict;
76     }
77
78     /**
79      * returns a reference to the property event handler. this can be used
80      * to distribute property change event in the system.
81      **/

82     public PropertyChangeSupport getPCL() {
83         return pcl;
84     }
85
86
87
88     /* ----------------------------------------------------------------------
89        construct the ui
90        ---------------------------------------------------------------------- */

91     public void build() {
92     
93         fcp.setLayout( new BoxLayout( fcp, BoxLayout.Y_AXIS));
94         fcp.add(headarea);
95         // ha.setBackground( Color.green);
96
fcp.add(Box.createVerticalStrut( 10));
97         fcp.add(Box.createVerticalGlue());
98         fcp.add(cardarea);
99         fcp.add(Box.createVerticalStrut( 30));
100         fcp.add(Box.createVerticalGlue());
101         fcp.add(naviarea);
102         // fcp.add( Box.createVerticalStrut( 10));
103

104         new ScreenWelcome(this, InstallerScreen.BUTTONS_START);
105         new ScreenDirChooser(this, InstallerScreen.BUTTONS_MIDDLE);
106         new ScreenDBSetup(this, InstallerScreen.BUTTONS_MIDDLE);
107         new ScreenWMSetup(this, InstallerScreen.BUTTONS_MIDDLE);
108         new ScreenFinish(this, InstallerScreen.BUTTONS_FINISH);
109         new ScreenDone(this, InstallerScreen.BUTTONS_END);
110         
111         stepTo( 0 );
112     }
113     
114     /**
115      * adds a screen (card) to end of the list of children of the card
116      * panel.
117      * @param title the title string of the card; this string is displayed
118      * as a headline on the card
119      * @param panel the card implementation as a JPanel component
120      * @param navigator the corresponding navigator for this card.
121      * @return returns the index to which the card as been stored in the
122      * card panel. */

123     public int addScreen(String JavaDoc title, Component panel, Component navigator) {
124         lastCard++;
125         
126         JPanel head = new JPanel( new BorderLayout());
127         
128         if (title == null)
129             title = "";
130         StringTokenizer st = new StringTokenizer(title, "\n", false);
131         int numLines = st.countTokens();
132         JPanel lines = new JPanel(new GridLayout(numLines, 1, 2, 2));
133         while (st.hasMoreTokens()) {
134             lines.add(new JLabel(st.nextToken(), javax.swing.SwingConstants.LEFT));
135         }
136         if (numLines > 0)
137             head.add(lines, BorderLayout.NORTH);
138         
139         if (panel instanceof javax.swing.JScrollPane JavaDoc)
140             ((JScrollPane)panel).setPreferredSize(new Dimension(250, 200));
141         
142         headarea.add(head, "" + lastCard);
143         cardarea.add(panel, "" + lastCard);
144         naviarea.add(navigator, "" + lastCard);
145         
146         return lastCard;
147     }
148     
149
150     /* ----------------------------------------------------------------------
151        navigation methods
152        ---------------------------------------------------------------------- */

153     private void fireStepEvent(String JavaDoc direct, int oldidx, int newidx) {
154         pcl.firePropertyChange(new PropertyChangeEvent(this, direct,
155                                                        new Integer JavaDoc(oldidx), new Integer JavaDoc(newidx)));
156     }
157     
158     public void leave( int exitCode) {
159         System.exit(exitCode);
160     }
161     
162     public void stepForward() {
163         int oldCard = currentCard;
164         currentCard++;
165         fireStepEvent(EventConstants.LEAVE_SCREEN, oldCard, currentCard);
166         fireStepEvent(EventConstants.ENTER_SCREEN, oldCard, currentCard);
167         stepTo();
168     }
169     
170     public void stepBackward() {
171         int oldCard = currentCard;
172         currentCard--;
173         fireStepEvent(EventConstants.LEAVE_SCREEN, oldCard, currentCard);
174         fireStepEvent(EventConstants.ENTER_SCREEN, oldCard, currentCard);
175         stepTo();
176     }
177
178     public void stepFinish() {
179         int oldCard = currentCard;
180         currentCard = lastCard;
181         fireStepEvent(EventConstants.LEAVE_SCREEN, oldCard, currentCard);
182         fireStepEvent(EventConstants.ENTER_SCREEN, oldCard, currentCard);
183         stepTo();
184     }
185
186     public void stepTo(int i) {
187         int oldCard = currentCard;
188         currentCard = i;
189         fireStepEvent(EventConstants.LEAVE_SCREEN, oldCard, currentCard);
190         fireStepEvent(EventConstants.ENTER_SCREEN, oldCard, currentCard);
191         stepTo();
192     }
193     
194     private void stepTo() {
195         if (lastCard < 0)
196             return;
197         if (currentCard < 0) {
198             currentCard = 0;
199         }
200         if (currentCard > lastCard) {
201             currentCard = lastCard;
202         }
203         
204         naviarea.setVisible( false );
205         cardarea.setVisible( false );
206         headarea.setVisible( false );
207
208         headarea.showCard( currentCard);
209         cardarea.showCard( currentCard);
210         naviarea.showCard( currentCard);
211
212         headarea.setVisible( true );
213         cardarea.setVisible( true );
214         naviarea.setVisible( true );
215     }
216
217     public void show() {
218 // fcp.addNotify();
219
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
220         f.pack();
221
222         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
223         f.setLocation(
224             new Point(
225                 (int) (Math.abs( screenSize.getWidth() - f.getWidth()) / 2),
226                 (int) (Math.abs( screenSize.getHeight() - f.getHeight()) / 2)
227             )
228         );
229         // f.setResizable( false);
230
f.setVisible(true);
231     }
232
233
234
235     /* ----------------------------------------------------------------------
236        private methods
237        ---------------------------------------------------------------------- */

238     /**
239      * gets the current working directory. this is not the same as the pwd
240      * (under unix) but the directory from where the installer jar file has
241      * been called. the cwd is determined on base of the
242      * de/webman/wminstaller directory <strong>inside the jar
243      * file</strong>, so it is expected that the parameter
244      * <code>file</code> ends with ".jar!/de/webman/wminstaller"
245      *
246      * @param file a file path to a resource inside the jar file
247      * @return the absolute path to the directory where the installer jar
248      * lives.
249      **/

250     private File JavaDoc getCWD(File JavaDoc file) {
251         /* 1. find the last "!" */
252         String JavaDoc str = file.toString();
253         int pos = str.lastIndexOf("!");
254
255         /* if no exclamation mark is found, assume that the path is
256            absolute path already. Correct? */

257         if (pos < 0)
258             return new File JavaDoc(str);
259
260
261         /* 2. from this position find the last preceding "/" */
262         int slash_pos = str.lastIndexOf(File.separatorChar, pos);
263
264         /* if the path does not contain a "/", than it's simply wrong */
265         if (slash_pos < 0)
266             throw new IllegalArgumentException JavaDoc("file path needs to be a valid \"file:/\" url");
267         
268         return new File JavaDoc(str.substring(0, slash_pos));
269     }
270
271
272     /**
273      * checks if a file path points to a (readable) directory. we have to
274      * take into account, that file paths could begins sometimes with
275      * "file:" scheme
276      */

277     public static final String JavaDoc FILE_SCHEME = "file:";
278     private boolean isDirectory(File JavaDoc file) {
279         String JavaDoc t = file.toString();
280         if (t.startsWith(FILE_SCHEME)) {
281             String JavaDoc t2 = t.substring(FILE_SCHEME.length());
282             if ("nt".equals(System.getProperty("system"))) {
283                 /* TODO. replace "<drive>:" by "<drive>|" ?? */
284             }
285             
286             return new File JavaDoc(t2).isDirectory();
287         }
288         
289         return file.isDirectory();
290     }
291
292 }
293
294
Popular Tags