Developing with the SDK > Using and Writing Data Models > JavaBeans Example > The Molecule Model

To connect the existing classes Atom, Bond and Molecule to the diagram component, you need to write a custom SDM model. Since there are already JavaBeans that represent the nodes and links of the graph (the Atom and Bond classes), the easiest solution is to write a subclass of IlvJavaBeanSDMModel. This subclass is called MoleculeModel.

Code Sample 2.3 shows the MoleculeModel class.

public class MoleculeModel extends IlvJavaBeanSDMModel
{

Code Sample 2.3 A Subclass of the JavaBean Model Class

This class represents a molecule, so you can store an instance of the Molecule class as follows:

 private Molecule molecule;

The constructor takes the molecule as a parameter. The constructor must also initialize the model to know which Bean property holds the node identifier and which Bean properties hold the link ends.

Code Sample 2.4 shows the constructor.

  public MoleculeModel(Molecule molecule)
  {
    this.molecule = molecule;
    setIDProperty("id");
    setFromProperty("firstAtom");
    setToProperty("secondAtom");
  }

Code Sample 2.4 The Constructor for the Molecule Class

The call to setIDProperty tells the superclass IlvJavaBeanSDMModel that it can use the id property (through the setId and getId methods of the Atom class) to set and retrieve the identifier of a node.

The calls to setFromProperty and setToProperty tell the superclass which properties represent the two end nodes of a link. For example, to retrieve the source node of a link (a Bond instance), the model will call getFirstAtom on the Bond instance.

The model needs to retrieve all the nodes and links of the graph, that is the Atom and Bond objects of the molecule.

Code Sample 2.5 shows the getObjects method, which retrieves the objects.

public Enumeration getObjects()
  {
    Vector v = new Vector();
    Atom[] atoms = molecule.getAtoms();
    for (int i = 0; i < atoms.length; i++) {
      v.add(atoms[i]);
    }
    Bond[] bonds = molecule.getBonds();
    for (int i = 0; i < bonds.length; i++) {
      v.add(bonds[i]);
    }
    return v.elements();
  }

Code Sample 2.5 The Method to Retrieve All Objects

The model must be able to differentiate between nodes and links.

Code Sample 2.6 shows the isLinks method, which returns true for Bond objects and false for Atom objects.

public boolean isLink(Object obj)
  {
    return obj instanceof Bond;
  }
}

Code Sample 2.6 The Boolean Method to Distinguish Between Node and Link