X

Geertjan's Blog

  • November 9, 2011

Generic Node Popup Registration Solution

Geertjan Wielenga
Product Manager

Here's a generic solution for registering Actions to be displayed on the second-level in the right-click popup menu of Nodes:


public final class GenericNodePopupPresenter extends AbstractAction implements Presenter.Popup {
    private final Map map;
    static GenericNodePopupPresenter create(Map m) {
        return new GenericNodePopupPresenter(m);
    }
    public GenericNodePopupPresenter(Map m) {
        this.map = m;
    }
    @Override
    public JMenuItem getPopupPresenter() {
        String mainMenuDisplayName = map.get("displayName").toString();
        JMenu mainPopupMenu = new JMenu(mainMenuDisplayName);
        FileObject subs = FileUtil.getConfigFile(map.get("subs").toString());
        for (FileObject oneSub : subs.getChildren()) {
            String subMenuDisplayName = oneSub.getAttribute("displayName").toString();
            JMenuItem subPopupMenu = new JMenuItem(subMenuDisplayName);
            subPopupMenu.addActionListener((ActionListener)oneSub.getAttribute("delegate"));
            mainPopupMenu.add(subPopupMenu);
        }
        return mainPopupMenu;
    }
    @Override
    public void actionPerformed(ActionEvent ev) {
        //nothing happens here
        //because the main popup menu
        //is only a container for a sub popup menu
        //and cannot itself perform an action
    }
}

This is how Actions are registered as the first-level popup menus (via Actions/Scenario, for example, so that the getActions in the Node can access these) and as second-level popup menus, notice that the first-level points to a second-level by referring to another folder where these are found:

<folder name="Actions">
    <folder name="Scenario">
        <file name="org-demo-EditMainPopupMenuAction.instance">
            <attr name="instanceCreate" methodvalue="org.demo.GenericNodePopupPresenter.create"/>
            <attr name="position" intvalue="10"/>
            <attr name="displayName" stringvalue="Edit"/>
            <attr name="subs" stringvalue="ScenarioEditSubActions"/>
        </file>
        <file name="org-demo-AnalyzeMainPopupMenuAction.instance">
            <attr name="instanceCreate" methodvalue="org.demo.GenericNodePopupPresenter.create"/>
            <attr name="position" intvalue="20"/>
            <attr name="displayName" stringvalue="Analyze"/>
            <attr name="subs" stringvalue="ScenarioAnalyzeSubActions"/>
        </file>
    </folder>
</folder>

<folder name="ScenarioEditSubActions">
    <file name="org-demo-EditLevel1PopupMenuAction.instance">
        <attr name="displayName" stringvalue="Edit Level 1"/>
        <attr name="delegate" newvalue="org.demo.EditLevel1Action"/>
        <attr name="position" intvalue="10"/>
    </file>
    <file name="org-demo-EditLevel2PopupMenuAction.instance">
        <attr name="displayName" stringvalue="Edit Level 2"/>
        <attr name="delegate" newvalue="org.demo.EditLevel2Action"/>

        <attr name="position" intvalue="20"/>
    </file>
</folder>

<folder name="ScenarioAnalyzeSubActions">
    <file name="org-demo-AnalyzeLevel1PopupMenuAction.instance">
        <attr name="displayName" stringvalue="Analyze Level 1"/>
        <attr name="delegate" newvalue="org.demo.AnalyzeLevel1Action"/>
        <attr name="position" intvalue="10"/>
    </file>
</folder>

The key to everything is the "instanceCreate", which points to "org.demo.GenericNodePopupPresenter.create",  passing in the Map of all the attributes needed for constructing the registered popup menu. Now anyone wanting to add a submenu to the Edit popup menu, can do so by registering it in the sub-level folder assigned to the Edit popup menu.

Join the discussion

Comments ( 1 )
  • Javier Ortiz Thursday, November 10, 2011

    Looks great but the code listing #1 was cut on the class declaration.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.