Learning The "J"

Home » Netbeans Platform » Adding Menu to the Netbeans Platform Application

Adding Menu to the Netbeans Platform Application

Programatically adding/deleting menus in netbeans platform.


public LeftSideMenuTopComponent() {
        initComponents();
        setName(Bundle.CTL_LeftSideMenuTopComponent());
        setToolTipText(Bundle.HINT_LeftSideMenuTopComponent());

	deleteTopLevelMenu("Pathology");
        FileObject mnuTemp = createParentMenu("Menu", "Pathology");

        mnuTemp = createParentMenu("Menu/Pathology", "Interfacing");
        createFinalMenu(mnuTemp, "Host Setup", "HostSetupTopComponent");

        deleteTopLevelMenu("Radiology");
        mnuTemp = createParentMenu("Menu", "Radiology");
        createFinalMenu(mnuTemp, "Radiologist Menu", "mnuFormsTopComponent");
		
}
 
void deleteTopLevelMenu(String mnuName) {
        try {
            FileObject mnuToDel = FileUtil.getConfigFile("Menu/" + mnuName);
            if (mnuToDel != null) {
                mnuToDel.delete();
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
}

FileObject createParentMenu(String mainMenu, String subMenu) {
        FileObject mnuSub = null;
        try {
            FileObject mnuMain = FileUtil.getConfigFile(mainMenu);
            mnuSub = mnuMain.createFolder(subMenu);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return mnuSub;
}

void createFinalMenu(final FileObject mnuParent, final String mnuLabel, final String tcName) {
        SwingUtilities.invokeLater(new Runnable() {
            class MyAction extends AbstractAction {
                private String mnuLabel;
                private String tcName;
                MyAction(String mnuLabel, String tcName) {
                    super(mnuLabel);
                    this.mnuLabel = mnuLabel;
                    this.tcName = tcName;
                }

                @Override
                public void actionPerformed(ActionEvent e) {
                    //JOptionPane.showMessageDialog(null, mnuLabel);

                    //content.add(dbController);
                    TopComponent tc = WindowManager.getDefault().findTopComponent(tcName);
                    tc.open();
                    tc.requestActive();
                }

//                @Override
//                public String toString() {
//                    return "MyAction{" + "mnuLabel=" + mnuLabel + '}';
//                }
                @Override
                public void setEnabled(boolean newValue) {
                    super.setEnabled(newValue);
                }
            }

            @Override
            public void run() {
                try {
                    FileObject newMenu = mnuParent.createData(mnuLabel, "instance");
                    MyAction action = new MyAction(mnuLabel, tcName);

                    newMenu.setAttribute("instanceCreate", action);
                    newMenu.setAttribute("instanceClass", action.getClass().getName());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Start here

%d bloggers like this: