SystemTray/test/dorkbox/TestReAddTray.java

90 lines
2.8 KiB
Java

/*
* Copyright 2021 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.net.URL;
import dorkbox.systemTray.MenuItem;
import dorkbox.systemTray.SystemTray;
import dorkbox.util.CacheUtil;
/**
* Icons from 'SJJB Icons', public domain/CC0 icon set
*/
public
class TestReAddTray {
public static final URL LT_GRAY_TRAIN = TestReAddTray.class.getResource("transport_train_station.p.666666.32.png");
public static
void main(String[] args) {
// make sure JNA jar is on the classpath!
new TestReAddTray();
}
private SystemTray systemTray;
public
TestReAddTray() {
SystemTray.DEBUG = true; // for test apps, we always want to run in debug mode
// for test apps, make sure the cache is always reset. These are the ones used, and you should never do this in production.
CacheUtil.clear("SysTrayExample");
// SwingUtil.setLookAndFeel(null); // set Native L&F (this is the System L&F instead of CrossPlatform L&F)
// SystemTray.SWING_UI = new CustomSwingUI();
this.systemTray = SystemTray.get("SysTrayExample");
if (systemTray == null) {
throw new RuntimeException("Unable to load SystemTray!");
}
systemTray.setTooltip("Mail Checker");
systemTray.setImage(LT_GRAY_TRAIN);
systemTray.setStatus("No Mail");
systemTray.getMenu().add(new MenuItem("Quit", e->{
systemTray.shutdown();
//System.exit(0); not necessary if all non-daemon threads have stopped.
})).setShortcut('q'); // case does not matter
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("Removing then adding new tray");
systemTray.remove();
systemTray = SystemTray.get("SysTrayExample");
if (systemTray == null) {
throw new RuntimeException("Unable to load SystemTray!");
}
systemTray.setTooltip("Mail Checker");
systemTray.setImage(LT_GRAY_TRAIN);
systemTray.setStatus("SUPER Mail");
systemTray.getMenu().add(new MenuItem("Quit", e->{
systemTray.shutdown();
//System.exit(0); not necessary if all non-daemon threads have stopped.
})).setShortcut('q'); // case does not matter
}
}