public static void generateModel(AbstractJChemPaintPanel chemPaintPanel, IAtomContainer molecule, boolean generateCoordinates, boolean shiftPasted)
throws CDKException {
if (molecule == null) return;
IChemModel chemModel = chemPaintPanel.getChemModel();
IAtomContainerSet moleculeSet = chemModel.getMoleculeSet();
if (moleculeSet == null) {
moleculeSet = new AtomContainerSet();
}
// On copy & paste on top of an existing drawn structure, prevent the
// pasted section to be drawn exactly on top or to far away from the
// original by shifting it to a fixed position next to it.
if (shiftPasted) {
double maxXCurr = Double.NEGATIVE_INFINITY;
double minXPaste = Double.POSITIVE_INFINITY;
for (IAtomContainer atc : moleculeSet.atomContainers()) {
// Detect the right border of the current structure..
for (IAtom atom : atc.atoms()) {
if(atom.getPoint2d().x>maxXCurr)
maxXCurr = atom.getPoint2d().x;
}
// Detect the left border of the pasted structure..
for (IAtom atom : molecule.atoms()) {
if(atom.getPoint2d().x<minXPaste)
minXPaste = atom.getPoint2d().x;
}
}
if (maxXCurr != Double.NEGATIVE_INFINITY && minXPaste != Double.POSITIVE_INFINITY) {
// Shift the pasted structure to be nicely next to the existing one.
final int MARGIN=1;
final double SHIFT = maxXCurr - minXPaste;
for (IAtom atom : molecule.atoms()) {
atom.setPoint2d(new Point2d (atom.getPoint2d().x+MARGIN+SHIFT, atom.getPoint2d().y ));
}
}
}
if(generateCoordinates){
// now generate 2D coordinates
StructureDiagramGenerator sdg = new StructureDiagramGenerator();
sdg.setTemplateHandler(new TemplateHandler(moleculeSet.getBuilder()));
try {
sdg.setMolecule(molecule);
sdg.generateCoordinates();
molecule = sdg.getMolecule();
} catch (Exception exc) {
JOptionPane.showMessageDialog(chemPaintPanel, GT.get("Structure could not be generated"));
throw new CDKException(
"Cannot depict structure");
}
}
if(moleculeSet.getAtomContainer(0).getAtomCount()==0)
moleculeSet.getAtomContainer(0).add(molecule);
else
moleculeSet.addAtomContainer(molecule);
IUndoRedoFactory undoRedoFactory= chemPaintPanel.get2DHub().getUndoRedoFactory();
UndoRedoHandler undoRedoHandler= chemPaintPanel.get2DHub().getUndoRedoHandler();
if (undoRedoFactory!=null) {