2016-02-14 17:49:02 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2016-09-22 21:45:53 +02:00
|
|
|
import java.net.URL;
|
|
|
|
|
2016-02-14 17:49:02 +01:00
|
|
|
import org.eclipse.swt.SWT;
|
|
|
|
import org.eclipse.swt.widgets.Display;
|
|
|
|
import org.eclipse.swt.widgets.Shell;
|
|
|
|
import org.eclipse.swt.widgets.Text;
|
|
|
|
|
2016-10-10 00:26:52 +02:00
|
|
|
import dorkbox.systemTray.Entry;
|
2016-09-29 01:44:11 +02:00
|
|
|
import dorkbox.systemTray.Menu;
|
2016-09-22 21:45:53 +02:00
|
|
|
import dorkbox.systemTray.SystemTray;
|
|
|
|
import dorkbox.systemTray.SystemTrayMenuAction;
|
2016-02-14 17:49:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Icons from 'SJJB Icons', public domain/CC0 icon set
|
|
|
|
*
|
|
|
|
* Needs SWT to run
|
|
|
|
*/
|
|
|
|
public
|
|
|
|
class TestTraySwt {
|
|
|
|
|
2016-09-25 15:52:15 +02:00
|
|
|
public static final URL BLACK_MAIL = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
|
|
|
|
public static final URL GREEN_MAIL = TestTray.class.getResource("transport_bus_station.p.39AC39.32.png");
|
|
|
|
public static final URL LT_GRAY_MAIL = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
|
2016-02-14 17:49:02 +01:00
|
|
|
|
|
|
|
public static
|
|
|
|
void main(String[] args) {
|
2016-04-05 14:05:53 +02:00
|
|
|
// make sure JNA jar is on the classpath!
|
2016-09-22 21:45:53 +02:00
|
|
|
System.setProperty("SWT_GTK3", "0"); // Can change between GTK2 and GTK3
|
2016-02-15 00:55:03 +01:00
|
|
|
|
2016-02-14 17:49:02 +01:00
|
|
|
new TestTraySwt();
|
|
|
|
}
|
|
|
|
|
|
|
|
private SystemTray systemTray;
|
|
|
|
private SystemTrayMenuAction callbackGreen;
|
|
|
|
private SystemTrayMenuAction callbackGray;
|
|
|
|
|
|
|
|
public
|
|
|
|
TestTraySwt() {
|
2016-02-15 13:27:33 +01:00
|
|
|
final Display display = new Display ();
|
2016-02-14 21:54:21 +01:00
|
|
|
final Shell shell = new Shell(display);
|
2016-02-14 17:49:02 +01:00
|
|
|
|
|
|
|
Text helloWorldTest = new Text(shell, SWT.NONE);
|
2016-02-14 21:54:21 +01:00
|
|
|
helloWorldTest.setText("Hello World SWT ................. ");
|
2016-02-14 17:49:02 +01:00
|
|
|
helloWorldTest.pack();
|
|
|
|
|
|
|
|
|
2016-09-29 01:44:11 +02:00
|
|
|
this.systemTray = SystemTray.get();
|
2016-02-14 17:49:02 +01:00
|
|
|
if (systemTray == null) {
|
|
|
|
throw new RuntimeException("Unable to load SystemTray!");
|
|
|
|
}
|
|
|
|
|
2016-10-08 23:54:45 +02:00
|
|
|
this.systemTray.setImage(LT_GRAY_MAIL);
|
2016-02-14 17:49:02 +01:00
|
|
|
|
|
|
|
systemTray.setStatus("No Mail");
|
|
|
|
|
|
|
|
callbackGreen = new SystemTrayMenuAction() {
|
|
|
|
@Override
|
|
|
|
public
|
2016-10-10 00:26:52 +02:00
|
|
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
2016-02-14 17:49:02 +01:00
|
|
|
systemTray.setStatus("Some Mail!");
|
2016-10-08 23:54:45 +02:00
|
|
|
systemTray.setImage(GREEN_MAIL);
|
2016-02-15 13:27:33 +01:00
|
|
|
|
2016-10-10 00:26:52 +02:00
|
|
|
entry.setCallback(callbackGray);
|
|
|
|
entry.setImage(BLACK_MAIL);
|
|
|
|
entry.setText("Delete Mail");
|
2016-10-03 23:55:25 +02:00
|
|
|
// systemTray.remove(menuEntry);
|
2016-02-14 17:49:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
callbackGray = new SystemTrayMenuAction() {
|
|
|
|
@Override
|
|
|
|
public
|
2016-10-10 00:26:52 +02:00
|
|
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
2016-02-14 17:49:02 +01:00
|
|
|
systemTray.setStatus(null);
|
2016-10-08 23:54:45 +02:00
|
|
|
systemTray.setImage(BLACK_MAIL);
|
2016-02-14 17:49:02 +01:00
|
|
|
|
2016-10-10 00:26:52 +02:00
|
|
|
entry.setCallback(null);
|
2016-02-14 17:49:02 +01:00
|
|
|
// systemTray.setStatus("Mail Empty");
|
2016-10-10 00:26:52 +02:00
|
|
|
systemTray.remove(entry);
|
2016-02-14 17:49:02 +01:00
|
|
|
System.err.println("POW");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-10 00:26:52 +02:00
|
|
|
Entry menuEntry = this.systemTray.addEntry("Green Mail", GREEN_MAIL, callbackGreen);
|
2016-10-08 23:54:45 +02:00
|
|
|
// case does not matter
|
|
|
|
menuEntry.setShortcut('G');
|
|
|
|
|
2016-10-03 23:55:25 +02:00
|
|
|
this.systemTray.addSeparator();
|
2016-02-14 17:49:02 +01:00
|
|
|
|
2016-10-03 23:55:25 +02:00
|
|
|
final Menu submenu = this.systemTray.addMenu("Options", BLACK_MAIL);
|
|
|
|
submenu.addEntry("Disable menu", LT_GRAY_MAIL, new SystemTrayMenuAction() {
|
2016-02-14 17:49:02 +01:00
|
|
|
@Override
|
|
|
|
public
|
2016-10-10 00:26:52 +02:00
|
|
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
2016-10-03 23:55:25 +02:00
|
|
|
submenu.setEnabled(false);
|
|
|
|
}
|
|
|
|
});
|
2016-10-10 01:15:17 +02:00
|
|
|
submenu.addEntry("Hide tray", BLACK_MAIL, new SystemTrayMenuAction() {
|
|
|
|
@Override
|
|
|
|
public
|
|
|
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
|
|
|
systemTray.setEnabled(false);
|
|
|
|
}
|
|
|
|
});
|
2016-10-03 23:55:25 +02:00
|
|
|
submenu.addEntry("Remove menu", GREEN_MAIL, new SystemTrayMenuAction() {
|
|
|
|
@Override
|
|
|
|
public
|
2016-10-10 00:26:52 +02:00
|
|
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
2016-10-03 23:55:25 +02:00
|
|
|
submenu.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
systemTray.addEntry("Quit", new SystemTrayMenuAction() {
|
|
|
|
@Override
|
|
|
|
public
|
2016-10-10 00:26:52 +02:00
|
|
|
void onClick(final SystemTray systemTray, final Menu parent, final Entry entry) {
|
2016-02-14 17:49:02 +01:00
|
|
|
systemTray.shutdown();
|
2016-02-15 00:55:03 +01:00
|
|
|
|
2016-02-15 13:27:33 +01:00
|
|
|
display.asyncExec(new Runnable() {
|
2016-02-15 00:55:03 +01:00
|
|
|
public void run() {
|
2016-02-15 13:27:33 +01:00
|
|
|
shell.dispose();
|
2016-02-15 00:55:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-14 17:49:02 +01:00
|
|
|
//System.exit(0); not necessary if all non-daemon threads have stopped.
|
|
|
|
}
|
2016-10-08 23:54:45 +02:00
|
|
|
}).setShortcut('q'); // case does not matter
|
2016-02-14 17:49:02 +01:00
|
|
|
|
2016-02-14 21:54:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
shell.pack();
|
|
|
|
shell.open();
|
|
|
|
|
|
|
|
while (!shell.isDisposed()) {
|
|
|
|
if (!display.readAndDispatch()) {
|
|
|
|
display.sleep();
|
|
|
|
}
|
2016-02-14 17:49:02 +01:00
|
|
|
}
|
2016-02-14 21:54:21 +01:00
|
|
|
|
|
|
|
display.dispose();
|
2016-02-14 17:49:02 +01:00
|
|
|
}
|
|
|
|
}
|