Programming/Eclipse RCP

SWT Tray/Menu Snippet

Lawmin 2012. 2. 17. 14:40
Member 변수
protected Shell shlAgentMonitor;
Image trayImage;
Tray tray;
TrayItem trayItem;

초기화쪽
Display display = shlAgentMonitor.getDisplay();
trayImage = new Image(display, this.getClass().getResourceAsStream("image/plsql.gif"));
tray = display.getSystemTray();
if (tray == null) {
System.out.println ("The system tray is not available");
}
else {
trayItem = new TrayItem (tray, SWT.NONE);
trayItem.setToolTipText("Tray!");
shlAgentMonitor.addShellListener(new ShellAdapter() {
public void shellIconified(ShellEvent e) {
shlAgentMonitor.setVisible(false);
}
});
trayItem.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event event) {
if (!shlAgentMonitor.isVisible()) {
shlAgentMonitor.setVisible(true);
shlAgentMonitor.setMinimized(false);
}
}
});
final Menu menu = new Menu (shlAgentMonitor, SWT.POP_UP);
MenuItem miRestore = new MenuItem (menu, SWT.PUSH);
miRestore.setText ("Restore");
miRestore.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
if (!shlAgentMonitor.isVisible()) {
shlAgentMonitor.setVisible(true);
shlAgentMonitor.setMinimized(false);
}
}
});
menu.setDefaultItem(miRestore);
MenuItem miExit = new MenuItem (menu, SWT.PUSH);
miExit.setText ("Exit");
miExit.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
shlAgentMonitor.dispose();
}
});
menu.setDefaultItem(miExit);
trayItem.addListener (SWT.MenuDetect, new Listener () {
public void handleEvent (Event event) {
menu.setVisible (true);
}
});
trayItem.setImage(trayImage);
}

dispose 쪽
if(trayImage != null) {
trayImage.dispose();
trayItem.dispose();
}