Eclipse RCP:View 与 Editor 交互
2012-09-06 16:39
323 查看
parts 之间有三种交互方式:
(1)使用 selection
IWorkbenchSite 允许 view 和 editor 调用其 setSelectionProvider 方法发布 其selection,其他 view 和 editor 可以通过 ISelectionService.addSelectionListener(ISelectionListener) 注册监听selection,然后做出交互。
例如,ContactView 将 TreeViewer 发布为 SelectionProvider,AddContactAction将自己注册为 SelectionListener,当 TreeViewer 发生 selection 事件时通知AddContactAction 进行处理。
org.eclipsercp.hyperbola/ContactsView
public void createPartControl(Composite parent) {
treeViewer= new TreeViewer(parent, SWT.BORDER | SWT.MULTI
|SWT.V_SCROLL);
getSite().setSelectionProvider(treeViewer);
……
}
org.eclipsercp.hyperbola/AddContactAction
public AddContactAction(IWorkbenchWindow window) {
this.window = window;
setId(ID);
setActionDefinitionId(ID);
setText("&AddContact..." );
setToolTipText("Adda contact to your contacts list." );
setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(
Application.PLUGIN_ID,IImageKeys.ADD_CONTACT));
window.getSelectionService().addSelectionListener(this);
}
public void dispose() {
window.getSelectionService().removeSelectionListener(this);
}
public void selectionChanged (IWorkbenchPart part, ISelectionincoming) {
//Selection containing elements
if(incoming instanceof IStructuredSelection) {
selection= (IStructuredSelection) incoming;
setEnabled(selection.size()== 1
&&selection.getFirstElement() instanceof RosterGroup);
}else {
//Other selections, for example containing text or of otherkinds.
setEnabled(false);
}
}
(2)Part listeners
使用 IPartService 注册 IPartListener,当 part 发生打开、关闭和隐藏等事件时通知IPartListener。
例如,当打开一个 chat editor 后,高亮选中 contact view 中的相应联系人,就可以使用此种方式。
创建 IPartListener
org.eclipsercp.hyperbola/ContactsView
private IPartListener partListener = new IPartListener() {
public void partOpened(IWorkbenchPart part) {
trackOpenChatEditors(part);
}
public void partClosed(IWorkbenchPart part) {
trackOpenChatEditors(part);
}
private void trackOpenChatEditors(IWorkbenchPart part) {
if(! (part instanceof ChatEditor))
return;
ChatEditoreditor = (ChatEditor) part;
ChatEditorInputinput = (ChatEditorInput) editor.getEditorInput();
Stringparticipant = input.getParticipant();
if(openEditors.contains(participant)) {
openEditors.remove(participant);
}else {
openEditors.add(participant);
}
treeViewer.refresh(true);
}
...
};
注册 IPartListener
org.eclipsercp.hyperbola/ContactsView
public void createPartControl(Composite parent) {
...
getSite().getWorkbenchWindow().
getPartService().addPartListener(partListener);
}
public void dispose() {
getSite().getWorkbenchWindow().
getPartService().removePartListener(partListener);
}
ContactView 的 LabelProvider 根据 openEditors 来修改联系人的字体或者颜色:
org.eclipsercp.hyperbola/ContactsView
private class ContactsDecorator implements ILabelDecorator,IFontDecorator {
...
publicFont decorateFont(Object element) {
if(element instanceof RosterEntry) {
RosterEntryentry = (RosterEntry)element;
if(ContactsView.this.openEditors.contains(entry.getUser()))
returnJFaceResources.getFontRegistry().
(1)使用 selection
IWorkbenchSite 允许 view 和 editor 调用其 setSelectionProvider 方法发布 其selection,其他 view 和 editor 可以通过 ISelectionService.addSelectionListener(ISelectionListener) 注册监听selection,然后做出交互。
例如,ContactView 将 TreeViewer 发布为 SelectionProvider,AddContactAction将自己注册为 SelectionListener,当 TreeViewer 发生 selection 事件时通知AddContactAction 进行处理。
org.eclipsercp.hyperbola/ContactsView
public void createPartControl(Composite parent) {
treeViewer= new TreeViewer(parent, SWT.BORDER | SWT.MULTI
|SWT.V_SCROLL);
getSite().setSelectionProvider(treeViewer);
……
}
org.eclipsercp.hyperbola/AddContactAction
public AddContactAction(IWorkbenchWindow window) {
this.window = window;
setId(ID);
setActionDefinitionId(ID);
setText("&AddContact..." );
setToolTipText("Adda contact to your contacts list." );
setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(
Application.PLUGIN_ID,IImageKeys.ADD_CONTACT));
window.getSelectionService().addSelectionListener(this);
}
public void dispose() {
window.getSelectionService().removeSelectionListener(this);
}
public void selectionChanged (IWorkbenchPart part, ISelectionincoming) {
//Selection containing elements
if(incoming instanceof IStructuredSelection) {
selection= (IStructuredSelection) incoming;
setEnabled(selection.size()== 1
&&selection.getFirstElement() instanceof RosterGroup);
}else {
//Other selections, for example containing text or of otherkinds.
setEnabled(false);
}
}
(2)Part listeners
使用 IPartService 注册 IPartListener,当 part 发生打开、关闭和隐藏等事件时通知IPartListener。
例如,当打开一个 chat editor 后,高亮选中 contact view 中的相应联系人,就可以使用此种方式。
创建 IPartListener
org.eclipsercp.hyperbola/ContactsView
private IPartListener partListener = new IPartListener() {
public void partOpened(IWorkbenchPart part) {
trackOpenChatEditors(part);
}
public void partClosed(IWorkbenchPart part) {
trackOpenChatEditors(part);
}
private void trackOpenChatEditors(IWorkbenchPart part) {
if(! (part instanceof ChatEditor))
return;
ChatEditoreditor = (ChatEditor) part;
ChatEditorInputinput = (ChatEditorInput) editor.getEditorInput();
Stringparticipant = input.getParticipant();
if(openEditors.contains(participant)) {
openEditors.remove(participant);
}else {
openEditors.add(participant);
}
treeViewer.refresh(true);
}
...
};
注册 IPartListener
org.eclipsercp.hyperbola/ContactsView
public void createPartControl(Composite parent) {
...
getSite().getWorkbenchWindow().
getPartService().addPartListener(partListener);
}
public void dispose() {
getSite().getWorkbenchWindow().
getPartService().removePartListener(partListener);
}
ContactView 的 LabelProvider 根据 openEditors 来修改联系人的字体或者颜色:
org.eclipsercp.hyperbola/ContactsView
private class ContactsDecorator implements ILabelDecorator,IFontDecorator {
...
publicFont decorateFont(Object element) {
if(element instanceof RosterEntry) {
RosterEntryentry = (RosterEntry)element;
if(ContactsView.this.openEditors.contains(entry.getUser()))
returnJFaceResources.getFontRegistry().
相关文章推荐
- Eclipse RCP:View 与 Editor 交互
- Eclipse RCP中Viewer交互的三种方式/Make your Eclipse applications richer with view linking
- Eclipse RCP中Viewer交互的三种方式/Make your Eclipse applications richer with view linking
- android控件webview与js交互
- android中Webview与javascript的交互(互相调用)
- swift - WKWebView JS 交互
- [iOS] 使用WebViewJavascriptBridge实现OC与JS交互
- Android基础—WebView和JavaScrip交互基础
- 主线程负责交互(事件处理)和UI绘制(onDraw), 只能在主线程访问View, 其他线程不能直接访问View (Android)
- OC与JS交互之WKWebView
- WKWebView新特性及JS交互
- Android WebView 与JS的数据交互
- 安卓webview和js+html交互利用的addJavascriptInterface和webview.loadUrl("javascript:*
- iOS用WKWebView与JS交互获取系统图片及WKWebView的Alert,Confirm,TextInput的监听代理方法使用,屏蔽WebView的可选菜单
- webview与js交互在targetsdkversion设置为19的时候出现的问题
- iOS开发使用WebViewJavascriptBridge实现OC与JS交互
- ios与js交互获取webview元素和赋值
- android webView js方法和java 方法交互
- iOS-js与iOS的交互(基于WKWebViewJavascriptBridge第三方)
- webView 与 Android交互详解