SystemTray/test/dorkbox/TestTrayJavaFX.java

257 lines
8.8 KiB
Java
Raw Normal View History

/*
* Copyright 2015 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox;
import java.awt.event.ActionListener;
import java.io.IOException;
2016-09-22 16:18:50 +02:00
import java.net.URL;
import dorkbox.systemTray.Checkbox;
import dorkbox.systemTray.Menu;
import dorkbox.systemTray.MenuItem;
import dorkbox.systemTray.Separator;
import dorkbox.systemTray.SystemTray;
2017-07-15 18:25:46 +02:00
import dorkbox.util.CacheUtil;
import dorkbox.util.Desktop;
import dorkbox.util.JavaFX;
import dorkbox.util.OS;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* Icons from 'SJJB Icons', public domain/CC0 icon set
*
* Needs JavaFX to run. NOTE: JavaFX on Mac (Java7) has many bugs when also used with AWT. This class does NOT extend 'Application'
* (javafx) class on purpose, so that we can work around those issues
*/
public
class TestTrayJavaFX {
public static final URL BLUE_CAMPING = TestTray.class.getResource("accommodation_camping.glow.0092DA.32.png");
public static final URL BLACK_FIRE = TestTray.class.getResource("amenity_firestation.p.000000.32.png");
public static final URL BLACK_MAIL = TestTray.class.getResource("amenity_post_box.p.000000.32.png");
public static final URL GREEN_MAIL = TestTray.class.getResource("amenity_post_box.p.39AC39.32.png");
public static final URL BLACK_BUS = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
public static final URL LT_GRAY_BUS = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
public static final URL BLACK_TRAIN = TestTray.class.getResource("transport_train_station.p.000000.32.png");
public static final URL GREEN_TRAIN = TestTray.class.getResource("transport_train_station.p.39AC39.32.png");
public static final URL LT_GRAY_TRAIN = TestTray.class.getResource("transport_train_station.p.666666.32.png");
private static TestTrayJavaFX testTrayJavaFX;
public static
class MyApplication extends Application {
public
MyApplication() {
}
@Override
public
void start(final Stage stage) throws Exception {
if (testTrayJavaFX == null) {
testTrayJavaFX = new TestTrayJavaFX();
}
testTrayJavaFX.doJavaFxStuff(stage);
}
}
public static
void main(String[] args) {
if (OS.isMacOsX() && OS.javaVersion <= 7) {
System.setProperty("javafx.macosx.embedded", "true");
java.awt.Toolkit.getDefaultToolkit();
}
testTrayJavaFX = new TestTrayJavaFX();
Application application = new MyApplication();
2016-04-05 14:05:53 +02:00
// make sure JNA jar is on the classpath!
application.launch(MyApplication.class);
}
private SystemTray systemTray;
private ActionListener callbackGray;
public
TestTrayJavaFX() {
}
public
void doJavaFxStuff(final Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
2017-07-15 18:25:46 +02:00
CacheUtil.clear(); // for test apps, make sure the cache is always reset. You should never do this in production.
2017-07-14 00:46:12 +02:00
// SwingUtil.setLookAndFeel(null); // set Native L&F (this is the System L&F instead of CrossPlatform L&F)
2017-07-14 00:46:12 +02:00
// SystemTray.SWING_UI = new CustomSwingUI();
this.systemTray = SystemTray.get();
if (systemTray == null) {
throw new RuntimeException("Unable to load SystemTray!");
}
systemTray.setTooltip("Mail Checker");
systemTray.setImage(LT_GRAY_TRAIN);
systemTray.setStatus("No Mail");
callbackGray = new ActionListener() {
@Override
public
void actionPerformed(final java.awt.event.ActionEvent e) {
final MenuItem entry = (MenuItem) e.getSource();
systemTray.setStatus(null);
systemTray.setImage(BLACK_TRAIN);
entry.setCallback(null);
// systemTray.setStatus("Mail Empty");
systemTray.getMenu().remove(entry);
System.err.println("POW");
}
};
Menu mainMenu = systemTray.getMenu();
MenuItem greenEntry = new MenuItem("Green Mail", new ActionListener() {
@Override
public
void actionPerformed(final java.awt.event.ActionEvent e) {
final MenuItem entry = (MenuItem) e.getSource();
systemTray.setStatus("Some Mail!");
systemTray.setImage(GREEN_TRAIN);
2016-10-10 00:26:52 +02:00
entry.setCallback(callbackGray);
entry.setImage(BLACK_MAIL);
entry.setText("Delete Mail");
// systemTray.remove(menuEntry);
}
});
greenEntry.setImage(GREEN_MAIL);
// case does not matter
greenEntry.setShortcut('G');
mainMenu.add(greenEntry);
Checkbox checkbox = new Checkbox("Euro € Mail", new ActionListener() {
@Override
public
void actionPerformed(final java.awt.event.ActionEvent e) {
System.err.println("Am i checked? " + ((Checkbox) e.getSource()).getChecked());
}
});
checkbox.setShortcut('€');
mainMenu.add(checkbox);
mainMenu.add(new Separator());
mainMenu.add(new MenuItem("About", new ActionListener() {
@Override
public
void actionPerformed(final java.awt.event.ActionEvent e) {
try {
Desktop.browseURL("https://github.com/dorkbox/SystemTray");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}));
Menu submenu = new Menu("Options", BLUE_CAMPING);
submenu.setShortcut('t');
mainMenu.add(submenu);
MenuItem disableMenu = new MenuItem("Disable menu", BLACK_BUS, new ActionListener() {
@Override
public
void actionPerformed(final java.awt.event.ActionEvent e) {
MenuItem source = (MenuItem) e.getSource();
source.getParent().setEnabled(false);
}
});
submenu.add(disableMenu);
submenu.add(new MenuItem("Hide tray", LT_GRAY_BUS, new ActionListener() {
@Override
public
void actionPerformed(final java.awt.event.ActionEvent e) {
systemTray.setEnabled(false);
}
}));
submenu.add(new MenuItem("Remove menu", BLACK_FIRE, new ActionListener() {
@Override
public
void actionPerformed(final java.awt.event.ActionEvent e) {
MenuItem source = (MenuItem) e.getSource();
source.getParent().remove();
}
}));
systemTray.getMenu().add(new MenuItem("Quit", new ActionListener() {
@Override
public
void actionPerformed(final java.awt.event.ActionEvent e) {
systemTray.shutdown();
if (!JavaFX.isEventThread()) {
JavaFX.dispatch(new Runnable() {
@Override
public
void run() {
primaryStage.hide(); // must do this BEFORE Platform.exit() otherwise odd errors show up
Platform.exit(); // necessary to close javaFx
}
});
} else {
primaryStage.hide(); // must do this BEFORE Platform.exit() otherwise odd errors show up
Platform.exit(); // necessary to close javaFx
}
//System.exit(0); not necessary if all non-daemon threads have stopped.
}
})).setShortcut('q'); // case does not matter
}
}