Example usage for java.awt Desktop isSupported

List of usage examples for java.awt Desktop isSupported

Introduction

In this page you can find the example usage for java.awt Desktop isSupported.

Prototype

public boolean isSupported(Action action) 

Source Link

Document

Tests whether an action is supported on the current platform.

Usage

From source file:org.duracloud.syncui.SyncUIDriver.java

private static void launchBrowser(final String url) {
    if (!java.awt.Desktop.isDesktopSupported()) {
        log.warn("Desktop is not supported. Unable to open");

    } else {// ww w .ja v  a  2 s . co m
        java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
        if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
            log.warn("Desktop doesn't support the browse action.");
        } else {
            java.net.URI uri;
            try {
                uri = new java.net.URI(url);
                desktop.browse(uri);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils.java

/**
 * Open the default browser in the given URL.
 *
 * @param url The (not-null) URL that is going to be opened.
 * @throws IOException when the URL could not be opened
 *//*w  w w  .  j a va2s . c om*/
public static void browse(URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException();
    }
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(url.toURI());
        } catch (URISyntaxException e1) {
            throw new IOException(e1);
        }
    } else {
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("xdg-open " + url);
    }
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

private static void showURLExternal(@Nonnull final URL url) {
    if (Desktop.isDesktopSupported()) {
        final Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(url.toURI());
            } catch (Exception x) {
                LOGGER.error("Can't browse URL in Desktop", x);
            }//w  w w.j a  va2s.c om
        } else if (SystemUtils.IS_OS_LINUX) {
            final Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("xdg-open " + url);
            } catch (IOException e) {
                LOGGER.error("Can't browse URL under Linux", e);
            }
        } else if (SystemUtils.IS_OS_MAC) {
            final Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("open " + url);
            } catch (IOException e) {
                LOGGER.error("Can't browse URL on MAC", e);
            }
        }
    }

}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

public static void openInSystemViewer(@Nonnull final File file) {
    final Runnable startEdit = new Runnable() {
        @Override//ww w  .  j  a  va 2 s . c  o m
        public void run() {
            boolean ok = false;
            if (Desktop.isDesktopSupported()) {
                final Desktop dsk = Desktop.getDesktop();
                if (dsk.isSupported(Desktop.Action.OPEN)) {
                    try {
                        dsk.open(file);
                        ok = true;
                    } catch (Throwable ex) {
                        LOGGER.error("Can't open file in system viewer : " + file, ex);//NOI18N
                    }
                }
            }
            if (!ok) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        DialogProviderManager.getInstance().getDialogProvider()
                                .msgError("Can't open file in system viewer! See the log!");//NOI18N
                        Toolkit.getDefaultToolkit().beep();
                    }
                });
            }
        }
    };
    final Thread thr = new Thread(startEdit, " MMDStartFileEdit");//NOI18N
    thr.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(final Thread t, final Throwable e) {
            LOGGER.error("Detected uncaught exception in openInSystemViewer() for file " + file, e);
        }
    });

    thr.setDaemon(true);
    thr.start();
}

From source file:edu.umd.cs.submit.CommandLineSubmit.java

public static String[] getSubmitUserForOpenId(String courseKey, String projectNumber, String baseURL)
        throws UnsupportedEncodingException, URISyntaxException, IOException {
    Console console = System.console();

    boolean requested = false;
    String encodedProjectNumber = URLEncoder.encode(projectNumber, "UTF-8");
    URI u = new URI(baseURL + "/view/submitStatus.jsp?courseKey=" + courseKey + "&projectNumber="
            + encodedProjectNumber);/* www .  j  a  v  a  2 s  . c o m*/

    if (java.awt.Desktop.isDesktopSupported()) {
        Desktop desktop = java.awt.Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            System.out.println(
                    "Your browser will connect to the submit server, which may require you to authenticate yourself");
            System.out.println("Please do so, and then you will be shown a page with a textfield on it");
            System.out.println("Then copy that text and paste it into the prompt here");
            desktop.browse(u);
            requested = true;
        }
    }
    if (!requested) {
        System.out.println("Please enter the following URL into your browser");
        System.out.println("  " + u);
        System.out.println();
        System.out.println(
                "Your browser will connect to the submit server, which may require you to authenticate yourself");
        System.out.println("Please do so, and then you will be shown a page with a textfield on it");
        System.out.println("Then copy that text and paste it into the prompt here");

    }
    while (true) {
        System.out.println();
        System.out.println("Submission verification information from browser? ");
        String info = new String(console.readLine());
        if (info.length() > 2) {
            int checksum = Integer.parseInt(info.substring(info.length() - 1), 16);
            info = info.substring(0, info.length() - 1);
            int hash = info.hashCode() & 0x0f;
            if (checksum == hash) {
                String fields[] = info.split(";");
                if (fields.length == 2) {
                    return fields;

                }
            }
        }
        System.out.println("That doesn't seem right");
        System.out.println(
                "The information should be your account name and a string of hexidecimal digits, separated by a semicolon");
        System.out.println("Please try again");
        System.out.println();
    }
}

From source file:xtrememp.update.SoftwareUpdate.java

public static void checkForUpdates(final boolean showDialogs) {
    checkForUpdatesWorker = new SwingWorker<Version, Void>() {

        @Override/*from   ww w. j a  v  a 2s.com*/
        protected Version doInBackground() throws Exception {
            logger.debug("checkForUpdates: started...");
            long startTime = System.currentTimeMillis();
            Version version = getLastVersion(new URL(updatesURL));
            if (showDialogs) {
                // Simulate (if needed) a delay of 2 sec max to let the user cancel the task.
                long delayTime = System.currentTimeMillis() - startTime - 2000;
                if (delayTime > 0) {
                    Thread.sleep(delayTime);
                }
            }
            return version;
        }

        @Override
        protected void done() {
            logger.debug("checkForUpdates: done");
            if (checkForUpdatesDialog != null && checkForUpdatesDialog.isVisible()) {
                checkForUpdatesDialog.dispose();
            }
            if (!isCancelled()) {
                try {
                    newerVersion = get();
                    if (newerVersion != null && newerVersion.compareTo(currentVersion) == 1) {
                        logger.debug("checkForUpdates: currentVersion = {}", currentVersion);
                        logger.debug("checkForUpdates: newerVersion = {}", newerVersion);
                        logger.debug("SoftwareUpdate::checkForUpdates: updates found");
                        Object[] options = { tr("Button.Cancel") };
                        Desktop desktop = null;
                        if (Desktop.isDesktopSupported()) {
                            desktop = Desktop.getDesktop();
                            if (desktop.isSupported(Desktop.Action.BROWSE)) {
                                options = new Object[] { tr("Button.Download"), tr("Button.Cancel") };
                            }
                        }
                        JPanel panel = new JPanel(new BorderLayout(0, 10));
                        panel.add(new JLabel("<html>" + tr("Dialog.SoftwareUpdate.UpdatesFound") + " ("
                                + newerVersion + ")</html>"), BorderLayout.CENTER);
                        JCheckBox hideCheckBox = null;
                        if (Settings.isAutomaticUpdatesEnabled()) {
                            hideCheckBox = new JCheckBox(
                                    tr("Dialog.SoftwareUpdate.DisableAutomaticCheckForUpdates"));
                            panel.add(hideCheckBox, BorderLayout.SOUTH);
                        }
                        int option = JOptionPane.showOptionDialog(XtremeMP.getInstance().getMainFrame(), panel,
                                tr("Dialog.SoftwareUpdate"), JOptionPane.OK_CANCEL_OPTION,
                                JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                        if (hideCheckBox != null) {
                            Settings.setAutomaticUpdatesEnabled(!hideCheckBox.isSelected());
                        }
                        if ((options.length == 2) && (option == JOptionPane.OK_OPTION)) {
                            try {
                                URL url = new URL(newerVersion.getDownloadURL());
                                desktop.browse(url.toURI());
                            } catch (Exception ex) {
                                logger.error(ex.getMessage(), ex);
                            }
                        }
                    } else {
                        logger.debug("checkForUpdates: no updates found");
                        if (showDialogs) {
                            JOptionPane.showMessageDialog(XtremeMP.getInstance().getMainFrame(),
                                    tr("Dialog.SoftwareUpdate.NoUpdatesFound"), tr("Dialog.SoftwareUpdate"),
                                    JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                } catch (Exception ex) {
                    logger.error(ex.getMessage(), ex);
                    if (showDialogs) {
                        JOptionPane.showMessageDialog(XtremeMP.getInstance().getMainFrame(),
                                tr("Dialog.SoftwareUpdate.ConnectionFailure"), tr("Dialog.SoftwareUpdate"),
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        }
    };
    checkForUpdatesWorker.execute();
}

From source file:com.igormaznitsa.ideamindmap.utils.IdeaUtils.java

public static void openInSystemViewer(@Nonnull final DialogProvider dialogProvider,
        @Nullable final VirtualFile theFile) {
    final File file = vfile2iofile(theFile);

    if (file == null) {
        LOGGER.error("Can't find file to open, null provided");
        dialogProvider.msgError("Can't find file to open");
    } else {//from  w  w w  .j  a  v  a2  s.com
        final Runnable startEdit = new Runnable() {
            @Override
            public void run() {
                boolean ok = false;
                if (Desktop.isDesktopSupported()) {
                    final Desktop dsk = Desktop.getDesktop();
                    if (dsk.isSupported(Desktop.Action.OPEN)) {
                        try {
                            dsk.open(file);
                            ok = true;
                        } catch (Throwable ex) {
                            LOGGER.error("Can't open file in system viewer : " + file, ex);//NOI18N
                        }
                    }
                }
                if (!ok) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dialogProvider.msgError("Can't open file in system viewer! See the log!");//NOI18N
                            Toolkit.getDefaultToolkit().beep();
                        }
                    });
                }
            }
        };
        final Thread thr = new Thread(startEdit, " MMDStartFileEdit");//NOI18N
        thr.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(final Thread t, final Throwable e) {
                LOGGER.error("Detected uncaught exception in openInSystemViewer() for file " + file, e);
            }
        });

        thr.setDaemon(true);
        thr.start();
    }
}

From source file:net.semanticmetadata.lire.utils.FileUtils.java

/**
 * Opens a browser windows th<t shows the given URI.
 *
 * @param uri the path to the file to show in the browser window.
 *//*from  w w w . j  a  v  a2  s  .  c  o  m*/
public static void browseUri(String uri) {
    if (!java.awt.Desktop.isDesktopSupported()) {
        System.err.println("Desktop is not supported (fatal)");
        System.exit(1);
    }

    java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

    if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
        System.err.println("Desktop doesn't support the browse action (fatal)");
        System.exit(1);
    }

    try {
        java.net.URI url = new java.net.URI(uri);
        desktop.browse(url);
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

From source file:com.xmage.launcher.XMageLauncher.java

private static void openWebpage(String uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {/*from   w  w  w .ja  v  a  2  s  . c o m*/
            desktop.browse(new URI(uri));
        } catch (URISyntaxException ex) {
            logger.error("Error: ", ex);
        } catch (IOException ex) {
            logger.error("Error: ", ex);
        }
    }
}

From source file:entity.files.SYSFilesTools.java

/**
 * Diese Methode ermittelt zu einer gebenen Datei und einer gewnschten Aktion das passende Anzeigeprogramm.
 * Falls die Desktop API nicht passendes hat, werdne die lokal definierten Anzeigeprogramme verwendet.
 * <p>//from w  w  w .jav  a 2s.c  o  m
 * Bei Linux mssen dafr unbedingt die Gnome Libraries installiert sein.
 * apt-get install libgnome2-0
 *
 * @param file
 * @param action
 */
public static void handleFile(File file, java.awt.Desktop.Action action) {
    if (file == null) {
        return;
    }
    Desktop desktop = null;

    if (getLocalDefinedApp(file) != null) {
        try {
            Runtime.getRuntime().exec(getLocalDefinedApp(file));
        } catch (IOException ex) {
            OPDE.getLogger().error(ex);
        }
    } else {
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            if (action == Desktop.Action.OPEN && desktop.isSupported(Desktop.Action.OPEN)) {
                try {
                    desktop.open(file);
                } catch (IOException ex) {
                    OPDE.getDisplayManager().addSubMessage(
                            new DisplayMessage(SYSTools.xx("misc.msg.noviewer"), DisplayMessage.WARNING));
                }
            } else if (action == Desktop.Action.PRINT && desktop.isSupported(Desktop.Action.PRINT)) {
                try {
                    desktop.print(file);
                } catch (IOException ex) {
                    OPDE.getDisplayManager().addSubMessage(
                            new DisplayMessage(SYSTools.xx("misc.msg.noprintprog"), DisplayMessage.WARNING));
                }
            } else {
                OPDE.getDisplayManager().addSubMessage(
                        new DisplayMessage(SYSTools.xx("misc.msg.nofilehandler"), DisplayMessage.WARNING));
            }
        } else {
            OPDE.getDisplayManager().addSubMessage(
                    new DisplayMessage(SYSTools.xx("misc.msg.nojavadesktop"), DisplayMessage.WARNING));
        }
    }
}