if (makePseudoAtom) {
newAtom = chemModel.getBuilder().newInstance(IPseudoAtom.class,atomType);
} else {
newAtom = chemModel.getBuilder().newInstance(IAtom.class,atomType);
}
IBond newBond;
if (order == IBond.Order.DOUBLE) {
newBond = chemModel.getBuilder().newInstance(IBond.class,atom, newAtom,
CDKConstants.BONDORDER_DOUBLE, stereo);
} else if (order == IBond.Order.TRIPLE) {
newBond = chemModel.getBuilder().newInstance(IBond.class,atom, newAtom,
CDKConstants.BONDORDER_TRIPLE, stereo);
} else {
newBond = chemModel.getBuilder().newInstance(IBond.class,atom, newAtom,
CDKConstants.BONDORDER_SINGLE, stereo);
}
IAtomContainer atomCon = ChemModelManipulator.getRelevantAtomContainer(
chemModel, atom);
if (atomCon == null) {
atomCon = chemModel.getBuilder().newInstance(IAtomContainer.class);
IAtomContainerSet moleculeSet = chemModel.getMoleculeSet();
if (moleculeSet == null) {
moleculeSet = chemModel.getBuilder().newInstance(IAtomContainerSet.class);
chemModel.setMoleculeSet(moleculeSet);
}
moleculeSet.addAtomContainer(atomCon);
}
// The AtomPlacer generates coordinates for the new atom
AtomPlacer atomPlacer = new AtomPlacer();
atomPlacer.setMolecule(chemModel.getBuilder().newInstance(IAtomContainer.class,atomCon));
double bondLength;
if (atomCon.getBondCount() >= 1) {
bondLength = GeometryTools.getBondLengthAverage(atomCon);
} else {
bondLength = calculateAverageBondLength(chemModel.getMoleculeSet());
}
// determine the atoms which define where the
// new atom should not be placed
List<IAtom> connectedAtoms = atomCon.getConnectedAtomsList(atom);
if (connectedAtoms.size() == 0) {
Point2d newAtomPoint = new Point2d(atom.getPoint2d());
double angle = Math.toRadians(-30);
Vector2d vec1 = new Vector2d(Math.cos(angle), Math.sin(angle));
vec1.scale(bondLength);
newAtomPoint.add(vec1);
newAtom.setPoint2d(newAtomPoint);
} else if (connectedAtoms.size() == 1) {
IAtomContainer ac = atomCon.getBuilder().newInstance(IAtomContainer.class);
ac.addAtom(atom);
ac.addAtom(newAtom);
Point2d distanceMeasure = new Point2d(0, 0); // XXX not sure about
// this?
IAtom connectedAtom = connectedAtoms.get(0);
Vector2d v = atomPlacer.getNextBondVector(atom, connectedAtom,
distanceMeasure, true);
atomPlacer.placeLinearChain(ac, v, bondLength);
} else {
IAtomContainer placedAtoms = atomCon.getBuilder().newInstance(IAtomContainer.class);
for (IAtom conAtom : connectedAtoms)
placedAtoms.addAtom(conAtom);
Point2d center2D = GeometryTools.get2DCenter(placedAtoms);
IAtomContainer unplacedAtoms = atomCon.getBuilder()
.newInstance(IAtomContainer.class);
unplacedAtoms.addAtom(newAtom);
atomPlacer.distributePartners(atom, placedAtoms, center2D,
unplacedAtoms, bondLength);
}
atomCon.addAtom(newAtom);
atomCon.addBond(newBond);
updateAtom(newBond.getAtom(0));
updateAtom(newBond.getAtom(1));
// shift the new atom a bit if it is in range of another atom
JChemPaintRendererModel model = this.getRenderer().getRenderer2DModel();
double nudgeDistance = model.getHighlightDistance() / model.getScale();
if (getClosestAtom(newAtom) != null)