Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » Menu contribution problem in project Explorer
Menu contribution problem in project Explorer [message #1213816] Wed, 27 November 2013 12:03 Go to next message
Johan Hardy is currently offline Johan HardyFriend
Messages: 47
Registered: December 2012
Member

Dear all,

I have developed an Acceleo generator plugin able to generate WordML documentation from UML models. My UML editor is papyrus 0.10.0.

I would like to execute a command from a popup menu (right click) when I select the UML file of the Papyrus model. I have tested the commands and they work fine.

However, in the project explorer of papyrus, my popup doesn't appear. When I'm a regular project with an classic UML file, I see my popup. Here is my plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
         <menu
               icon="icons/document.gif"
               id="doc_generators"
               label="Generate documents"
               tooltip="Utilities able to generate documentation">
         </menu>
      </menuContribution>
      <menuContribution
            locationURI="popup:doc_generators?after=additions">
         <command
               commandId="com.spacebel.acceleo.uml.to.wordml.ui.generateADD"
               label="Generate ADD (WordML)"
               mode="FORCE_TEXT"
               style="push"
               tooltip="Generation of the ADD document in WordML">
            <visibleWhen>
               <with
                     variable="selection">
                  <iterate
                        ifEmpty="false">
                     <instanceof
                           value="org.eclipse.core.resources.IResource">
                     </instanceof>
                     <test
                           property="org.eclipse.core.resources.name"
                           value="*.uml">
                     </test>
                  </iterate>
               </with>
            </visibleWhen>
         </command>
         <command
               commandId="com.spacebel.acceleo.uml.to.wordml.ui.generateSRD"
               label="Generate SRD (WordML)"
               mode="FORCE_TEXT"
               style="push"
               tooltip="Generation of the SRD document in WordML">
            <visibleWhen>
               <with
                     variable="selection">
                  <iterate
                        ifEmpty="false">
                     <test
                           property="org.eclipse.core.resources.name"
                           value="*.uml">
                     </test>
                     <instanceof
                           value="org.eclipse.core.resources.IResource">
                     </instanceof>
                  </iterate>
               </with>
            </visibleWhen>
         </command>
      </menuContribution>
   </extension>
   <extension
         point="org.eclipse.ui.commands">
      <command
            id="com.spacebel.acceleo.uml.to.wordml.ui.generateADD"
            name="Generate ADD (WordML)">
      </command>
      <command
            id="com.spacebel.acceleo.uml.to.wordml.ui.generateSRD"
            name="Generate SRD (WordML)">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="com.spacebel.acceleo.uml.to.wordml.ui.GenerateSRDHandler"
            commandId="com.spacebel.acceleo.uml.to.wordml.ui.generateSRD">
      </handler>
      <handler
            class="com.spacebel.acceleo.uml.to.wordml.ui.GenerateADDHandler"
            commandId="com.spacebel.acceleo.uml.to.wordml.ui.generateADD">
      </handler>
   </extension>

</plugin>


Does anyone have an idea ? I guess that the instanceOf in my test or the URI is not correct ...

In advance, thanks for your help.
Regards;
Re: Menu contribution problem in project Explorer [message #1215992 is a reply to message #1213816] Thu, 28 November 2013 09:24 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 926
Registered: February 2011
Senior Member
Hi Johan,

If you are using the Project Explorer with the "Di View" filter activated (It is on by default), you cannot use "instanceof" in your "visible when" expression. You must use "Adapt" instead.

Here's an example of a Project Explorer menu contribution, which works for all files (Associated to Papyrus or not):

<menuContribution
    allPopups="false"
    locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
	<command
	       commandId="org.eclipse.papyrus.doSomething"
	       style="push">
	    <visibleWhen
	          checkEnabled="false">
	       <with
	             variable="selection">
	          <iterate
	                ifEmpty="false"
	                operator="and">
	             <adapt
	                   type="org.eclipse.core.resources.IFile">
	                <or>
	                   <test
	                         property="org.eclipse.core.resources.extension"
	                         value="uml">
	                   </test>
	                </or>
	             </adapt>
	          </iterate>
	       </with>
	    </visibleWhen>
	</command>
</menuContribution>


Regards,
Camille


Camille Letavernier
Re: Menu contribution problem in project Explorer [message #1216030 is a reply to message #1215992] Thu, 28 November 2013 09:45 Go to previous messageGo to next message
Johan Hardy is currently offline Johan HardyFriend
Messages: 47
Registered: December 2012
Member

Camille,

Thank you for your proposition. Actually, I have already tried this solution. However, it is not possible to adapt the SubResourceFile into an IFile. If I use your piece of code, I have an exception:

!ENTRY org.eclipse.ui 4 0 2013-11-28 10:39:07.115
!MESSAGE Unhandled event loop exception
!STACK 0
org.eclipse.e4.core.di.InjectionException: java.lang.ClassCastException: org.eclipse.papyrus.infra.onefile.model.impl.SubResourceFile cannot be cast to org.eclipse.core.resources.IFile
	at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:63)
	at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:243)
	at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:224)
etc ...


Best regards;
Johan

[Updated on: Thu, 28 November 2013 09:46]

Report message to a moderator

Re: Menu contribution problem in project Explorer [message #1216485 is a reply to message #1216030] Thu, 28 November 2013 13:53 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 926
Registered: February 2011
Senior Member
Hi Johan,

I'm using this piece of code right now, so, it definitely works. There should not be a "ClassCastException", because "Adapt" is not a Cast (Unless there is another piece of code/extension, elsewhere, which tries to cast the SubResourceFile to an IFile instead of Adapting it).

You should try to define your menu first, and, once it appears correctly, add the command/handler, and see at which point it actually fails.

Regards,
Camille


Camille Letavernier
Re: Menu contribution problem in project Explorer [message #1216586 is a reply to message #1216485] Thu, 28 November 2013 14:48 Go to previous messageGo to next message
Johan Hardy is currently offline Johan HardyFriend
Messages: 47
Registered: December 2012
Member

Hi Camille,

Your are right, it is indeed a problem in my handler:

	public Object execute(ExecutionEvent event) throws ExecutionException {
	ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
        if (selection != null & selection instanceof IStructuredSelection) {
        	model = (IFile)((IStructuredSelection) selection).getFirstElement();
etc ...


The casting from Object to IFile with the IStructuredSelection doesn't work anymore.

Is there any generic adaptable to have the IFile from IStructuredSelection ?

Thanks for your help;
Johan
Re: Menu contribution problem in project Explorer [message #1216597 is a reply to message #1216586] Thu, 28 November 2013 14:52 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 926
Registered: February 2011
Senior Member
Hi Johan,


The IStructuredSelection itself is not adaptable. However, the element inside the selection is.

if (selection != null & selection instanceof IStructuredSelection) {
    if (! selection.isEmpty()){
        Object selectedElement = ((IStructuredSelection)selection).getFirstElement();
        if (selectedElement instanceof IAdaptable){
            model = (IFile)((IAdaptable)selectedElement).getAdapter(IFile.class);
        }
    }
}



Regards,
Camille


Camille Letavernier
Re: Menu contribution problem in project Explorer [message #1216599 is a reply to message #1216597] Thu, 28 November 2013 14:54 Go to previous message
Johan Hardy is currently offline Johan HardyFriend
Messages: 47
Registered: December 2012
Member

Ahaha I have just found out the solution too but in two lines .. and in one line for you.
It works perfectly !

Thank you Camille !!
Previous Topic:Attributes not displaying in class box
Next Topic:Not possible to install Papyrus
Goto Forum:
  


Current Time: Thu Jul 09 21:18:29 GMT 2020

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

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

Back to the top