📄 abstractcontroller2d.java
字号:
*/ UndoableEdit edit = new AdjustBondOrdersEdit(changedBonds); undoRedoHandler.postEdit(edit); updateAtoms(container, bondInRange.atoms()); } else { IAtomContainer undoRedoContainer = chemModel.getBuilder().newAtomContainer(); if (atomInRange != null) { logger.debug("We had an atom in range"); newAtom1 = atomInRange; } else if (!wasDragged) { // create a new molecule logger.debug("We make a new molecule"); newAtom1 = undoRedoContainer.getBuilder().newAtom(c2dm.getDrawElement(), new Point2d(startX, startY)); IAtomContainer atomCon = ChemModelManipulator.createNewMolecule(chemModel); atomCon.addAtom(newAtom1); r2dm.setRenderingCoordinate(newAtom1,new Point2d(startX, startY)); // update atoms updateAtom(atomCon, newAtom1); undoRedoContainer.add(atomCon); } if (wasDragged) { if (dragMode == DRAG_DRAWING_PROPOSED_BOND) { int endX = r2dm.getPointerVectorEnd().x; int endY = r2dm.getPointerVectorEnd().y; atomInRange = getAtomInRange(endX, endY); // OK, there is some repartitioning done on atomCon later, so put everything in the first AC IAtomContainer atomCon = null; Iterator atomCons = ChemModelManipulator.getAllAtomContainers(chemModel).iterator(); while (atomCons.hasNext()) { if (atomCon == null) { atomCon = (IAtomContainer)atomCons.next(); } else { atomCon.add((IAtomContainer)atomCons.next()); } } if (atomCon == null) atomCon = chemModel.getBuilder().newAtomContainer(); if (atomInRange != null) { logger.debug("*** atom in range"); newAtom2 = atomInRange; logger.debug("atomCon.getAtomCount() " + atomCon.getAtomCount()); } else { logger.debug("*** new atom"); newAtom2 = atomCon.getBuilder().newAtom(c2dm.getDrawElement(), new Point2d(endX, endY)); atomCon.addAtom(newAtom2); r2dm.setRenderingCoordinate(newAtom2,new Point2d(endX, endY)); undoRedoContainer.addAtom(newAtom2); } newAtom1 = lastAtomInRange; if (newAtom1 == null) { newAtom1 = atomCon.getBuilder().newAtom(c2dm.getDrawElement(), new Point2d(r2dm.getPointerVectorStart().x, r2dm.getPointerVectorStart().y)); undoRedoContainer.addAtom(newAtom1); } if (newAtom1 != newAtom2) { newBond = atomCon.getBuilder().newBond(newAtom1, newAtom2, 1); if(c2dm.getDrawMode() == Controller2DModel.UP_BOND) newBond.setStereo(CDKConstants.STEREO_BOND_UP); if(c2dm.getDrawMode() == Controller2DModel.DOWN_BOND) newBond.setStereo(CDKConstants.STEREO_BOND_DOWN); logger.debug(newAtom1 + " - " + newAtom2); atomCon.addBond(newBond); undoRedoContainer.addBond(newBond); } try { IMoleculeSet setOfMolecules = ConnectivityChecker.partitionIntoMolecules(atomCon); chemModel.setMoleculeSet(setOfMolecules); logger.debug("We have " + setOfMolecules.getAtomContainerCount() + " molecules on screen"); } catch (Exception exception) { logger.warn("Could not partition molecule: ", exception.getMessage()); logger.debug(exception); return; } // update atoms updateAtom(atomCon, newAtom2); /* * PRESERVE THIS. This notifies the * the listener responsible for * undo and redo storage that it * should store this change of an atom symbol */ isUndoableChange = true; /* * --- */ } } else if (atomInRange != null) { // add a new atom to the current atom in some random // direction IAtomContainer atomCon = ChemModelManipulator.getRelevantAtomContainer(chemModel, atomInRange); newAtom2 = atomCon.getBuilder().newAtom(c2dm.getDrawElement(), ((Point2d)r2dm.getRenderingCoordinate(atomInRange))); // now create 2D coords for new atom double bondLength = r2dm.getBondLength(); java.util.List connectedAtoms = atomCon.getConnectedAtomsList(atomInRange); logger.debug("connectedAtoms.length: " + connectedAtoms.size()); IAtomContainer placedAtoms = atomCon.getBuilder().newAtomContainer(); //placedAtoms.addAtom(atomInRange); for (int i = 0; i < connectedAtoms.size(); i++) { placedAtoms.addAtom((IAtom)connectedAtoms.get(i)); } IAtomContainer unplacedAtoms = atomCon.getBuilder().newAtomContainer(); unplacedAtoms.addAtom(newAtom2); AtomPlacer atomPlacer = new AtomPlacer(); atomPlacer.setMolecule(atomCon.getBuilder().newMolecule(atomCon)); Point2d center2D = GeometryTools.get2DCenter(placedAtoms,r2dm.getRenderingCoordinates()); logger.debug("placedAtoms.getAtomCount(): " + placedAtoms.getAtomCount()); logger.debug("unplacedAtoms.getAtomCount(): " + unplacedAtoms.getAtomCount()); if (placedAtoms.getAtomCount() == 1) { Vector2d bondVector = atomPlacer.getNextBondVector( atomInRange, placedAtoms.getAtom(0), GeometryTools.get2DCenter(atomCon.getBuilder().newMolecule(atomCon),r2dm.getRenderingCoordinates()), true // FIXME: is this correct? (see SF bug #1367002) ); Point2d atomPoint = new Point2d(((Point2d)r2dm.getRenderingCoordinate(atomInRange))); bondVector.normalize(); bondVector.scale(bondLength); atomPoint.add(bondVector); newAtom2.setPoint2d(atomPoint); r2dm.setRenderingCoordinate(newAtom2,atomPoint); } else { atomPlacer.distributePartners(atomInRange, placedAtoms, center2D, unplacedAtoms, bondLength); } // now add the new atom atomCon.addAtom(newAtom2); undoRedoContainer.addAtom(newAtom2); r2dm.setRenderingCoordinate(newAtom2,new Point2d(newAtom2.getPoint2d())); newBond= undoRedoContainer.getBuilder().newBond(atomInRange, newAtom2, 1.0); atomCon.addBond(newBond); undoRedoContainer.addBond(newBond); if(c2dm.getDrawMode() == Controller2DModel.UP_BOND) newBond.setStereo(CDKConstants.STEREO_BOND_UP); if(c2dm.getDrawMode() == Controller2DModel.DOWN_BOND) newBond.setStereo(CDKConstants.STEREO_BOND_DOWN); // update atoms updateAtom(atomCon, atomInRange); updateAtom(atomCon, newAtom2); } UndoableEdit edit = new AddAtomsAndBondsEdit(chemModel, undoRedoContainer, "Add Bond",c2dm); undoRedoHandler.postEdit(edit); } r2dm.fireChange(); fireChange(); if(newAtom1!=null && r2dm.getRenderingCoordinate(newAtom1)==null) r2dm.setRenderingCoordinate(newAtom1,new Point2d(newAtom1.getPoint2d())); if(newAtom2!=null) r2dm.setRenderingCoordinate(newAtom2,new Point2d(newAtom2.getPoint2d())); centerAtom(newAtom1,chemModel); centerAtom(newAtom2,chemModel); this.updateMoleculeCoordinates(); } private void decreaseCharge() { IAtom atomInRange = r2dm.getHighlightedAtom(); if (atomInRange != null) { int formerCharge = atomInRange.getFormalCharge(); atomInRange.setFormalCharge(atomInRange.getFormalCharge() - 1); // update atom IAtomContainer container = ChemModelManipulator.getRelevantAtomContainer(chemModel, atomInRange); updateAtom(container, atomInRange); //undoredo support UndoableEdit edit = new ChangeChargeEdit(atomInRange, formerCharge, atomInRange.getFormalCharge()); undoRedoHandler.postEdit(edit); r2dm.fireChange(); fireChange(); } } private void enterElement() { IAtom atomInRange = r2dm.getHighlightedAtom(); if (atomInRange != null) { String[] funcGroupsKeys=new String[funcgroupsmap.keySet().size()+1]; Iterator it=funcgroupsmap.keySet().iterator(); int h=1; funcGroupsKeys[0]=""; while(it.hasNext()){ funcGroupsKeys[h]=(String)it.next(); h++; } String x=EnterElementOrGroupDialog.showDialog(null,null, "Enter an element symbol or choose/enter a functional group abbrivation:", "Enter element", funcGroupsKeys, "",""); try{ IAtomContainer ac=(IAtomContainer)funcgroupsmap.get(x.toLowerCase()); //this means a functional group was entered //TODO undo-redo if(ac!=null && !x.equals("")){ ac=(IAtomContainer)((IAtomContainer)funcgroupsmap.get(x)).clone(); IAtomContainer container = ChemModelManipulator.getRelevantAtomContainer(chemModel, atomInRange); IAtom lastplaced=null; int counter=0; //this is the starting point for placing ac.getAtom(0).setPoint2d((Point2d)r2dm.getRenderingCoordinate(atomInRange)); r2dm.setRenderingCoordinate(ac.getAtom(0), (Point2d)r2dm.getRenderingCoordinate(atomInRange)); lastplaced=ac.getAtom(0); counter=1; container.add(ac); List connbonds=container.getConnectedBondsList(atomInRange); //this is needed for undo redo IAtomContainer undoredocontainer=ac.getBuilder().newAtomContainer(); undoredocontainer.addAtom(atomInRange); for(int i=0;i<connbonds.size();i++){ IBond bond=(IBond)connbonds.get(i); if(bond.getAtom(0)==atomInRange){ bond.setAtom(ac.getAtom(0), 0); undoredocontainer.addBond(bond); }else{ bond.setAtom(ac.getAtom(0), 1); undoredocontainer.addBond(bond); } } container.removeAtomAndConnectedElectronContainers(atomInRange); AtomPlacer ap=new AtomPlacer(); while(lastplaced!=null){ IAtomContainer placedNeighbours=container.getBuilder().newAtomContainer(); IAtomContainer unplacedNeighbours=container.getBuilder().newAtomContainer(); List l=container.getConnectedAtomsList(lastplaced); for(int i=0;i<l.size();i++){ if(r2dm.getRenderingCoordinate((IAtom)l.get(i))!=null) placedNeighbours.addAtom((IAtom)l.get(i)); else unplacedNeighbours.addAtom((IAtom)l.get(i)); } ap.distributePartners(lastplaced, placedNeighbours, GeometryTools.get2DCenter(placedNeighbours,r2dm.getRenderingCoordinates()), unplacedNeighbours, r2dm.getBondLength(), r2dm.getRenderingCoordinates()); lastplaced=ac.getAtom(counter); counter++; if(counter==ac.getAtomCount()) lastplaced=null; } it=container.atoms(); while(it.hasNext()){ IAtom atom=(IAtom)it.next(); if(r2dm.getRenderingCoordinate(atom)==null) r2dm.setRenderingCoordinate(atom, atom.getPoint2d()); } /* * PRESERVE THIS. This notifies the * the listener responsible for * undo and redo storage that it * should store this change of an atom symbol */ isUndoableChange = true; /* * --- */ // undoredo support UndoableEdit edit = new AddFuncGroupEdit(chemModel, undoredocontainer ,ac, "add "+x); undoRedoHandler.postEdit(edit); //undoRedoHandler.postEdit(edit); r2dm.fireChange(); fireChange(); }else if(x!=null && x.length()>0){ String formerSymbol=""; if(Character.isLowerCase(x.toCharArray()[0])) x=Character.toUpperCase(x.charAt(0))+x.substring(1); IsotopeFactory ifa=IsotopeFactory.getInstance(r2dm.getHighlightedAtom().getBuilder()); IIsotope iso=ifa.getMajorIsotope(x); formerSymbol=r2dm.getHighlightedAtom().getSymbol(); if(iso!=null) r2dm.getHighlightedAtom().setSymbol(x); // update atom IAtomContainer container = ChemModelManipulator.getRelevantAtomContainer(chemModel, atomInRange); updateAtom(container, atomInRange); /* * PRESERVE THIS. This notifies the * the listener responsible for * undo and redo storage that it * should store this change of an atom symbol */ isUndoableChange = true; /* * --- */ // undoredo support UndoableEdit edit = new ChangeAtomSymbolEdit(atomInRange, formerSymbol, x); undoRedoHandler.postEdit(edit); r2dm.fireChange(); fireChange(); } }catch(Exception ex){ ex.printStackTrace(); logger.debug(ex.getMessage()+" in SELECTELEMENT"); } } } private void increaseCharge() { IAtom atomInRange = r2dm.getHighlightedAtom(); if (atomInRange != null) { int formerCharge = atomInRange.getFormalCharge(); atomInRange.setFormalCharge(atomInRange.getFormalCharge() + 1); // update atom IAtomContainer container = ChemModelManipulator.getRelevantAtomContainer(chemModel, atomInRange); updateAtom(container, atomInRange); //undoredo support UndoableEdit edit = new ChangeChargeEdit(atomInRange, formerCharge, atomInRange.getFormalCharge()); undoRedoHandler.postEdit(edit); r2dm.fireChange(); fireChange(); } } private void changeElement() { IAtom atomInRange = r2dm.getHighlightedAtom(); if (atomInRange != null) { String symbol = c2dm.getDrawElement(); if (!(atomInRange.getSymbol().equals(symbol))) { // only change symbol if needed String formerSymbol = atomInRange.getSymbol(); atomInRange.setSymbol(symbol); // configure the atom, so that the atomic number matches the symbol try { IsotopeFactory.getInstance(atomInRange.getBuilder()).configure(atomInRange); } catch (Exception exception) { logger.error("Error while configuring atom"); logger.debug(exception); } // update atom IAtomContainer container = ChemModelManipulator.getRelevantAtomContainer(chemModel, atomInRange); updateAtom(container, atomInRange); /* * PRESERVE THIS. This notifies the * the listener responsible for * undo and redo storage that it * should store this change of an atom symbol */// isUndoableChange = true; /* * --- */ // undoredo support UndoableEdit edit = new ChangeAtomSymbolEdit(atomInRange, formerSymbol, symbol); undoRedoHandler.postEdit(edit); r2dm.fireChange(); fireChange(); } }else{ int startX = r2dm.getPointerVectorStart().x; int startY = r2dm.getPointerVectorStart().y; IAtom newAtom1 = chemModel.getBuilder().newAtom(c2dm.getDrawElement(), new Point2d(startX, startY)); r2dm.setRenderingCoordinate(newAtom1,new Point2d(startX, startY)); IAtomContainer atomCon = ChemModelManipulator.createNewMolecule(chemModel); atomCon.addAtom(newAtom1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -