From: <sh...@us...> - 2009-03-06 11:10:38
|
Revision: 14361 http://cdk.svn.sourceforge.net/cdk/?rev=14361&view=rev Author: shk3 Date: 2009-03-06 11:10:33 +0000 (Fri, 06 Mar 2009) Log Message: ----------- moved undo/redo to hub Modified Paths: -------------- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddAtomModule.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddBondModule.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddRingModule.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AlterBondStereoModule.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/ControllerHub.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/DrawBondModule.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/IChemModelRelay.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/MoveModule.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/RemoveModule.java Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddAtomModule.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddAtomModule.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddAtomModule.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -58,45 +58,9 @@ if (closestAtom == null || closestAtom.getPoint2d().distance(worldCoord) > dH) { - IChemModel chemModel = chemModelRelay.getIChemModel(); - - IAtomContainer undoredocontainer = - chemModel.getBuilder().newAtomContainer(); - undoredocontainer.addAtom( - chemModelRelay.addAtom(atomType, worldCoord)); - - if (chemModelRelay.getUndoRedoFactory() != null - && chemModelRelay.getUndoRedoHandler() != null) { - IUndoRedoable undoredo = - chemModelRelay.getUndoRedoFactory().getAddAtomsAndBondsEdit( - chemModel, undoredocontainer, "Add Atom", - chemModelRelay.getController2DModel()); - - chemModelRelay.getUndoRedoHandler().postEdit(undoredo); - } + chemModelRelay.addAtom(atomType, worldCoord); } else { - String oldsymbol = closestAtom.getSymbol(); - closestAtom.setSymbol(atomType); - if (chemModelRelay.getUndoRedoFactory() != null - && chemModelRelay.getUndoRedoHandler() != null) { - String symbol = closestAtom.getSymbol(); - IUndoRedoable undoredo = - chemModelRelay.getUndoRedoFactory().getChangeAtomSymbolEdit( - closestAtom, - oldsymbol, - symbol, - "Change atom symbol to "+ symbol); - chemModelRelay.getUndoRedoHandler().postEdit(undoredo); - } - // configure the atom, so that the atomic number matches the symbol - try { - IsotopeFactory.getInstance( - closestAtom.getBuilder()).configure(closestAtom); - } catch (Exception exception) { - // logger.error("Error while configuring atom"); - // logger.debug(exception); - - } + chemModelRelay.setSymbol(closestAtom, atomType); } chemModelRelay.updateView(); } Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddBondModule.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddBondModule.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddBondModule.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -49,84 +49,10 @@ */ public class AddBondModule extends ControllerModuleAdapter { - IUndoRedoFactory undoredofactory; - UndoRedoHandler undoredohandler; - public AddBondModule(IChemModelRelay relay) { super(relay); - this.undoredohandler=relay.getUndoRedoHandler(); - this.undoredofactory=relay.getUndoRedoFactory(); } - private void cycleBondValence(IBond bond) { - IBond.Order[] orders=new IBond.Order[2]; - Integer[] stereos=new Integer[2]; - orders[1]=bond.getOrder(); - stereos[1]=bond.getStereo(); - // special case : reset stereo bonds - if (bond.getStereo() != STEREO_BOND_NONE) { - bond.setStereo(STEREO_BOND_NONE); - }else{ - // cycle the bond order up to maxOrder - IBond.Order maxOrder = - super.chemModelRelay.getController2DModel().getMaxOrder(); - if (BondManipulator.isLowerOrder(bond.getOrder(), maxOrder)) { - BondManipulator.increaseBondOrder(bond); - } else { - bond.setOrder(IBond.Order.SINGLE); - } - } - orders[0]=bond.getOrder(); - stereos[0]=bond.getStereo(); - Map<IBond, IBond.Order[]> changedBonds = new HashMap<IBond, IBond.Order[]>(); - Map<IBond, Integer[]> changedBondsStereo = new HashMap<IBond, Integer[]>(); - changedBonds.put(bond,orders); - changedBondsStereo.put(bond, stereos); - if(undoredofactory!=null && undoredohandler!=null){ - IUndoRedoable undoredo = undoredofactory.getAdjustBondOrdersEdit(changedBonds, changedBondsStereo, "Adjust Bond Order"); - undoredohandler.postEdit(undoredo); - } - chemModelRelay.updateView(); - } - - private void addBondToAtom(IAtom atom) { - IAtomContainer undoRedoContainer = chemModelRelay.getIChemModel().getBuilder().newAtomContainer(); - String atomType = - chemModelRelay.getController2DModel().getDrawElement(); - IAtom newAtom = chemModelRelay.addAtom(atomType, atom); - undoRedoContainer.addAtom(newAtom); - IAtomContainer atomContainer = - ChemModelManipulator.getRelevantAtomContainer( - chemModelRelay.getIChemModel(), newAtom); - IBond newBond = atomContainer.getBond(atom, newAtom); - undoRedoContainer.addBond(newBond); - chemModelRelay.updateView(); - if(undoredofactory!=null && undoredohandler!=null){ - IUndoRedoable undoredo = undoredofactory.getAddAtomsAndBondsEdit(chemModelRelay.getIChemModel(), undoRedoContainer, "Add Bond",chemModelRelay.getController2DModel()); - undoredohandler.postEdit(undoredo); - } - } - - private void addNewBond(Point2d worldCoordinate) { - IAtomContainer undoRedoContainer = chemModelRelay.getIChemModel().getBuilder().newAtomContainer(); - String atomType = - chemModelRelay.getController2DModel().getDrawElement(); - IAtom atom = chemModelRelay.addAtom(atomType, worldCoordinate); - undoRedoContainer.addAtom(atom); - IAtom newAtom = chemModelRelay.addAtom(atomType, atom); - undoRedoContainer.addAtom(newAtom); - IAtomContainer atomContainer = - ChemModelManipulator.getRelevantAtomContainer( - chemModelRelay.getIChemModel(), newAtom); - IBond newBond = atomContainer.getBond(atom, newAtom); - undoRedoContainer.addBond(newBond); - chemModelRelay.updateView(); - if(undoredofactory!=null && undoredohandler!=null){ - IUndoRedoable undoredo = undoredofactory.getAddAtomsAndBondsEdit(chemModelRelay.getIChemModel(), undoRedoContainer, "Add Bond",chemModelRelay.getController2DModel()); - undoredohandler.postEdit(undoredo); - } - } - public void mouseClickedDown(Point2d worldCoordinate) { IAtom closestAtom = chemModelRelay.getClosestAtom(worldCoordinate); IBond closestBond = chemModelRelay.getClosestBond(worldCoordinate); @@ -136,19 +62,23 @@ double dB = super.distanceToBond(closestBond, worldCoordinate); if (noSelection(dA, dB, dH)) { - addNewBond(worldCoordinate); + chemModelRelay.addNewBond(worldCoordinate); } else if (isAtomOnlyInHighlightDistance(dA, dB, dH)) { - this.addBondToAtom(closestAtom); + String atomType = + chemModelRelay.getController2DModel().getDrawElement(); + chemModelRelay.addAtom(atomType, closestAtom); } else if (isBondOnlyInHighlightDistance(dA, dB, dH)) { - this.cycleBondValence(closestBond); + chemModelRelay.cycleBondValence(closestBond); } else { if (dA <= dB) { - this.addBondToAtom(closestAtom); + String atomType = + chemModelRelay.getController2DModel().getDrawElement(); + chemModelRelay.addAtom(atomType, closestAtom); } else { - this.cycleBondValence(closestBond); + chemModelRelay.cycleBondValence(closestBond); } } - + chemModelRelay.updateView(); } public String getDrawModeString() { Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddRingModule.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddRingModule.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddRingModule.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -26,7 +26,6 @@ import javax.vecmath.Point2d; -import org.openscience.cdk.controller.undoredo.IUndoRedoable; import org.openscience.cdk.interfaces.IAtom; import org.openscience.cdk.interfaces.IBond; import org.openscience.cdk.interfaces.IRing; @@ -89,21 +88,16 @@ double dA = super.distanceToAtom(closestAtom, worldCoord); double dB = super.distanceToBond(closestBond, worldCoord); - IRing newring; if (noSelection(dA, dB, dH)) { - newring = this.addRingToEmptyCanvas(worldCoord); + this.addRingToEmptyCanvas(worldCoord); } else if (isAtomOnlyInHighlightDistance(dA, dB, dH) || dA < dB) { - newring = this.addRingToAtom(closestAtom); + this.addRingToAtom(closestAtom); } else if (isBondOnlyInHighlightDistance(dA, dB, dH) || dB < dA) { - newring = this.addRingToBond(closestBond); + this.addRingToBond(closestBond); } else { // the closest bond and closest atom are equidistant - newring = this.addRingToAtom(closestAtom); + this.addRingToAtom(closestAtom); } - if(chemModelRelay.getUndoRedoFactory()!=null && chemModelRelay.getUndoRedoHandler()!=null){ - IUndoRedoable undoredo = chemModelRelay.getUndoRedoFactory().getAddAtomsAndBondsEdit(chemModelRelay.getIChemModel(), newring.getBuilder().newAtomContainer(newring), this.getDrawModeString(),chemModelRelay.getController2DModel()); - chemModelRelay.getUndoRedoHandler().postEdit(undoredo); - } chemModelRelay.updateView(); } Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AlterBondStereoModule.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AlterBondStereoModule.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AlterBondStereoModule.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -37,6 +37,7 @@ import javax.vecmath.Point2d; +import org.openscience.cdk.controller.ControllerHub.Direction; import org.openscience.cdk.controller.undoredo.IUndoRedoable; import org.openscience.cdk.interfaces.IAtom; import org.openscience.cdk.interfaces.IAtomContainer; @@ -53,107 +54,15 @@ */ public class AlterBondStereoModule extends ControllerModuleAdapter { - public enum Direction { UP, DOWN }; private Direction desiredDirection; public AlterBondStereoModule(IChemModelRelay chemModelRelay, Direction desiredDirection) { super(chemModelRelay); this.desiredDirection = desiredDirection; } - - /** - * Change the stereo bond from start->end to start<-end. - * - * @param bond the bond to change - * @param stereo the current stereo of that bond - */ - private void flipDirection(IBond bond, int stereo) { - if (stereo == STEREO_BOND_UP) bond.setStereo(STEREO_BOND_UP_INV); - else if (stereo == STEREO_BOND_UP_INV) bond.setStereo(STEREO_BOND_UP); - else if (stereo == STEREO_BOND_DOWN_INV) bond.setStereo(STEREO_BOND_DOWN); - else if (stereo == STEREO_BOND_DOWN) bond.setStereo(STEREO_BOND_DOWN_INV); - } - - /** - * Change the stereo of the bond from UP<->DOWN. - * @param bond the bond to change - * @param stereo the current stereo of the bond - */ - private void flipOrientation(IBond bond, int stereo) { - if (stereo == STEREO_BOND_UP) bond.setStereo(STEREO_BOND_DOWN_INV); - else if (stereo == STEREO_BOND_UP_INV) bond.setStereo(STEREO_BOND_DOWN); - else if (stereo == STEREO_BOND_DOWN_INV) bond.setStereo(STEREO_BOND_UP); - else if (stereo == STEREO_BOND_DOWN) bond.setStereo(STEREO_BOND_UP_INV); - } - - private boolean isUp(int stereo) { - return stereo == STEREO_BOND_UP || stereo == STEREO_BOND_UP_INV; - } - - private boolean isDown(int stereo) { - return stereo == STEREO_BOND_DOWN || stereo == STEREO_BOND_DOWN_INV; - } - - private boolean noStereo(int stereo) { - return stereo == STEREO_BOND_NONE || stereo == STEREO_BOND_UNDEFINED; - } - - private void makeNewStereoBond(IAtom atom) { - String atomType = - chemModelRelay.getController2DModel().getDrawElement(); - IAtom newAtom = chemModelRelay.addAtom(atomType, atom); - IAtomContainer undoRedoContainer=chemModelRelay.getIChemModel().getBuilder().newAtomContainer(); - - // XXX these calls would not be necessary if addAtom returned a bond - IAtomContainer atomContainer = - ChemModelManipulator.getRelevantAtomContainer( - chemModelRelay.getIChemModel(), newAtom); - IBond newBond = atomContainer.getBond(atom, newAtom); - if (desiredDirection == Direction.UP) { - newBond.setStereo(STEREO_BOND_UP); - } else { - newBond.setStereo(STEREO_BOND_DOWN); - } - undoRedoContainer.addAtom(newAtom); - undoRedoContainer.addBond(newBond); - if(chemModelRelay.getUndoRedoFactory()!=null && chemModelRelay.getUndoRedoHandler()!=null){ - IUndoRedoable undoredo = chemModelRelay.getUndoRedoFactory().getAddAtomsAndBondsEdit(chemModelRelay.getIChemModel(), undoRedoContainer, "Add Stereo Bond",chemModelRelay.getController2DModel()); - chemModelRelay.getUndoRedoHandler().postEdit(undoredo); - } - } - - private void makeBondStereo(IBond bond) { - int stereo = bond.getStereo(); - boolean isUp = isUp(stereo); - boolean isDown = isDown(stereo); - boolean noStereo = noStereo(stereo); - if (isUp && desiredDirection == Direction.UP) { - flipDirection(bond, stereo); - } else if (isDown && desiredDirection == Direction.UP) { - flipOrientation(bond, stereo); - } else if (isUp && desiredDirection == Direction.DOWN) { - flipOrientation(bond, stereo); - } else if (isDown && desiredDirection == Direction.DOWN) { - flipDirection(bond, stereo); - } else if (noStereo && desiredDirection == Direction.UP) { - bond.setStereo(STEREO_BOND_UP); - } else if (noStereo && desiredDirection == Direction.DOWN) { - bond.setStereo(STEREO_BOND_DOWN); - } - Integer[] stereos=new Integer[2]; - stereos[1]=stereo; - stereos[0]=bond.getStereo(); - Map<IBond, IBond.Order[]> changedBonds = new HashMap<IBond, IBond.Order[]>(); - Map<IBond, Integer[]> changedBondsStereo = new HashMap<IBond, Integer[]>(); - changedBondsStereo.put(bond, stereos); - if(chemModelRelay.getUndoRedoFactory()!=null && chemModelRelay.getUndoRedoHandler()!=null){ - IUndoRedoable undoredo = chemModelRelay.getUndoRedoFactory().getAdjustBondOrdersEdit(changedBonds, changedBondsStereo, "Adjust Bond Stereo"); - chemModelRelay.getUndoRedoHandler().postEdit(undoredo); - } - } - - public void mouseClickedDown(Point2d worldCoord) { + + public void mouseClickedDown(Point2d worldCoord) { IAtom atom = this.chemModelRelay.getClosestAtom(worldCoord); IBond bond = this.chemModelRelay.getClosestBond(worldCoord); @@ -165,12 +74,12 @@ // probably don't make empty stereo bonds in blank space.. return; } else if (super.isAtomOnlyInHighlightDistance(dA, dB, dH)) { - this.makeNewStereoBond(atom); + chemModelRelay.makeNewStereoBond(atom, desiredDirection); } else if (super.isBondOnlyInHighlightDistance(dA, dB, dH)) { - this.makeBondStereo(bond); + chemModelRelay.makeBondStereo(bond, desiredDirection); } else { // if same distance, default to changing the bond - this.makeBondStereo(bond); + chemModelRelay.makeBondStereo(bond, desiredDirection); } chemModelRelay.updateView(); } Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/ControllerHub.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/ControllerHub.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/ControllerHub.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -25,6 +25,12 @@ */ package org.openscience.cdk.controller; +import static org.openscience.cdk.CDKConstants.STEREO_BOND_DOWN; +import static org.openscience.cdk.CDKConstants.STEREO_BOND_DOWN_INV; +import static org.openscience.cdk.CDKConstants.STEREO_BOND_NONE; +import static org.openscience.cdk.CDKConstants.STEREO_BOND_UP; +import static org.openscience.cdk.CDKConstants.STEREO_BOND_UP_INV; + import java.awt.geom.Rectangle2D; import java.io.IOException; import java.util.ArrayList; @@ -69,6 +75,7 @@ import org.openscience.cdk.renderer.selection.IChemObjectSelection; import org.openscience.cdk.tools.SaturationChecker; import org.openscience.cdk.tools.manipulator.AtomContainerManipulator; +import org.openscience.cdk.tools.manipulator.BondManipulator; import org.openscience.cdk.tools.manipulator.ChemModelManipulator; import org.openscience.cdk.tools.manipulator.ReactionSetManipulator; import org.openscience.cdk.validate.ProblemMarker; @@ -86,6 +93,7 @@ */ public class ControllerHub implements IMouseEventRelay, IChemModelRelay { + public enum Direction { UP, DOWN }; private IChemModel chemModel; private IControllerModel controllerModel; @@ -362,7 +370,7 @@ } - public IAtomContainer removeAtom(IAtom atom) { + public IAtomContainer removeAtomWithoutUndo(IAtom atom) { IAtomContainer ac = atom.getBuilder().newAtomContainer(); ac.addAtom(atom); Iterator<IBond> connbonds = ChemModelManipulator.getRelevantAtomContainer(chemModel, atom).getConnectedBondsList(atom).iterator(); @@ -373,7 +381,26 @@ return ac; } + public IAtomContainer removeAtom(IAtom atom) { + IAtomContainer ac = removeAtomWithoutUndo(atom); + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IUndoRedoable undoredo = getUndoRedoFactory().getRemoveAtomsAndBondsEdit(getIChemModel(), ac, "Remove Atom"); + getUndoRedoHandler().postEdit(undoredo); + } + return ac; + } + public IAtom addAtom(String atomType, Point2d worldCoord) { + IAtomContainer undoRedoContainer = chemModel.getBuilder().newAtomContainer(); + undoRedoContainer.addAtom(addAtomWithoutUndo(atomType, worldCoord)); + if (getUndoRedoFactory() != null && getUndoRedoHandler() != null) { + IUndoRedoable undoredo = getUndoRedoFactory().getAddAtomsAndBondsEdit(chemModel, undoRedoContainer, "Add Atom", getController2DModel()); + getUndoRedoHandler().postEdit(undoredo); + } + return undoRedoContainer.getAtom(0); + } + + public IAtom addAtomWithoutUndo(String atomType, Point2d worldCoord) { //FIXME: update atoms for implicit H's or so IAtom newAtom = chemModel.getBuilder().newAtom(atomType, worldCoord); @@ -393,7 +420,22 @@ return newAtom; } - public IAtom addAtom(String atomType, IAtom atom) { + public IAtom addAtom(String atomType, IAtom atom) { + IAtomContainer undoRedoContainer = atom.getBuilder().newAtomContainer(); + undoRedoContainer.addAtom(addAtomWithoutUndo(atomType, atom)); + IAtomContainer atomContainer = + ChemModelManipulator.getRelevantAtomContainer( + getIChemModel(), undoRedoContainer.getAtom(0)); + IBond newBond = atomContainer.getBond(atom, undoRedoContainer.getAtom(0)); + undoRedoContainer.addBond(newBond); + if (getUndoRedoFactory() != null && getUndoRedoHandler() != null) { + IUndoRedoable undoredo = getUndoRedoFactory().getAddAtomsAndBondsEdit(chemModel, undoRedoContainer, "Add Atom", getController2DModel()); + getUndoRedoHandler().postEdit(undoredo); + } + return undoRedoContainer.getAtom(0); + } + + public IAtom addAtomWithoutUndo(String atomType, IAtom atom) { IAtom newAtom = chemModel.getBuilder().newAtom(atomType); IBond newBond = chemModel.getBuilder().newBond(atom, newAtom); IAtomContainer atomCon = @@ -453,7 +495,82 @@ return newAtom; } - public void moveTo( IAtom atom, Point2d worldCoords ) { + public void addNewBond(Point2d worldCoordinate) { + IAtomContainer undoRedoContainer = getIChemModel().getBuilder().newAtomContainer(); + String atomType = + getController2DModel().getDrawElement(); + IAtom atom = addAtomWithoutUndo(atomType, worldCoordinate); + undoRedoContainer.addAtom(atom); + IAtom newAtom = addAtomWithoutUndo(atomType, atom); + undoRedoContainer.addAtom(newAtom); + IAtomContainer atomContainer = + ChemModelManipulator.getRelevantAtomContainer( + getIChemModel(), newAtom); + IBond newBond = atomContainer.getBond(atom, newAtom); + undoRedoContainer.addBond(newBond); + structureChanged(); + if(undoredofactory!=null && undoredohandler!=null){ + IUndoRedoable undoredo = undoredofactory.getAddAtomsAndBondsEdit(getIChemModel(), undoRedoContainer, "Add Bond",getController2DModel()); + undoredohandler.postEdit(undoredo); + } + } + + public void cycleBondValence(IBond bond) { + IBond.Order[] orders=new IBond.Order[2]; + Integer[] stereos=new Integer[2]; + orders[1]=bond.getOrder(); + stereos[1]=bond.getStereo(); + // special case : reset stereo bonds + if (bond.getStereo() != STEREO_BOND_NONE) { + bond.setStereo(STEREO_BOND_NONE); + }else{ + // cycle the bond order up to maxOrder + IBond.Order maxOrder = + getController2DModel().getMaxOrder(); + if (BondManipulator.isLowerOrder(bond.getOrder(), maxOrder)) { + BondManipulator.increaseBondOrder(bond); + } else { + bond.setOrder(IBond.Order.SINGLE); + } + } + orders[0]=bond.getOrder(); + stereos[0]=bond.getStereo(); + Map<IBond, IBond.Order[]> changedBonds = new HashMap<IBond, IBond.Order[]>(); + Map<IBond, Integer[]> changedBondsStereo = new HashMap<IBond, Integer[]>(); + changedBonds.put(bond,orders); + changedBondsStereo.put(bond, stereos); + if(undoredofactory!=null && undoredohandler!=null){ + IUndoRedoable undoredo = undoredofactory.getAdjustBondOrdersEdit(changedBonds, changedBondsStereo, "Adjust Bond Order"); + undoredohandler.postEdit(undoredo); + } + } + + public void makeNewStereoBond(IAtom atom, Direction desiredDirection) { + String atomType = + getController2DModel().getDrawElement(); + IAtom newAtom = addAtomWithoutUndo(atomType, atom); + IAtomContainer undoRedoContainer=getIChemModel().getBuilder().newAtomContainer(); + + // XXX these calls would not be necessary if addAtom returned a bond + IAtomContainer atomContainer = + ChemModelManipulator.getRelevantAtomContainer( + getIChemModel(), newAtom); + IBond newBond = atomContainer.getBond(atom, newAtom); + + if (desiredDirection == Direction.UP) { + newBond.setStereo(STEREO_BOND_UP); + } else { + newBond.setStereo(STEREO_BOND_DOWN); + } + undoRedoContainer.addAtom(newAtom); + undoRedoContainer.addBond(newBond); + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IUndoRedoable undoredo = getUndoRedoFactory().getAddAtomsAndBondsEdit(getIChemModel(), undoRedoContainer, "Add Stereo Bond",getController2DModel()); + getUndoRedoHandler().postEdit(undoredo); + } + } + + public void moveToWithoutUndo( IAtom atom, Point2d worldCoords ) { if ( atom != null ) { Point2d atomCoord = new Point2d( worldCoords ); atom.setPoint2d( atomCoord ); @@ -461,7 +578,21 @@ coordinatesChanged(); } - public void moveTo( IBond bond, Point2d point ) { + public void moveTo( IAtom atom, Point2d worldCoords ) { + if ( atom != null ) { + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IAtomContainer undoRedoContainer = chemModel.getBuilder().newAtomContainer(); + undoRedoContainer.addAtom(atom); + Vector2d end=new Vector2d(); + end.sub(worldCoords,atom.getPoint2d()); + IUndoRedoable undoredo = getUndoRedoFactory().getMoveAtomEdit(undoRedoContainer, end, "Move atom"); + getUndoRedoHandler().postEdit(undoredo); + } + moveToWithoutUndo(atom, worldCoords); + } + } + + public void moveToWithoutUndo( IBond bond, Point2d point ) { if (bond != null) { Point2d center = bond.get2DCenter(); for (IAtom atom : bond.atoms()) { @@ -476,6 +607,21 @@ coordinatesChanged(); } + public void moveTo( IBond bond, Point2d point ) { + if (bond != null) { + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IAtomContainer undoRedoContainer = chemModel.getBuilder().newAtomContainer(); + undoRedoContainer.addAtom(bond.getAtom(0)); + undoRedoContainer.addAtom(bond.getAtom(1)); + Vector2d end=new Vector2d(); + end.sub(point,bond.getAtom(0).getPoint2d()); + IUndoRedoable undoredo = getUndoRedoFactory().getMoveAtomEdit(undoRedoContainer, end, "Move atom"); + getUndoRedoHandler().postEdit(undoredo); + } + moveToWithoutUndo(bond, point); + } + } + public IBond addBond(IAtom fromAtom, IAtom toAtom) { IBond newBond = chemModel.getBuilder().newBond(fromAtom, toAtom); chemModel.getMoleculeSet().getAtomContainer(0).addBond(newBond); @@ -566,6 +712,74 @@ chemModel.setReactionSet(chemModel.getBuilder().newReactionSet()); structureChanged(); } + + public void makeBondStereo(IBond bond, Direction desiredDirection) { + int stereo = bond.getStereo(); + boolean isUp = isUp(stereo); + boolean isDown = isDown(stereo); + boolean noStereo = noStereo(stereo); + if (isUp && desiredDirection == Direction.UP) { + flipDirection(bond, stereo); + } else if (isDown && desiredDirection == Direction.UP) { + flipOrientation(bond, stereo); + } else if (isUp && desiredDirection == Direction.DOWN) { + flipOrientation(bond, stereo); + } else if (isDown && desiredDirection == Direction.DOWN) { + flipDirection(bond, stereo); + } else if (noStereo && desiredDirection == Direction.UP) { + bond.setStereo(STEREO_BOND_UP); + } else if (noStereo && desiredDirection == Direction.DOWN) { + bond.setStereo(STEREO_BOND_DOWN); + } + Integer[] stereos=new Integer[2]; + stereos[1]=stereo; + stereos[0]=bond.getStereo(); + Map<IBond, IBond.Order[]> changedBonds = new HashMap<IBond, IBond.Order[]>(); + Map<IBond, Integer[]> changedBondsStereo = new HashMap<IBond, Integer[]>(); + changedBondsStereo.put(bond, stereos); + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IUndoRedoable undoredo = getUndoRedoFactory().getAdjustBondOrdersEdit(changedBonds, changedBondsStereo, "Adjust Bond Stereo"); + getUndoRedoHandler().postEdit(undoredo); + } + } + + /** + * Change the stereo bond from start->end to start<-end. + * + * @param bond the bond to change + * @param stereo the current stereo of that bond + */ + private void flipDirection(IBond bond, int stereo) { + if (stereo == STEREO_BOND_UP) bond.setStereo(STEREO_BOND_UP_INV); + else if (stereo == STEREO_BOND_UP_INV) bond.setStereo(STEREO_BOND_UP); + else if (stereo == STEREO_BOND_DOWN_INV) bond.setStereo(STEREO_BOND_DOWN); + else if (stereo == STEREO_BOND_DOWN) bond.setStereo(STEREO_BOND_DOWN_INV); + } + + + /** + * Change the stereo of the bond from UP<->DOWN. + * @param bond the bond to change + * @param stereo the current stereo of the bond + */ + private void flipOrientation(IBond bond, int stereo) { + if (stereo == STEREO_BOND_UP) bond.setStereo(STEREO_BOND_DOWN_INV); + else if (stereo == STEREO_BOND_UP_INV) bond.setStereo(STEREO_BOND_DOWN); + else if (stereo == STEREO_BOND_DOWN_INV) bond.setStereo(STEREO_BOND_UP); + else if (stereo == STEREO_BOND_DOWN) bond.setStereo(STEREO_BOND_UP_INV); + } + + private boolean isUp(int stereo) { + return stereo == STEREO_BOND_UP || stereo == STEREO_BOND_UP_INV; + } + + private boolean isDown(int stereo) { + return stereo == STEREO_BOND_DOWN || stereo == STEREO_BOND_DOWN_INV; + } + + private boolean noStereo(int stereo) { + return stereo == STEREO_BOND_NONE || stereo == CDKConstants.STEREO_BOND_UNDEFINED; + } public void cleanup() { Map<IAtom, Point2d[]> coords=new HashMap<IAtom, Point2d[]>(); @@ -683,7 +897,11 @@ } container.add(ring); structureChanged(); - return ring; + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IUndoRedoable undoredo = getUndoRedoFactory().getAddAtomsAndBondsEdit(getIChemModel(), ring.getBuilder().newAtomContainer(ring), "Ring" + " " + ringSize,getController2DModel()); + getUndoRedoHandler().postEdit(undoredo); + } + return ring; } public IRing addPhenyl(Point2d worldcoord) { @@ -709,7 +927,11 @@ } container.add(ring); structureChanged(); - return ring; + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IUndoRedoable undoredo = getUndoRedoFactory().getAddAtomsAndBondsEdit(getIChemModel(), ring.getBuilder().newAtomContainer(ring), "Benzene",getController2DModel()); + getUndoRedoHandler().postEdit(undoredo); + } + return ring; } public IRing addRing(IAtom atom, int ringSize) { @@ -823,6 +1045,15 @@ ringAtoms[ringSize - 1], ringAtoms[0], IBond.Order.SINGLE) ); newRing.setAtoms(ringAtoms); + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IAtomContainer undoRedoContainer = newRing.getBuilder().newAtomContainer(newRing); + for(IAtom atom : sharedAtoms.atoms()) + undoRedoContainer.removeAtom(atom); + for(IBond bond : sharedAtoms.bonds()) + undoRedoContainer.removeBond(bond); + IUndoRedoable undoredo = getUndoRedoFactory().getAddAtomsAndBondsEdit(getIChemModel(), undoRedoContainer, "Ring" + " " + ringSize,getController2DModel()); + getUndoRedoHandler().postEdit(undoredo); + } return newRing; } @@ -989,12 +1220,22 @@ return newRing; } - public void removeBond(IBond bond) { + public void removeBondWithoutUndo(IBond bond) { IAtomContainer sourceContainer = ChemModelManipulator .getRelevantAtomContainer(chemModel, bond); sourceContainer.removeBond(bond); structureChanged(); } + + public void removeBond(IBond bond) { + removeBondWithoutUndo(bond); + IAtomContainer undAtomContainer = bond.getBuilder().newAtomContainer(); + undAtomContainer.addBond(bond); + if(getUndoRedoFactory()!=null && getUndoRedoHandler()!=null){ + IUndoRedoable undoredo = getUndoRedoFactory().getRemoveAtomsAndBondsEdit(getIChemModel(), undAtomContainer,"Remove Bond"); + getUndoRedoHandler().postEdit(undoredo); + } + } public void addPhantomAtom( IAtom atom ) { this.phantoms.addAtom(atom); Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/DrawBondModule.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/DrawBondModule.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/DrawBondModule.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -113,7 +113,7 @@ } else if (closestAtom != null && closestAtom != atomToBondFrom && closestAtom != newAtom) { chemModelRelay.addBond(atomToBondFrom, closestAtom); - chemModelRelay.removeAtom(newAtom); + chemModelRelay.removeAtomWithoutUndo(newAtom); chemModelRelay.updateView(); } else { showBondTo(worldCoord); @@ -128,7 +128,7 @@ newAtom = chemModelRelay.getClosestAtom(to); chemModelRelay.addBond(atomToBondFrom, newAtom); } else { - chemModelRelay.moveTo(newAtom, to); + chemModelRelay.moveToWithoutUndo(newAtom, to); } chemModelRelay.updateView(); } @@ -136,7 +136,7 @@ private IAtom addAtomTo(Point2d to) { String atomType = chemModelRelay.getController2DModel() .getDrawElement(); - IAtom newAtom = chemModelRelay.addAtom(atomType, to); + IAtom newAtom = chemModelRelay.addAtomWithoutUndo(atomType, to); chemModelRelay.updateView(); return newAtom; } Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/IChemModelRelay.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/IChemModelRelay.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/IChemModelRelay.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -28,6 +28,7 @@ import javax.vecmath.Point2d; +import org.openscience.cdk.controller.ControllerHub.Direction; import org.openscience.cdk.controller.undoredo.IUndoRedoFactory; import org.openscience.cdk.controller.undoredo.UndoRedoHandler; import org.openscience.cdk.exception.CDKException; @@ -115,9 +116,12 @@ /* Editing actions for atoms */ public abstract IAtomContainer removeAtom(IAtom atom); + public abstract IAtomContainer removeAtomWithoutUndo(IAtom atom); public abstract IAtom addAtom(String element, Point2d worldcoord); + public abstract IAtom addAtomWithoutUndo(String element, Point2d worldcoord); public abstract IAtom addAtom(String element, IAtom atom); - public abstract void moveTo(IAtom atom, Point2d point); + public abstract IAtom addAtomWithoutUndo(String element, IAtom atom); + public abstract void moveToWithoutUndo(IAtom atom, Point2d point); public abstract void setSymbol(IAtom atom, String symbol); public abstract void setCharge(IAtom atom, int charge); public abstract void setMassNumber(IAtom atom, int charge); @@ -126,11 +130,16 @@ /* Editing actions for bonds */ public abstract IBond addBond(IAtom fromAtom, IAtom toAtom); + public abstract void removeBondWithoutUndo(IBond bond); public abstract void removeBond(IBond bond); - public abstract void moveTo(IBond bond, Point2d point); + public abstract void moveToWithoutUndo(IBond bond, Point2d point); public abstract void setOrder(IBond bond, IBond.Order order); public abstract void setWedgeType(IBond bond, int type); - + public abstract void addNewBond(Point2d worldCoordinate); + public void cycleBondValence(IBond bond); + public void makeBondStereo(IBond bond, Direction desiredDirection); + public void makeNewStereoBond(IAtom atom, Direction desiredDirection); + public IUndoRedoFactory getUndoRedoFactory(); public UndoRedoHandler getUndoRedoHandler(); Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/MoveModule.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/MoveModule.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/MoveModule.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -144,10 +144,10 @@ atomCoord.add(worldCoordTo, offset); switch (type) { case ATOM: - chemModelRelay.moveTo(atom, atomCoord); + chemModelRelay.moveToWithoutUndo(atom, atomCoord); break; case BOND: - chemModelRelay.moveTo(bond, atomCoord); + chemModelRelay.moveToWithoutUndo(bond, atomCoord); break; case SELECTION: Point2d d = new Point2d(); Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/RemoveModule.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/RemoveModule.java 2009-03-06 09:36:49 UTC (rev 14360) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/RemoveModule.java 2009-03-06 11:10:33 UTC (rev 14361) @@ -60,44 +60,19 @@ if (super.noSelection(dA, dB, dH)) { return; } else if (super.isAtomOnlyInHighlightDistance(dA, dB, dH)) { - removeAtom(closestAtom); + chemModelRelay.removeAtom(closestAtom); } else if (super.isBondOnlyInHighlightDistance(dA, dB, dH)) { - removeBond(closestBond); + chemModelRelay.removeBond(closestBond); } else { if (dA <= dB) { - removeAtom(closestAtom); + chemModelRelay.removeAtom(closestAtom); } else { - removeBond(closestBond); + chemModelRelay.removeBond(closestBond); } } } - private void removeAtom(IAtom atom) { - IAtomContainer undAtomContainer = atom.getBuilder().newAtomContainer(); - undAtomContainer.addAtom(atom); - Iterator<IBond> it=ChemModelManipulator.getRelevantAtomContainer(chemModelRelay.getIChemModel(),atom).getConnectedBondsList(atom).iterator(); - while(it.hasNext()) - undAtomContainer.addBond(it.next()); - chemModelRelay.removeAtom(atom); - chemModelRelay.updateView(); - if(chemModelRelay.getUndoRedoFactory()!=null && chemModelRelay.getUndoRedoHandler()!=null){ - IUndoRedoable undoredo = chemModelRelay.getUndoRedoFactory().getRemoveAtomsAndBondsEdit(chemModelRelay.getIChemModel(), undAtomContainer,this.getDrawModeString()); - chemModelRelay.getUndoRedoHandler().postEdit(undoredo); - } - } - - private void removeBond(IBond bond) { - IAtomContainer undAtomContainer = bond.getBuilder().newAtomContainer(); - chemModelRelay.removeBond(bond); - undAtomContainer.addBond(bond); - chemModelRelay.updateView(); - if(chemModelRelay.getUndoRedoFactory()!=null && chemModelRelay.getUndoRedoHandler()!=null){ - IUndoRedoable undoredo = chemModelRelay.getUndoRedoFactory().getRemoveAtomsAndBondsEdit(chemModelRelay.getIChemModel(), undAtomContainer,this.getDrawModeString()); - chemModelRelay.getUndoRedoHandler().postEdit(undoredo); - } - } - public String getDrawModeString() { return "Delete"; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |