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 bean,

  • a text area representing the molecule contained by the bean as String (e.g. Smiles),

  • and 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/48992727/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 3 working modes:

  1. The 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.

  2. The Auto-Import mode updates the molecule when the text is changed in the text area using a document change listener. Note, that 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.

  3. In the 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 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();
}
}
});