在Eclipse 中,每选中一个文件,底部的状态栏上都会显示出该文件的有关信息。客户端软件通常都会将一些全局的或当前的信息显示在底部的状态栏上。本文将在RCP中添加状态栏功能。
首先,必须在WorkbenchWindowAdvisor. preWindowOpen()启用状态栏功能,configurer.setShowStatusLine(true);
ApplicationWorkbenchWindowAdvisor.java |
01 public void preWindowOpen() { 02 IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); 03 configurer.setInitialSize(new Point(600, 400)); 04 configurer.setShowCoolBar(true); 05 configurer.setShowStatusLine(true); 06 configurer.setShowFastViewBars(true); 07 configurer.setShowPerspectiveBar(true); 08 configurer.setShowMenuBar(true); 09 configurer.setShowProgressIndicator(true); 10 configurer.setShowStatusLine(true); 11 PlatformUI.getPreferenceStore().setDefault( 12 IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS, true); 13 PlatformUI.getPreferenceStore().setDefault( 14 IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS, 15 false); 16 PlatformUI.getPreferenceStore().setDefault( 17 IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR, 18 IWorkbenchPreferenceConstants.TOP_RIGHT); 19 } |
和menu bar 和 toolbar一样,状态栏也是由ActionBarAdvisor控制,在ActionBarAdvisor.fillStatusLine (IStatusLineManager)中,状态栏信息的显示都由IStatusLineManager控制。
状态栏的结构
![]()
Fast views 可以将View拖到Fast views中,提供View的快捷访问。
Icon/message 这里可以显示图标和信息
Progress 用来显示进度条,通常是不可见的。当有IWorkbenchWindow.run() 调用时,显示一个进度条和一个取消按钮。
Contributions 由来显示一些额外信息,例如Eclipse选中一个java editor时,底部的读写状态,行数等信息。
Jobs progress 用来显示执行的Job,默认是不现实。要启用此功能,要设置IWorkbenchWindowConfigurator.setShowProgressIndicator(boolean).
一般Icon/message已经可以满足显示信息的要求,如果想要添加,可以在ActionBarAdvisor.fillStatusLine (IStatusLineManager)执行IStatusLineManager.add(IContributionItem).
在View中使用StatusLine,当选中某个节点时,在状态栏显示节点信息。
为View中的TreeViewer添加监听选择事件:
1 viewer.addSelectionChangedListener(new ISelectionChangedListener() { 2 public void selectionChanged(SelectionChangedEvent event) { 3 updateStatusline(event); 4 } 5 }); |
01 protected void updateStatusline(SelectionChangedEvent event) { 02 IStatusLineManager statusline = this.getViewSite().getActionBars() 03 .getStatusLineManager(); 04 IStructuredSelection selection = (IStructuredSelection) event 05 .getSelection(); 06 String msg = getSelectionMessage(selection); 07 08 statusline.setMessage(msg); 09 } 10 11 private String getSelectionMessage(IStructuredSelection selection) { 12 if (selection.isEmpty()) { 13 return ""; 14 } 15 if (selection.size() == 1) { 16 return selection.getFirstElement().toString(); 17 } 18 return selection.size() + " items selected"; 19 } |
在View中使用StatusLine,当输入不合法时,显示出错信息。
1 protected void inputChagned() { 2 if (groupName.getText().length() == 0) { 3 showErrorOnStatusline("Group name can not be blank"); 4 return; 5 } 6 showErrorOnStatusline(null); 7 setDirty(true); 8 } |
1 protected void showErrorOnStatusline(String error) { 2 IStatusLineManager statusline = this.getEditorSite().getActionBars() 3 .getStatusLineManager(); 4 if (error == null) { 5 statusline.setErrorMessage(null, error); 6 } else { 7 statusline.setErrorMessage(ImageShop.get("error"), error); 8 } 9 } |