下面列出了怎么用java.awt.Desktop.Action的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean isSupported(Action action) {
switch(action) {
case OPEN:
case EDIT:
case PRINT:
case MAIL:
case BROWSE:
case MOVE_TO_TRASH:
case APP_SUDDEN_TERMINATION:
case APP_EVENT_SYSTEM_SLEEP:
return true;
case APP_EVENT_USER_SESSION:
return isEventUserSessionSupported;
default:
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());
}
}
public static boolean openFile(File file) {
if (java.awt.Desktop.isDesktopSupported() && java.awt.Desktop.getDesktop().isSupported(Action.OPEN)) {
try {
java.awt.Desktop.getDesktop().open(file);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "IOException when opening file.", e);
return false;
}
return true;
}
if (OSIdentifier.IS_WINDOWS) {
return openFileWindows(file);
}
if (OSIdentifier.IS_LINUX) {
return openFileLinux(file);
}
if (OSIdentifier.IS_OSX) {
return openFileOSX(file);
}
return false;
}
/**
* View a URI in a browser.
*
* @param uri URI to display in browser.
* @throws IOException if browser can not be launched
*/
private static void viewInBrowser(URI uri) throws IOException
{
if (Desktop.isDesktopSupported() && DESKTOP.isSupported(Action.BROWSE))
{
DESKTOP.browse(uri);
}
else
{
Dialog<ButtonType> alert = GuiUtility.runOnJavaFXThreadNow(() -> new Alert(Alert.AlertType.WARNING));
Logging.debugPrint("unable to browse to " + uri);
alert.setTitle(LanguageBundle.getString("in_err_browser_err"));
alert.setContentText(LanguageBundle.getFormattedString("in_err_browser_uri", uri));
GuiUtility.runOnJavaFXThreadNow(alert::showAndWait);
}
}
/**
* View a URI in a browser.
*
* @param uri URI to display in browser.
* @throws IOException if browser can not be launched
*/
private static void viewInBrowser(URI uri) throws IOException
{
if (Desktop.isDesktopSupported() && DESKTOP.isSupported(Action.BROWSE))
{
DESKTOP.browse(uri);
}
else
{
Dialog<ButtonType> alert = GuiUtility.runOnJavaFXThreadNow(() -> new Alert(Alert.AlertType.WARNING));
Logging.debugPrint("unable to browse to " + uri);
alert.setTitle(LanguageBundle.getString("in_err_browser_err"));
alert.setContentText(LanguageBundle.getFormattedString("in_err_browser_uri", uri));
GuiUtility.runOnJavaFXThreadNow(alert::showAndWait);
}
}
public LinkEntry(int color, String text, String url, String failMsg, boolean localize) {
super(text, () -> {
if(!checkedDesktop) {
checkedDesktop = true;
if(Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
canBrowse = desktop.isSupported(Action.BROWSE);
}
}
if(canBrowse) {
// try to open the download link directly from the browser.
try {
URI uri = URI.create(url);
Game.setMenu(new TempDisplay(3000, false, true, new Menu.Builder(true, 0, RelPos.CENTER, new StringEntry(Localization.getLocalized(openMsg))).createMenu()));
desktop.browse(uri);
} catch(IOException e) {
System.err.println("Could not parse LinkEntry url \""+url+"\" into valid URI:");
e.printStackTrace();
canBrowse = false;
}
}
if(!canBrowse) {
Game.setMenu(new BookDisplay(failMsg, false));
}
}, localize);
this.color = color;
}
private static JButton actionButton(String text, Desktop.Action action, Runnable listener)
{
final JButton button = new JButton(text);
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(action)) {
button.addActionListener(e -> listener.run());
return button;
}
button.setEnabled(false);
return button;
}
/** Show download page. */
public void showDownloadPage() {
if (client != null) {
URL url = client.getDownloadURL();
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Action.BROWSE)) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(url.toURI());
} catch (IOException | URISyntaxException e) {
ConsoleManager.getInstance().exception(this, e);
}
}
}
}
/**
* 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());
}
}
/**
* 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 browse(String uri) {
try {
if (java.awt.Desktop.isDesktopSupported()) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
if (desktop.isSupported(Action.BROWSE)) {
desktop.browse(new URI(uri));
}
}
} catch (Exception e) {
log.error("Browse URI failed", e);
}
}
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported.
// Though we don't really differentiate between OPEN / EDIT
return true;
}
@Override
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported on windows.
return true;
}
public boolean isSupported(Action type) {
return supportedActions.contains(type);
}
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported.
// Though we don't really differentiate between OPEN / EDIT
return true;
}
@Override
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported on windows.
return true;
}
public boolean isSupported(Action type) {
return supportedActions.contains(type);
}
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported.
// Though we don't really differentiate between OPEN / EDIT
return true;
}
@Override
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported on windows.
return true;
}
public boolean isSupported(Action type) {
return supportedActions.contains(type);
}
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported.
// Though we don't really differentiate between OPEN / EDIT
return true;
}
@Override
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported on windows.
return true;
}
public boolean isSupported(Action type) {
return supportedActions.contains(type);
}
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported.
// Though we don't really differentiate between OPEN / EDIT
return true;
}
@Override
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported on windows.
return true;
}
public boolean isSupported(Action type) {
return supportedActions.contains(type);
}
public boolean isSupported(Action type) {
return supportedActions.contains(type);
}
@Override
public boolean isSupported(Action action) {
return true;
}
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported.
// Though we don't really differentiate between OPEN / EDIT
return true;
}
@Override
public boolean isSupported(Action action) {
// OPEN, EDIT, PRINT, MAIL, BROWSE all supported on windows.
return true;
}