Created
November 9, 2012 18:29
-
-
Save johnmay/4047369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ChangeAtomSymbol extends AbstractUndoableEdit { | |
| IAtomContainer container; | |
| IAtom remove, replace; | |
| ChangeAtomSymbol(IAtomContainer container, | |
| IAtom original, | |
| String newSymbol) { | |
| Atom copy = new Atom(original); // do not clone! | |
| copy.setSymbol(newSymbol); | |
| this.container = container; | |
| this.remove = original; | |
| this.replace = copy; | |
| } | |
| @Override | |
| public void undo() throws CannotUndoException { | |
| // undo: remove the replace atom and add the remove back in | |
| container.removeAtom(replace); | |
| container.addAtom(remove); | |
| // replace in bonds | |
| for (IBond bond : container.getConnectedBondsList(replace)) { | |
| for (int i = 0; i < bond.getAtomCount(); i++) { | |
| if (bond.getAtom(i) == replace) { // object equivalence | |
| bond.setAtom(remove, i); // swap out the bond | |
| } | |
| } | |
| } | |
| // replace in electron containers... | |
| } | |
| @Override | |
| public void redo() throws CannotRedoException { | |
| // do the opposite of above | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment