I am new to Eclipse Adapter concept. However I do have some experience in Eclipe RCP.
I searched in internet to get the idea about Eclipse Adapter pattern. I got the idea that it transforms one object to another object. Especially what I understood is, if we check both the object reference(Adapter and Adaptable) it will be same(My understanding, Pleae correct me If I am wrong.). I saw one article in internet that converting of Apple to Orange.
It does not contain full source code. So I started implemented in my own way. And I got stuck in getAdapter(Object adaptableObject, Class adapterType) method of AdaptorFactory class. I am confused to implement the getAdapter(adaptable,adapter)method. I implemented the following way.
@Override
public <T> T getAdapter(Object sourceObject, Class<T> adapterType) {
if (adapterType == Orange.class) {
if (sourceObject instanceof Apple) {
return (T) new Orange((Apple) sourceObject);
}
}
return null;
}
In this case it creates a new object. Now my doubt what is the real adavantage of doing like this? The same could be achieved through Core Java. Could any one help me on this? Please correct if the implementation is wrong.
Code of Mine
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
public class Fruit implements IAdaptable {
@Override
public <T> T getAdapter(Class<T> adapter) {
// delegate to the global adapter manager
return Platform.getAdapterManager().getAdapter(this, adapter);
}
}
public class Apple extends Fruit{
}
public class Orange extends Fruit {
private Apple apple;
public Orange(Apple apple){
this.apple = apple;
}
}
import org.eclipse.core.runtime.IAdapterFactory;
public class FruiFactory implements IAdapterFactory {
private Class<?>[] adapterList = { Orange.class };
@Override
public <T> T getAdapter(Object sourceObject, Class<T> adapterType) {
if (adapterType == Orange.class) {
if (sourceObject instanceof Apple) {
return (T) new Orange((Apple) sourceObject);
}
}
return null;
}
@Override
public Class<?>[] getAdapterList() {
return adapterList;
}
}
public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID = "HeadLessRCP"; //$NON-NLS-1$
// The shared instance
private static Activator plugin;
/**
* The constructor
*/
public Activator() {
}
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
System.out.println("HealdlessRCP");
Apple apple = new Apple();
Platform.getAdapterManager().registerAdapters(new FruiFactory(), Apple.class);
Orange orange = (Orange) apple.getAdapter(Orange.class);
System.out.println(orange);
System.out.println(apple);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.
* BundleContext)
*/
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given plug-in
* relative path
*
* @param path
* the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return imageDescriptorFromPlugin(PLUGIN_ID, path);
}
}
Aucun commentaire:
Enregistrer un commentaire