javapluginsmagic-draw

How to add a new button to the main menu bar using MagicDraw OpenAPI?


I am working with the MagicDraw OpenAPI and would like to add a new button named "TEST" to the main menu bar of MagicDraw, to the right of "Help" button (see image below, I want the button at the red circle). Result that I want

Here are useful resources

Here is my current code example, when I run the codes, the button I want doesn't appear at all:

public class Main extends Plugin{   
    public void init()
    {
        initialized = true;
        NMAction action = new MyAction("test", "TEST");
        MainMenuConfiguration configurator = new MainMenuConfiguration((MyAction) action);
        ActionsConfiguratorsManager.getInstance().addMainMenuConfigurator(configurator);
    } 
    }
public class MainMenuConfiguration implements AMConfigurator {
    public void configure(ActionsManager actionsManager) {
        ActionsCategory newCategory = new ActionsCategory("idTest", "TEST");
        ActionsManager newManager = new ActionsManager();
        newManager.addCategory(newCategory);

    }
}

Solution

  • Problem solved by creating a class

    public abstract class MainMenuConfigurator implements AMConfigurator {
    private final String id;
    private final List<NMAction> actions;
    
    public MainMenuConfigurator(String id) {
        this.id = id;
        this.actions = new ArrayList();
    }
    
    public void addAction(NMAction action) {
        this.actions.add(action);
    }
    
    public void configure(ActionsManager manager) {
        MDActionsCategory category = (MDActionsCategory)manager.getActionFor(this.id);
        if (category == null) {
            category = this.createActionsCategory();
            manager.addCategory(category);
        }
    
        category.addActions(this.actions);
    }
    
    @Nonnull
    protected abstract MDActionsCategory createActionsCategory();}
    

    Then extend this class like this

    public class MyMainMenu extends MainMenuConfigurator {
    
    public MyMainMenu() {
        super("suchi.main.menu");
    }
    
    @Nonnull
    protected MDActionsCategory createActionsCategory() {
        MDActionsCategory category = new MDActionsCategory("myID", "TEST");
        category.setNested(true);
        return category;
    }
    
    public int getPriority() {return 10;}}
    

    Finally call it

    ActionsConfiguratorsManager configurationManager = ActionsConfiguratorsManager.getInstance();
    MyMainMenu myMainMenu = new MyMainMenu();
    configurationManager.addMainMenuConfigurator(myMainMenu);