NBIDevFaqShortcuts

Revision as of 14:39, 5 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

Contents

Creating shortcuts

How to create Start Menu / Desktop shortcuts on Windows

This example shows how to create Start Menu shortcut on Windows during installation and remove it during uninstallation.
To do the same for Desktop shortcuts just use another LocationType pair values (CURRENT_USER_DESKTOP and ALL_USERS_DESKTOP) when calling SystemUtils.create(remove)Shortcut() methods.
It is also worth to remove call to shortcut.setRelativePath in the example below even though it has no effect.

This code is suitable for putting it in the ConfigurationLogic class of your product. Don`t forget to fix imports after that.

    public void install(final Progress progress) throws InstallationException {    
        File installLocation = getProduct().getInstallationLocation();
        try {
            SystemUtils.createShortcut(getShortcut(installLocation), LocationType.ALL_USERS_START_MENU);
        } catch (NativeException e) {
            LogManager.log(e);
        }
    }

    public void uninstall(final Progress progress) throws UninstallationException {    
        File installLocation = getProduct().getInstallationLocation();
        try {
            SystemUtils.removeShortcut(getShortcut(installLocation), LocationType.ALL_USERS_START_MENU, true);
        } catch (NativeException e) {
            LogManager.log(e);
        }

    }

    private Shortcut getShortcut(final File location) {
        final String name = "My Application";
        final String relativePath = "My Company";
        final String description = "This is the description of my application";
        final File icon = new File(location, "bin<br>myapp.exe");
        final File executable = new File(location, "bin<br>myapp.exe");
        final FileShortcut shortcut = new FileShortcut(name, executable);
        shortcut.setDescription(description);
        shortcut.setIcon(icon);
        shortcut.setRelativePath(relativePath);
        shortcut.setWorkingDirectory(location);
        return shortcut;
    }


If you wish to use Start Menu for the current user (if he don`t have enough permissions to write to access all-users area), you can use

SystemUtils.isCurrentUserAdmin() ? LocationType.ALL_USERS_START_MENU : LocationType.CURRENT_USER_START_MENU
instead of
LocationType.ALL_USERS_START_MENU

in the code above.

In the real life it is possible that installation is performed under one user (restricted), and uninstallation - under another one (admin).
It that case it is worth to store the choice (current or all-users scope) during installation and the use it during uninstallation.
\

 
   public void install(final Progress progress) throws InstallationException {    
       ...
       getProduct().setProperty("start.menu.shortcut.location", SystemUtils.isCurrentUserAdmin() ? "all.users" : "current.user");
       ...
   }
   public void uninstall(final Progress progress) throws UninstallationException {    
       ...
       final String shortcutLocation = getProduct().getProperty("start.menu.shortcut.location");
       LocationType lt = ((shortcutLocation == null) || shortcutLocation.equals("current.user")) ? LocationType.CURRENT_USER_START_MENU : LocationType.ALL_USERS_START_MENU;
       SystemUtils.removeShortcut(getShortcut(installLocation), type, true);
       ...
   }
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo