Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » IAdapterFactory never called by Eclipse framework
IAdapterFactory never called by Eclipse framework [message #871888] Tue, 15 May 2012 04:44 Go to next message
Janusz Dalecki is currently offline Janusz DaleckiFriend
Messages: 63
Registered: January 2010
Location: Sydney
Member
Hi,
I would like to re-use an existing handler for executing some code when my view item is selected. My view has a TableViewer i use for setting viewer selection provider.
getViewSite().setSelectionProvider(tableViewer);
The handler is defined in other plugin like this:
<handler
class="com.tycoint.meridian.situationgui.viewer.commands.OpenSituationEditorHandler"
commandId="com.tycoint.meridian.situationgui.viewer.openSituationEditor">
<enabledWhen>
<and>
<count
value="1">
</count>
<iterate>
<adapt
type="tycoint.odyssey.situationmanager.SituationValue">
</adapt>
</iterate>
</and>
</enabledWhen>
</handler>

So my handler is enabled only if there is single selection and the object selected can be adapted to SituationValue.
In my plugin (which depends on the plugin containing handler) I have provided an adapter factory like this:
<factory
adaptableType="com.tycoint.penlink.timeline.PipData"
class="com.tycoint.penlink.controlcentre.PipDataAdapterFactory">
<adapter
type="tycoint.odyssey.situationmanager.SituationValue">
</adapter>
</factory>

And the code for a factory is :
public class PipDataAdapterFactory implements IAdapterFactory {

/* (non-Javadoc)
* @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object, java.lang.Class)
*/
@Override
public Object getAdapter(Object adaptableObject, Class adapterType) {
if (adapterType.equals(SituationValue.class)) {
RoadUsageSituationValue s = (RoadUsageSituationValue)((PipData) adaptableObject).event.getUserData();
return s;
}
return null;
}

/* (non-Javadoc)
* @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
*/
@Override
public Class[] getAdapterList() {
return new Class[] { SituationValue.class };
}

}
My tableViewer contains PipData objects, but the factory is never consulted (calling getAdapter method) to enable the handler. So when I try to execute the handler via handler service:
IHandlerService service = (IHandlerService) viewer.getViewSite().getService(
IHandlerService.class);

if (service != null) {
try {
service.executeCommand("com.tycoint.meridian.situationgui.viewer.openSituationEditor",
null);
} catch (Exception ex) {
SituationViewerActivator.getDefault().handleError("Error executing command", ex, log);
}
}

I get exception that the handler is disabled.
Any suggestion why? Crying or Very Sad
Thanks,
Regards
Re: IAdapterFactory never called by Eclipse framework [message #872042 is a reply to message #871888] Tue, 15 May 2012 11:07 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Hi Janusz,
your handler expression is wrong. You need to specify a variable on which you want to perform count/iterate.

Try for example the following:
<handler
class="com.tycoint.meridian.situationgui.viewer.commands.OpenSituationEditorHandler"
commandId="com.tycoint.meridian.situationgui.viewer.openSituationEditor">
<enabledWhen>
<with variable="selection">  <!-- or maybe activeMenuSelection -->
   <and>
     <count
       value="1">
     </count>
     <iterate>
       <adapt
         type="tycoint.odyssey.situationmanager.SituationValue">
       </adapt>
     </iterate>
   </and>
</with>
</enabledWhen>
</handler>


Regards
Thorsten
Re: IAdapterFactory never called by Eclipse framework [message #872335 is a reply to message #872042] Tue, 15 May 2012 23:32 Go to previous messageGo to next message
Janusz Dalecki is currently offline Janusz DaleckiFriend
Messages: 63
Registered: January 2010
Location: Sydney
Member
Hi Thorsten,
The documentation states that the IEvaluationService provides the global selection as the default variable - so I thought I didn't need to specify "selection".
Anyway I have changed it to what you have suggested, but it still does not work.
Regards,
Janusz
Re: IAdapterFactory never called by Eclipse framework [message #872400 is a reply to message #872335] Wed, 16 May 2012 03:43 Go to previous messageGo to next message
Catalin Gerea is currently offline Catalin GereaFriend
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

Hello Janusz

Try first the expression from Thorsten without the adapt part to see if it works. Then add the adapt part.

Related to the adapter you can query the IAdapterManager for an adapter (see http://wiki.eclipse.org/FAQ_How_do_I_use_IAdaptable_and_IAdapterFactory%3F). If it returns null it is possible that you encounter this bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=82973 (look on the bug page for possible workarounds).


Time is what you make of it.
Re: IAdapterFactory never called by Eclipse framework [message #872451 is a reply to message #872400] Wed, 16 May 2012 06:31 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Hello Janusz,
I wasn't aware about "selection" being the default variable. If that is the case your initial expression is probably correct. In the past I also encountered the problem of the adapterManager not forcing the adapter plugin to being loaded. I wasn't aware that this is still an issue. Try the expression without the adaptable part as Catalin suggests. If that is your problem you can force your plugin to load on startup either via the launch configuration, or by implementing the org.eclipse.ui.startup extension. If you do not want to force load on startup then you can force plugin activation by explicitly requesting the respective adapter via the adapterManager (following the link Catalin posted), once your view is activated. In that case make sure that you use the getAdapter() method with the force parameter

AdapterManager.getAdapter(Object adaptable, String adapterType, boolean force)


This should force the adapter plugin to being loaded. The adapterFactory should then be available when your expression is evaluated.

Regards,
Thorsten
Re: IAdapterFactory never called by Eclipse framework [message #872466 is a reply to message #872451] Wed, 16 May 2012 07:23 Go to previous messageGo to next message
Janusz Dalecki is currently offline Janusz DaleckiFriend
Messages: 63
Registered: January 2010
Location: Sydney
Member
Thanks Guys for the reply,
1) How do I call AdapterManager.getAdapter method - it seems to be private.
2) I have checked that the plugin containing the expression is loaded before the one containing adapter factory.
Regards,
Janusz
Re: IAdapterFactory never called by Eclipse framework [message #872479 is a reply to message #872466] Wed, 16 May 2012 07:46 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Hm, you are right. I just remebered that there is such a method. Probably because if haved debuged through that code quite often due to the same problems. I have now looked it up in older coder. I have used the loadAdapter() method in order to force plugin activation. loadAdapter() simply calls the above mentioned method with force=true.

Regards,
Thorsten
Re: IAdapterFactory never called by Eclipse framework [message #885558 is a reply to message #872479] Wed, 13 June 2012 08:48 Go to previous message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Hi Janusz,

I think the problem is that your handler isn't active.
You have to specify when is the handler active and then, when it's chosen as the active one, the enableWhen expression is evaluated by the command framework.

Try something like that:
<handler
    class="com.tycoint.meridian.situationgui.viewer.commands.OpenSituationEditorHandler"
    commandId="com.tycoint.meridian.situationgui.viewer.openSituationEditor">
<activeWhen>
<iterate>
    <adapt
        type="tycoint.odyssey.situationmanager.SituationValue">
    </adapt>
</iterate>
<activeWhen>
<enabledWhen>
    <count
        value="1">
    </count>
</enabledWhen>
</handler>
Previous Topic:Read Only Text in Styled Text Editor
Next Topic:filter the table with viewerfilter
Goto Forum:
  


Current Time: Tue Mar 19 04:09:45 GMT 2024

Powered by FUDForum. Page generated in 0.02647 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top