KickJava   Java API By Example, From Geeks To Geeks.

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


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

22 package com.izforge.izpack.panels;
23
24 import com.izforge.izpack.ExecutableFile;
25 import com.izforge.izpack.installer.AutomatedInstallData;
26 import com.izforge.izpack.installer.PanelAutomation;
27 import com.izforge.izpack.installer.UninstallData;
28 import com.izforge.izpack.util.Debug;
29 import com.izforge.izpack.util.FileExecutor;
30 import com.izforge.izpack.util.OsConstraint;
31 import com.izforge.izpack.util.OsVersion;
32 import com.izforge.izpack.util.TargetFactory;
33 import com.izforge.izpack.util.os.Shortcut;
34
35 import net.n3.nanoxml.XMLElement;
36
37 import java.io.File JavaDoc;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 /**
44  * The ShortcutPanelAutomationHelper is responsible to create Shortcuts during the automated
45  * installation. Most code comes copied from the ShortcutPanel
46  *
47  * @author Marc Eppelmann (marc.eppelmann@gmx.de)
48  * @version $Revision: 1540 $
49  */

50 public class ShortcutPanelAutomationHelper implements PanelAutomation
51 {
52
53     // ~ Methods ****************************************************************************
54

55     /**
56      * dummy method
57      *
58      * @param idata DOCUMENT ME!
59      * @param panelRoot DOCUMENT ME!
60      */

61     public void makeXMLData(AutomatedInstallData idata, XMLElement panelRoot)
62     {
63         Debug.log(this.getClass().getName() + "::entering makeXMLData()");
64
65         // ShortcutPanel.getInstance().makeXMLData( idata, panelRoot );
66
}
67
68     /**
69      * Implementation of the Shortcut Specific Automation Code
70      *
71      * @param installData DOCUMENT ME!
72      * @param panelRoot DOCUMENT ME!
73      *
74      * @return DOCUMENT ME!
75      */

76     public boolean runAutomated(AutomatedInstallData installData, XMLElement panelRoot)
77     {
78         Shortcut shortcut;
79
80         /**
81          * A list of ShortcutData> objects. Each object is the complete specification for one
82          * shortcut that must be created.
83          */

84         Vector JavaDoc shortcuts = new Vector JavaDoc();
85
86         Vector JavaDoc execFiles = new Vector JavaDoc();
87
88         /**
89          * Holds a list of all the shortcut files that have been created. Note: this variable
90          * contains valid data only after createShortcuts() has been called. This list is created so
91          * that the files can be added to the uninstaller.
92          */

93         Vector JavaDoc files = new Vector JavaDoc();
94
95         Debug.log(this.getClass().getName() + " Entered runAutomated()");
96
97         try
98         {
99             shortcut = (Shortcut) (TargetFactory.getInstance()
100                     .makeObject("com.izforge.izpack.util.os.Shortcut"));
101             shortcut.initialize(Shortcut.APPLICATIONS, "-");
102         }
103         catch (Throwable JavaDoc exception)
104         {
105             Debug.log("Could not create shortcut instance");
106             exception.printStackTrace();
107
108             return true;
109         }
110
111         // ----------------------------------------------------
112
// if shortcuts are not supported, then we can not
113
// create shortcuts, even if there was any install
114
// data. Just return.
115
// ----------------------------------------------------
116
if (!shortcut.supported())
117         {
118             Debug.log("shortcuts not supported here");
119
120             return true;
121         }
122
123         if (!OsConstraint.oneMatchesCurrentSystem(panelRoot))
124         {
125             Debug.log("Shortcuts Not oneMatchesCurrentSystem");
126
127             return true;
128         }
129
130         shortcuts = new Vector JavaDoc();
131
132         Vector JavaDoc shortcutElements;
133         ShortcutData data;
134         XMLElement dataElement;
135
136         // ----------------------------------------------------
137
// set the name of the program group
138
// ----------------------------------------------------
139
dataElement = panelRoot.getFirstChildNamed(ShortcutPanel.AUTO_KEY_PROGRAM_GROUP);
140
141         String JavaDoc groupName = dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_NAME);
142
143         if (groupName == null)
144         {
145             groupName = "";
146         }
147
148         // ----------------------------------------------------
149
// add the details for each of the shortcuts
150
// ----------------------------------------------------
151
shortcutElements = panelRoot.getChildrenNamed(ShortcutPanel.AUTO_KEY_SHORTCUT);
152
153         for (int i = 0; i < shortcutElements.size(); i++)
154         {
155             Debug.log(this.getClass().getName() + "runAutomated:shortcutElements " + i);
156             data = new ShortcutData();
157             dataElement = (XMLElement) shortcutElements.elementAt(i);
158
159             data.name = dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_NAME);
160             data.addToGroup = Boolean.valueOf(
161                     dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_GROUP)).booleanValue();
162
163             if (OsVersion.IS_WINDOWS)
164             {
165                 data.type = Integer.valueOf(
166                         dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_TYPE)).intValue();
167             }
168             else
169             {
170                 Debug.log("WARN: On Linux data.type is NOT an int. Ignored.");
171             }
172
173             data.commandLine = dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_COMMAND);
174             data.description = dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_DESCRIPTION);
175             data.iconFile = dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_ICON);
176             data.iconIndex = Integer.valueOf(
177                     dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_ICON_INDEX)).intValue();
178             data.initialState = Integer.valueOf(
179                     dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_INITIAL_STATE))
180                     .intValue();
181             data.target = dataElement.getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_TARGET);
182             data.workingDirectory = dataElement
183                     .getAttribute(ShortcutPanel.AUTO_ATTRIBUTE_WORKING_DIR);
184
185             // Linux
186
data.deskTopEntryLinux_Encoding = dataElement.getAttribute(
187                     ShortcutPanel.SPEC_ATTRIBUTE_ENCODING, "");
188             data.deskTopEntryLinux_MimeType = dataElement.getAttribute(
189                     ShortcutPanel.SPEC_ATTRIBUTE_MIMETYPE, "");
190             data.deskTopEntryLinux_Terminal = dataElement.getAttribute(
191                     ShortcutPanel.SPEC_ATTRIBUTE_TERMINAL, "");
192             data.deskTopEntryLinux_TerminalOptions = dataElement.getAttribute(
193                     ShortcutPanel.SPEC_ATTRIBUTE_TERMINAL_OPTIONS, "");
194             data.deskTopEntryLinux_Type = dataElement.getAttribute(
195                     ShortcutPanel.SPEC_ATTRIBUTE_TYPE, "");
196
197             data.deskTopEntryLinux_URL = dataElement.getAttribute(ShortcutPanel.SPEC_ATTRIBUTE_URL,
198                     "");
199
200             data.deskTopEntryLinux_X_KDE_SubstituteUID = dataElement.getAttribute(
201                     ShortcutPanel.SPEC_ATTRIBUTE_KDE_SUBST_UID, "false");
202
203             data.deskTopEntryLinux_X_KDE_UserName = dataElement.getAttribute(
204                     ShortcutPanel.SPEC_ATTRIBUTE_KDE_USERNAME, "root");
205
206             data.Categories = dataElement.getAttribute(ShortcutPanel.SPEC_CATEGORIES,
207                     "Application;Development");
208
209             data.TryExec = dataElement.getAttribute(ShortcutPanel.SPEC_TRYEXEC, "");
210
211             data.createForAll = new Boolean JavaDoc(dataElement.getAttribute(ShortcutPanel.CREATE_FOR_ALL,
212                     "false"));
213             data.userType = Integer.valueOf(
214                     dataElement.getAttribute(ShortcutPanel.USER_TYPE, Integer
215                             .toString(Shortcut.CURRENT_USER))).intValue();
216             // END LINUX
217
shortcuts.add(data);
218         }
219
220         System.out.print("[ Creating shortcuts ");
221
222         // ShortcutData data;
223
for (int i = 0; i < shortcuts.size(); i++)
224         {
225             data = (ShortcutData) shortcuts.elementAt(i);
226
227             try
228             {
229                 if( data.subgroup!=null )
230                 {
231                   groupName = groupName + data.subgroup;
232                 }
233                 shortcut.setUserType(data.userType);
234                 shortcut.setLinkName(data.name);
235
236                 if (OsVersion.IS_WINDOWS)
237                 {
238                     shortcut.setLinkType(data.type);
239                 }
240
241                 shortcut.setArguments(data.commandLine);
242                 shortcut.setDescription(data.description);
243                 shortcut.setIconLocation(data.iconFile, data.iconIndex);
244
245                 shortcut.setShowCommand(data.initialState);
246                 shortcut.setTargetPath(data.target);
247                 shortcut.setWorkingDirectory(data.workingDirectory);
248                 shortcut.setEncoding(data.deskTopEntryLinux_Encoding);
249                 shortcut.setMimetype(data.deskTopEntryLinux_MimeType);
250
251                 shortcut.setTerminal(data.deskTopEntryLinux_Terminal);
252                 shortcut.setTerminalOptions(data.deskTopEntryLinux_TerminalOptions);
253
254                 if (!OsVersion.IS_WINDOWS)
255                 {
256                     shortcut.setType(data.deskTopEntryLinux_Type);
257                 }
258
259                 shortcut.setKdeSubstUID(data.deskTopEntryLinux_X_KDE_SubstituteUID);
260                 shortcut.setURL(data.deskTopEntryLinux_URL);
261                 shortcut.setCreateForAll(data.createForAll);
262
263                 if (data.addToGroup)
264                 {
265                     shortcut.setProgramGroup(groupName);
266                 }
267                 else
268                 {
269                     shortcut.setProgramGroup("");
270                 }
271
272                 try
273                 {
274                     // save the shortcut
275
System.out.print(".");
276                     System.out.flush();
277
278                     shortcut.save();
279
280                     // add the file and directory name to the file list
281
String JavaDoc fileName = shortcut.getFileName();
282                     files.add(0, fileName);
283
284                     File file = new File(fileName);
285                     File base = new File(shortcut.getBasePath());
286                     Vector JavaDoc intermediates = new Vector JavaDoc();
287
288                     // String directoryName = shortcut.getDirectoryCreated ();
289
execFiles.add(new ExecutableFile(fileName, ExecutableFile.UNINSTALL,
290                             ExecutableFile.IGNORE, new ArrayList JavaDoc(), false));
291
292                     files.add(fileName);
293
294                     while ((file = file.getParentFile()) != null)
295                     {
296                         if (file.equals(base))
297                         {
298                             break;
299                         }
300
301                         intermediates.add(file);
302                     }
303
304                     if (file != null)
305                     {
306                         Enumeration JavaDoc filesEnum = intermediates.elements();
307
308                         while (filesEnum.hasMoreElements())
309                         {
310                             files.add(0, filesEnum.nextElement().toString());
311                         }
312                     }
313                 }
314                 catch (Exception JavaDoc exception)
315                 {}
316             }
317             catch (Throwable JavaDoc exception)
318             {
319                 continue;
320             }
321         }
322
323         // }
324
//
325
try
326         {
327             if (execFiles != null)
328             {
329                 FileExecutor executor = new FileExecutor(execFiles);
330
331                 //
332
// TODO: Hi Guys,
333
// TODO The following commented-out line sometimes produces an uncatchable
334
// nullpointer Exception!
335
// TODO evaluate for what reason the files should exec.
336
// TODO if there is a serious explanation, why to do that,
337
// TODO the code must be more robust
338
// evaluate executor.executeFiles( ExecutableFile.NEVER, null );
339
}
340         }
341         catch (NullPointerException JavaDoc nep)
342         {
343             nep.printStackTrace();
344         }
345         catch (RuntimeException JavaDoc cannot)
346         {
347             cannot.printStackTrace();
348         }
349
350         System.out.println(" done. ]");
351         System.out.print("[ Add shortcuts to uninstaller ");
352
353         UninstallData uninstallData = UninstallData.getInstance();
354
355         for (int i = 0; i < files.size(); i++)
356         {
357             uninstallData.addFile((String JavaDoc) files.elementAt(i));
358             System.out.print(".");
359             System.out.flush();
360         }
361
362         System.out.println(" done. ]");
363
364         return true;
365     }
366 }
367
Popular Tags