Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / util / os / Win_Shortcut.java @ 15280

History | View | Annotate | Download (18.6 KB)

1
/*
2
 * $Id: Win_Shortcut.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 * IzPack
4
 * Copyright (C) 2002 by Elmar Grom
5
 *
6
 * File :               Win_Shortcut.java
7
 * Description :        mapping class for the shortcut API
8
 * Author's email :     elmar@grom.net
9
 * Website :            http://www.izforge.com
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24
 */
25

    
26
package   com.izforge.izpack.util.os;
27

    
28
import java.io.File;
29
import java.util.Vector;
30
  
31
/*---------------------------------------------------------------------------*/
32
/**
33
 * This is the Microsoft Windows specific implementation of <code>Shortcut</code>.
34
 *
35
 * @version  0.0.1 / 3/4/02
36
 * @author   Elmar Grom
37
 */
38
/*---------------------------------------------------------------------------*/
39
public class Win_Shortcut extends Shortcut
40
{
41
  // ------------------------------------------------------------------------
42
  // Constant Definitions
43
  // ------------------------------------------------------------------------
44

    
45
  // ------------------------------------------------------------------------
46
  // Variable Declarations
47
  // ------------------------------------------------------------------------
48
  private ShellLink   shortcut;
49
  
50
 /*--------------------------------------------------------------------------*/
51
 /**
52
  * This method initializes the object. It is used as a replacement for the
53
  * constructor because of the way it is instantiated through the
54
  * <code>TargetFactory</code>.
55
  *
56
  * @param     type   the type or classification of the program group in which
57
  *                   the link should exist. The following types are recognized:
58
  *                   <br><ul>
59
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#APPLICATIONS}
60
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#START_MENU}
61
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#DESKTOP}
62
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#START_UP}
63
  *                   </ul>
64
  * @param     name   the name of the shortcut.
65
  */
66
  public void initialize (int    type,
67
                          String name) throws Exception
68
  {
69
    switch (type)
70
    {
71
      case APPLICATIONS : 
72
        {
73
          shortcut = new ShellLink (ShellLink.PROGRAM_MENU, name);
74
          break;
75
        }
76
      case START_MENU :
77
        {
78
          shortcut = new ShellLink (ShellLink.START_MENU, name);
79
          break;
80
        }
81
      case DESKTOP :
82
        {
83
          shortcut = new ShellLink (ShellLink.DESKTOP, name);
84
          break;
85
        }
86
      case START_UP :
87
        {
88
          shortcut = new ShellLink (ShellLink.STARTUP, name);
89
          break;
90
        }
91
      default :
92
        {
93
          shortcut = new ShellLink (ShellLink.PROGRAM_MENU, name);
94
          break;
95
        }
96
    }
97
  }
98

    
99
 /*--------------------------------------------------------------------------*/
100
 /**
101
  * Returns the base path of the shortcut depending on type. The base path is
102
  * the directory that the short cut, (or its program group) will be created
103
  * in. For instance, on Windows NT, a shortcut with user-type ALL_USERS, and
104
  * link-type DESKTOP might have the base path
105
  * "C:\Program&nbsp;Files\All&nbsp;Users\Desktop"
106
  *
107
  * @see #setLinkType(int)
108
  * @see #setUserType(int)
109
  *
110
  * translates from ShellLink-UserTypes to Shortcut-UserTypes.
111
  */
112
  public String getBasePath () throws Exception
113
  {
114
    return shortcut.getLinkPath (shortcut.getUserType());
115
  }
116

    
117
 /**
118
  * Returns a list of currently existing program groups, based on the
119
  * requested type. For example if the type is <code>APPLICATIONS</code> then
120
  * all the names of the program groups in the Start Menu\Programs menu would be
121
  * returned.
122
  *
123
  * @param     userType   the type of user for the program group set.
124
  *                       (as Shortcut.utype)
125
  *
126
  * @return    a <code>Vector</code> of <code>String</code> objects that
127
  *            represent the names of the existing program groups. It is
128
  *            theoretically possible that this list is empty.
129
  *
130
  * @see       #APPLICATIONS
131
  * @see       #START_MENU  
132
  */
133
  public Vector getProgramGroups (int userType)
134
  {
135
    // ----------------------------------------------------
136
    // translate the user type
137
    // ----------------------------------------------------
138
    int type = ShellLink.CURRENT_USER;
139
    
140
    if (userType == ALL_USERS)
141
    {
142
      type = ShellLink.ALL_USERS;
143
    }
144
    else
145
    {
146
      type = ShellLink.CURRENT_USER;
147
    }
148
    
149
    // ----------------------------------------------------
150
    // get a list of all files and directories that are
151
    // located at the link path.
152
    // ----------------------------------------------------
153
    String  linkPath  = shortcut.getLinkPath (type);
154
    
155
    // in case there is a problem obtaining a path return
156
    // an empty vector (there are no preexisting program
157
    // groups)
158
    if (linkPath == null)
159
    {
160
      return (new Vector ());
161
    }
162
    
163
    File    path      = new File (linkPath);
164
        File [] file      = path.listFiles ();
165
    
166
        // ----------------------------------------------------
167
        // build a vector that contains only the names of
168
        // the directories.
169
        // ----------------------------------------------------
170
        Vector  groups    = new Vector ();
171
    
172
        if ( file != null )
173
        {
174
                for (int i = 0; i < file.length; i++)
175
                {
176
                  if (file [i].isDirectory ())
177
                  {
178
                        groups.add (file [i].getName ());
179
                  }
180
                }    
181
        }
182
    
183
    return (groups);
184
  }
185

    
186
 /*--------------------------------------------------------------------------*/
187
 /**
188
  * Returns the fully qualified file name under which the link is saved on
189
  * disk. <b>Note:</b> this method returns valid results only if the instance
190
  * was created from a file on disk or after a successful save operation.
191
  *
192
  * @return    the fully qualified file name for the shell link
193
  */
194
  public String getFileName ()
195
  {
196
    return (shortcut.getFileName ());
197
  }
198

    
199
 /*--------------------------------------------------------------------------*/
200
 /**
201
  * Returns the path of the directory where the link file is stored, if it
202
  * was necessary during the previous save operation to create the directory.
203
  * This method returns <code>null</code> if no save operation was carried
204
  * out or there was no need to create a directory during the previous save
205
  * operation.
206
  *
207
  * @return    the path of the directory where the link file is stored or
208
  *            <code>null</code> if no save operation was carried out or
209
  *            there was no need to create a directory during the previous
210
  *            save operation.
211
  */
212
  public String getDirectoryCreated ()
213
  {
214
    return (shortcut.getDirectoryCreated ());
215
  }
216

    
217
 /*--------------------------------------------------------------------------*/
218
 /**
219
  * Returns <code>true</code> if the target OS supports current user and
220
  * all users.
221
  *
222
  * @return    <code>true</code> if the target OS supports current and all users.
223
  */
224
  public boolean multipleUsers ()
225
  {
226
        // Win NT4 won't have PROGRAMS for CURRENT_USER.
227
        // Win 98 may not have 'Start Menu\Programs' for ALL_USERS
228
        String au = shortcut.getallUsersLinkPath();
229
        String cu = shortcut.getcurrentUserLinkPath();
230

    
231
        if ( au == null || cu == null )
232
                return false;
233

    
234
        return ( au.length() > 0 && cu.length() > 0 );
235
  }
236

    
237
 /*--------------------------------------------------------------------------*/
238
 /**
239
  * Signals that this flavor of <code>{@link com.izforge.izpack.util.os.Shortcut}</code>
240
  * supports the creation of shortcuts.
241
  *
242
  * @return    always <code>true</code>
243
  */
244
  public boolean supported ()
245
  {
246
    return (true);
247
  }
248

    
249
 /*--------------------------------------------------------------------------*/
250
 /**
251
  * Sets the command line arguments that will be passed to the target when
252
  * the link is activated.
253
  *
254
  * @param     arguments    the command line arguments
255
  */
256
  public void setArguments (String arguments)
257
  {
258
    shortcut.setArguments (arguments);
259
  }
260

    
261
 /*--------------------------------------------------------------------------*/
262
 /**
263
  * Sets the description string that is used to identify the link in a menu
264
  * or on the desktop.
265
  *
266
  * @param     description  the descriptiojn string
267
  */
268
  public void setDescription (String description)
269
  {
270
    shortcut.setDescription (description);
271
  }
272

    
273
 /*--------------------------------------------------------------------------*/
274
 /**
275
  * Sets the location of the icon that is shown for the shortcut on the
276
  * desktop.
277
  *
278
  * @param     path   a fully qualified file name of a file that contains
279
  *                   the icon.
280
  * @param     index  the index of the specific icon to use in the file.
281
  *                   If there is only one icon in the file, use an index
282
  *                   of 0.
283
  */
284
  public void setIconLocation (String path,
285
                               int    index)
286
  {
287
    shortcut.setIconLocation (path, index);
288
  }
289

    
290
 /*--------------------------------------------------------------------------*/
291
 /**
292
  * returns icon Location
293
  *
294
  * @return iconLocation
295
  */
296
  public String getIconLocation()
297
  {
298
      return shortcut.getIconLocation();
299
  }
300

    
301
 /*--------------------------------------------------------------------------*/
302
 /**
303
  * Sets the name of the program group this ShellLinbk should be placed in.
304
  *
305
  * @param     groupName    the name of the program group
306
  */
307
  public void setProgramGroup (String groupName)
308
  {
309
    shortcut.setProgramGroup (groupName);
310
  }
311

    
312
 /*--------------------------------------------------------------------------*/
313
 /**
314
  * Sets the show command that is passed to the target application when the
315
  * link is activated. The show command determines if the the window will be
316
  * restored to the previous size, minimized, maximized or visible at all. 
317
  * <br><br>
318
  * <b>Note:</b><br>
319
  * Using <code>HIDE</code> will cause the target window not to show at
320
  * all. There is not even a button on the taskbar. This is a very useful
321
  * setting when batch files are used to launch a Java application as it
322
  * will then appear to run just like any native Windows application.<br>
323
  *
324
  *
325
  * @param     show   the show command. Valid settings are: <br>
326
  *                   <ul>
327
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#HIDE}
328
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#NORMAL}
329
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#MINIMIZED}
330
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#MAXIMIZED}
331
  *                   </ul>
332
  *
333
  * @see       #getShowCommand
334
  * internally maps from Shortcut.XXX to ShellLink.XXX
335
  */
336
  public void setShowCommand (int show) throws IllegalArgumentException
337
  {
338

    
339
    switch (show)
340
    {
341
      case HIDE : 
342
        {
343
          shortcut.setShowCommand (ShellLink.MINNOACTIVE);
344
          break;
345
        }
346
      case NORMAL : 
347
        {
348
          shortcut.setShowCommand (ShellLink.NORMAL);
349
          break;
350
        }
351
      case MINIMIZED : 
352
        {
353
          shortcut.setShowCommand (ShellLink.MINNOACTIVE);
354
          break;
355
        }
356
      case MAXIMIZED : 
357
        {
358
          shortcut.setShowCommand (ShellLink.MAXIMIZED);
359
          break;
360
        }
361
      default:
362
        {
363
          throw (new IllegalArgumentException (show + "is not recognized as a show command"));
364
        }
365
    }
366
  }
367

    
368
 /* returns current showCommand. 
369
  * internally maps from ShellLink.XXX to Shortcut.XXX
370
  *
371
  */
372
  public int getShowCommand ()
373
  {
374
          int showCommand = shortcut.getShowCommand();
375

    
376
        switch( showCommand )
377
        {
378
                case ShellLink.NORMAL :
379
                        showCommand = NORMAL;
380
                        break;
381
                // both MINNOACTIVE and MINIMIZED map to Shortcut.MINIMIZED
382
                case ShellLink.MINNOACTIVE :
383
                case ShellLink.MINIMIZED :
384
                        showCommand = MINIMIZED;
385
                        break;
386
                case ShellLink.MAXIMIZED:
387
                        showCommand = MAXIMIZED;
388
                        break;
389
                default:
390
                        break;
391
        }
392

    
393
        return showCommand;
394
  }
395

    
396
 /*--------------------------------------------------------------------------*/
397
 /**
398
  * Sets the absolute path to the shortcut target.
399
  *
400
  * @param     path     the fully qualified file name of the target
401
  */
402
  public void setTargetPath (String path)
403
  {
404
    shortcut.setTargetPath (path);
405
  }
406

    
407
 /*--------------------------------------------------------------------------*/
408
 /**
409
  * Sets the working directory for the link target.
410
  *
411
  * @param     dir    the working directory
412
  */
413
  public void setWorkingDirectory (String dir)
414
  {
415
    shortcut.setWorkingDirectory (dir);
416
  }
417

    
418
 /*--------------------------------------------------------------------------*/
419
 /**
420
  * Gets the working directory for the link target.
421
  *
422
  * @return the working directory.
423
  */
424
  public String getWorkingDirectory ()
425
  {
426
    return shortcut.getWorkingDirectory ();
427
  }
428

    
429
 /*--------------------------------------------------------------------------*/
430
 /**
431
  * Sets the name shown in a menu or on the desktop for the link.
432
  *
433
  * @param     name   The name that the link should display on a menu or on
434
  *                   the desktop. Do not include a file extension.
435
  */
436
  public void setLinkName (String name)
437
  {
438
    shortcut.setLinkName (name);
439
  }
440

    
441
 /*--------------------------------------------------------------------------*/
442
 /**
443
  * Gets the type of link
444
  *  types are: <br>
445
  *                   <ul>
446
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#DESKTOP}
447
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#APPLICATIONS}
448
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#START_MENU}
449
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#START_UP}
450
  *                   </ul>
451
  * maps from ShellLink-types to Shortcut-types.
452
  */
453
  public int getLinkType()
454
  {
455
          int typ = shortcut.getLinkType();
456

    
457
        switch(typ)
458
        {
459
                case ShellLink.DESKTOP:
460
                        typ = DESKTOP;
461
                        break;
462
                case ShellLink.PROGRAM_MENU :
463
                        typ = APPLICATIONS;
464
                        break;
465
                case ShellLink.START_MENU :
466
                        typ = START_MENU;
467
                        break;
468
                case ShellLink.STARTUP :
469
                        typ = START_UP;
470
                        break;
471
                default:
472
                        break;
473
        }
474

    
475
        return typ;
476
  }
477

    
478
 /*--------------------------------------------------------------------------*/
479
 /**
480
  * Sets the type of link
481
  *
482
  * @param     type   The type of link desired. The following values can be set:<br>
483
  *  (note APPLICATION on Windows is 'Start Menu\Programs')
484
  *  APPLICATION is a Mac term.
485
  *                   <ul>
486
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#DESKTOP}
487
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#APPLICATIONS}
488
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#START_MENU}
489
  *                   <li>{@link com.izforge.izpack.util.os.Shortcut#START_UP}
490
  *                   </ul>
491
  *
492
  * @exception IllegalArgumentException if an an invalid type is passed
493
  */
494
  public void setLinkType (int type) throws IllegalArgumentException
495
  {
496
    switch (type)
497
    {
498
      case DESKTOP : 
499
        {
500
          shortcut.setLinkType (ShellLink.DESKTOP);
501
          break;
502
        }
503
      case APPLICATIONS : 
504
        {
505
          shortcut.setLinkType (ShellLink.PROGRAM_MENU);
506
          break;
507
        }
508
      case START_MENU : 
509
        {
510
          shortcut.setLinkType (ShellLink.START_MENU);
511
          break;
512
        }
513
      case START_UP : 
514
        {
515
          shortcut.setLinkType (ShellLink.STARTUP);
516
          break;
517
        }
518
      default:
519
        {
520
          throw (new IllegalArgumentException (type + "is not recognized as a valid link type"));
521
        }
522
    }
523
  }
524

    
525
 /*--------------------------------------------------------------------------*/
526
 /**
527
  * Gets the user type for the link
528
  *
529
  * @return  userType 
530
  * @see       #CURRENT_USER
531
  * @see       #ALL_USERS
532
  */
533
  public int getUserType()
534
  {
535
          int utype = shortcut.getUserType();
536

    
537
        switch(utype)
538
        {
539
                case ShellLink.ALL_USERS :
540
                utype = ALL_USERS;
541
                break;
542

    
543
                case ShellLink.CURRENT_USER :
544
                utype = CURRENT_USER;
545
                break;
546
        }
547

    
548
          return utype;
549
  }
550

    
551
 /*--------------------------------------------------------------------------*/
552
 /**
553
  * Sets the user type for the link
554
  *
555
  * @param     type  the type of user for the link.
556
  * 
557
  * @see       Shortcut#CURRENT_USER
558
  * @see       Shortcut#ALL_USERS
559
  *
560
  * if the linkPath for that type is empty, refuse to set.
561
  */
562
 /*--------------------------------------------------------------------------*/
563
  public void setUserType (int type)
564
  {
565
    if (type == CURRENT_USER)
566
    {
567
          if ( shortcut.getcurrentUserLinkPath().length() > 0 )
568
          {
569
                  shortcut.setUserType (ShellLink.CURRENT_USER);
570
          }
571
    }
572
    else if (type == ALL_USERS)
573
    {
574
          if ( shortcut.getallUsersLinkPath().length() > 0 )
575
          {
576
                  shortcut.setUserType (ShellLink.ALL_USERS);
577
          }
578
    }
579
  }
580

    
581
 /*--------------------------------------------------------------------------*/
582
 /**
583
  * Saves this link.
584
  *
585
  * @exception Exception if problems are encountered
586
  */
587
  public void save () throws Exception
588
  {
589
    shortcut.save ();
590
  }
591

    
592
 /*--------------------------------------------------------------------------*/
593
 /**
594
  * Gets the link hotKey
595
  *
596
  * @return  int hotKey
597
  */
598
  public int getHotkey()
599
  {
600
          return shortcut.getHotkey();
601
  }
602

    
603
 /*--------------------------------------------------------------------------*/
604
 /**
605
  * Sets the link hotKey
606
  *
607
  * @param hotkey
608
  *
609
  * incoming 2 byte hotkey is:
610
  *  high byte modifier:
611
  *  SHIFT        = 0x01
612
  *  CONTROL= 0x02
613
  *  ALT        = 0x04
614
  *  EXT        = 0x08
615
  *
616
  *  lower byte contains ascii letter.
617
  *  ie 0x0278 represents CTRL+x
618
  *     0x068a represents CTRL+ALT+z
619
  */
620
  public void setHotkey(int hotkey)
621
  {
622
          shortcut.setHotkey(hotkey);
623
  }
624
}
625
/*---------------------------------------------------------------------------*/
626