Eclipse RCP:View 与 Editor 交互
2010-01-22 15:07
155 查看
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("&Add Contact..."
);
setToolTipText("Add a 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, ISelection incoming) {
// 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 other kinds.
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
;
ChatEditor editor = (ChatEditor) part;
ChatEditorInput input = (ChatEditorInput) editor.getEditorInput();
String participant = 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 {
...
public
Font decorateFont(Object element) {
if
(element instanceof
RosterEntry) {
RosterEntry entry = (RosterEntry)element;
if
(ContactsView.this.openEditors.contains(entry.getUser()))
return
JFaceResources.getFontRegistry().
getBold(JFaceResources.DEFAULT_FONT);
}
return
null
;
}
}
public
void
createPartControl(Composite parent) {
treeViewer = new
TreeViewer(parent, SWT.BORDER | SWT.MULTI
| SWT.V_SCROLL);
getSite().setSelectionProvider(treeViewer);
HyperbolaLabelProvider hyperbolaLabelProvider = new
HyperbolaLabelProvider();
DecoratingLabelProvider decorator = new
DecoratingLabelProvider(hyperbolaLabelProvider,
new
ContactsDecorator());
treeViewer.setLabelProvider(decorator);
...
}
}
(3)直接交互
可以在 view 或者 editor 中调用其他 view、editor 的引用,也可以使用 IWorkbenchPage
来打开关闭其他 view 或 editor 。但这种做法增加了 view、editor 的耦合。
参考:
《Eclipse Rich Client Platform Designing, Coding, and Packaging Java Applications》:chapter16
(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("&Add Contact..."
);
setToolTipText("Add a 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, ISelection incoming) {
// 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 other kinds.
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
;
ChatEditor editor = (ChatEditor) part;
ChatEditorInput input = (ChatEditorInput) editor.getEditorInput();
String participant = 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 {
...
public
Font decorateFont(Object element) {
if
(element instanceof
RosterEntry) {
RosterEntry entry = (RosterEntry)element;
if
(ContactsView.this.openEditors.contains(entry.getUser()))
return
JFaceResources.getFontRegistry().
getBold(JFaceResources.DEFAULT_FONT);
}
return
null
;
}
}
public
void
createPartControl(Composite parent) {
treeViewer = new
TreeViewer(parent, SWT.BORDER | SWT.MULTI
| SWT.V_SCROLL);
getSite().setSelectionProvider(treeViewer);
HyperbolaLabelProvider hyperbolaLabelProvider = new
HyperbolaLabelProvider();
DecoratingLabelProvider decorator = new
DecoratingLabelProvider(hyperbolaLabelProvider,
new
ContactsDecorator());
treeViewer.setLabelProvider(decorator);
...
}
}
(3)直接交互
可以在 view 或者 editor 中调用其他 view、editor 的引用,也可以使用 IWorkbenchPage
来打开关闭其他 view 或 editor 。但这种做法增加了 view、editor 的耦合。
参考:
《Eclipse Rich Client Platform Designing, Coding, and Packaging Java Applications》:chapter16
相关文章推荐
- 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
- webview 笔记二(android和js交互、包括链接跳转常见问题处理,加载监视,拦截Url等)
- Android学习WebView交互开发总结
- android webview js交互 第一节 (java和js交互)
- 学习笔记-Android webview和JS交互
- android控件webview与js交互
- iOS 与HTML5交互之捕捉HTML5按钮点击事件,获取webview上按钮的点击事件
- WKWebView详解&WKWebVieW和JS交互
- Android中webview和js之间的交互
- Android:WebView与js交互方式
- iOS webView 和 H5 页面交互(点击获取H5页面中的按钮,做自己想做的操作)
- WebView跳转H5页面后无法交互问题
- Asp.net Mvc Framework 九 (View与Controller交互)
- [Android进阶]---WebView与js交互
- OC与JS交互之WebViewJavascriptBridge
- android面试热点:webview使用及与js数据交互
- js与webView的简单交互
- android WebView(四)与html交互