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

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().

           
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息