JChemPaintRendererModel rModel =
chemModelRelay.getRenderer().getRenderer2DModel();
IAtom atom = chemModelRelay.getClosestAtom(worldCoord);
IBond bond = chemModelRelay.getClosestBond(worldCoord);
IChemObjectSelection localSelection = rModel.getSelection();
IChemObject chemObject = getHighlighted(worldCoord, atom, bond);
if (localSelection==null || !localSelection.contains(chemObject)) {
if (chemObject != null) {
localSelection = new SingleSelection<IChemObject>(chemObject);
} else {
//if clicked inside a square comprising the selection, keep it, otherwise void it
Double upperX = null, lowerX = null, upperY = null, lowerY = null;
IAtomContainer selectedAtoms=null;
if(localSelection!=null)
selectedAtoms = localSelection.getConnectedAtomContainer();
if(selectedAtoms!=null) {
for (int i = 0; i < selectedAtoms.getAtomCount(); i++) {
if (upperX == null) {
upperX = lowerX = selectedAtoms.getAtom(i).getPoint2d().x;
upperY = lowerY = selectedAtoms.getAtom(i).getPoint2d().y;
} else {
double currX = selectedAtoms.getAtom(i).getPoint2d().x;
if (currX > upperX)
upperX = currX;
if (currX < lowerX)
lowerX = currX;
double currY = selectedAtoms.getAtom(i).getPoint2d().y;
if (currY > upperY)
upperY = currY;
if (currY < lowerY)
lowerY = currY;
}
}
}
if (upperX!=null && upperY!=null) {
if (!(worldCoord.x>=lowerX &&
worldCoord.y>=lowerY &&
worldCoord.x<=upperX &&
worldCoord.y<=upperY)
)
localSelection = AbstractSelection.EMPTY_SELECTION;
}
else
localSelection = AbstractSelection.EMPTY_SELECTION;
}
}
setSelection(localSelection);
return localSelection.getConnectedAtomContainer();
}