Skip to content

Instantly share code, notes, and snippets.

@johnmay
Created November 9, 2012 18:29
Show Gist options
  • Select an option

  • Save johnmay/4047369 to your computer and use it in GitHub Desktop.

Select an option

Save johnmay/4047369 to your computer and use it in GitHub Desktop.
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