Eclipse 主菜单(顶层)显示操作:MainMenuEvent
我搜索了 Google 并在 eclipse 论坛中询问,但仍然困惑如何做这个简单的事情: 我得到了 SWT 的示例:
public class MenuActionListenerShow {
public static void main(String[] args) { 显示显示 = new Display(); Shell shell = new Shell(显示);
Listener showListener = new Listener() {
public void handleEvent(Event event) {
Menu menu = (Menu) event.widget;
MenuItem item = menu.getParentItem();
if (item != null) {
System.out.println(item.getText());
}
}
};
Menu menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
fileItem.setText("File");
MenuItem editItem = new MenuItem(menuBar, SWT.CASCADE);
editItem.setText("Edit");
Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
fileMenu.addListener(SWT.Show, showListener);
fileItem.setMenu(fileMenu);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
)
。
我希望做同样的事情,但主菜单(例如文件/搜索/编辑 我有一个插件,其目的是当文件/编辑/搜索...(菜单)按下/显示/级联时,带有菜单名称的文本显示在我的视图中。 是否可以或不允许获取Eclipse主菜单的API引用? 对于一些提示来说这会很棒。
谢谢
I searched Google And Asked In eclipse forum but still confused how to do this simple thing:
I got example for SWT :
public class MenuActionListenerShow {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Listener showListener = new Listener() {
public void handleEvent(Event event) {
Menu menu = (Menu) event.widget;
MenuItem item = menu.getParentItem();
if (item != null) {
System.out.println(item.getText());
}
}
};
Menu menuBar = new Menu(shell, SWT.BAR);
shell.setMenuBar(menuBar);
MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
fileItem.setText("File");
MenuItem editItem = new MenuItem(menuBar, SWT.CASCADE);
editItem.setText("Edit");
Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
fileMenu.addListener(SWT.Show, showListener);
fileItem.setMenu(fileMenu);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
I am looking to do the same thing but to Main Menu (e.g File/Search/Edit) .
I have plug-in with view that my purpose is when File/Edit/Search.... (Menus) pressed/shown/cascaded , a text with the Menu name shows in my view.
Is it possible or its not allowed to get the API references of Main Menu of Eclipse?
It will be great for some tips.
THanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做到,尽管它并不像看起来那么容易。 Eclipse 中的主菜单非常动态。例如,子菜单是惰性创建的,或者编辑器的切换可能会在顶部带来新的菜单。您必须确保始终注册/取消注册您的听众。
如果您创建一个视图,您可以使用给定的父组合来获取主菜单并注册您的侦听器。但正如我所说,您必须确保始终注册/取消注册侦听器,否则您将遇到内存泄漏、多个注册侦听器......
下面是一个示例视图,查看是否进行了注册。它不会跟踪新项目/消失的项目(将此作为家庭作业;))。
You can do it althought it isn't as easy as it might look like. The main menu in eclipse is very dynamic. Submenus are created lazy for example, or a switch of an editor might bring new menus on top. You have to ensure to register/unregister your listeners all the time.
If you create a view you can use the given parent composite to get the main menu and register your listeners. But as I said, you have to ensure to register/unregister the listeners all the time or you will get memory leaks, multiple registered listeners ...
Here is an example view the does the registering. It does not do the tracking of new items/disappearing items (do this as homework ;) ).