Customizing MarvinSketch GUI - Assign New Action

    Define a new action for the chemaxon.marvin.beans.MSketchPane component that you would like to integrate into your custom application. Your action has to be an extension of the javax.swing.AbstractAction class.

    In the below example, the new action is called Foo.

     private final Action newFooAction = new AbstractAction("Foo") {
      public void actionPerformed(ActionEvent ev) {
       System.err.println(ev);
      }
     };

    An action that has a non-null value for Action.SELECTED_KEY property is displayed in different ways in the menu, depending on the value of the Radio property. If the Radio is set to true it is displayed as a JRadioButtonMenuItem otherwise, as a JCheckBoxMenuItem. This property does not affect the behavior of the action in toolbars. It will be always displayed as a JToggleButton there. For examples, see the code of the newToggleAction and newRadioAction actions in the attached source code.

    After that, add the new action to the action map of the sketcher

    msketchPane.getActionMap().put("foo", newFooAction);

    To be able to access it in the Customize dialog, set the useComponentActions parameter to true.

    msketchPane.setParams("useComponentActions=true\n");

    To test this functionality, run your custom application, open View/Customize menu, push the Add button. The Custom entry appears among Categories. Under this category, you will see Foo in the Commands list.

    Add commands dialog

    Thus, you can insert this command manually into the running application to the desired place. For example, you can insert the Foo action to the Atom popup menu. But this modification is not preserved for the next time.

    To add the new action permanently, define a menu configuration XML for your application. For more information about exporting a configuration file, see Configurations of MarvinSketch. In our example, the new action appears in the menu config file like this:

    <add path="popup/atom">
       <item id="foo"/>
    </add>

    The full XML file can be download from here: foo.xml.

    If the config XML is ready, place it into the codebase of your application. The codebase is the same directory where the main jar of your application is located.

    Modify the initialization of the sketcher component. Create a UserSettings object and specify the location of the XML file in the menuconfig parameter then give it for MSketchPane.

    UserSettings settings = new UserSettings();
    settings.setProperty("menuconfig","foo.xml");
    MSketchPane msketchPane = new MSketchPane(settings);

    The full java code is available here: NewActionInSketch.java