Example usage for java.awt Desktop isDesktopSupported

List of usage examples for java.awt Desktop isDesktopSupported

Introduction

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

Prototype

public static boolean isDesktopSupported() 

Source Link

Document

Tests whether this class is supported on the current platform.

Usage

From source file:au.org.ala.delta.intkey.ui.WebSearchDialog.java

@Action
public void WebSearchDialog_search() {
    String term = txtSearch.getText().trim();
    if (!org.apache.commons.lang.StringUtils.isEmpty(term)) {
        SearchEngineDescriptor selected = (SearchEngineDescriptor) cmbSearchEngine.getSelectedItem();
        if (selected != null) {
            if (Desktop.isDesktopSupported()) {
                URI uri = URI.create(selected.getUrl(term));
                try {
                    Desktop.getDesktop().browse(uri);
                } catch (IOException e) {
                    e.printStackTrace();
                }/*from  ww  w  .j  a  v a 2s  .  c o  m*/
            }
        }
    }
}

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   ww w.  j  a v a2  s  . c  om*/
        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:de.perdoctus.ebikeconnect.gui.MainWindowController.java

public void openGithubUrl() {
    if (Desktop.isDesktopSupported()) {
        try {//  w  w  w.  j  a  va  2s  .  co m
            Desktop.getDesktop().browse(URI.create(rb.getString("github-url")));
        } catch (IOException e) {
            logger.error("Failed to open browser.", e);
        }
    }
}

From source file:com.ethercamp.harmony.desktop.HarmonyDesktop.java

private static void openBrowser(String url) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {//from   w  w w .j  a  v  a 2  s  .c  o m
            desktop.browse(URI.create(url));
        } catch (Exception e) {
            log.error("Problem opening browser", e);
        }
    }
}

From source file:Exporters.ExportToWord.java

public void compileNotes(File outfile, Vulnerability vuln) {
    System.out.println("==ExportToWord=compileNotes: " + outfile.getAbsolutePath());
    try {// w  ww.j ava  2 s  .com

        WordprocessingMLPackage template = getTemplate();
        MainDocumentPart bodyPart = template.getMainDocumentPart();

        // get and delete the vuln table from the document.
        //List<Object> Tables = getAllElementFromObject(bodyPart, XWPFTable.class);
        //XWPFTable table = (XWPFTable)Tables.get(0);
        /*
        List<Object> content = bodyPart.getContent();
        Iterator it = content.iterator();
        while (it.hasNext()) {
        Object obj = it.next();
        content.remove(obj);
        }
            */

        bodyPart.addStyledParagraphOfText("Heading2", vuln.getTitle());

        Enumeration enums = vuln.getAffectedHosts().elements();
        while (enums.hasMoreElements()) {
            Host host = (Host) enums.nextElement();
            String header = host.getIp_address() + " - " + host.getHostname() + " - " + host.getPortnumber()
                    + "/" + host.getProtocol();
            bodyPart.addStyledParagraphOfText("Heading3", header);
            Note note = host.getNotes();
            String[] paragraphs = note.getNote_text().split("\\n");
            for (String para : paragraphs) {
                bodyPart.addStyledParagraphOfText("Code", para);
            }
        }

        if (outfile.exists() == false) {
            template.save(outfile);
        }

        try {
            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(outfile);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.k42b3.neodym.oauth.Oauth.java

public boolean authorizeToken() throws Exception {
    String url;/* w  ww  . j a  va2s . co m*/

    if (this.provider.getAuthorizationUrl().indexOf('?') == -1) {
        url = this.provider.getAuthorizationUrl() + "?oauth_token=" + this.token;
    } else {
        url = this.provider.getAuthorizationUrl() + "&oauth_token=" + this.token;
    }

    URI authUrl = new URI(url);

    if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();

        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            desktop.browse(authUrl);
        } else {
            JOptionPane.showMessageDialog(null, "Visit the following URL: " + authUrl);
        }
    } else {
        JOptionPane.showMessageDialog(null, "Visit the following URL: " + authUrl);
    }

    verificationCode = JOptionPane.showInputDialog("Please enter the verification Code");

    return true;
}

From source file:io.bitsquare.common.util.Utilities.java

public static void openDirectory(File directory) throws IOException {
    if (!isLinux() && Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
        Desktop.getDesktop().open(directory);
    } else {/*from   w w  w.j a v a  2s. co m*/
        // Maybe Application.HostServices works in those cases?
        // HostServices hostServices = getHostServices();
        // hostServices.showDocument(uri.toString());

        // On Linux Desktop is poorly implemented.
        // See https://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform
        if (!DesktopUtil.open(directory))
            throw new IOException("Failed to open directory: " + directory.toString());
    }
}

From source file:org.languagetool.gui.ResultArea.java

private String getRuleMatchHtml(List<RuleMatch> ruleMatches, String text, String startCheckText) {
    final ContextTools contextTools = new ContextTools();
    final StringBuilder sb = new StringBuilder(200);
    sb.append(startCheckText);//from ww w.jav a  2s.  co m
    sb.append("<br>\n");
    int i = 0;
    for (final RuleMatch match : ruleMatches) {
        final String output = Tools.makeTexti18n(messages, "result1", i + 1, match.getLine() + 1,
                match.getColumn());
        sb.append(output);
        final String msg = match.getMessage().replaceAll("<suggestion>", "<b>")
                .replaceAll("</suggestion>", "</b>").replaceAll("<old>", "<b>").replaceAll("</old>", "</b>");
        sb.append("<b>").append(messages.getString("errorMessage")).append("</b> ");
        sb.append(msg);
        final RuleLink ruleLink = RuleLink.buildDeactivationLink(match.getRule());
        sb.append(" <a href=\"").append(ruleLink).append("\">").append(messages.getString("deactivateRule"))
                .append("</a><br>\n");
        if (match.getSuggestedReplacements().size() > 0) {
            final String replacement = StringTools.listToString(match.getSuggestedReplacements(), "; ");
            sb.append("<b>").append(messages.getString("correctionMessage")).append("</b> ").append(replacement)
                    .append("<br>\n");
        }
        if (ITSIssueType.Misspelling.equals(match.getRule().getLocQualityIssueType())) {
            contextTools.setErrorMarkerStart(SPELL_ERROR_MARKER_START);
        } else {
            contextTools.setErrorMarkerStart(LT_ERROR_MARKER_START);
        }
        final String context = contextTools.getContext(match.getFromPos(), match.getToPos(), text);
        sb.append("<b>").append(messages.getString("errorContext")).append("</b> ").append(context);
        sb.append("<br>\n");
        if (match.getRule().getUrl() != null && Desktop.isDesktopSupported()) {
            sb.append("<b>").append(messages.getString("moreInfo")).append("</b> <a href=\"");
            final String url = match.getRule().getUrl().toString();
            sb.append(url);
            final String shortUrl = StringUtils.abbreviate(url, 60);
            sb.append("\">").append(shortUrl).append("</a><br>\n");
        }
        i++;
    }
    sb.append(Main.HTML_GREY_FONT_START);
    sb.append(getDisabledRulesHtml());
    final String checkDone = Tools.makeTexti18n(messages, "checkDone", ruleMatches.size(), runTime);
    sb.append("<br>\n").append(checkDone);
    sb.append("<br>\n").append(messages.getString("makeLanguageToolBetter"));
    sb.append(Main.HTML_FONT_END).append("<br>\n");
    return sb.toString();
}

From source file:org.tinymediamanager.ui.TmmUIHelper.java

public static void openFile(Path file) throws Exception {
    String fileType = "." + FilenameUtils.getExtension(file.getFileName().toString());
    String abs = file.toAbsolutePath().toString();

    if (StringUtils.isNotBlank(Globals.settings.getMediaPlayer())
            && Globals.settings.getAllSupportedFileTypes().contains(fileType)) {
        if (SystemUtils.IS_OS_MAC_OSX) {
            Runtime.getRuntime()/*w  ww  .  j a va  2s  . co  m*/
                    .exec(new String[] { "open", Globals.settings.getMediaPlayer(), "--args", abs });
        } else {
            Runtime.getRuntime().exec(new String[] { Globals.settings.getMediaPlayer(), abs });
        }
    } else if (SystemUtils.IS_OS_WINDOWS) {
        // use explorer directly - ship around access exceptions and the unresolved network bug
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6780505
        Runtime.getRuntime().exec(new String[] { "explorer", abs });
    } else if (SystemUtils.IS_OS_LINUX) {
        // try all different starters
        boolean started = false;
        try {
            Runtime.getRuntime().exec(new String[] { "gnome-open", abs });
            started = true;
        } catch (IOException e) {
        }

        if (!started) {
            try {
                Runtime.getRuntime().exec(new String[] { "kde-open", abs });
                started = true;
            } catch (IOException e) {
            }
        }

        if (!started) {
            try {
                Runtime.getRuntime().exec(new String[] { "xdg-open", abs });
                started = true;
            } catch (IOException e) {
            }
        }

        if (!started && Desktop.isDesktopSupported()) {
            Desktop.getDesktop().open(file.toFile());
        }
    } else if (Desktop.isDesktopSupported()) {
        Desktop.getDesktop().open(file.toFile());

    } else {
        throw new UnsupportedOperationException();
    }
}

From source file:com.google.oacurl.Login.java

private static void launchBrowser(LoginOptions options, String authorizationUrl) {
    logger.log(Level.INFO, "Redirecting to URL: " + authorizationUrl);

    boolean browsed = false;
    if (options.getBrowser() == null) {
        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
            if (desktop.isSupported(Action.BROWSE)) {
                try {
                    desktop.browse(URI.create(authorizationUrl));
                    browsed = true;/* w ww .j  a va  2 s . c o m*/
                } catch (IOException e) {
                    // In some situations "BROWSE" appears supported but throws an
                    // exception.
                    logger.log(Level.WARNING, "Error opening browser for Desktop#browse(String)",
                            options.isVerbose() ? e : null);
                }
            } else {
                logger.log(Level.WARNING, "java.awt.Desktop BROWSE action not supported.");
            }
        } else {
            logger.log(Level.WARNING, "java.awt.Desktop not supported. You should use Java 1.6.");
        }
    }

    if (!browsed) {
        String browser = options.getBrowser();
        if (browser == null) {
            browser = "google-chrome";
        }

        try {
            Runtime.getRuntime().exec(new String[] { browser, authorizationUrl });
        } catch (IOException e) {
            logger.log(Level.SEVERE,
                    "Error running browser: " + browser + ". "
                            + "Specify a browser with --browser or use --nobrowser to print URL.",
                    options.isVerbose() ? e : null);
            System.exit(-1);
        }
    }
}