Eclipse RCP Tutorial 7

작성일: 2010. 10. 27.

원문: http://www.vogella.de/articles/EclipseRCP/article.html Version 5.1

시스템 트레이

다음은 시스템 트레이의 RCP 어플리케이션에 아이콘을 추가하고, 이 아이콘에 메뉴를 추가하는 것이다. 윈도우가 최소화되면 프로그램이 작업표시줄에서 보이지 않게 되는(트레이 아이콘을 통해서만 보임) 기능을 추가한다.

de.vogella.rcp.intro.traytest 라고 프로젝트를 새로 생성한다. Hello RCP 템플릿을 사용한다. 어플리케이션에 de.vogella.rcp.intro.traytest.exitCommand 라는 id 를 가진 명령을 생성한다.

ApplicationWorkbenchWindowAdvisor 클래스를 열고 다음의 코드를 적용한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package de.vogella.rcp.intro.traytest;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.plugin.AbstractUIPlugin;
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
private IWorkbenchWindow window;
private TrayItem trayItem;
private Image trayImage;
private final static String COMMAND_ID = "de.vogella.rcp.intro.traytest.exitCommand";
public ApplicationWorkbenchWindowAdvisor(
IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
public ActionBarAdvisor createActionBarAdvisor(
IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(400, 300));
configurer.setShowCoolBar(false);
configurer.setShowStatusLine(false);
configurer.setTitle("Hello RCP"); //$NON-NLS-1$
}
// As of here is the new stuff
@Override
public void postWindowOpen() {
super.postWindowOpen();
window = getWindowConfigurer().getWindow();
trayItem = initTaskItem(window);
// Some OS might not support tray items
if (trayItem != null) {
minimizeBehavior();
// Create exit and about action on the icon
hookPopupMenu();
}
}
// Add a listener to the shell
private void minimizeBehavior() {
window.getShell().addShellListener(new ShellAdapter() {
// If the window is minimized hide the window
public void shellIconified(ShellEvent e) {
window.getShell().setVisible(false);
}
});
// If user double-clicks on the tray icons the application will be
// visible again
trayItem.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event event) {
Shell shell = window.getShell();
if (!shell.isVisible()) {
window.getShell().setMinimized(false);
shell.setVisible(true);
}
}
});
}
// We hook up on menu entry which allows to close the application
private void hookPopupMenu() {
trayItem.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
Menu menu = new Menu(window.getShell(), SWT.POP_UP);
// Creates a new menu item that terminates the program
// when selected
MenuItem exit = new MenuItem(menu, SWT.NONE);
exit.setText("Goodbye!");
exit.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
// Lets call our command
IHandlerService handlerService = (IHandlerService) window
.getService(IHandlerService.class);
try {
handlerService.executeCommand(COMMAND_ID, null);
} catch (Exception ex) {
throw new RuntimeException(COMMAND_ID);
}
}
});
// We need to make the menu visible
menu.setVisible(true);
}
});
}
// This methods create the tray item and return a reference
private TrayItem initTaskItem(IWorkbenchWindow window) {
final Tray tray = window.getShell().getDisplay().getSystemTray();
TrayItem trayItem = new TrayItem(tray, SWT.NONE);
trayImage = AbstractUIPlugin.imageDescriptorFromPlugin(
"de.vogella.rcp.intro.traytest", "/icons/alt_about.gif")
.createImage();
trayItem.setImage(trayImage);
trayItem.setToolTipText("TrayItem");
return trayItem;
}
// We need to clean-up after ourself
@Override
public void dispose() {
if (trayImage != null) {
trayImage.dispose();
}
if (trayItem != null) {
trayItem.dispose();
}
}
}

어플리케이션을 실행하면 시스템 트레이 아이콘을 볼 수 있다. 메뉴와 최소화 동작을 테스트해라. 만약 어플리케이션이 최소화되면 작업표시줄에서 보이지 않게 되고 시스템 트레이에서만 보일 것이다.

Share Comments