org.eclipse.core.runtime.adapters
附上demo代码下载地址:http://download.csdn.net/detail/sidihuo/9773976
1.模型接口,属性视图显示模型的名字
public interface IName { /** * @return the name */ public String getName(); /** * @param name * the name to set */ public void setName(final String name); } public class Person implements IName { private String name; /** * @return the name */ @Override public String getName() { return name; } /** * @param name * the name to set */ @Override public void setName(final String name) { this.name = name; } }2。简单的方法直接实现IPropertySource接口即可
public class PersonPropertySource extends Person implements IPropertySource { /* * ************************************* * Person和IPropertySource高度耦合 * ************************************* */ private final String PROP_NAME = "PROP_NAME"; /** * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue() */ @Override public Object getEditableValue() { // TODO Auto-generated method stub return null; } /** * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors() */ @Override public IPropertyDescriptor[] getPropertyDescriptors() { return new IPropertyDescriptor[] { new PropertyDescriptor(PROP_NAME, "名字") }; } /** * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object) */ @Override public Object getPropertyValue(final Object id) { return getName(); } /** * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object) */ @Override public boolean isPropertySet(final Object id) { // TODO Auto-generated method stub return false; } /** * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object) */ @Override public void resetPropertyValue(final Object id) { // TODO Auto-generated method stub } /** * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object) */ @Override public void setPropertyValue(final Object id, final Object value) { // TODO Auto-generated method stub } }3,使用IAdaptable
public class PersonAdapter implements IPropertySource { private final String PROP_NAME = "PROP_NAME"; private final IName person; /** * @param person */ public PersonAdapter(final IName person) { super(); this.person = person; } /** * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue() */ @Override public Object getEditableValue() { // TODO Auto-generated method stub return null; } /** * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors() */ @Override public IPropertyDescriptor[] getPropertyDescriptors() { return new IPropertyDescriptor[] { new PropertyDescriptor(PROP_NAME, "名字") }; } /** * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object) */ @Override public Object getPropertyValue(final Object id) { // TODO Auto-generated method stub return person.getName(); } /** * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object) */ @Override public boolean isPropertySet(final Object id) { // TODO Auto-generated method stub return false; } /** * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object) */ @Override public void resetPropertyValue(final Object id) { // TODO Auto-generated method stub } /** * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object) */ @Override public void setPropertyValue(final Object id, final Object value) { // TODO Auto-generated method stub } } public class PersonIAdaptable extends Person implements IAdaptable { /* * ************************************* * Person和PersonAdapter高度耦合 * ************************************* */ /** * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class) */ @SuppressWarnings("rawtypes") @Override public Object getAdapter(final Class adapter) { if (adapter == IPropertySource.class) { return new PersonAdapter(this); } return null; } }4.使用适配器工厂
public class PersonIAdaptableByFactory extends Person implements IAdaptable { /* * ************************************* * Person和IAdaptable高度耦合 ViewsPlugin * ************************************* */ /** * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class) */ @SuppressWarnings("rawtypes") @Override public Object getAdapter(final Class adapter) { return Platform.getAdapterManager().getAdapter(this, adapter); } }public class PersonAdapterFactory implements IAdapterFactory { /** * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object, java.lang.Class) */ @SuppressWarnings("rawtypes") @Override public Object getAdapter(final Object adaptableObject, final Class adapterType) { if (adapterType == IPropertySource.class) { return new PersonAdapter((IName) adaptableObject); } return null; } /** * @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList() */ @SuppressWarnings("rawtypes") @Override public Class[] getAdapterList() { return new Class[] { IPropertySource.class }; } } 这个工厂需要注册的,代码注册或者扩展点注册都可以
5.测试代码
public class ViewPart extends org.eclipse.ui.part.ViewPart { public ViewPart() { } @Override public void createPartControl(final Composite parent) { final TableViewer tv = new TableViewer(parent, SWT.BORDER); tv.setContentProvider(new ArrayContentProvider()); tv.setLabelProvider(new LabelProvider() { /** * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object) */ @Override public String getText(final Object element) { if (element instanceof IName) { return ((IName) element).getName(); } return super.getText(element); } }); final List<IName> models = new ArrayList<IName>(); /*******************************************/ IName person = new Person(); person.setName("aaa "); models.add(person); person = new Person(); person.setName("bbb "); models.add(person); /******************* IPropertySource ************************/ person = new PersonPropertySource(); person.setName("aaa IPropertySource"); models.add(person); person = new PersonPropertySource(); person.setName("bbb IPropertySource"); models.add(person); /******************* IAdaptable ************************/ person = new PersonIAdaptable(); person.setName("aaa IAdaptable"); models.add(person); person = new PersonIAdaptable(); person.setName("bbb IAdaptable"); models.add(person); /******************* PersonIAdaptableByFactory ************************/ person = new PersonIAdaptableByFactory(); person.setName("aaa ByFactory"); models.add(person); person = new PersonIAdaptableByFactory(); person.setName("bbb ByFactory"); models.add(person); Platform.getAdapterManager().registerAdapters( new PersonAdapterFactory(), PersonIAdaptableByFactory.class); /******************* perfect ************************/ // Platform.getAdapterManager().registerAdapters(new // PersonAdapterFactory(), Person.class); tv.setInput(models); getSite().setSelectionProvider(tv); } @Override public void setFocus() { } }6。属性视图
public class Perspective implements IPerspectiveFactory { @Override public void createInitialLayout(final IPageLayout layout) { final String properties = "org.eclipse.ui.views.PropertySheet"; final String editorArea = layout.getEditorArea(); final IFolderLayout leftTopFolder = layout.createFolder("RightTop", IPageLayout.RIGHT, 0.5f, editorArea); leftTopFolder.addView(properties); layout.setEditorAreaVisible(false); } }over,注册适配器工厂那个perfect是最好的,模型没有任何修改,很干净,不包含其他任何信息eclipse内部有大量使用;
看一下源码:
public static Object getAdapter(Object sourceObject, Class adapter, boolean activatePlugins) { Assert.isNotNull(adapter); if (sourceObject == null) { return null; } if (adapter.isInstance(sourceObject)) { return sourceObject; } if (sourceObject instanceof IAdaptable) { IAdaptable adaptable = (IAdaptable) sourceObject; Object result = adaptable.getAdapter(adapter); if (result != null) { // Sanity-check Assert.isTrue(adapter.isInstance(result)); return result; } } if (!(sourceObject instanceof PlatformObject)) { Object result; if (activatePlugins) { result = Platform.getAdapterManager().loadAdapter(sourceObject, adapter.getName()); } else { result = Platform.getAdapterManager().getAdapter(sourceObject, adapter); } if (result != null) { return result; } } return null; } 可见:先看是否直接实现了 IPropertySource接口,再看是否是实现了 IAdaptable接口,最后在注册的 适配器工厂寻找适配器转换成IPropertySource,如果还找不到,属性视图就不会显示任何东西了