Marvin Sketch Simple Bean

    In this example, MarvinSketch is used as a JavaBean component. The actual JavaBean class is called MSketchPane.

    The main goal of the example is to show how to create and embed the MarvinSketch bean in a JPanel with some additional functionalities such as getting and setting molecules.

    A GUI component is created in the example which consists of three major parts:

    • A MarvinSketch bea.,

    • A text area representing the molecule contained by the bean as String (for example, Smiles).

    • Some additional components to synchronize the content of the bean with the text area.

    The String representation of the molecule is called the source of the molecule or simply source .

    The image below shows the component that is created:

    images/download/attachments/1802734/beans.png

    There are two major operations; export and import . When the Export button is pressed, the source of the current molecule will be set in the text area. The format of the molecule source is determined by the state of a format chooser combo box. Pressing the Import button makes the bean read a molecule by converting the string content of the text area to a molecule.

    The component has the following working modes:

    • Auto-Exporter Mode: Automatically exports the molecule on every molecule modification. The source of the molecule is continuously updated upon sketching the molecule. This is solved by using a property change listener because the MarvinSketch bean fires a property change event every time the molecule is changed.

    • Auto-Import Mode: Updates the molecule when the text is changed in the text area using a document change listener. In case the text area contains an invalid molecule string, an error message appears. This can be prevented for example by adding a timer to the text area so that the molecule is refreshed only when typing is complete. We do not intend to use this mechanism in this example.

    • Manual Mode: No auto-synchronization is performed.

    The following paragraphs demonstrate the major parts of the SketchSimple.java code.

    Creating the JavaBean

    Creating the JavaBean component is very easy. With a single line of code, it is ready and can simply be added to any Swing container.

    
    sketchPane = new MSketchPane();

    The complexity rises at using the parameters of the bean since there are many of them. The following examples will show the ones that are the most frequently used.

    Getting the Source of the Current Molecule

    The following method exports the current molecule being visible on the MarvinSketch canvas in the desired format.

    
    String molS = sketchPane.getMol(format);

    Setting the Current Molecule from Source

    The content of the MarvinSketch canvas can be changed by setting a molecule. This replaces the current molecule by discarding it, and it can only be retrieved using the Undo command. If the format is not set explicitly, MSketchPane automatically detects the format of the molecule being set in the String representation.

    
    sketchPane.setMol(molS);

    Adding a Property Change Listener to Follow Molecule Changes

    The MarvinSketch bean fires a java.beans.PropertyChangeEvent every time the molecule is modified or replaced, which can be handled by using a java.beans.PropertyChangeListener.

    
        sketchPane.addPropertyChangeListener("mol", new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if(behavior == EXPORTER) {
                    exportActionPerformed();
                }
            }
        });