You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// TODO log create URI exception rather than printing stack trace
privateMenuItemquit;
/************************************************************************** * * * Constructor(s) * * * *************************************************************************//** * Creates a system tray controller with the application's system tray icon * image set to the default system tray icon image. * * @throws UnsupportedOperationException if the system tray is not supported */publicSystemTrayController() throwsUnsupportedOperationException {
this(null);
}
/** * Creates a system tray controller and sets the application's system tray * icon image to the image loaded from the specified image url. * * @param iconImageUrl - the url to load the tray icon's image from, or * {@code null} if the default system tray icon image should be used * @throws UnsupportedOperationException if the system tray is not supported */publicSystemTrayController(URLiconImageUrl) throwsUnsupportedOperationException {
// ensure awt toolkit is initialized.java.awt.Toolkit.getDefaultToolkit();
// assign system tray or throw if not supportedtray = getSystemTrayOrThrowIfNotSupported();
// listen for app raised to foreground/moved to background eventsDesktop.getDesktop().addAppEventListener(newAppFocusListener());
// load specified icon image or use the default toolkit icon imagedefIcon = loadIconImageOrUseDefault(iconImageUrl);
// create tray icon and add it to the system trayinitTrayIcon();
addAppTrayIconToSystemTray();
}
/************************************************************************** * * * Public API * * * *************************************************************************//** * Displays a system tray notification to the user. * @param caption - the notification caption * @param text - the notification detail text * @param type - the type of notification * @see MessageType */publicvoidnotifyUser(Stringcaption, Stringtext, MessageTypetype) {
runLater(() -> trayIcon.displayMessage(caption, text, type));
}
/** * Shuts down the system tray if it's supported, otherwise calling this * method is a no-op. */publicvoidshutdown() {
if (trayIcon == null) { return; }
System.out.println("SystemTrayController: closing");
runLater(() -> {
Platform.exit();
tray.remove(trayIcon);
});
}
/** * Specifies the system tray icon's tool-tip message. * @param tip - the icon's tool-tip message to display */publicvoidsetToolTip(Stringtip) {
if (trayIcon == null) { return; }
runLater(() -> trayIcon.setToolTip(tip));
}
/** * Sets a status string at the first position in the context menu. * <p> * This status string appears as a disabled menu entry. * @param status - the string to display at the first position in the * context menu * @see SystemTray#setStatus(String) */publicvoidsetStatus(Stringstatus) {
if (trayIcon == null) { return; }
// javax.swing.SwingUtilities.invokeLater(() -> trayIcon.get().);
}
/** * Specifies the image to use for the system tray icon. * @param imageUrl - image to use for the system tray icon * @see SystemTray#setImage(URL) */publicvoidsetIconImage(URLimageUrl) {
if (trayIcon == null) { return; }
runLater(() -> {
defIcon = loadIconImageOrUseDefault(imageUrl);
trayIcon.setImage(defIcon);
});
}
/** * Specifies the URL of the website to open in a browser when the 'About' * menu item is selected. * @param url - the website URL to open when the 'About' menu item is * selected */publicvoidsetAboutBrowserUrl(Stringurl) {
runLater(() -> {
try {
aboutUri = URI.create(url);
} catch (Exceptione) {
// TODO log create URI exception rather than printing stack tracee.printStackTrace();
}
});
}
/** * Specifies the enabled state of the 'Run Scraper' menu item. * @param enabled - the enabled state of the 'Run Scraper' menu item */publicvoidsetRunScraperEnabled(booleanenabled) {
runLater(() -> runScraper.setEnabled(enabled));
}
/** * Sets the about tray icon menu item selected action listener. * @param listener - the about selected listener */publicvoidsetOnAboutSelected(ActionListenerlistener) {
setMenuItemActionListener(about, listener);
}
/** * Sets the run scraper tray icon menu item selected action listener. * @param listener - the run scraper selected listener */publicvoidsetOnRunScraperSelected(ActionListenerlistener) {
setMenuItemActionListener(runScraper, listener);
}
/** * Sets the dashboard tray icon menu item selected action listener. * @param listener - the dashboard selected listener */publicvoidsetOnDashboardSelected(ActionListenerlistener) {
setMenuItemActionListener(dashboard, listener);
}
/** * Sets the settings tray icon menu item selected action listener. * @param listener - the settings selected listener */publicvoidsetOnSettingsSelected(ActionListenerlistener) {
setMenuItemActionListener(settings, listener);
}
/** * Sets the view logs tray icon menu item selected action listener. * @param listener - the view logs selected listener */publicvoidsetOnViewLogsSelected(ActionListenerlistener) {
setMenuItemActionListener(logs, listener);
}
/** * Sets the quit tray icon menu item selected action listener. * <p> * The quit tray icon menu item has a preset action listener that handles * exiting of the application, therefore changing the action listener * should rarely, if ever, be needed. * @param listener - the quit selected listener */publicvoidsetOnQuitSelected(ActionListenerlistener) {
setMenuItemActionListener(quit, listener);
}
/************************************************************************** * * * Private API * * * *************************************************************************//** Gets the system tray or throws an UnsupportedOperationException if the * system tray is not supported. */privateSystemTraygetSystemTrayOrThrowIfNotSupported() {
UnsupportedOperationExceptionuoe = newUnsupportedOperationException("system tray not supported.");
if (!SystemTray.isSupported()) { throwuoe; }
SystemTraytmp = null;
try {
tmp = SystemTray.getSystemTray();
} catch (Exceptione) {
throwuoe;
}
returntmp;
}
/** Initializes the tray icon. */privatevoidinitTrayIcon() {
trayIcon = newTrayIcon(defIcon, Main.APP_NAME);
// allow the system to auto-size the tray icontrayIcon.setImageAutoSize(true);
// set the tray icon's tool-tip// trayIcon.get().setToolTip("");// add notification click listener to tray icontrayIcon.setActionCommand("notification-clicked");
trayIcon.addActionListener(this::onNotificationClicked);
// create and set tray icon's popup menutrayIcon.setPopupMenu(createPopupMenu());
}
/** * Helper that loads the specified icon image url or uses the default * toolkit icon image. */privateImageloadIconImageOrUseDefault(URLiconImageUrl) {
Imageicon = null;
if (iconImageUrl != null) {
try {
icon = Toolkit.getDefaultToolkit().createImage(iconImageUrl);
} catch (Exceptione) {
// TODO log if an exception occurred while loading the icon imageicon = Taskbar.getTaskbar().getIconImage();
}
} else {
icon = Taskbar.getTaskbar().getIconImage();
}
returnicon;
}
/** Adds application tray icon to system tray. */privatevoidaddAppTrayIconToSystemTray() {
try {
tray.add(trayIcon);
} catch (AWTExceptione) {
// TODO log tray add exception rather than printing stack tracee.printStackTrace();
}
}
/** * Handles clicking actions on notifications. * @param action - the clicking action to handle */privatevoidonNotificationClicked(ActionEventaction) {
System.out.println(action.getActionCommand());
System.out.println("Notification Clicked");
}
/** Listeners for app raised to foreground/moved to background events. */privateclassAppFocusListenerimplementsAppForegroundListener {
@OverridepublicvoidappRaisedToForeground(AppForegroundEvente) {
System.out.println("SystemTrayController: app moved to foreground");
}
@OverridepublicvoidappMovedToBackground(AppForegroundEvente) {
System.out.println("SystemTrayController: app moved to background");
}
} // class AppFocusListener/** Convenience helper that invokes {@code SwingUtils.invokeLater(toRun)}. */privatevoidrunLater(finalRunnabletoRun) {
javax.swing.SwingUtilities.invokeLater(toRun);
}
/** Helper method used by setOn*Selected methods. */privatevoidsetMenuItemActionListener(MenuItemmenuItem, ActionListenerlistener) {
runLater(() -> {
// remove any action listeners from menu itemfor (ActionListeneral : menuItem.getActionListeners()) { menuItem.removeActionListener(al); }
// add specified action listener to menu itemmenuItem.addActionListener(listener);
});
}
/************************************************************************** * * * Popup Menu Creation * * * *************************************************************************//** Creates the menu items and lays out the tray icon's popup menu. */privatePopupMenucreatePopupMenu() {
// init the menu items before adding them to the menuinitMenuItems();
PopupMenumenu = newPopupMenu();
menu.add(about);
menu.add(runScraper);
menu.addSeparator();
menu.add(dashboard);
menu.add(settings);
menu.add(logs);
menu.addSeparator();
menu.add(quit);
returnmenu;
}
/** Initializes the popup menu's items. */privatevoidinitMenuItems() {
runScraper = newMenuItem("Run Scraper");
dashboard = newMenuItem("Dashboard");
settings = newMenuItem("Settings");
logs = newMenuItem("View Logs");
// about and quit have default action listenersinitAboutMenuItem();
initQuitMenuItem();
}
/** * Creates about menu item and sets its action listener to open a browser * to the application's website. */privatevoidinitAboutMenuItem() {
about = newMenuItem("About");
about.addActionListener(e -> {
System.out.println("About was selected");
try {
Desktop.getDesktop().browse(aboutUri);
} catch (IOExceptione1) {
// TODO log browser exception rather than print stack tracee1.printStackTrace();
}
});
}
/** * Creates quit menu item and sets its action listener to handle exiting of * the application and removal of the system tray icon. */privatevoidinitQuitMenuItem() {
quit = newMenuItem("Quit Bookie Scrape");
quit.addActionListener(e -> {
System.out.println("Quit was selected");
Platform.exit();
tray.remove(trayIcon);
});
}
} // class SystemTrayController
e193d924a7b355374d2167c04f99b0880b094c2d
The text was updated successfully, but these errors were encountered:
log create URI exception rather than printing stack trace
bookie-scrape/src/main/java/com/bookiescrape/app/sample/SystemTrayController.java
Line 168 in 0392c1e
e193d924a7b355374d2167c04f99b0880b094c2d
The text was updated successfully, but these errors were encountered: