From: <sh...@us...> - 2009-02-19 13:25:28
|
Revision: 14195 http://cdk.svn.sourceforge.net/cdk/?rev=14195&view=rev Author: shk3 Date: 2009-02-19 11:42:10 +0000 (Thu, 19 Feb 2009) Log Message: ----------- Added a skeleton undo redo. An email will follow. Modified Paths: -------------- cdk/branches/jchempaint-primary/META-INF/MANIFEST.MF cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddBondModule.java Added Paths: ----------- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/AddAtomsAndBondsEdit.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoListener.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoRedoFactory.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoRedoable.java cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/UndoRedoHandler.java Modified: cdk/branches/jchempaint-primary/META-INF/MANIFEST.MF =================================================================== --- cdk/branches/jchempaint-primary/META-INF/MANIFEST.MF 2009-02-18 17:12:15 UTC (rev 14194) +++ cdk/branches/jchempaint-primary/META-INF/MANIFEST.MF 2009-02-19 11:42:10 UTC (rev 14195) @@ -19,6 +19,7 @@ org.openscience.cdk.config.elements, org.openscience.cdk.config.isotopes, org.openscience.cdk.controller, + org.openscience.cdk.controller.undoredo, org.openscience.cdk.debug, org.openscience.cdk.dict, org.openscience.cdk.dict.data, 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-02-18 17:12:15 UTC (rev 14194) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/AddBondModule.java 2009-02-19 11:42:10 UTC (rev 14195) @@ -29,9 +29,14 @@ import javax.vecmath.Point2d; +import org.openscience.cdk.controller.undoredo.IUndoRedoable; +import org.openscience.cdk.controller.undoredo.IUndoRedoFactory; +import org.openscience.cdk.controller.undoredo.UndoRedoHandler; import org.openscience.cdk.interfaces.IAtom; +import org.openscience.cdk.interfaces.IAtomContainer; import org.openscience.cdk.interfaces.IBond; import org.openscience.cdk.tools.manipulator.BondManipulator; +import org.openscience.cdk.tools.manipulator.ChemModelManipulator; /** * Adds a bond on clicking an atom, or cycles the order of clicked bonds. @@ -40,8 +45,13 @@ */ public class AddBondModule extends ControllerModuleAdapter { - public AddBondModule(IChemModelRelay relay) { + IUndoRedoFactory undoredofactory; + UndoRedoHandler undoredohandler; + + public AddBondModule(IChemModelRelay relay, IUndoRedoFactory undoredofactory, UndoRedoHandler undoredohandler) { super(relay); + this.undoredohandler=undoredohandler; + this.undoredofactory=undoredofactory; } private void cycleBondValence(IBond bond) { @@ -71,11 +81,21 @@ } private void addNewBond(Point2d worldCoordinate) { + IAtomContainer undoRedoContainer = chemModelRelay.getIChemModel().getBuilder().newAtomContainer(); String atomType = chemModelRelay.getController2DModel().getDrawElement(); - chemModelRelay.addAtom(atomType, - chemModelRelay.addAtom(atomType, worldCoordinate)); + 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(); + IUndoRedoable undoredo = undoredofactory.getAddAtomsAndBondsEdit(chemModelRelay.getIChemModel(), undoRedoContainer, "Add Bond",chemModelRelay.getController2DModel()); + undoredohandler.postEdit(undoredo); } public void mouseClickedDown(Point2d worldCoordinate) { Added: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/AddAtomsAndBondsEdit.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/AddAtomsAndBondsEdit.java (rev 0) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/AddAtomsAndBondsEdit.java 2009-02-19 11:42:10 UTC (rev 14195) @@ -0,0 +1,150 @@ +/* $RCSfile$ + * $Author: gilleain $ + * $Date: 2008-11-26 16:01:05 +0000 (Wed, 26 Nov 2008) $ + * $Revision: 13311 $ + * + * Copyright (C) 2005-2008 Tobias Helmus, Stefan Kuhn + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + */ +package org.openscience.cdk.controller.undoredo; + +import java.util.Iterator; + +import org.openscience.cdk.atomtype.CDKAtomTypeMatcher; +import org.openscience.cdk.controller.IControllerModel; +import org.openscience.cdk.graph.ConnectivityChecker; +import org.openscience.cdk.interfaces.IAtom; +import org.openscience.cdk.interfaces.IAtomContainer; +import org.openscience.cdk.interfaces.IAtomType; +import org.openscience.cdk.interfaces.IBond; +import org.openscience.cdk.interfaces.IChemModel; +import org.openscience.cdk.interfaces.IMolecule; +import org.openscience.cdk.interfaces.IMoleculeSet; +import org.openscience.cdk.tools.CDKHydrogenAdder; +import org.openscience.cdk.tools.manipulator.AtomTypeManipulator; +import org.openscience.cdk.tools.manipulator.ChemModelManipulator; + +/** + * @cdk.module control + * @cdk.svnrev $Revision: 13311 $ + */ +public class AddAtomsAndBondsEdit implements IUndoRedoable { + + private static final long serialVersionUID = -7667903450980188402L; + + private IChemModel chemModel; + + private IAtomContainer undoRedoContainer; + + private String type; + + private IControllerModel c2dm=null; + + /** + * @param chemModel + * @param undoRedoContainer + * @param c2dm The controller model; if none, set to null + */ + public AddAtomsAndBondsEdit(IChemModel chemModel, + IAtomContainer undoRedoContainer, String type, IControllerModel c2dm) { + this.chemModel = chemModel; + this.undoRedoContainer = undoRedoContainer; + this.type = type; + this.c2dm=c2dm; + } + + public void redo() { + IAtomContainer container = chemModel.getBuilder().newAtomContainer(); + Iterator containers = ChemModelManipulator.getAllAtomContainers(chemModel).iterator(); + while (containers.hasNext()) { + container.add((IAtomContainer)containers.next()); + } + for (int i = 0; i < undoRedoContainer.getBondCount(); i++) { + IBond bond = undoRedoContainer.getBond(i); + container.addBond(bond); + } + for (int i = 0; i < undoRedoContainer.getAtomCount(); i++) { + IAtom atom = undoRedoContainer.getAtom(i); + container.addAtom(atom); + } + for (int i = 0; i < container.getAtomCount(); i++) { + this.updateAtom(container,container.getAtom(i)); + } + IMolecule molecule = container.getBuilder().newMolecule(container); + IMoleculeSet moleculeSet = ConnectivityChecker + .partitionIntoMolecules(molecule); + chemModel.setMoleculeSet(moleculeSet); + } + + public void undo() { + for (int i = 0; i < undoRedoContainer.getBondCount(); i++) { + IBond bond = undoRedoContainer.getBond(i); + ChemModelManipulator.getRelevantAtomContainer(chemModel, bond).removeBond(bond); + } + for (int i = 0; i < undoRedoContainer.getAtomCount(); i++) { + IAtom atom = undoRedoContainer.getAtom(i); + ChemModelManipulator.getRelevantAtomContainer(chemModel, atom).removeAtom(atom); + } + Iterator containers = ChemModelManipulator.getAllAtomContainers(chemModel).iterator(); + while (containers.hasNext()) { + IAtomContainer container = (IAtomContainer)containers.next(); + for (int i = 0; i < container.getAtomCount(); i++) { + this.updateAtom(container,container.getAtom(i)); + } + } + } + + public boolean canRedo() { + return true; + } + + public boolean canUndo() { + return true; + } + + /* + * (non-Javadoc) + * + * @see javax.swing.undo.UndoableEdit#getPresentationName() + */ + public String getPresentationName() { + return type; + } + + + /** + * Updates an atom with respect to its hydrogen count + * + *@param container The AtomContainer to work on + *@param atom The Atom to update + */ + public void updateAtom(IAtomContainer container, IAtom atom) + { + if (c2dm!=null && c2dm.getAutoUpdateImplicitHydrogens()) { + try { + CDKAtomTypeMatcher matcher = CDKAtomTypeMatcher.getInstance(atom.getBuilder()); + CDKHydrogenAdder hAdder = CDKHydrogenAdder.getInstance(atom.getBuilder()); + IAtomType type = matcher.findMatchingAtomType(container, atom); + AtomTypeManipulator.configure(atom, type); + hAdder.addImplicitHydrogens(container, atom); + } catch (Exception exception) { + //we fail silently, when the handling of implicit Hs can't done + } + } + } +} Added: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoListener.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoListener.java (rev 0) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoListener.java 2009-02-19 11:42:10 UTC (rev 14195) @@ -0,0 +1,43 @@ +/* + * $RCSfile$ + * $Author: egonw $ + * $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $ + * $Revision: 7634 $ + * + * Copyright (C) 1997-2008 Stefan Kuhn + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * All I ask is that proper credit is given for my work, which includes + * - but is not limited to - adding the above copyright notice to the beginning + * of your source code files, and to any copyright notice that you may distribute + * with programs based on this work. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +package org.openscience.cdk.controller.undoredo; + +/** + * This interface is implemented by applications and registered onto UndoRedoHandler. + * The events then get passed onto the actual undo/redo implementation of the gui + * framework. + * + * @cdk.module control + */ +public interface IUndoListener { + + public void doUndo(IUndoRedoable undoredo); + +} Added: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoRedoFactory.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoRedoFactory.java (rev 0) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoRedoFactory.java 2009-02-19 11:42:10 UTC (rev 14195) @@ -0,0 +1,45 @@ +/* + * $RCSfile$ + * $Author: egonw $ + * $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $ + * $Revision: 7634 $ + * + * Copyright (C) 1997-2008 Stefan Kuhn + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * All I ask is that proper credit is given for my work, which includes + * - but is not limited to - adding the above copyright notice to the beginning + * of your source code files, and to any copyright notice that you may distribute + * with programs based on this work. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +package org.openscience.cdk.controller.undoredo; + +import org.openscience.cdk.controller.IControllerModel; +import org.openscience.cdk.interfaces.IAtomContainer; +import org.openscience.cdk.interfaces.IChemModel; + +/** + * This interface needs to be implemented in an application to return instances of classes + * extending the implementations of IUndoRedoable suitable for the respective gui framework + * (e. g. swing or swt). + * + * @cdk.module control + */ +public interface IUndoRedoFactory { + public IUndoRedoable getAddAtomsAndBondsEdit(IChemModel chemModel, IAtomContainer undoRedoContainer, String type, IControllerModel c2dm); +} Added: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoRedoable.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoRedoable.java (rev 0) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/IUndoRedoable.java 2009-02-19 11:42:10 UTC (rev 14195) @@ -0,0 +1,43 @@ +/* + * $RCSfile$ + * $Author: egonw $ + * $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $ + * $Revision: 7634 $ + * + * Copyright (C) 1997-2008 Stefan Kuhn + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * All I ask is that proper credit is given for my work, which includes + * - but is not limited to - adding the above copyright notice to the beginning + * of your source code files, and to any copyright notice that you may distribute + * with programs based on this work. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +package org.openscience.cdk.controller.undoredo; + +/** + * This interface is implemented by all the actual edit events. + * + * @cdk.module control + */ + +public interface IUndoRedoable { + public void redo(); + public void undo(); + public boolean canRedo(); + public boolean canUndo(); +} Added: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/UndoRedoHandler.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/UndoRedoHandler.java (rev 0) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/controller/undoredo/UndoRedoHandler.java 2009-02-19 11:42:10 UTC (rev 14195) @@ -0,0 +1,66 @@ +/* + * $RCSfile$ + * $Author: egonw $ + * $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $ + * $Revision: 7634 $ + * + * Copyright (C) 1997-2008 The Chemistry Development Kit (CDK) project + * + * Contact: cdk...@li... + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * All I ask is that proper credit is given for my work, which includes + * - but is not limited to - adding the above copyright notice to the beginning + * of your source code files, and to any copyright notice that you may distribute + * with programs based on this work. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +package org.openscience.cdk.controller.undoredo; + +import java.util.ArrayList; +import java.util.List; + + +/** + * This class needs to be passed to all controller modules, who want to/need to post + * undo/redoable events. Applications register an IUndoListener on this and pass the edits + * to the actual undo/redo implementation (provided e. g. by swing or swt). + * + * @cdk.module control + */ +public class UndoRedoHandler { + + List<IUndoListener> undoListeners=new ArrayList<IUndoListener>(); + + /** + * Only constructor + * + * @param c2dm The Controller2dModel of the current application + */ + public UndoRedoHandler() { + } + + + public void addIUndoListener(IUndoListener listener){ + undoListeners.add(listener); + } + + public void postEdit(IUndoRedoable edit) { + for(IUndoListener listener:undoListeners){ + listener.doUndo(edit); + } + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |