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

The following examples show how to use java.awt.Desktop#isSupported() . 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: WebBrowser.java    From DeconvolutionLab2 with GNU General Public License v3.0 6 votes vote down vote up
public static boolean open(String url) {
	Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
	if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
		try {
			desktop.browse(new URL(url).toURI());
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	JFrame frame = new JFrame("Help");
	JLabel lbl = new JLabel(url);
	frame.add(lbl);
	frame.pack();
	frame.setVisible(true);	
	return false;
}
 
Example 2
Source File: KaramelServiceApplication.java    From karamel with Apache License 2.0 6 votes vote down vote up
public synchronized 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 (Exception e) {
      e.printStackTrace();
    }
  } else {
    logger.error("Brower UI could not be launched using Java's Desktop library. "
      + "Are you running a window manager?");
    logger.error("If you are using Ubuntu, try: sudo apt-get install libgnome");
    logger.error("Retrying to launch the browser now using a different method.");
    BareBonesBrowserLaunch.openURL(uri.toASCIIString());
  }
}
 
Example 3
Source File: LinkBrowser.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static boolean attemptDesktopBrowse(String url)
{
	if (!Desktop.isDesktopSupported())
	{
		return false;
	}

	final Desktop desktop = Desktop.getDesktop();

	if (!desktop.isSupported(Desktop.Action.BROWSE))
	{
		return false;
	}

	try
	{
		desktop.browse(new URI(url));
		return true;
	}
	catch (IOException | URISyntaxException ex)
	{
		log.warn("Failed to open Desktop#browse {}", url, ex);
		return false;
	}
}
 
Example 4
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 5
Source File: ShowLibraryFolderCommand.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent event) {
    try {
        File    dir     = mLibrary.getPath().toFile();
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Action.BROWSE_FILE_DIR)) {
            File[] contents = dir.listFiles();
            if (contents != null && contents.length > 0) {
                Arrays.sort(contents);
                dir = contents[0];
            }
            desktop.browseFileDirectory(dir.getCanonicalFile());
        } else {
            desktop.open(dir);
        }
    } catch (Exception exception) {
        WindowUtils.showError(null, exception.getMessage());
    }
}
 
Example 6
Source File: View.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
public static boolean navigateUrl(String url) {
    if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                URI uri = new URI(url);
                desktop.browse(uri);
                return true;
            } catch (URISyntaxException | IOException ex) {
                Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    return false;
}
 
Example 7
Source File: MenuSupportAction.java    From HBaseClient with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onClick(ActionEvent arg0)
{
    try
    {
        URI     v_URI     = URI.create(AppMain.$SourceCode);
        Desktop v_Desktop = Desktop.getDesktop();
        
        // 判断系统桌面是否支持要执行的功能
        if ( v_Desktop.isSupported(Desktop.Action.BROWSE) )
        {
            // 获取系统默认浏览器打开链接
            v_Desktop.browse(v_URI);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
 
Example 8
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 9
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 10
Source File: Utils.java    From Game with GNU General Public License v3.0 5 votes vote down vote up
public static void openWebpage(final String url) {
	final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
	if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
		try {
			desktop.browse(new URL(url).toURI());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
Example 11
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 12
Source File: SharingHelper.java    From jeddict with Apache License 2.0 5 votes vote down vote up
public static void openWebpage(String url) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(URI.create(url));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example 13
Source File: UpdatePrompt.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
@CalledOnlyBy(AmidstThread.EDT)
private void openURL(URI uri) throws IOException, UnsupportedOperationException {
	if (Desktop.isDesktopSupported()) {
		Desktop desktop = Desktop.getDesktop();
		if (desktop.isSupported(Desktop.Action.BROWSE)) {
			desktop.browse(uri);
		} else {
			throw new UnsupportedOperationException("Unable to open browser page.");
		}
	} else {
		throw new UnsupportedOperationException("Unable to open browser.");
	}
}
 
Example 14
Source File: Utils.java    From datasync with MIT License 5 votes vote down vote up
/**
 * Open given uri in local web browser
 * @param uri to open in browser
 */
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 (Exception e) {
            System.out.println("Error: cannot open web page");
        }
    }
}
 
Example 15
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 16
Source File: AppUpdateRunner.java    From rest-client with Apache License 2.0 5 votes vote down vote up
private void openUrl(String url) {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(new URI(url));
        }
        catch(URISyntaxException | IOException ex) {
            LOG.log(Level.INFO, "Error when opening browser", ex);
        }
    }
}
 
Example 17
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 18
Source File: CreditsDialog.java    From WorldGrower with GNU General Public License v3.0 5 votes vote down vote up
private 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) {
            throw new IllegalStateException("Problem opening " + uri.toString(), e);
        }
    }
}
 
Example 19
Source File: GenerateToken.java    From socialauth with MIT License 4 votes vote down vote up
private void getAccessToken() throws Exception {
	SocialAuthConfig config = SocialAuthConfig.getDefault();
	config.load();
	SocialAuthManager manager = new SocialAuthManager();
	manager.setSocialAuthConfig(config);

	URL aURL = new URL(successURL);
	host = aURL.getHost();
	port = aURL.getPort();
	port = port == -1 ? 80 : port;
	callbackPath = aURL.getPath();

	if (tokenFilePath == null) {
		tokenFilePath = System.getProperty("user.home");
	}
	String url = manager.getAuthenticationUrl(providerId, successURL);
	startServer();

	if (Desktop.isDesktopSupported()) {
		Desktop desktop = Desktop.getDesktop();
		if (desktop.isSupported(Action.BROWSE)) {
			try {
				desktop.browse(URI.create(url));
				// return;
			} catch (IOException e) {
				// handled below
			}
		}
	}

	lock.lock();
	try {
		while (paramsMap == null && error == null) {
			gotAuthorizationResponse.awaitUninterruptibly();
		}
		if (error != null) {
			throw new IOException("User authorization failed (" + error
					+ ")");
		}
	} finally {
		lock.unlock();
	}
	stop();

	AccessGrant accessGrant = manager.createAccessGrant(providerId,
			paramsMap, successURL);

	Exporter.exportAccessGrant(accessGrant, tokenFilePath);

	LOG.info("Access Grant Object saved in a file  :: " + tokenFilePath
			+ File.separatorChar + accessGrant.getProviderId()
			+ "_accessGrant_file.txt");
}
 
Example 20
Source File: DesktopSetup.java    From mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void run() {

  logger.finest("Configuring desktop settings");

  // Set basic desktop handlers
  final Desktop awtDesktop = Desktop.getDesktop();
  if (awtDesktop != null) {

    // Setup About handler
    if (awtDesktop.isSupported(Desktop.Action.APP_ABOUT)) {
      awtDesktop.setAboutHandler(e -> {
        MZmineGUI.showAboutWindow();
      });
    }

    // Setup Quit handler
    if (awtDesktop.isSupported(Desktop.Action.APP_QUIT_HANDLER)) {
      awtDesktop.setQuitHandler((e, response) -> {
        ExitCode exitCode = MZmineCore.getDesktop().exitMZmine();
        if (exitCode == ExitCode.OK)
          response.performQuit();
        else
          response.cancelQuit();
      });
    }
  }

  if (Taskbar.isTaskbarSupported()) {

    final Taskbar taskBar = Taskbar.getTaskbar();

    // Set the main app icon
    if ((mzMineIcon != null) && taskBar.isSupported(Taskbar.Feature.ICON_IMAGE)) {
      final java.awt.Image mzMineIconAWT = SwingFXUtils.fromFXImage(mzMineIcon, null);
      taskBar.setIconImage(mzMineIconAWT);
    }

    // Add a task controller listener to show task progress
    MZmineCore.getTaskController().addTaskControlListener((numOfWaitingTasks, percentDone) -> {
      if (numOfWaitingTasks > 0) {
        if (taskBar.isSupported(Taskbar.Feature.ICON_BADGE_NUMBER)) {
          String badge = String.valueOf(numOfWaitingTasks);
          taskBar.setIconBadge(badge);
        }

        if (taskBar.isSupported(Taskbar.Feature.PROGRESS_VALUE))
          taskBar.setProgressValue(percentDone);

      } else {

        if (taskBar.isSupported(Taskbar.Feature.ICON_BADGE_NUMBER))
          taskBar.setIconBadge(null);
        /*
         * if (taskBar.isSupported( Taskbar.Feature.PROGRESS_STATE_WINDOW))
         * taskBar.setWindowProgressState( MZmineCore.getDesktop().getMainWindow(),
         * Taskbar.State.OFF);
         */
        if (taskBar.isSupported(Taskbar.Feature.PROGRESS_VALUE))
          taskBar.setProgressValue(-1);
        /*
         * if (taskBar.isSupported( Taskbar.Feature.PROGRESS_VALUE_WINDOW))
         * taskBar.setWindowProgressValue( MZmineCore.getDesktop().getMainWindow(), -1);
         */
      }
    });

  }

  // Let the OS decide the location of new windows. Otherwise, all windows
  // would appear at the top left corner by default.
  // TODO: investigate if this applies to JavaFX windows
  System.setProperty("java.awt.Window.locationByPlatform", "true");

}