示例#1
0
文件: RGroupEdit.java 项目: asad/jcp
 public RGroupEdit(
     String _type,
     boolean _isNewRgrp,
     IChemModelRelay _hub,
     RGroupHandler _rgrpHandler,
     Map<IAtom, IAtomContainer> _existingAtomDistr,
     Map<IBond, IAtomContainer> _existingBondDistr,
     IAtomContainer _existingRoot,
     Map<IAtom, Map<Integer, IBond>> _existingRootAttachmentPoints,
     Map<RGroup, Map<Integer, IAtom>> _existingRGroupApo,
     Map<Integer, RGroupList> _existingRgroupLists,
     IAtomContainer _userSelection) {
   this.type = _type;
   this.isNewRgrp = _isNewRgrp;
   this.hub = _hub;
   this.rgrpHandler = _rgrpHandler;
   this.existingRoot = _existingRoot;
   this.existingRootAttachmentPoints = _existingRootAttachmentPoints;
   this.existingRGroupApo = _existingRGroupApo;
   this.existingAtomDistr = _existingAtomDistr;
   this.existingBondDistr = _existingBondDistr;
   this.existingRgroupLists = _existingRgroupLists;
   this.redoRootStructure = rgrpHandler.getrGroupQuery().getRootStructure();
   this.userSelection = (IMolecule) _userSelection;
   if (_existingRgroupLists != null) {
     redoRgroupLists = new HashMap<Integer, RGroupList>();
     for (Iterator<Integer> itr =
             rgrpHandler.getrGroupQuery().getRGroupDefinitions().keySet().iterator();
         itr.hasNext(); ) {
       int rNum = itr.next();
       redoRgroupLists.put(rNum, rgrpHandler.getrGroupQuery().getRGroupDefinitions().get(rNum));
     }
   }
   if (existingRGroupApo != null) {
     RGroup undoRGroup = existingRGroupApo.keySet().iterator().next();
     for (Iterator<Integer> rnumItr =
             hub.getRGroupHandler().getrGroupQuery().getRGroupDefinitions().keySet().iterator();
         rnumItr.hasNext(); ) {
       for (RGroup rgrp :
           hub.getRGroupHandler()
               .getrGroupQuery()
               .getRGroupDefinitions()
               .get(rnumItr.next())
               .getRGroups()) {
         if (rgrp.equals(undoRGroup)) {
           redoRGroupApo = new HashMap<RGroup, Map<Integer, IAtom>>();
           HashMap<Integer, IAtom> map = new HashMap<Integer, IAtom>();
           map.put(1, rgrp.getFirstAttachmentPoint());
           map.put(2, rgrp.getSecondAttachmentPoint());
           redoRGroupApo.put(rgrp, map);
         }
       }
     }
   }
 }
示例#2
0
文件: RGroupEdit.java 项目: asad/jcp
  /** Redo actions */
  public void redo() {

    if (type.equals("setRoot") || type.equals("setSubstitute")) {

      if (isNewRgrp) {
        hub.setRGroupHandler(rgrpHandler);
      }

      IRGroupQuery rgrpQ = rgrpHandler.getrGroupQuery();
      for (Iterator<IAtom> atItr = existingAtomDistr.keySet().iterator(); atItr.hasNext(); ) {
        IAtom atom = atItr.next();
        existingAtomDistr.get(atom).removeAtom(atom);
      }
      for (Iterator<IBond> bndItr = existingBondDistr.keySet().iterator(); bndItr.hasNext(); ) {
        IBond bond = bndItr.next();
        existingBondDistr.get(bond).removeBond(bond);
      }
      hub.getChemModel().getMoleculeSet().addAtomContainer(userSelection);

      if (type.equals("setRoot")) {
        rgrpQ.setRootStructure(redoRootStructure);
        rgrpQ.getRootStructure().setProperty(CDKConstants.TITLE, RGroup.ROOT_LABEL);
        rgrpQ.setRootAttachmentPoints(redoRootAttachmentPoints);
      } else if (type.equals("setSubstitute")) {
        if (redoRgroupLists != null) {
          for (Iterator<Integer> rNums = redoRgroupLists.keySet().iterator(); rNums.hasNext(); ) {
            int rNum = rNums.next();
            rgrpQ.getRGroupDefinitions().put(rNum, redoRgroupLists.get(rNum));
          }
        }
      }
    } else if (type.startsWith("setAtomApoAction")) {
      RGroup redoRGroup = redoRGroupApo.keySet().iterator().next();
      for (Iterator<Integer> rnumItr =
              hub.getRGroupHandler().getrGroupQuery().getRGroupDefinitions().keySet().iterator();
          rnumItr.hasNext(); ) {
        for (RGroup rgrp :
            hub.getRGroupHandler()
                .getrGroupQuery()
                .getRGroupDefinitions()
                .get(rnumItr.next())
                .getRGroups()) {
          if (rgrp.equals(redoRGroup)) {
            IAtom apo1 = redoRGroupApo.get(redoRGroup).get(1);
            IAtom apo2 = redoRGroupApo.get(redoRGroup).get(2);
            rgrp.setFirstAttachmentPoint(apo1);
            rgrp.setSecondAttachmentPoint(apo2);
          }
        }
      }
    } else if (type.startsWith("setBondApoAction")) {
      for (Iterator<IAtom> atItr = redoRootAttachmentPoints.keySet().iterator();
          atItr.hasNext(); ) {
        IAtom rAtom = atItr.next();
        Map<Integer, IBond> apoBonds =
            hub.getRGroupHandler().getrGroupQuery().getRootAttachmentPoints().get(rAtom);

        apoBonds.remove(1);
        apoBonds.remove(2);
        Map<Integer, IBond> redoApo = redoRootAttachmentPoints.get(rAtom);

        if (redoApo.get(1) != null) {
          apoBonds.put(1, redoApo.get(1));
        }
        if (redoApo.get(2) != null) {
          apoBonds.put(2, redoApo.get(2));
        }
      }
    } else if (type.equals("clearRgroup")) {
      hub.unsetRGroupHandler();
    }
  }
	/**
	 * Returns an IChemModel, using the reader provided (picked).
	 * 
	 * @param cor
	 * @param panel
	 * @return
	 * @throws CDKException
	 */
	public static IChemModel getChemModelFromReader(
			final ISimpleChemObjectReader cor,
			final AbstractJChemPaintPanel panel) throws CDKException {
		panel.get2DHub().setRGroupHandler(null);
		String error = null;
		ChemModel chemModel = null;
		IChemFile chemFile = null;
		if (cor.accepts(IChemFile.class) && chemModel == null) {
			// try to read a ChemFile
			try {
				chemFile = (IChemFile) cor.read((IChemObject) new ChemFile());
				if (chemFile == null) {
					error = "The object chemFile was empty unexpectedly!";
				}
			} catch (final Exception exception) {
				error = "Error while reading file: " + exception.getMessage();
				exception.printStackTrace();
			}
		}
		if (error != null) {
			throw new CDKException(error);
		}
		if (chemModel == null && chemFile != null) {
			chemModel = (ChemModel) chemFile.getChemSequence(0).getChemModel(0);
		}
		if (cor.accepts(ChemModel.class) && chemModel == null) {
			// try to read a ChemModel
			try {

				chemModel = (ChemModel) cor.read((IChemObject) new ChemModel());
				if (chemModel == null) {
					error = "The object chemModel was empty unexpectedly!";
				}
			} catch (final Exception exception) {
				error = "Error while reading file: " + exception.getMessage();
				exception.printStackTrace();
			}
		}

		// Smiles reading
		if (cor.accepts(AtomContainerSet.class) && chemModel == null) {
			// try to read a AtomContainer set
			try {
				final IAtomContainerSet som = cor.read(new AtomContainerSet());
				chemModel = new ChemModel();
				chemModel.setMoleculeSet(som);
				if (chemModel == null) {
					error = "The object chemModel was empty unexpectedly!";
				}
			} catch (final Exception exception) {
				error = "Error while reading file: " + exception.getMessage();
				exception.printStackTrace();
			}
		}

		// MDLV3000 reading
		if (cor.accepts(AtomContainer.class) && chemModel == null) {
			// try to read a AtomContainer
			final IAtomContainer mol = cor.read(new AtomContainer());
			if (mol != null) {
				try {
					final IAtomContainerSet newSet = new AtomContainerSet();
					newSet.addAtomContainer(mol);
					chemModel = new ChemModel();
					chemModel.setMoleculeSet(newSet);
					if (chemModel == null) {
						error = "The object chemModel was empty unexpectedly!";
					}
				} catch (final Exception exception) {
					error = "Error while reading file: "
							+ exception.getMessage();
					exception.printStackTrace();
				}
			}
		}

		// RGroupQuery reading
		if (cor.accepts(RGroupQuery.class) && chemModel == null) {
			final IRGroupQuery rgroupQuery = cor.read(new RGroupQuery());
			if (rgroupQuery != null) {
				try {
					chemModel = new ChemModel();
					final RGroupHandler rgHandler = new RGroupHandler(
							rgroupQuery);
					panel.get2DHub().setRGroupHandler(rgHandler);
					chemModel.setMoleculeSet(rgHandler
							.getMoleculeSet(chemModel));
					rgHandler.layoutRgroup();

				} catch (final Exception exception) {
					error = "Error while reading file: "
							+ exception.getMessage();
					exception.printStackTrace();
				}
			}
		}

		if (error != null) {
			throw new CDKException(error);
		}

		if (chemModel == null && chemFile != null) {
			chemModel = (ChemModel) chemFile.getChemSequence(0).getChemModel(0);
		}

		// SmilesParser sets valencies, switch off by default.
		if (cor instanceof SMILESReader) {
			final IAtomContainer allinone = JChemPaintPanel
					.getAllAtomContainersInOne(chemModel);
			for (int k = 0; k < allinone.getAtomCount(); k++) {
				allinone.getAtom(k).setValency(null);
			}
		}
		return chemModel;
	}