如何处理eclipse插件开发中定制编辑器的属性表? [英] How to handle property sheet from customized editor in eclipse plugin development?

查看:166
本文介绍了如何处理eclipse插件开发中定制编辑器的属性表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在属性表中绑定我的编辑器小部件对象。所以我可以从属性视图中获取我的小部件的属性。
请帮助我,如果可能的话给我一些代码片段。

解决方案

你有一个很好的例子,相同的原则应该对于编辑器而不是视图有效。


属性表侦听工作台页面选择提供者。

选择提供者取决于哪个查看器/编辑器处于活动状态

每个编辑器/查看器都提供自己的选择提供者在编辑器/查看器处于活动状态时使用

这样,属性表不关心谁处于活动状态只需监听选择提供者。

这样取决于视图,会显示一组不同的属性。

例如, Navigator 视图提供 IResource 选择,因此当导航器处于活动状态时,属性表将显示 IResource 属性。 / p>

工作台选择机制在这篇文章


ISelectionListener 是一个简单的界面,只有一种方法。

一个典型的实现如下所示:




  private ISelectionListener mylistener = new ISelectionListener(){
public void selectionChanged(IWorkbenchPart sourcepart,ISelection selection){
if(sourcepart!= MyView.this&& // 1
选择instanceof IStructuredSelection){// 2
doSomething(((IStructuredSelection)selection).toList()); // 3
}
}
};




根据您的要求,您的监听器实现可能需要处理以下内容如上面的代码片段所示的问题:




  • 如果我们还提供选择(例如视图或编辑器 )我们应该排除我们自己的选择事件的处理。这样可以避免在用户选择我们的部分(1)中的元素时出现意想不到的结果。

  • 检查我们是否可以处理这种选择(2)。

  • 从选择中获取选定的内容(3)。



I have to bind my editor widget objects in property sheet.So that i can the property of my widget from property view. Please help me on this, if possible provide me some code snippets.

解决方案

You have a good example in the Getting started with Properties

Using the Properties view is simple enough.
Since it shows properties for the selected object, the first step to using it is to make sure that the workbench selection service knows about the object selected in your view. There’s an entire Eclipse Corner article written on the subject of the selection service

public void createPartControl(Composite parent) {
    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.setContentProvider(new ViewContentProvider());
    viewer.setLabelProvider(new ViewLabelProvider());

    getSite().setSelectionProvider(viewer);

    viewer.setInput(getViewSite());
}

Once you have your view contributing to the workbench selection, you need to make sure that the objects that your view is selecting contribute properties

(extract)

public class Person implements IPropertySource {
    private String name;
    private Object street;
    private Object city;

    public Person(String name) {
        this.name = name;
        this.street = "";
        this.city = "";
    }

    public Object getEditableValue() {
        return this;
    }

    public IPropertyDescriptor[] getPropertyDescriptors() {
        return new IPropertyDescriptor[] {
                new TextPropertyDescriptor("name", "Name"),
                new TextPropertyDescriptor("street", "Street"),
                new TextPropertyDescriptor("city", "City")
        };
    }

I indicated earlier that this solution is "not necessarily [the] most correct". This is because, for this to work, my domain object needs to know about the very view-centric (and Eclipse-centric) notion of being a property source; in short, there is a tight-coupling between the model and view and this not a good thing™.

Using adapter is a better approach, as described in this article:
Person should implement IAdaptable.


See also this recent article on how to create a custom property view

how to hack the Properties View to listen only to a specific view.

The isImportant() method is the one which decides whether to create an IPage for the specific IWorkbenchPart or not.
The idea is to override that method and return false for all the workbenchPart that we are not interested in. Lets create the view first:

<view
            class="com.eclipse_tips.views.CustomPropertiesView"
            icon="icons/sample.gif"
            id="com.eclipse-tips.views.customePropertiesView"
            name="My Properties View">
</view>

The CustomPropertiesView should extend PropertySheet and override the isImportant():

public class CustomPropertiesView extends PropertySheet {

 @Override
 protected boolean isImportant(IWorkbenchPart part) {
  if (part.getSite().getId().equals(IPageLayout.ID_PROJECT_EXPLORER))
   return true;
  return false;
 }
}

In this case, I'm making the view only to respond to Project Explorer and ignore other views


According to this thread, the same principle should be valid for an Editor instead of a View.

The property sheet listens to the workbench page selection provider.
The selection provider depends on what viewer/editor is active.
Each editor/viewer provides their own selection provider to use when that editor/viewer is active.
This way the property sheet doesn't care who is active, it just listens to the selection provider.
That way depending upon the view, a different set of properties are displayed.
For example, the Navigator view provides IResource selections, so the property sheet then displays IResource properties when the Navigator is active.

The Workbench Selection mechanism is illustrated in this article

The ISelectionListener is a simple interface with just one method.
A typical implementation looks like this:

private ISelectionListener mylistener = new ISelectionListener() {
    public void selectionChanged(IWorkbenchPart sourcepart, ISelection selection) {
    if (sourcepart != MyView.this &&                               // 1
        selection instanceof IStructuredSelection) {               // 2
        doSomething(((IStructuredSelection) selection).toList());  // 3
        }
    }
};

Depending on your requirements your listener implementation probably needs to deal with the following issues as shown in the code snippet above:

  • In case we also provide selections (e.g. a view or editor) we should exclude our own selection events from processing. This avoids unexpected results when the user selects elements within our part (1).
  • Check whether we can handle this kind of selection (2).
  • Get the selected content from the selection and process it (3).

这篇关于如何处理eclipse插件开发中定制编辑器的属性表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆