KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * @creation 28/02/05
3  *
4 */

5
6 package com.memoire.vainstall;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 /**
12   * A means of creating Windows shortcuts using the JNI interface.
13   * @author Dick Repasky
14   *
15 */

16
17 public class JNIWindowsShortcut {
18                         /** Value for location.
19                             If you use this value,
20                             the value of linkname
21                             should include the
22                             full name of the
23                             directory that will
24                             contain the new link. */

25     public static final int CUSTOM_LOCATION = 0;
26                         /** Value for location. */
27     public static final int ON_DESKTOP = 1;
28                         /** Value for location. */
29     public static final int ON_START_MENU = 2;
30
31                         /** Value for forwhom. */
32     public static final int USER_ONLY = 0;
33                         /** Value for forwhom. */
34     public static final int EVERYBODY = 1;
35
36                         /** Value for openas. */
37     public static final int SHOW_NORMAL = 0;
38                         /** Value for openas. */
39     public static final int SHOW_MAX = 1;
40                         /** Value for openas. */
41     public static final int SHOW_MIN = 2;
42     
43     public static boolean isDllLoaded_;
44     
45     public static void loadLib(){
46         if(!isDllLoaded_){
47             isDllLoaded_=true;
48             System.load( LIB_NAME);
49         }
50     }
51
52                         /** Name of dll. */
53     public static String JavaDoc LIB_NAME = "JNIWinShortcut";
54     private static final String JavaDoc DEFAULT_LINK = "LaunchIt";
55
56     /** Create a Windows Shortcut using the information provided.
57         @parm target name - binary to be executed. Required.
58                 Must be fully qualified path to work.
59         @parm workingdir - name of working directory for running
60             binary. Required.
61                 Must be fully qualified path to work.
62         @parm args - arguments to be passed to the binary.
63                 Optional.
64         @parm linkname - name of the link file. Omit .LNK suffix!
65                 Include leading directory names only if
66                 location is CUSTOM_LOCATION.
67         @parm location - one of CUSTOM_LOCATION, ON_DESKTOP,
68                 ON_STARTMENU.
69         @parm forwhom - one of USER_ONLY or EVERYBODY. Ignored if
70                 location is CUSTOM_LOCATION.
71         @parm iconfile - name of icon file. Must be fully qualified
72                 path to work.
73         @parm iconoffset - offest of icon within iconfile.
74         @parm description - speak freely here. But, be brief. I don't
75                 know what the limit is.
76     */

77     public static final String JavaDoc createShortcut(String JavaDoc targetname,
78                         String JavaDoc workingdir,
79                         String JavaDoc args,
80                         int openas,
81                         String JavaDoc linkname,
82                         int location,
83                         int forwhom,
84                         String JavaDoc iconfile,
85                         int iconoffset,
86                         String JavaDoc description)
87                     throws IOException JavaDoc {
88         loadLib();
89         if (targetname == null) targetname = "";
90         if (workingdir == null) workingdir = "";
91         if (args == null) args = "";
92         if (linkname == null || linkname.length() == 0) linkname =
93                                 DEFAULT_LINK;
94         if (iconfile == null) iconfile = "";
95         if (description == null) description = "";
96         String JavaDoc f=createShortcutJNI(targetname, workingdir, args,
97             openas, linkname, location, forwhom, iconfile,
98             iconoffset, description);
99         File JavaDoc shortcutFile = new File JavaDoc(f);
100         if(!shortcutFile.exists()){
101             if(VAGlobals.DEBUG) VAGlobals.printDebug("shortcut not found "+shortcutFile.getName());
102             String JavaDoc name=shortcutFile.getName();
103             int idxDot=name.lastIndexOf('.');
104             if(idxDot>0){
105                 name=name.substring(0,idxDot);
106                 File JavaDoc test=new File JavaDoc(shortcutFile.getParentFile(),name+".pif");
107                 System.err.println("file tested "+test);
108                 if(test.exists()) {
109                     if(VAGlobals.DEBUG) VAGlobals.printDebug("shortcut guessed "+test.getName());
110
111                     f=test.getAbsolutePath();
112                 }
113             }
114         }
115         return f;
116     }
117
118
119         /**
120           * Return the name of the directory that would contain
121           * a link of the type give.
122           * @parm location should be one of ON_DESKTOP or ON_START_MENU.
123           * @parm forwhom should be one of EVERYBODY or USER_ONLY.
124           *
125         */

126
127     public static final String JavaDoc getShortcutDir(int location,
128                         int forwhom) {
129         loadLib();
130         return getLinkDir(location, forwhom);
131     }
132
133
134         /**
135           * Return the name of the shortcut that would be created if
136           * the following arguments were used. Arguments are as
137           * for createShortcut.
138           * @exception java.io.IOException if full path name is
139           * longer than maximum allowed.
140         */

141
142     public static final String JavaDoc getShortcutName(String JavaDoc linkname,
143                         int location,
144                         int forwhom)
145                     throws IOException JavaDoc {
146         loadLib();
147         if (linkname == null || linkname.length() == 0) linkname =
148                                 DEFAULT_LINK;
149         return getLinkName(linkname, location, forwhom);
150     }
151
152         /**
153           * Return maximum allowable Win32 path length (i.e.,
154           * value of the constant MAX_PATH).
155           *
156         */

157
158     public static final int getMaxPathlength() {
159         loadLib();
160         return getMAX_PATH();
161     }
162
163         /** Create a shortcut. */
164     private static final native String JavaDoc createShortcutJNI(String JavaDoc targetname,
165                 String JavaDoc workingdir,
166                 String JavaDoc args,
167                 int openas,
168                 String JavaDoc linkname,
169                 int location,
170                 int forwhom,
171                 String JavaDoc iconfile,
172                 int iconoffset,
173                 String JavaDoc descriptions) throws IOException JavaDoc;
174
175         /**
176           * Get the name of the directory for links of the type
177           * given.
178           *
179         */

180     private static final native String JavaDoc getLinkDir(int location,
181                         int forwhom);
182
183         /**
184           * Get the full file name of a shortcut that would
185           * be created using the arguments.
186           * @exception java.io.IOException if full path name is
187           * longer than maximum allowed.
188         */

189
190     private static final native String JavaDoc getLinkName(String JavaDoc linkname,
191                         int location,
192                         int forwhom) throws IOException JavaDoc;
193
194         /**
195           * Return value of Win32 MAX_PATH.
196           *
197         */

198
199     private static final native int getMAX_PATH();
200
201 }
202
Popular Tags