Custom Browser and Diagram Context Menus

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Custom Browser and Diagram Context Menus

Postby nnorwood@mitre.org » Thu Apr 18, 2019 9:33 am

Hello,
I am working on a plugin and would like to add custom context menus for both the browser and diagrams. I have been able to do this successfully using the BrowerContextAMContextAMConfigurator and the DiagramConextAMConfigurator classes as shown below from my MyPlugin class that extends Plugin
Code: Select all
   @Override
   public void init()
   {
      initialized = true;
      Application.getInstance().getGUILog().showMessage("MyPlugin Initialized.");
      ActionsConfiguratorsManager manager = ActionsConfiguratorsManager.getInstance();
      manager.addDiagramContextConfigurator(DiagramTypeConstants.UML_ANY_DIAGRAM, configureDiagramContextActions());
      manager.addContainmentBrowserContextConfigurator(configureBrowserContext());
   }
   

   private BrowserContextAMConfigurator configureBrowserContext()
   {
      BrowserContextAMConfigurator configurator = new BrowserContextAMConfigurator()
      {
         @Override
         public int getPriority()
         {
            return ConfiguratorWithPriority.HIGH_PRIORITY;
         }
         @Override
         public void configure(ActionsManager manager, Tree tree)
         {
            ActionsCategory category = new ActionsCategory(null, "My Browser Actions");
            category.addAction(new SampleNMActionA(null, "Sample Action A", tree));
            category.addAction(new SampleNMActionB(null, "Sample Action B", tree));
            manager.addCategory(category);
         }

      };
      return configurator;
   }

   private DiagramContextAMConfigurator configureDiagramContextActions()
   {
      DiagramContextAMConfigurator configurator = new DiagramContextAMConfigurator()
      {         
         @Override
         public void configure(ActionsManager mngr,
               DiagramPresentationElement dpe,
               PresentationElement[] peArray,
               PresentationElement pe)
         {
            ActionsCategory category = new ActionsCategory(null, "My Diagram Actions");
            category.addAction(new SampleNMActionA(null, "Sample Action A", dpe));
            category.addAction(new SampleNMActionB(null, "Sample Action B", dpe));
            mngr.addCategory(category);
         }

         @Override
         public int getPriority()
         {
            return ConfiguratorWithPriority.MEDIUM_PRIORITY;
         }

      
      };
      return configurator;
   }



My goal is to have only one of those actions visible depending on the browser item or presentation element selected in the diagram.

For example, for anything that is a UML Class (or extension) I'd like to show one menu option (NMAction extension) and for anything that is an Operation, I'd like to have a different NMAction extension displayed in the context menu. I am having some trouble figuring this out just looking at the Open API. Could you first let me know if this is possible with the OpenAPI and second, please point me to the best way to accomplish this task.

Thank you,
Nathan
nnorwood@mitre.org
Forum Beginner
Forum Beginner
 
Posts: 40
Posts Rating:4
Joined: Tue Jun 17, 2014 11:19 am

Re: Custom Browser and Diagram Context Menus

Postby nnorwood@mitre.org » Fri Apr 19, 2019 5:23 am

Solved! Simply needed to check in the configure methods like this. Not sure why I didn't try this earlier.

Code: Select all
private BrowserContextAMConfigurator configureBrowserContext()
   {
      BrowserContextAMConfigurator configurator = new BrowserContextAMConfigurator()
      {
         @Override
         public int getPriority()
         {
            return ConfiguratorWithPriority.HIGH_PRIORITY;
         }
         @Override
         public void configure(ActionsManager manager, Tree tree)
         {
            ActionsCategory category = new ActionsCategory(null, "My Browser Actions");
             if(tree.getSelectedNode().getElementForCopyAction() instanceof Class){
                        category.addAction(new SampleNMActionA(null, "Sample Action A", tree));
                        manager.addCategory(category);
            }
            if(tree.getSelectedNode().getElementForCopyAction() instanceof Operation){
                    category.addAction(new SampleNMActionB(null, "Sample Action B", tree));
                    manager.addCategory(category);
           }
      };
      return configurator;
   }



However, that does bring up another question. I would like to know if there is a better way to get the element selected in the browser. The Tree#getElementForCopyAction() method that I am currently using is deprecated but I could not find a different method to get the Element selected.

Thank you!
Nathan
nnorwood@mitre.org
Forum Beginner
Forum Beginner
 
Posts: 40
Posts Rating:4
Joined: Tue Jun 17, 2014 11:19 am

Re: Custom Browser and Diagram Context Menus

Postby gdhardy1 » Fri Apr 19, 2019 12:33 pm

You can use tree.getSelectedNode().getUserObject()
gdhardy1
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:2
Joined: Thu Oct 18, 2018 11:24 am


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 0 guests