KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > memoire > vainstall > VALinkWindows


1 /**
2  * $RCSfile: VALinkWindows.java,v $
3  * @creation 12/03/00
4  * @modification $Date: 2005/03/07 20:52:02 $
5  */

6 package com.memoire.vainstall;
7
8 import java.io.File JavaDoc;
9 import java.io.FileInputStream JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15
16 /**
17  * @version $Id: VALinkWindows.java,v 1.8 2005/03/07 20:52:02 deniger Exp $
18  * @author Axel von Arnim
19  */

20 public class VALinkWindows {
21
22     public static boolean move(File JavaDoc _from, File JavaDoc _to) throws IOException JavaDoc {
23         if (_from == null || !_from.exists())
24             return false;
25         boolean b = _from.renameTo(_to);
26         if (b)
27             return true;
28         copy(_from, _to);
29         return _from.delete();
30     }
31
32     public static void copy(File JavaDoc _from, File JavaDoc _to) throws IOException JavaDoc {
33         if (_from == null || !_from.exists())
34             return;
35         FileOutputStream JavaDoc out = null;
36         FileInputStream JavaDoc in = null;
37         try {
38             out = new FileOutputStream JavaDoc(_to);
39             in = new FileInputStream JavaDoc(_from);
40             byte[] buf = new byte[2048];
41             int read = in.read(buf);
42             while (read > 0) {
43                 out.write(buf, 0, read);
44                 read = in.read(buf);
45             }
46         } catch (IOException JavaDoc _e) {
47             throw _e;
48         } finally {
49             if (in != null)
50                 in.close();
51             if (out != null)
52                 out.close();
53         }
54     }
55
56     /**
57      * Create one Windows shortcut for each javalauncher script that was
58      * created.
59      * <p>
60      * Links will be named after the corresponding launch scripts.
61      * <p>
62      * Icons can be used. If in the install dir an icon file (suffix .ico)
63      * exists that matches the name of the launcher, it will be used as the icon
64      * for that launcher. Alternatively, if an icon file exists in the install
65      * dir that matches LINK_ENTRY_ICON, that icon file will be used. Otherwise,
66      * no icon is used.
67      *
68      * @see com.memoire.vainstall.VASetup
69      */

70     public static boolean create(VAShortcutEntry[] _launchparms,
71             File JavaDoc sharedDir, String JavaDoc _installClassName, Set JavaDoc shortcuts)
72             throws IOException JavaDoc {
73         try {
74             String JavaDoc linkname, iconfile;
75
76             if (_launchparms == null)
77                 return false;
78             String JavaDoc section = null;
79             if ((!"applications".equals(VAGlobals.LINK_SECTION_NAME
80                     .toLowerCase()))
81                     && (!"utilities".equals(VAGlobals.LINK_SECTION_NAME
82                             .toLowerCase()))
83                     && (!"programs".equals(VAGlobals.LINK_SECTION_NAME
84                             .toLowerCase())))
85                 section = VAGlobals.LINK_SECTION_NAME;
86             if (section == null
87                     && (_launchparms.length > 1 || _launchparms.length == 1
88                             && VAGlobals.CREATE_UNINSTALL_SHORTCUT)) {
89                 section = VAGlobals.APP_NAME;
90             }
91             String JavaDoc menulinkdir = null;
92             Set JavaDoc shortcutToCreate = new HashSet JavaDoc();
93             if (section != null) {
94                 menulinkdir = JNIWindowsShortcut.getShortcutDir(
95                         JNIWindowsShortcut.ON_START_MENU,
96                         JNIWindowsShortcut.EVERYBODY)
97                         + "\\" + section;
98                 File JavaDoc mld = new File JavaDoc(menulinkdir);
99                 if (!mld.exists()) {
100                     if (!mld.mkdirs()) {
101                         throw new IOException JavaDoc("unable to create " + menulinkdir);
102                     }
103                 } else {
104                     //add the new folder to the shortcut lists ->delete when
105
// uninstall
106
shortcuts.add(mld.getAbsolutePath());
107                 }
108                 if (VAGlobals.DEBUG)
109                     VAGlobals.printDebug("menu link dir=" + menulinkdir);
110
111             }
112             for (int i = 0; i < _launchparms.length; i++) {
113                 VAShortcutEntry parmset = _launchparms[i];
114                 if (parmset.getIconPath() == null) {
115                     if (parmset.isUninstall()) {
116                         iconfile = getWindowsIconFile("uninstall", false);
117                     } else
118                         iconfile = getWindowsIconFile(parmset.getName(), true);
119                     parmset.setIconPath(iconfile);
120                 }
121                 parmset.setWorkingDirectory(VAGlobals.DEST_PATH);
122                 //we don't create a shortcut if the user does not want to
123
if (parmset.isCreateOnDesktop()) {
124                     String JavaDoc shortcut = create(parmset,
125                             JNIWindowsShortcut.ON_DESKTOP);
126                     //if created
127
if (new File JavaDoc(shortcut).exists())
128                         shortcuts.add(shortcut);
129                 }
130                 String JavaDoc shortcut = null;
131                 if (menulinkdir != null) {
132                     if (VAGlobals.DEBUG)
133                         VAGlobals.printDebug("menu to write="
134                                 + parmset.getName());
135
136                     try {
137                         shortcut = createCustom(parmset, menulinkdir + "\\"
138                                 + parmset.getName());
139                     } catch (IOException JavaDoc _e1) {
140                         //Fred : there is bug with the accent é,é
141
//I don't know how to solve it ....
142
shortcut = create(parmset,
143                                 JNIWindowsShortcut.ON_START_MENU);
144                         File JavaDoc shortcutFile = new File JavaDoc(shortcut);
145                         if (shortcutFile.exists()) {
146                             File JavaDoc destFile = new File JavaDoc(menulinkdir, shortcutFile
147                                     .getName());
148                             if (VAGlobals.DEBUG && shortcutFile.exists())
149                                 VAGlobals.printDebug("rename shortcut from=\n "
150                                         + shortcutFile.getAbsolutePath()
151                                         + "\nto\n" + destFile);
152                             move(shortcutFile, destFile);
153                             if (shortcutFile.exists())
154                                 shortcutFile.delete();
155                             shortcut = destFile.getAbsolutePath();
156                         }
157                     }
158                     if (VAGlobals.DEBUG)
159                         VAGlobals.printDebug("menu written="
160                                 + parmset.getName());
161                 } else {
162                     shortcut = create(parmset, JNIWindowsShortcut.ON_START_MENU);
163                 }
164                 if (shortcut != null && new File JavaDoc(shortcut).exists()) {
165                     shortcuts.add(shortcut);
166                     shortcutToCreate.add(new File JavaDoc(shortcut));
167                 }
168             }
169             //the shortcut for the uninstaller is already created
170
// if (VAGlobals.CREATE_UNINSTALL_SHORTCUT) {
171
// int location = JNIWindowsShortcut.ON_START_MENU;
172
// if (menulinkdir != null) {
173
// location = JNIWindowsShortcut.CUSTOM_LOCATION;
174
// linkname = menulinkdir + "\\";
175
// } else {
176
// linkname = "";
177
// }
178
// linkname += "Uninstall " + VAGlobals.APP_NAME;
179
// iconfile = getWindowsIconFile("uninstall");
180
// String shortcut = JNIWindowsShortcut.createShortcut(
181
// VAGlobals.DEST_PATH
182
// + System.getProperty("file.separator")
183
// + "uninstall_" + VAGlobals.APP_NAME + "_"
184
// + VAGlobals.APP_VERSION + ".bat",
185
// VAGlobals.DEST_PATH, "", // no arguments
186
// JNIWindowsShortcut.SHOW_NORMAL, linkname, location,
187
// JNIWindowsShortcut.EVERYBODY, iconfile, 0, // icon
188
// // offset
189
// "" // description
190
// );
191
// shortcuts.add(shortcut);
192
// }
193
if (VAGlobals.SHORTCUTS_IN_INSTALLDIR) {
194                 File JavaDoc dest = new File JavaDoc(VAGlobals.DEST_PATH);
195                 for (Iterator JavaDoc it = shortcutToCreate.iterator(); it.hasNext();) {
196                     File JavaDoc f = (File JavaDoc) it.next();
197                     File JavaDoc shortcutInInstall = new File JavaDoc(dest, f.getName());
198                     copy(f, shortcutInInstall);
199                     shortcuts.add(shortcutInInstall.getAbsolutePath());
200                 }
201
202             }
203             return true;
204         } catch (IOException JavaDoc _e) {
205
206             _e.printStackTrace();
207             throw _e;
208         }
209     }
210
211     /**
212      * Create a short cut from the entry.
213      *
214      * @parm dest should be one of JNIWindowsShortcut.ON_DESKTOP or
215      * JNIWindowsShortcut.ON_START_MENU.
216      *
217      */

218     private static final String JavaDoc create(VAShortcutEntry parmset, int dest)
219             throws IOException JavaDoc {
220         return JNIWindowsShortcut.createShortcut(parmset.getExePath(), parmset
221                 .getWorkingDirectory(), "", // no arguments
222
JNIWindowsShortcut.SHOW_NORMAL, parmset.getName(), dest,
223                 JNIWindowsShortcut.EVERYBODY, parmset.getIconPath(), 0, // icon
224
// offset
225
parmset.getComment());
226     }
227
228     /**
229      * @parm dest should be one of JNIWindowsShortcut.ON_DESKTOP or
230      * JNIWindowsShortcut.ON_START_MENU.
231      *
232      */

233     private static final String JavaDoc createCustom(VAShortcutEntry parmset,
234             String JavaDoc dest) throws IOException JavaDoc {
235         return JNIWindowsShortcut.createShortcut(parmset.getExePath(), parmset
236                 .getWorkingDirectory(), "", // no arguments
237
JNIWindowsShortcut.SHOW_NORMAL, dest,
238                 JNIWindowsShortcut.CUSTOM_LOCATION,
239                 JNIWindowsShortcut.EVERYBODY, parmset.getIconPath(), 0, // icon
240
// offset
241
parmset.getComment());
242     }
243
244     /**
245      * Return the absolute path of the icon for the <code>name</code>
246      * executable. If this icon can't be found, the default icon
247      * <code>LINK_ENTRY_ICON</code> is tried.
248      */

249     private static final String JavaDoc getWindowsIconFile(String JavaDoc name,
250             boolean _useDefault) {
251         // get the default icon
252
File JavaDoc defaultIcon = null;
253         if ((VAGlobals.LINK_ENTRY_ICON != null)
254                 && (VAGlobals.LINK_ENTRY_ICON.length() > 0)) {
255             defaultIcon = new File JavaDoc(VAGlobals.DEST_PATH,
256                     VAGlobals.LINK_ENTRY_ICON.replace('/', File.separatorChar)
257                             + ".ico");
258             VAGlobals.printDebug("default icon= " + defaultIcon);
259         }
260         File JavaDoc tryIcon;
261         if (defaultIcon != null)
262             tryIcon = new File JavaDoc(defaultIcon.getParentFile(), name + ".ico");
263         else
264             tryIcon = new File JavaDoc(VAGlobals.DEST_PATH, name + ".ico");
265         VAGlobals.printDebug("try icon= " + tryIcon);
266         if (tryIcon.exists()) {
267             return tryIcon.getAbsolutePath();
268         }
269         if (_useDefault && defaultIcon.exists())
270             return defaultIcon.getAbsolutePath();
271         return null;
272     }
273
274 }
Popular Tags