下文是从 http://www.vogella.de/articles/EclipsePlugIn/article.html 截取的片段,讲得比较好,没废话,转载过来。
============================================================== 5. Adapters
5.1. Overview
Adapters help to display information about objects in view without having to adjust the existing views. In this example we will create a small view which allows to select objects and use the properties view to display them.
Adapters are used on several places for example you can use an adapter to display your data in the outline view. See Outline View Example for an example how to do this.
properties视图,可以显示所选中对象的信息,也可以编辑。
5.2. Example
We will simple use an adapter to show our data in the property view. Create a new plugin project "de.vogella.plugin.adapter". Use the "Plug-in with a view" template with the following settings.
Add the dependency "org.eclipse.ui.views" in tab dependencies of plugin.xml.
Create the following data model.
首先,这是一个提供数据的Bean。
package de.vogella.plugin.adapter.model;
public class Todo { private String summary; private String description; public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; }
}
Change the code of SampleView.java to the following. After this change you should be able to run your project, open your view and see your todo items.
下面是主程序,用一个TableViewer展示数据。 重点是getSite().setSelectionProvider(viewer);这句话, 由TableViewer作为Selection的提供者,在TableViewer上的选择动作将激发Selection Change事件。
package de.vogella.plugin.adapter.views;
import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.ISharedImages; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart;
import de.vogella.plugin.adapter.model.Todo;
public class SampleView extends ViewPart { public static final String ID = "de.vogella.plugin.adapter.views.SampleView";
private TableViewer viewer;
class ViewContentProvider implements IStructuredContentProvider { private Todo[] todos; public void inputChanged(Viewer v, Object oldInput, Object newInput) { todos = (Todo[]) newInput; }
public void dispose() { }
@Override public Object[] getElements(Object inputElement) { return todos; }
}
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { public String getColumnText(Object obj, int index) { Todo todo = (Todo) obj; return todo.getSummary(); }
public Image getColumnImage(Object obj, int index) { return getImage(obj); }
public Image getImage(Object obj) { return PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_OBJ_ELEMENT); } }
/** * This is a callback that will allow us to create the viewer and initialize * it. */ 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(getElements());
}
/** * Passing the focus request to the viewer's control. */ public void setFocus() { viewer.getControl().setFocus(); } // Build up a simple data model private Todo[] getElements() { Todo[] todos = new Todo[2]; Todo todo = new Todo(); todo.setSummary("First Todo"); todo.setDescription("A very good description"); todos[0] = todo; todo = new Todo(); todo.setSummary("Second Todo"); todo.setDescription("Second super description"); todos[1] = todo; return todos; } }
To displays its values in the property view, add the extension point "org.eclipse.core.runtime.adapters" to your project. The data of the extension point should be like the following.
在plugin.xml中追加适配器的对应关系。老外喜欢倒叙,这个应该最后说。 就是对于adaptableType指定的数据模型,使用class指定的工厂来取得适配后的数据类型。
<extension point="org.eclipse.core.runtime.adapters"> <factory adaptableType="de.vogella.plugin.adapter.model.Todo" class="de.vogella.plugin.adapter.TodoAdapterFactory"> <adapter type="org.eclipse.ui.views.properties.IPropertySource"> </adapter> </factory> </extension>
Implement the factory and the new class "TodoPropertySource" which implements "IPropertySource".
这个就是适配器工厂。 如果要适配的类型是IPropertySource.class,即properties视图要用, 则判断原始的数据类型,对应返回实现了IPropertySource接口的数据类型。 后者就是支持properties视图使用的数据类型。
package de.vogella.plugin.adapter;
import org.eclipse.core.runtime.IAdapterFactory; import org.eclipse.ui.views.properties.IPropertySource;
import de.vogella.plugin.adapter.model.Todo;
public class TodoAdapterFactory implements IAdapterFactory {
@Override public Object getAdapter(Object adaptableObject, Class adapterType) { if (adapterType== IPropertySource.class && adaptableObject instanceof Todo){ return new TodoPropertySource((Todo) adaptableObject); } return null; }
@Override public Class[] getAdapterList() { return new Class[] { IPropertySource.class }; }
}
这个就是实现了IPropertySource接口的数据类型,给每种原始数据类型对应写一个就行了。 properties视图是支持编辑的,当然也可以只显示。 根据需要使用不同的PropertyDescriptor就行了。 ----- PropertyDescriptor - 只显示的 TextPropertyDescriptor - 可以用文本框编辑的 CheckboxPropertyDescriptor - 可以用复选框的 ComboBoxPropertyDescriptor - 下拉框的 ColorPropertyDescriptor - 用ColorCellEditor 编辑的 ------ 这几个类使用还比较简单,摸索一下就行了。
package de.vogella.plugin.adapter;
import org.eclipse.ui.views.properties.IPropertyDescriptor; import org.eclipse.ui.views.properties.IPropertySource; import org.eclipse.ui.views.properties.TextPropertyDescriptor;
import de.vogella.plugin.adapter.model.Todo;
public class TodoPropertySource implements IPropertySource {
private final Todo todo;
public TodoPropertySource(Todo todo) { this.todo = todo; }
@Override public boolean isPropertySet(Object id) { return false; }
@Override public Object getEditableValue() { return this; }
@Override public IPropertyDescriptor[] getPropertyDescriptors() {
return new IPropertyDescriptor[] { new TextPropertyDescriptor("summary", "Summary"), new TextPropertyDescriptor("description", "Description") }; }
@Override public Object getPropertyValue(Object id) { if (id.equals("summary")) { return todo.getSummary(); } if (id.equals("description")) { return todo.getDescription(); } return null; }
@Override public void resetPropertyValue(Object id) {
}
@Override public void setPropertyValue(Object id, Object value) { String s = (String) value; if (id.equals("summary")) { todo.setSummary(s); } if (id.equals("description")) { todo.setDescription(s); } }
}
记得把properties视图弄出来,否则看不到。 If you run your workbench and open your View via Windows -> Show View -> Others -> Sample Category -> Sample View and the property view you should be able to view your data.
当然,最好是自己做一个布局,自动把properties视图弄出来。如下面的小例子。
public class ContactPerspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) { String editorArea = layout.getEditorArea(); layout.addView(ContactView.ID, IPageLayout.LEFT, 0.25f, editorArea); layout.addView(IPageLayout.ID_PROP_SHEET, IPageLayout.BOTTOM, 0.6f, editorArea); }
}
怎么切换布局?我还不会呢。
|