[Eclipse RCP] RCP中 如何在 状态栏中 显示下拉菜单 ???

521java 2011-06-20
在RCP开发中:
  如何在 状态栏中 显示下拉菜单 ?

  对于 下拉菜单 可以在工具栏、视图或编辑器中显示,但是在状态栏显示,我一直没有解决的方案啊?还望gs指教一下!

  我重写了 StatusLineContributionItem 类的fill方法,但是具体如何实现,我还是没有思路啊,还请gs指点一下!
521java 2011-06-20
JasonRight 2011-06-28

需要自己写ToolBar.

@see http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet67.java?view=co

 

显示statusline

//class ApplicationWorkbenchWindowAdvisor

    public void preWindowOpen() {
        //other settings ....
        configurer.setShowStatusLine(true);
    } 

创建ToolBar

//class ApplicationActionBarAdvisor

    @Override
	protected void fillStatusLine(IStatusLineManager statusLine) {
		StatusLineContributionItem statusItem = new StatusLineContributionItem("DopDownMenuInStatus") {
			@Override
			public void fill(Composite parent) {
				Shell shell = parent.getShell();
				final ToolBar toolBar = new ToolBar(parent, SWT.NONE);
				final Menu menu = new Menu(shell, SWT.POP_UP);
				for (int i = 0; i < 8; i++) {
					MenuItem item = new MenuItem(menu, SWT.PUSH);
					item.setText("Item " + i);
				}
				final ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN);
				//Can not call setText(),otherwise the tool bar is not shown
				//Seems the layout problem, need to debug more.
				//item.setText("Drop Down");
				
				//TODO:need to dispose this image.
				Image image = Activator.getImageDescriptor("icons/sample.gif").createImage(); 
				item.setImage(image);

				item.addListener(SWT.Selection, new Listener() {
					public void handleEvent(Event event) {
						if (event.detail == SWT.ARROW) {
							Rectangle rect = item.getBounds();
							Point pt = new Point(rect.x, rect.y + rect.height);
							pt = toolBar.toDisplay(pt);
							menu.setLocation(pt.x, pt.y);
							menu.setVisible(true);
						}
					}
				});
			}
		};
		statusLine.add(statusItem);

		//To not call super.fill(), these following statements are copied from
		//StatusLineContributionItem.setText()
		statusItem.setVisible(true);
		IContributionManager contributionManager = statusItem.getParent();

		if (contributionManager != null) {
			contributionManager.update(true);
		}
	}

有可能可以通过extension point来增加StatusLineContributionItem, 而不是重载fillStatusLine。我没有尝试.

 

这段代码还有一个问题不能调用ToolItem.setText(), 否则下拉菜单就无法显示。

 

menu in status

 

Global site tag (gtag.js) - Google Analytics