您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: