Java Code Examples for java.awt.Desktop#isDesktopSupported()

The following examples show how to use java.awt.Desktop#isDesktopSupported() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrmAbout.java    From MeteoInfo with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void jLabel_webMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel_webMouseClicked
    // TODO add your handling code here:
    try {
        URI uri = new URI("http://www.meteothink.org");
        Desktop desktop = null;
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        }
        if (desktop != null) {
            desktop.browse(uri);
        }
    } catch (URISyntaxException ex) {
        Logger.getLogger(FrmAbout.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ioe) {
    }
}
 
Example 2
Source File: Utils.java    From aurous-app with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Open a file using {@link Desktop} if supported, or a manual
 * platform-specific method if not.
 *
 * @param file
 *            The file to open.
 * @throws Exception
 *             if the file could not be opened.
 */
public static void openFile(final File file) throws Exception {
	final Desktop desktop = Desktop.isDesktopSupported() ? Desktop
			.getDesktop() : null;
	if ((desktop != null) && desktop.isSupported(Desktop.Action.OPEN)) {
		desktop.open(file);
	} else {
		final OperatingSystem system = Utils.getPlatform();
		switch (system) {
		case MAC:
		case WINDOWS:
			Utils.openURL(file.toURI().toURL());
			break;
		default:
			final String fileManager = Utils.findSupportedProgram(
					"file manager", Utils.FILE_MANAGERS);
			Runtime.getRuntime().exec(
					new String[] { fileManager, file.getAbsolutePath() });
			break;
		}
	}
}
 
Example 3
Source File: UiExceptionHandler.java    From magarena with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Displays a message to user in the event an unexpected exception occurs.
 * User can open logs folder and/or Issue tracker directly from this dialog.
 */
private static void doNotifyUser() {
    try {

        // By specifying a frame the JOptionPane will be shown in the taskbar.
        // Otherwise if the dialog is hidden it is easy to forget it is still open.
        final JFrame frame = new JFrame(MText.get(_S1));
        frame.setUndecorated(true);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

        String prompt = MText.get(_S2);
        if (Desktop.isDesktopSupported()) {
            prompt += String.format("\n\n%s\n%s", MText.get(_S3), MText.get(_S4));
            final int action = JOptionPane.showConfirmDialog(frame, prompt, MText.get(_S1), JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE, null);
            if (action == JOptionPane.YES_OPTION) {
                DesktopHelper.tryOpen(MagicFileSystem.getDataPath(MagicFileSystem.DataPath.LOGS).toFile());
            }
        } else {
            JOptionPane.showMessageDialog(frame, prompt, MText.get(_S1), JOptionPane.ERROR_MESSAGE);
        }

    } catch (Exception e) {
        // do nothing - crash report has already been generated and app is about to exit anyway.
    }
}
 
Example 4
Source File: Utils.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Opens the file manager to the given location.
 * If the location is a file, it will be highlighted if possible.
 * @param file the file or directory
 */
public static void openInFileManager(File file) throws IOException {
	File f = file;

	// try to highlight the file (platform-specific)
	if (f.isFile()) {
		String osName = System.getProperty("os.name");
		if (osName.startsWith("Win")) {
			// windows: select in Explorer
			Runtime.getRuntime().exec("explorer.exe /select," + f.getAbsolutePath());
			return;
		} else if (osName.startsWith("Mac")) {
			// mac: reveal in Finder
			Runtime.getRuntime().exec("open -R " + f.getAbsolutePath());
			return;
		}
		f = f.getParentFile();
	}

	// open directory using Desktop API
	if (f.isDirectory()) {
		if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN))
			Desktop.getDesktop().open(f);
	}
}
 
Example 5
Source File: LinktoURL.java    From CPE552-Java with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    URI uri = new URI("http://www.nytimes.com");
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        desktop.browse(uri);
    }
}
 
Example 6
Source File: DesktopServices.java    From Open-Lowcode with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Tries to open a web page in the desktop default browser. In its current
 * version, it will likely only work correctly on windows.
 * 
 * @param uri a valid URI / URL for the browser (e.g. https://openlowcode.com/
 *            ).
 */
public static void launchBrowser(String uri) {
	if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
		try {
			Desktop.getDesktop().browse(new URI(uri));
		} catch (Exception e) {
			logger.severe("Exception while trying to open site " + uri + " " + e.getClass().toString() + " "
					+ e.getMessage());
		}
	} else {
		logger.severe("Java Desktop services not supported on this platform");
	}
}
 
Example 7
Source File: OsUtil.java    From proxyee-down with Apache License 2.0 5 votes vote down vote up
public static void openBrowse(String url) throws Exception {
  Desktop desktop = Desktop.getDesktop();
  boolean flag = Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE);
  if (flag) {
    try {
      URI uri = new URI(url);
      desktop.browse(uri);
    } catch (Exception e) {
      throw new Exception("can't open browse", e);
    }
  } else {
    throw new Exception("don't support browse");
  }
}
 
Example 8
Source File: DesktopUtil.java    From jeveassets with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isSupported(final Desktop.Action action) {
	if (Desktop.isDesktopSupported()) {
		Desktop desktop = Desktop.getDesktop();
		if (desktop.isSupported(action)) {
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: Utils.java    From aurous-app with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open a URL using java.awt.Desktop or a couple different manual methods
 *
 * @param url
 *            The URL to open
 * @throws Exception
 *             If an error occurs attempting to open the url
 */
public static void openURL(final URL url) throws Exception {
	final Desktop desktop = Desktop.isDesktopSupported() ? Desktop
			.getDesktop() : null;
	if ((desktop != null) && desktop.isSupported(Desktop.Action.BROWSE)) {
		desktop.browse(url.toURI());
	} else {
		final OperatingSystem system = Utils.getPlatform();
		switch (system) {
		case MAC:
			Class.forName("com.apple.eio.FileManager")
					.getDeclaredMethod("openURL",
							new Class[] { String.class })
					.invoke(null, new Object[] { url.toString() });
			break;
		case WINDOWS:
			Runtime.getRuntime()
					.exec(new String[] { "rundll32",
							"url.dll,FileProtocolHandler", url.toString() });
			break;
		default:
			final String browser = Utils.findSupportedProgram("browser",
					Utils.BROWSERS);
			Runtime.getRuntime().exec(
					new String[] { browser, url.toString() });
			break;
		}
	}
}
 
Example 10
Source File: HelpHandler.java    From PolyGlot with MIT License 5 votes vote down vote up
public static void openHelpLocal() throws IOException {
    File readmeDir = IOHandler.unzipResourceToTempLocation(PGTUtil.HELP_FILE_ARCHIVE_LOCATION);
    File readmeFile = new File(readmeDir.getAbsolutePath() + File.separator + PGTUtil.HELP_FILE_NAME);

    if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
        Desktop.getDesktop().browse(readmeFile.toURI());
    } else if (PGTUtil.IS_LINUX) {
        Desktop.getDesktop().open(readmeFile);
    } else {
        InfoBox.warning("Menu Warning", "Unable to open browser. Please load manually at:\n"
                + "http://draquet.github.io/PolyGlot/readme.html\n(copied to clipboard for convenience)", null);
        new ClipboardHandler().setClipboardContents("http://draquet.github.io/PolyGlot/readme.html");
    }
}
 
Example 11
Source File: HttpUrlUtil.java    From oim-fx with MIT License 5 votes vote down vote up
public static boolean open(String url) {
	boolean mark = true;
	try {
		java.net.URI uri = new java.net.URI(url);
		java.awt.Desktop d = java.awt.Desktop.getDesktop();
		if (Desktop.isDesktopSupported() && d.isSupported(java.awt.Desktop.Action.BROWSE)) {
			java.awt.Desktop.getDesktop().browse(uri);
		} else {
			browse(url);
		}
	} catch (Exception e) {
		mark = false;
	}
	return mark;
}
 
Example 12
Source File: CollectEarthUtils.java    From collect-earth with MIT License 5 votes vote down vote up
public static void openFolderInExplorer(String folder) throws IOException {
	if (Desktop.isDesktopSupported()) {
		Desktop.getDesktop().open(new File(folder));
	}else{
		if (SystemUtils.IS_OS_WINDOWS){
			new ProcessBuilder("explorer.exe", "/open," + folder).start(); //$NON-NLS-1$ //$NON-NLS-2$
		}else if (SystemUtils.IS_OS_MAC){
			new ProcessBuilder("usr/bin/open", folder).start(); //$NON-NLS-1$ //$NON-NLS-2$
		}else if ( SystemUtils.IS_OS_UNIX){
			tryUnixFileExplorers(folder);
		}
	}
}
 
Example 13
Source File: Utils.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static boolean isBrowsingSupported() {
	if (!Desktop.isDesktopSupported()) {
		return false;
	}
	boolean result = false;
	Desktop desktop = java.awt.Desktop.getDesktop();
	if (desktop.isSupported(Desktop.Action.BROWSE)) {
		result = true;
	}
	return result;

}
 
Example 14
Source File: FakeAppletContext.java    From 07kit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void showDocument(URL url, String target) {
    if (Desktop.isDesktopSupported()) {
        try {
            Desktop.getDesktop().browse(url.toURI());
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
 
Example 15
Source File: DownloadView.java    From megabasterd with GNU General Public License v3.0 5 votes vote down vote up
private void open_folder_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_open_folder_buttonActionPerformed

        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().open(new File(_download.getDownload_path() + "/" + _download.getFile_name()).getParentFile());
            } catch (Exception ex) {
                LOG.log(Level.INFO, ex.getMessage());
            }
        }

    }
 
Example 16
Source File: ReportServer.java    From RepoSense with MIT License 5 votes vote down vote up
/**
 * Launches the default browser with {@code url}.
 */
private static void launchBrowser(String url) throws IOException {
    try {
        if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
            Desktop.getDesktop().browse(new URI(url));
            logger.info("Loading " + url + " on the default browser...");
        } else {
            logger.severe("Browser could not be launched. Please refer to the user guide to"
                    + " manually view the report");
        }
    } catch (URISyntaxException ue) {
        logger.log(Level.SEVERE, ue.getMessage(), ue);
    }
}
 
Example 17
Source File: DesktopHandler.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Initialize the Mac-specific properties.
 * Create an ApplicationAdapter to listen for Help, Prefs, and Quit.
 */
public static void initialize()
{
	if (initialized)
	{
		return;
	}
	initialized = true;

	if (!Desktop.isDesktopSupported())
	{
		return;
	}

	Desktop theDesktop = Desktop.getDesktop();
	if (theDesktop.isSupported(Action.APP_ABOUT))
	{
		theDesktop.setAboutHandler(new AboutHandler());
	}
	if (theDesktop.isSupported(Action.APP_PREFERENCES))
	{
		theDesktop.setPreferencesHandler(new PreferencesHandler());
	}
	if (theDesktop.isSupported(Action.APP_QUIT_HANDLER))
	{
		theDesktop.setQuitHandler(new QuitHandler());
	}
}
 
Example 18
Source File: NotifyUpdate.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Open the homepage when the label is clicked.
 * @param evt 
 */
private void clickLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clickLabelMouseClicked
    if(!Desktop.isDesktopSupported())
        return;
    try {
        NULogger.getLogger().log(Level.INFO, "{0}: Link clicked.. Opening the homepage..", getClass().getName());
        Desktop.getDesktop().browse(new URI("http://neembuu.com/uploader/downloads.html"));
        // http://neembuuuploader.sourceforge.net/
    } catch (Exception ex) {
        NULogger.getLogger().severe(ex.toString());
    }
}
 
Example 19
Source File: IOFunctions.java    From symja_android_library with GNU General Public License v3.0 4 votes vote down vote up
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
	if (Desktop.isDesktopSupported()) {
		IAST dialogNoteBook = null;
		if (ast.isAST2() && ast.arg2().isAST(F.DialogNotebook, 2)) {
			dialogNoteBook = (IAST) ast.arg2();
		} else if (ast.isAST1() && ast.arg1().isAST(F.DialogNotebook, 2)) {
			dialogNoteBook = (IAST) ast.arg1();
		}

		IAST list;
		if (dialogNoteBook == null) {
			if (ast.isAST1()) {
				if (ast.arg1().isList()) {
					list = (IAST) ast.arg1();
				} else {
					list = F.List(ast.arg1());
				}
			} else {
				return F.NIL;
			}
		} else {
			if (dialogNoteBook.arg1().isList()) {
				list = (IAST) dialogNoteBook.arg1();
			} else {
				list = F.List(dialogNoteBook.arg1());
			}
		}

		JDialog dialog = new JDialog();
		dialog.setTitle("DialogInput");
		dialog.setSize(320, 200);
		dialog.setModal(true);
		// dialog.setLayout(new FlowLayout(FlowLayout.LEFT));
		// dialog.setLayout(new GridLayout(list.argSize(), 1));
		IExpr[] result = new IExpr[] { F.NIL };
		if (addComponents(dialog, list, dialog, engine, result)) {
			dialog.setVisible(true);
			if (result[0].isPresent()) {
				return result[0];
			}
		}
	}
	return F.NIL;
}
 
Example 20
Source File: ExceptionAlert.java    From Recaf with MIT License 4 votes vote down vote up
private ExceptionAlert(Throwable t, String msg) {
	super(AlertType.ERROR);
	setTitle("An error occurred");
	String header = t.getMessage();
	if(header == null)
		header = "(" + translate("ui.noerrormsg") + ")";
	setHeaderText(header);
	setContentText(msg);
	// Get exception text
	StringWriter sw = new StringWriter();
	PrintWriter pw = new PrintWriter(sw);
	t.printStackTrace(pw);
	String exText = sw.toString();
	// Create expandable Exception.
	Label lbl = new Label("Exception stacktrace:");
	TextArea txt = new TextArea(exText);
	txt.setEditable(false);
	txt.setWrapText(false);
	txt.setMaxWidth(Double.MAX_VALUE);
	txt.setMaxHeight(Double.MAX_VALUE);
	Hyperlink link = new Hyperlink("[Bug report]");
	link.setOnAction(e -> {
		try {
			// Append suffix to bug report url for the issue title
			String suffix = t.getClass().getSimpleName();
			if (t.getMessage() != null)
				suffix = suffix + ": " + t.getMessage();
			suffix = URLEncoder.encode(suffix, "UTF-8");
			// Open link in default browser
			Desktop.getDesktop().browse(URI.create(BUG_REPORT_LINK + suffix));
		} catch(IOException exx) {
			error(exx, "Failed to open bug report link");
			show(exx, "Failed to open bug report link.");
		}
	});
	TextFlow prompt = new TextFlow(new Text(translate("ui.openissue")), link);
	GridPane.setVgrow(txt, Priority.ALWAYS);
	GridPane.setHgrow(txt, Priority.ALWAYS);
	GridPane grid = new GridPane();
	grid.setMaxWidth(Double.MAX_VALUE);
	grid.add(lbl, 0, 0);
	grid.add(txt, 0, 1);
	// If desktop is supported, allow click-able bug report link
	if (Desktop.isDesktopSupported())
		grid.add(prompt, 0, 2);
	getDialogPane().setExpandableContent(grid);
	getDialogPane().setExpanded(true);
	// Set icon
	Stage stage = (Stage) getDialogPane().getScene().getWindow();
	stage.getIcons().add(new Image(resource("icons/error.png")));
}