KickJava   Java API By Example, From Geeks To Geeks.

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


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

19
20 package com.izforge.izpack.panels;
21
22 import com.coi.tools.os.win.MSWinConstants;
23 import com.coi.tools.os.win.RegDataContainer;
24 import com.izforge.izpack.installer.InstallData;
25 import com.izforge.izpack.installer.InstallerFrame;
26 import com.izforge.izpack.util.AbstractUIHandler;
27 import com.izforge.izpack.util.os.RegistryDefaultHandler;
28 import com.izforge.izpack.util.os.RegistryHandler;
29
30 /**
31  * An extended hello panel class which detects whether the product was already installed or not.
32  * This class should be only used if the RegistryInstallerListener will be also used. Current the
33  * check will be only performed on Windows operating system. This class can be used also as example
34  * how to use the registry stuff to get informations from the current system.
35  *
36  * @author Klaus Bartz
37  */

38 public class CheckedHelloPanel extends HelloPanel implements MSWinConstants
39 {
40
41     /** Flag to break installation or not. */
42     protected boolean abortInstallation;
43
44     /**
45      * The constructor.
46      *
47      * @param parent The parent.
48      * @param idata The installation data.
49      */

50     public CheckedHelloPanel(InstallerFrame parent, InstallData idata)
51     {
52         super(parent, idata);
53         abortInstallation = isRegistered();
54     }
55
56     /**
57      * This method should only be called if this product was allready installed. It resolves the
58      * install path of the first already installed product and asks the user whether to install
59      * twice or not.
60      *
61      * @return whether a multiple Install should be performed or not.
62      * @throws Exception
63      */

64     protected boolean multipleInstall() throws Exception JavaDoc
65     {
66         // Let us play a little bit with the regstry...
67
// Just for fun we would resolve the path of the already
68
// installed application.
69
// First we need a handler. There is no overhead at a
70
// secound call of getInstance, therefore we do not buffer
71
// the handler in this class.
72
RegistryHandler rh = RegistryDefaultHandler.getInstance();
73         int oldVal = rh.getRoot(); // Only for security...
74
// We know, that the product is already installed, else we
75
// would not in this method. Now we search for the path...
76
String JavaDoc uninstallName = rh.getUninstallName();
77         String JavaDoc oldInstallPath = "<not found>";
78         while (true) // My goto alternative :-)
79
{
80
81             if (uninstallName == null) break; // Should never be...
82
// First we "create" the reg key.
83
String JavaDoc keyName = RegistryHandler.UNINSTALL_ROOT + uninstallName;
84             rh.setRoot(HKEY_LOCAL_MACHINE);
85             if (!rh.valueExist(keyName, "UninstallString"))
86             // We assume that the application was installed with
87
// IzPack. Therefore there should be the value "UninstallString"
88
// which contains the uninstaller call. If not we can do nothing.
89
break;
90             // Now we would get the value. A value can have different types.
91
// Therefore we get an container which can handle all possible types.
92
// There are different ways to handle. Use normally only one of the
93
// ways; at this point more are used to demonstrate the different ways.
94

95             // 1. If we are secure about the type, we can extract the value immediately.
96
String JavaDoc valString = rh.getValue(keyName, "UninstallString").getStringData();
97
98             // 2. If we are not so much interessted at the type, we can get the value
99
// as Object. A DWORD is then a Long Object not a long primitive type.
100
Object JavaDoc valObj = rh.getValue(keyName, "UninstallString").getDataAsObject();
101             if(valObj instanceof String JavaDoc ) // Only to inhibit warnings about local variable never read.
102
valString = (String JavaDoc) valObj;
103
104
105             // 3. If we are not secure about the type we should differ between possible
106
// types.
107
RegDataContainer val = rh.getValue(keyName, "UninstallString");
108             int typeOfVal = val.getType();
109             switch (typeOfVal)
110             {
111             case REG_EXPAND_SZ:
112             case REG_SZ:
113                 valString = val.getStringData();
114                 break;
115             case REG_BINARY:
116             case REG_DWORD:
117             case REG_LINK:
118             case REG_MULTI_SZ:
119                 throw new Exception JavaDoc("Bad data type of chosen registry value " + keyName);
120             default:
121                 throw new Exception JavaDoc("Unknown data type of chosen registry value " + keyName);
122             }
123             // That's all with registry this time... Following preparation of
124
// the received value.
125
// It is [java path] -jar [uninstaller path]
126
int start = valString.lastIndexOf("-jar") + 5;
127             if (start < 5 || start >= valString.length())
128             // we do not know what todo with it.
129
break;
130             String JavaDoc uPath = valString.substring(start).trim();
131             if (uPath.startsWith("\"")) uPath = uPath.substring(1).trim();
132             int end = uPath.indexOf("uninstaller");
133             if (end < 0)
134             // we do not know what todo with it.
135
break;
136             oldInstallPath = uPath.substring(0, end - 1);
137             // Much work for such a peanuts...
138
break; // That's the problem with the goto alternative. Forget this
139
// break produces an endless loop.
140
}
141
142         rh.setRoot(oldVal); // Only for security...
143

144         // The text will be to long for one line. Therefore we should use
145
// the multi line label. Unfortunately it has no icon. Nothing is
146
// perfect...
147
String JavaDoc noLuck = parent.langpack.getString("CheckedHelloPanel.productAlreadyExist0")
148                 + oldInstallPath
149                 + parent.langpack.getString("CheckedHelloPanel.productAlreadyExist1");
150         return (askQuestion(parent.langpack.getString("installer.error"), noLuck,
151                 AbstractUIHandler.CHOICES_YES_NO) == AbstractUIHandler.ANSWER_YES);
152     }
153
154     /**
155      * Returns wether the handled application is already registered or not. The validation will be
156      * made only on systems which contains a registry (Windows).
157      *
158      * @return wether the handled application is already registered or not
159      */

160     protected boolean isRegistered()
161     {
162         boolean retval = false;
163         try
164         {
165             // Get the default registry handler.
166
RegistryHandler rh = RegistryDefaultHandler.getInstance();
167             if (rh != null)
168             {
169                 rh.verify(idata);
170                 retval = rh.isProductRegistered();
171
172             }
173             // else we are on a os which has no registry or the
174
// needed dll was not bound to this installation. In
175
// both cases we forget the "already exist" check.
176

177         }
178         catch (Exception JavaDoc e)
179         { // Will only be happen if registry handler is good, but an
180
// exception at performing was thrown. This is an error...
181
e.printStackTrace();
182         }
183         return (retval);
184     }
185
186     /**
187      * Indicates wether the panel has been validated or not.
188      *
189      * @return true if the internal abort flag is not set, else false
190      */

191     public boolean isValidated()
192     {
193         return (!abortInstallation);
194     }
195
196     /*
197      * (non-Javadoc)
198      *
199      * @see com.izforge.izpack.installer.IzPanel#panelActivate()
200      */

201     public void panelActivate()
202     {
203         if (abortInstallation)
204         {
205             parent.lockNextButton();
206             try
207             {
208                 if (multipleInstall())
209                 {
210                     setUniqueUninstallKey();
211                     abortInstallation = false;
212                     parent.unlockNextButton();
213                 }
214             }
215             catch (Exception JavaDoc e)
216             {
217                 // TODO Auto-generated catch block
218
e.printStackTrace();
219             }
220
221         }
222         RegistryHandler rh = RegistryDefaultHandler.getInstance();
223         if( rh != null )
224             idata.setVariable("UNINSTALL_NAME", rh.getUninstallName());
225     }
226
227     /**
228      * @throws Exception
229      *
230      */

231     private void setUniqueUninstallKey() throws Exception JavaDoc
232     {
233         // Let us play a little bit with the regstry again...
234
// Now we search for an unique uninstall key.
235
// First we need a handler. There is no overhead at a
236
// secound call of getInstance, therefore we do not buffer
237
// the handler in this class.
238
RegistryHandler rh = RegistryDefaultHandler.getInstance();
239         int oldVal = rh.getRoot(); // Only for security...
240
// We know, that the product is already installed, else we
241
// would not in this method. First we get the
242
// "default" uninstall key.
243
if(oldVal > 100 ) // Only to inhibit warnings about local variable never read.
244
return;
245         String JavaDoc uninstallName = rh.getUninstallName();
246         int uninstallModifier = 1;
247         while (true)
248         {
249             if (uninstallName == null) break; // Should never be...
250
// Now we define a new uninstall name.
251
String JavaDoc newUninstallName = uninstallName + "(" + Integer.toString(uninstallModifier)
252                     + ")";
253             // Then we "create" the reg key with it.
254
String JavaDoc keyName = RegistryHandler.UNINSTALL_ROOT + newUninstallName;
255             rh.setRoot(HKEY_LOCAL_MACHINE);
256             if (!rh.keyExist(keyName))
257             { // That's the name for which we searched.
258
// Change the uninstall name in the reg helper.
259
rh.setUninstallName(newUninstallName);
260                 // Now let us inform the user.
261
emitNotification(parent.langpack
262                         .getString("CheckedHelloPanel.infoOverUninstallKey")
263                         + newUninstallName);
264                 // Now a little hack if the registry spec file contains
265
// the pack "UninstallStuff".
266
break;
267             }
268             uninstallModifier++;
269         }
270     }
271
272 }
273
Popular Tags