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
.
The following code example shows the
MoleculeModel
class.
public class MoleculeModel extends IlvJavaBeanSDMModel {
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.
The following code example shows the
constructor.
public MoleculeModel(Molecule molecule) { this.molecule = molecule; setIDProperty("id"); setFromProperty("firstAtom"); setToProperty("secondAtom"); }
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.
The following code example 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(); }
The model must be able to differentiate
between nodes and links.
The following code example shows the
isLinks
method, which returns
true
for
Bond
objects and
false
for
Atom
objects.
public boolean isLink(Object obj) { return obj instanceof Bond; } }