- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- Dr Alex Blewitt
- 383字
- 2021-07-14 10:36:30
Time for action – responding to the user
When the user clicks on the icon, nothing happens. That's because there is no registered listener on the TrayItem
itself. There are two listeners that can be registered; a SelectionListener
, called when the icon is clicked, and a MenuDetectListener
, which can respond to a context-sensitive menu. The former will be used to present a clock in its own window, which in SWT terms is called a Shell
.
- Open the
Activator
class. - Go to the lambda inside the
asyncExec
in theActivator
class'sstart
method. - After the creation of the
TrayItem
, calladdSelectionListener
with a new anonymous inner subclass ofSelectionListener
. In thewidgetSelected
method, anew Shell
should be created with aClockView
and then shown:trayItem.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { Shell shell = new Shell(display); shell.setLayout(new FillLayout()); new ClockWidget(shell, SWT.NONE, new RGB(255, 0, 255)); shell.pack(); shell.open(); } });
- Run the target Eclipse instance, open the Clock View, and click on the tray icon. A windowed clock will be shown:
- Run the target Eclipse instance, click on the
TrayItem
, and use the Host OSGi Console to stop and start the bundle.
What just happened?
When the TrayItem
is installed into the system's Tray
, event listeners can be registered to respond to user input. The listener that gets called when the icon is clicked on is the SelectionListener
, and this gives the opportunity to display the window (or Shell in SWT terminology).
The Display
associated with the TrayItem
is used when instantiating the Shell
. Although either Display.getDefault()
or Display.getCurrent()
could be used, neither of these would be the right option. When developers are running in multi-monitor mode, or with a virtual display (which spans multiple desktops), it's important to ensure that the Shell
is shown on the same display as the corresponding Tray
.
Without a LayoutManager
, the clock won't show up. A FillLayout
is used here to ensure that the clock is made as large as the window (and resizes accordingly to the window itself). Once the window is created, the pack
method is called, which sets the size of the window to the preferred size of its children; in this case, it's the ClockView
.
Finally, the window is shown with the open
call. When the window is closed, it is automatically disposed.
- UI設計基礎培訓教程
- Mastering Visual Studio 2017
- Java多線程編程實戰指南:設計模式篇(第2版)
- Kali Linux Web Penetration Testing Cookbook
- 碼上行動:用ChatGPT學會Python編程
- Instant Lucene.NET
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- Spring+Spring MVC+MyBatis從零開始學
- FFmpeg開發實戰:從零基礎到短視頻上線
- 網絡數據采集技術:Java網絡爬蟲實戰
- ASP.NET Web API Security Essentials
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- Python機器學習與量化投資
- Ubuntu Server Cookbook
- Learning Redux