Confused about IAdaptable with IActionFilters - eclipse

I'm confuse about IAdaptable and related classes. Which class is the adapter, the adaptee, the adaptable type?
[Context]
I have a context menu for entries of a table/tree viewer. Certain actions in the context menu must not be visible depending on the state of the respective object in the viewer (i.e. attribute value of a row in the table viewer).
I can achieve this with a config like this in plugin.xml:
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
adaptable="false"
id="<some_id>"
objectClass="<object_class_of_viewer_entry>">
<visibility>
<objectState name="buildable" value="true"/>
</visibility>
<action
class="<my_action_class>"
However, this only works if the object class implements org.eclipse.ui.IActionFilter.
[Problem]
My object class can't implement IActionFilter, I don't want to change its interface. Hence, I need to work around that using the IAdaptable mechanism.
Reading the Eclipse documentation left me all puzzled with terms (adapter, the adaptee, adaptable type) and I'm still confused about how to go about my problem.
The object class (referred to by in the above config) must remain untouched.
My approach was the following.
<extension
point="org.eclipse.core.runtime.adapters">
<factory
adaptableType="<object_class_of_viewer_entry>"
class="MyFactory">
<adapter
type="org.eclipse.ui.IActionFilter">
</adapter>
</factory>
</extension>
MyFactory is like this:
public class MyFactory implements IAdapterFactory {
private static final Class[] types = {
<object_class_of_viewer_entry>.class,
};
#Override
public Object getAdapter(Object adaptableObject, Class adapterType) {
return new <class_that_implements_IActionFilter>((<object_class_of_viewer_entry>) adaptableObject);
}
#Override
public Class[] getAdapterList() {
return types;
}
}
What's wrong about this? Where did I miss something?

Turns out everything, well, almost everything, was correct. I had simply mixed up interface and implementation of object_class_of_viewer_entry in the plugin.xml.
Two articles that helped: http://www.eclipsezone.com/articles/what-is-iadaptable/ and http://wiki.eclipse.org/FAQ_How_do_I_use_IAdaptable_and_IAdapterFactory%3F

Related

Extend ProposalProvider in external Eclipse Project via Extension Point

I try to extend my MyDSLProposalProvider from an external Eclipse RCP Project. I created an extension point schema which requires a class property which extends my ProposalProvider. In the new project I extend the class an overrode some methods justs to give me some output so I can see that the external method is called. But this is currently not happening. Is there anything I have to consider?
Currently the hirachy looks like:
MyDSLProposalProvider extends AbstractMyDSLProposalProvider
ExternalProposalProvider extends MyDSLProposalProvider
I rewrote a Method generated in the AbstractMyDSLProposalProvider but when its triggered the predefined Method in the AbstractMyDSLProposalProvider is called and not my new implementation.
public class ExternalMyDSLProposalPovider extends MyDSLProposalProvider
{
#Override
public void completeComponent_Name(EObject model, Assignment
assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
System.err.println("extern");
if(model instanceof Component)
{
createProposal("foo", "foo", context, acceptor);
}
super.completeComponent_Name(model, assignment, context, acceptor);
}
}
This is the class in the external Eclipse Project.
Thanks for the help.
When you declare an extension point using a schema that you have defined Eclipse puts that declaration in the extension point registry. That is all that is does, you must then write code to make uses of those declarations.
You read the extension point registry using something like:
IExtensionRegistry extRegistry = Platform.getExtensionRegistry();
IExtensionPoint extPoint = extRegistry.getExtensionPoint("your extension point id");
IConfigurationElement [] elements = extPoint.getConfigurationElements();
elements is now an array of the declarations in the various plugins using the extension point.
IConfigurationElement has various methods to get the values of the attributes of the declaration.
If you have defined a class in one of the attributes you can create an instance of the class using:
IConfigurationElement element = .... a config element
Object obj = element.createExecutableExtension("attribute name");
In your case the result should be your ExternalMyDSLProposalPovider.
You will then need to hook this object up with whatever is doing to proposals.

Abstract components via org.osgi.service.component annotations

I am migrating from org.apache.felix.scr annotations to org.osgi.service.component annotations. I have a set of Components that inherit from a common abstract class. In the felix case, I can use a #Component annotation with the option componentAbstract=true on the super class, and then use #Reference annotation in the super class. I cannot find how to migrate this to osgi annotations.
Is it possible to use Component annotations in a super class of a Component? And if so, what is then the appropriate way to handle the properties and metatype generation?
So, what I am looking for, is something like this
/* No component definition should be generated for the parent, as it is
abstract and cannot be instantiated */
#Component(property="parent.property=parentValue")
public abstract class Parent {
#Reference
protected Service aService;
protected activate(Map<String,Object> props) {
System.out.println("I have my parent property: "+props.get("parent.property"));
#Override
public abstract void doSomething();
}
/* For this class, the proper Component definition should be generated, also
including the information coming from the annotations in the parent */
#Component(property="child.property=childValue")
public class Child extends Parent {
#Activate
public activate(Map<String,Object> props) {
super.activate(props);
System.out.println("I have my child property: "+props.get("child.property"));
}
public void doSomething() {
aService.doSomething();
}
}
By default BND will not process DS annotations in parent classes. You can change that with -dsannotations-options: inherit but please see http://enroute.osgi.org/faq/ds-inheritance.html why you shouldn't!
2021-02-23 UPDATE: It seems like the page mentioned above is no longer available. I don't know if it was moved elsewhere or simply removed but its content (in Markdown format) is still available on GitHub: https://github.com/osgi/osgi.enroute.site/blob/pre-R7/_faq/ds-inheritance.md

Switching GWT.create(XXX.class) by Custom Annotation

I hope that you're all coding well. So... I was refactoring the client side of the GWT app I work on and I was wondering about something. Looking after an answer days after days, I decided to ask you for your point of view...
The title is quite understanding, but, there is a snippet of what I'd like to do.
I'd like to change stuff like this
public AnnotatedObject annotated = GWT.create(AnnotatedObject.class);
by something like this
#CreativeAnnotation
public AnnotatedObject;
I had to say that in my xxx.gwt.xml, I have done something like this :
<replace-with class="package.AnnotationObject2">
<when-type-is class="package.AnnotationObject" />
</replace-with>
As you can see, my deffered replacement class is AnnotationObject2, and for the moment, I add a line between the ones above and I have :
<replace-with class="package.AnnotationObject1">
<when-type-is class="package.AnnotationObject" />
<when-property-is name="type" value="object1" />
</replace-with>
<replace-with class="package.AnnotationObject2">
<when-type-is class="package.AnnotationObject" />
<when-property-is name="type" value="object2" />
</replace-with>
I don't really like to play with metadata of my xxx.html, so the result I'd like is this one :
#CreativeAnnotation(type = "object2")
public AnnotatedObject;
So, do you think that sort of thing is possible with GWT (I have to say that I work with GWT 2.5, 'cause of my client desires) ? If yes, may you help me ?
Thanks in advance.
EDIT : I mean, I know about GIN... Just wondering how to do it from scratch.
You can achieve this by using dependency injection with GIN.
GIN automatically use GWT.create() to create an object that has to be injected.
Ex:
class MyView {
interface MyUiBinder extends UiBinder<Widget, MyView> {
}
#Inject
MyView(MyUiBinder uiBinder) {
initWidget(uiBinder.createAndBindUi(this));
}
}
And with dependency injection, you also specify which implementation of your interface to instantiate in your GIN module:
public class MyModule extends AbstractGinModule {
protected void configure() {
bind(AnnotationObject.class).annotatedWith(Names.named("object1").to(AnnotationObject1.class);
bind(AnnotationObject.class).annotatedWith(Names.named("object2").to(AnnotationObject2.class);
}
}
And then in your code:
public class MyClass {
#Inject
public MyClass(#Named("object1") AnnotationObject annotationObject) {
...
}
}
You can also use custom binding annotation instead of the Named annotation.
You can write your own generator if you don't want to use GIN - for something like this it would be pretty trivial.

use GwtCreateResource to provide text programatically

I would like my uiBinder to use a ClientBundle which will provide some runtime customized labels. Kind of a TextResource but not from a text file !
I tried with GwtCreateResource but from the DevGuide it seems like it's not possible. Am I right ? (create() and name() are the only methods available)
What I would like to achieve is something like this:
client bundle:
public interface MyWidgetResources extends ClientBundle {
GwtCreateResource<WidgetLabels> labels();
#Source("lol.css")
CssResource style();
}
labels class:
public final class MyWidgetLabels {
public String title() {
return load("mywidget-title");
}
public String banner() {
return load("mywidget-banner");
}
private String load(String key) {
// load from external..
}
}
uiBinder:
<ui:with type="com.package.MyWidgetResources" field="res"/>
<gwt:SimplePanel>
<gwt:Label text="{res.labels.title}"></gwt:Label>
<gwt:Label text="{res.labels.banner}"></gwt:Label>
</gwt:SimplePanel>
My code looks like this already but res.label.title does not work because GwtCreateResource can only serve as class instantiator (res.labels.create().title()).
Is there a solution for me ? Maybe with a custom ResourceGenerator ?
As long as MyWidgetLabels can be created by GWT.create, you can put anything you want into that type, and you can make it behave however you'd like. You will need the create reference in your uibinder as you suggested at the end of the post to actually build the object, so your lines will look about like this:
<gwt:Label text="{res.labels.create.title}"></gwt:Label>
Each . separated piece (except the first, which is a ui:field/#UiField) is a no-arg method to be called - you declared labels() in MyWidgetResources, create() already existed in GwtCreateResource, and you created title() in your own MyWidgetLabels type.
Since that first piece is a ui:field/#UiField, you could have another that references res.labels.create as something like labels so that later you could instead say:
<gwt:Label text="{labels.title}"></gwt:Label>
Finally, yes, you could build your own ResourceGenerator which would enable you to do whatever you wanted to emit the type in question, as long as you extended the ResourcePrototype type and had a getName() method.

Using Gin to Inject Multiple Views

I'm attempting to put together a multi-project application, wherein one of the sub-projects has multiple views for a single presenter. I am using Gin to inject views into my presenters.
The sub-project contains the presenter and the 2 different views. I have 2 separate gin modules, each binding one of the views to the view interface.
As per Thomas Broyer's suggestion on the answer to this post, my Ginjectors are wrapped in a "holder" class that calls the GWT.create on the particular ginjector. The appropriate holder is configured in the gwt.xml file using a replace-with statement.
When I run my project in Dev Mode, I see the alternate view appear as I expect it to. However, when I compile the project, I still only get the default view. Also, only 6 permutations (I would expect more on account of the replace-with logic), and I do not get the view I expect in the different scenarios.
Here is some code to illustrate.
Subproject.gwt.xml contains this:
<replace-with class="com.example.GinjectorDesktopHolder">
<when-type-is class="com.example.GinjectorHolder" />
</replace-with>
<replace-with class="com.example.GinjectorTabletHolder">
<when-type-is class="com.example.GinjectorHolder" />
<when-property-is name="formfactor" value="tablet" />
</replace-with>
The "formfactor" variable is defined in a gwt.xml copied verbatim from GWT's mobilewebapp sample project.
The Holder classes look like this:
public abstract class GinjectorHolder {
public abstract Ginjector getGinjector();
}
public class GinjectorTabletHolder extends GinjectorHolder {
#Override
public Ginjector getGinjector() {
return GWT.create(GinjectorTablet.class);
}
}
public class GinjectorDesktopHolder extends GinjectorHolder {
#Override
public Ginjector getGinjector() {
return GWT.create(GinjectorDesktop.class);
}
}
My Ginjectors look like this:
public interface MyGinjector {
MyView getView();
EventBus getEventBus();
}
#GinModules({ModuleDesktop.class})
public interface GinjectorDesktop extends Ginjector, MyGinjector {}
#GinModules({ModuleTablet.class})
public interface GinjectorTablet extends Ginjector, MyGinjector {}
My modules look like this:
public class ModuleDesktop extends AbstractGinModule {
#Override
protected void configure() {
bind(MyPresenter.View.class).to(DesktopView.class);
}
}
public class ModuleTablet extends AbstractGinModule {
#Override
protected void configure() {
bind(MyPresenter.View.class).to(TabletView.class);
}
}
And finally, in my presenter proxy, basically the entry point into this particular sub-project, I have this line:
GinjectorHolder holder = GWT.create(GinjectorHolder.class);
MyGinjector ginjector = holder.getGinjector();
As mentioned earlier, when I run in Dev Mode and put in breakpoints, I can see the appropriate GinjectorHolder is created. The FormFactor.gwt.xml (linked above) provides a switch for using a URL param to switch to the context you'd like to see. So I can do formfactor=tablet in the URL and the Tablet Ginjector Holder is created.
As mentioned in the comments, removing the line
<collapse-property name="formfactor" values="*"/>
leads to the expected increase in the number of permutations.
Still, it's mysterious, why this is necessary, because usually it should be possible to collapse any properties you like - it just means, that each browser has to download more code, but should still get everything it needs. Could be a bug.
First of all instead of mapping view to viewimpl you can bind it to viewprovider, and then based on user-agent values you can return the appropriate instance to bind to.