下面列出了java.awt.Desktop#isSupported ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
* 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;
}
}
}
@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();
}
}
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;
}
@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());
}
}
/**
* 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;
}
}
}
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;
}
}
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());
}
}
/**
* 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");
}
}
}
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);
}
}
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();
}
}
}
/**
* 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());
}
}
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();
}
}
}
@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.");
}
}
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;
}
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);
}
}
}
private static boolean isSupported(final Desktop.Action action) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(action)) {
return true;
}
}
return false;
}
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);
}
}
}
@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");
}
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");
}