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:org.apache.marmotta.splash.startup.StartupListener.java

/**
 * React on the AFTER_START_EVENT of Tomcat and startup the browser to point to the Marmotta installation. Depending
 * on the state of the Marmotta installation, the following actions are carried out:
 * <ul>/*from ww w  .j a  v a 2  s .c  o m*/
 *     <li>in case the Marmotta is started for the first time, show a dialog box with options to select which IP-address to use for
 *     configuring the Marmotta; the IP address will be stored in a separate properties file in MARMOTTA_HOME</li>
 *     <li>in case the Marmotta has already been configured but the IP address that was used is no longer existing on the server,
 *     show a warning dialog (this can happen e.g. for laptops with dynamically changing network configurations)</li>
 *     <li>otherwise, open a browser using the network address that was used previously</li>
 * </ul>
 *
 * @param event LifecycleEvent that has occurred
 */
@Override
public void lifecycleEvent(LifecycleEvent event) {
    if (event.getType().equals(Lifecycle.AFTER_START_EVENT)) {
        if (!GraphicsEnvironment.isHeadless()) {

            String serverName = null;
            int serverPort = 0;

            // open browser window
            if (Desktop.isDesktopSupported()) {

                Properties startupProperties = getStartupProperties();
                if (startupProperties.getProperty("startup.host") != null
                        && startupProperties.getProperty("startup.port") != null) {

                    serverName = startupProperties.getProperty("startup.host");
                    serverPort = Integer.parseInt(startupProperties.getProperty("startup.port"));

                    if (!checkServerName(serverName) || serverPort != getServerPort()) {
                        MessageDialog.show("Warning", "Configured server name not found", "The host name ("
                                + serverName + ") that has been used to configure this \n"
                                + "installation is no longer available on this server. The system \n"
                                + "might behave unexpectedly. Please consider using a localhost configuration \n"
                                + "for systems with dynamic IP addresses!");
                    }

                } else {
                    // show a dialog listing all available addresses of this server and allowing the user to
                    // chose
                    List<Option> choices = new ArrayList<>();

                    Map<String, List<InetAddress>> addressList = listHostAddresses();

                    List<String> hostNames = new ArrayList<String>(addressList.keySet());
                    Collections.sort(hostNames);

                    int loopback = -1;
                    for (int i = 0; i < hostNames.size(); i++) {
                        String hostName = hostNames.get(i);
                        List<InetAddress> addresses = addressList.get(hostName);
                        String label = hostName + " \n(";
                        for (Iterator<InetAddress> it = addresses.iterator(); it.hasNext();) {
                            label += it.next().getHostAddress();
                            if (it.hasNext()) {
                                label += ", ";
                            }
                        }
                        label += ")";

                        String text;

                        if (addresses.get(0).isLoopbackAddress()) {
                            text = "Local IP-Address. Recommended for Laptop use or Demonstration purposes";
                            loopback = loopback < 0 ? i : loopback;
                        } else {
                            text = "Public IP-Address. Recommended for Workstation or Server use";
                        }

                        choices.add(new Option(label, text));
                    }

                    int choice = SelectionDialog.select("Select Server Address",
                            "Select host address to use for configuring the\nApache Marmotta Platform.",
                            WordUtils.wrap(
                                    "For demonstration purposes or laptop installations it is recommended to select \""
                                            + (loopback < 0 ? "localhost" : hostNames.get(loopback))
                                            + "\" below. For server and workstation installations, please select a public IP address.",
                                    60),
                            choices, loopback);

                    if (choice < 0) {
                        log.error("No Server Address selected, server will shut down.");
                        throw new IllegalArgumentException("No Server Addess was selected");
                    }

                    serverName = hostNames.get(choice);
                    serverPort = getServerPort();

                    startupProperties.setProperty("startup.host", serverName);
                    startupProperties.setProperty("startup.port", serverPort + "");

                    storeStartupProperties(startupProperties);
                }

                final Desktop desktop = Desktop.getDesktop();
                if (desktop.isSupported(Desktop.Action.BROWSE) && serverName != null && serverPort > 0) {
                    try {

                        URI uri = new URI("http", null, serverName, serverPort, "/", null, null);
                        desktop.browse(uri);

                    } catch (Exception e1) {
                        System.err.println("could not open browser window, message was: " + e1.getMessage());
                    }

                }
            }
        }

    }
}

From source file:org.planetcrypto.bitcoin.PlanetCryptoNetworkMethods.java

/**
 * // w w  w. j a  va2 s  .  c om
 * @param uri URL of Eligius user stats
 */
public static void openWebpage(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:fridgegameinstaller.installation.java

public void preinstall(int ram, boolean shader) {
    System.out.println("Installing...");

    //check Modpack Update
    progmsg.setText("Checking for Modpack Update...");
    if (true) {//from ww  w  . j  ava 2s .  com
        progmsg.setText("Locating .minecraft...");
        //set .minecraft location
        System.out.println(DEF.sysos);

        if (DEF.sysos.indexOf("win") >= 0) {
            mcloc = System.getenv("appdata") + "/.minecraft";

        }
        if (DEF.sysos.indexOf("mac") >= 0) {
            mcloc = System.getProperty("user.home") + "/Library/Application Support/minecraft";

        }
        if (DEF.sysos.indexOf("nix") >= 0 || DEF.sysos.indexOf("nux") >= 0 || DEF.sysos.indexOf("aix") > 0) {
            mcloc = System.getProperty("user.home") + "/.minecraft";

        }
        if (mcloc == null) {

            mainFrame.errorMsg("No support for this OS.Try to install manually.", "Error");

        }

        //old Modpack version existing?
        if (checkModpackUpdate()) {
            System.out.println("installing..");
            System.out.println(System.getenv("appdata"));

            uninstall();
            install(ram);
            if (shader) {

                installShader();
            }
            prgbar.setValue(100);
            progmsg.setText("100%");
            mainFrame.errorMsg("Installation finished!", "100%");
            mainFrame.setFormToPostInstallation();
            try {
                String url = "http://www.fridgegame.com/credits";
                if (Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().browse(new URI(url));
                } else {
                    Runtime.getRuntime().exec("xdg-open " + url);
                }
            } catch (URISyntaxException | IOException e) {
                e.printStackTrace();
            }

        } else {

        }

    } else {

        mainFrame.errorMsg(
                "The modpack is up to date.If you want to reinstall it to fix errors delete the fridgegame_version.fg file in your .minecraft",
                "Error");
        mainFrame.setFormToPostInstallation();
    }

}

From source file:org.rascalmpl.library.experiments.Compiler.RVM.Interpreter.help.HelpManager.java

public void openInBrowser(URI uri) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {//from  w  w w .  j  a va2 s.co  m
            desktop.browse(uri);
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    } else {
        System.err.println("Desktop not supported, cannout open browser");
    }
}

From source file:emailplugin.MailCreator.java

private void mail(final Frame parent, final String content, final String title) {
    // Java 6 desktop API
    boolean sent = false;
    if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        try {/*from  ww w .  j av  a  2s.c  om*/
            URI uriMailTo = new URI("mailto", "?body=" + content + "&subject=" + title, null);
            desktop.mail(uriMailTo);
            sent = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (sent) {
        return;
    }

    // fall back to non Java 6 code
    try {
        final String mailTo = "mailto:?body=" + encodeString(content) + "&subject=" + encodeString(title);
        String application;
        String execparam;

        if ((OperatingSystem.isMacOs() || OperatingSystem.isWindows())
                && mSettings.getUseDefaultApplication()) {

            if (OperatingSystem.isMacOs()) {
                application = "/usr/bin/open";
                execparam = mailTo;
            } else {
                application = "rundll32.exe";
                execparam = "url.dll,FileProtocolHandler " + mailTo;
            }

        } else if (StringUtils.isBlank(mSettings.getApplication())) {
            if (OperatingSystem.isOther()) {
                if (!showKdeGnomeDialog(parent)) {
                    return;
                }
                application = mSettings.getApplication();
                execparam = getContentParameter(content);
            } else {
                showNotConfiguredCorrectly(parent);
                return;
            }
        } else {
            application = mSettings.getApplication();
            execparam = getContentParameter(content);
        }

        new ExecutionHandler(execparam, application).execute();

        if (mSettings.getShowEmailOpened()) {
            showEMailOpenedDialog(parent);
        }

    } catch (Exception e) {
        e.printStackTrace();
        int ret = ErrorHandler.handle(
                mLocalizer.msg("ErrorWhileStarting", "Error while starting mail application"), e,
                ErrorHandler.SHOW_YES_NO);

        if (ret == ErrorHandler.YES_PRESSED) {
            MainFrame.getInstance().showSettingsDialog(mPlugin);
        }
    }
}

From source file:es.eucm.eadventure.editor.plugin.vignette.ServerProxy.java

public boolean exportAndShowJson(List<VignetteCharacterPreview> vcps, String json) {
    for (VignetteCharacterPreview vcp : vcps) {
        System.err.println("Sending " + vcp.getName());
        if (!sendBytes(vcp.getImageBytes(), "image/png", vcp.getImageName())) {
            return false;
        }/*from ww  w .j a  va 2s. co  m*/
    }

    if (!sendBytes(json.getBytes(Charset.forName("UTF-8")), "text/json", "json.json")) {
        return false;
    }

    boolean error = false;
    if (Desktop.isDesktopSupported()) {
        try {
            Desktop.getDesktop()
                    .browse(new URI(serviceURL + "/talk.php?" + "m=" + getMac() + "&f=" + userPath));
        } catch (IOException e) {
            error = true;
            e.printStackTrace();
        } catch (URISyntaxException e) {
            error = true;
            e.printStackTrace();
        }
    }
    return error;
}

From source file:org.astrojournal.utilities.DesktopBrowse.java

/**
 * Browse an URI using Java Desktop (not recommended).
 * /* www  . j  a  v a2 s.  c  o m*/
 * @param uri
 *            the URI to browser
 * @return true if the command succeeded, false otherwise.
 */
public static boolean browseJavaDesktop(URI uri) {
    log.debug("Trying to run Desktop.getDesktop().browse(" + uri.toString() + ")");
    try {
        if (!Desktop.isDesktopSupported()) {
            log.debug("Java Desktop is not supported on this platform (" + osInfo + ").\n"
                    + "Impossible to browse the URI: " + uri.toString());
            return false;
        }
        if (!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
            log.debug("Java Desktop does not support the action BROWSER on this platform (" + osInfo + ").\n");
            return false;
        }
        Desktop.getDesktop().browse(uri);
    } catch (IOException e) {
        // Report the stack trace to make visible that something went wrong.
        log.debug("Java Desktop did not work on this platform (" + osInfo + ")", e);
        return false;
    }
    return true;
}

From source file:edu.scripps.fl.pubchem.xmltool.gui.PubChemXMLCreatorGUI.java

public void setUpGUIComponents() {
    jlbFileTemplate = gc.createJLabel("XML Template:");
    jlbFileExcel = gc.createJLabel("Excel Workbook:");

    jtfFileTemplate = gc.createJTextField(template);
    jtfFileExcel = gc.createJTextField("");

    jbnFileTemplate = gc.createJButton("Open16",
            "Choose a PubChem xml template to add tid, panel, and xref information to. A blank template is already provided.",
            "icon");
    jbnFileExcel = gc.createJButton("Open16",
            "Choose an Excel file containing tid, panel, or xref information in the appropriate format.",
            "icon");
    jbnRunCreator = gc.createJButton("Create PubChem XML", "Run the program.", "text");
    jbnReportCreator = gc.createJButton("Create Report", "Creates a report from excel workbook.", "text");

    jtpExcelTemplate = gc.createJTextPane("Excel Template");
    jtpExcelTemplate.setToolTipText(//from  w  w w . java  2 s  .  c o m
            "Save excel template under a new file name and add tid, panel, and/or xref information.");
    jtpExample = gc.createJTextPane("Example Excel File");

    if (!Desktop.isDesktopSupported()) {
        jtpExcelTemplate.setVisible(false);
        jtpExample.setVisible(false);
    }

    jbnFileTemplate.addActionListener(this);
    jbnFileExcel.addActionListener(this);
    jbnRunCreator.addActionListener(this);
    jbnReportCreator.addActionListener(this);
    jtpExcelTemplate.addMouseListener(this);
    jtpExample.addMouseListener(this);
    jtfFileTemplate.addMouseListener(this);

    gbc01 = gc.createGridBagConstraint(0, 0, jlbFileTemplate, "line start");
    gbc02 = gc.createGridBagConstraint(1, 0, jtfFileTemplate, "line start");
    gbc03 = gc.createGridBagConstraint(2, 0, jbnFileTemplate, "line start");
    gbc04 = gc.createGridBagConstraint(0, 1, jlbFileExcel, "line start");
    gbc05 = gc.createGridBagConstraint(1, 1, jtfFileExcel, "line start");
    gbc06 = gc.createGridBagConstraint(2, 1, jbnFileExcel, "line start");
    gbc07 = gc.createGridBagConstraint(1, 4, jbnRunCreator, "line start");
    gbc08 = gc.createGridBagConstraint(1, 4, jbnReportCreator, "line end");
    gbc09 = gc.createGridBagConstraint(3, 1, jtpExcelTemplate, "center");
    gbc10 = gc.createGridBagConstraint(3, 0, jtpExample, "center");
}

From source file:com.photon.phresco.framework.rest.api.UtilService.java

/**
 * Open folder.//from www. j  a v  a 2 s . c  om
 *
 * @param appDirName the app dir name
 * @param type the type
 * @return the response
 */
@GET
@Path("/openFolder")
@Produces(MediaType.APPLICATION_JSON)
public Response openFolder(@QueryParam(REST_QUERY_APPDIR_NAME) String appDirName,
        @QueryParam(REST_QUERY_TYPE) String type, @QueryParam(REST_QUERY_MODULE_NAME) String moduleName) {
    ResponseInfo<String> responseData = new ResponseInfo<String>();
    try {
        if (Desktop.isDesktopSupported()) {
            //            if (StringUtils.isNotEmpty(moduleName)) {
            //               appDirName = appDirName + File.separator + moduleName;
            //            }

            String rootModulePath = "";
            String subModuleName = "";

            if (StringUtils.isNotEmpty(moduleName)) {
                rootModulePath = Utility.getProjectHome() + appDirName;
                subModuleName = moduleName;
            } else {
                rootModulePath = Utility.getProjectHome() + appDirName;
            }

            File file = new File(getPath(rootModulePath, subModuleName, type));
            if (file.exists()) {
                Desktop.getDesktop().open(file);
            } else {
                StringBuilder sbOpenPath = new StringBuilder(rootModulePath);
                Desktop.getDesktop().open(new File(sbOpenPath.toString()));
            }
        }
        ResponseInfo<String> finalOutput = responseDataEvaluation(responseData, null, null,
                RESPONSE_STATUS_SUCCESS, PHR3C00001);
        return Response.status(Status.OK).entity(finalOutput).header("Access-Control-Allow-Origin", "*")
                .build();
    } catch (Exception e) {
        ResponseInfo<ProjectInfo> finalOutput = responseDataEvaluation(responseData, e, null,
                RESPONSE_STATUS_ERROR, PHR3C10001);
        return Response.status(Status.OK).entity(finalOutput).header("Access-Control-Allow-Origin", "*")
                .build();
    }
}

From source file:plugin.notes.gui.JIcon.java

/**
 *  Launches a file into the appropriate program for the OS we are running on
 *//*from www.  j av  a 2  s .c  om*/
private void launchFile() {
    if (PCGFile.isPCGenCharacterOrPartyFile(launch)) {
        plugin.loadRecognizedFileType(launch);
    } else {
        boolean opened = false;

        // Use desktop if available
        if (Desktop.isDesktopSupported()) {
            Desktop d = Desktop.getDesktop();
            if (d.isSupported(Desktop.Action.OPEN)) {
                try {
                    d.open(launch);
                    opened = true;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        if (!opened) {
            if (SystemUtils.IS_OS_UNIX) {
                String openCmd = SystemUtils.IS_OS_MAC_OSX ? "/usr/bin/open" : "xdg-open";
                String filePath = launch.getAbsolutePath();
                String[] args = { openCmd, filePath };
                Logging.debugPrintLocalised("Runtime.getRuntime().exec: [{0}] [{1}]", args[0], args[1]);

                try {
                    Runtime.getRuntime().exec(args);
                } catch (IOException e) {
                    Logging.errorPrint(e.getMessage(), e);
                }
            } else if (SystemUtils.IS_OS_WINDOWS) {
                try {
                    String start = ("rundll32 url.dll,FileProtocolHandler file://" + launch.getAbsoluteFile());
                    Runtime.getRuntime().exec(start);
                } catch (Exception e) {
                    Logging.errorPrint(e.getMessage(), e);
                }
            }
        }
    }
}