Package org.openscience.cdk.renderer.selection

Examples of org.openscience.cdk.renderer.selection.IChemObjectSelection


                status = makeStatusBarString(hub.getFormula(), implicitHs);
            }
        } else if (position == 2) {
            // depict brutto formula of the selected molecule or part of
            // molecule
            IChemObjectSelection selection = renderer.getRenderer2DModel()
                    .getSelection();

            if (selection != null) {
                IAtomContainer ac = selection.getConnectedAtomContainer();
                if (ac != null) {
                    int implicitHs = 0;
                    for (IAtom atom : ac.atoms()) {
                        if (atom.getImplicitHydrogenCount() != null) {
                            implicitHs += atom.getImplicitHydrogenCount();
View Full Code Here


      panel.get2DHub().mouseClickedDown((int)(p1.x), (int)(p1.y));
        applet.panel("renderpanel").robot.waitForIdle();
      panel.get2DHub().mouseClickedUp((int)p1.x, (int)p1.y);
        panel.get2DHub().updateView();
        applet.panel("renderpanel").robot.waitForIdle();
        IChemObjectSelection sel = r.getRenderer2DModel().getSelection();
        Assert.assertEquals(true, sel.contains(ethane.getAtom(0)));

    double d = r.getRenderer2DModel().getSelectionRadius() / r.getRenderer2DModel().getScale();
      panel.get2DHub().mouseClickedDown((int)(p1.x+d/2), (int)(p1.y+d/2));
        applet.panel("renderpanel").robot.waitForIdle();
      panel.get2DHub().mouseDrag((int)(p1.x+d/2), (int)(p1.y+d/2),(int)p1.x+100, (int)p1.y+100);
View Full Code Here

    public IRenderingElement generate(IAtomContainer ac, RendererModel model) {
        JChemPaintRendererModel jcpModel = (JChemPaintRendererModel) model;
        if(model.getExternalSelectedPart()==null)
            return new ElementGroup();
        Color selectionColor = jcpModel.getExternalHighlightColor();
        IChemObjectSelection selection = new AtomContainerSelection(model.getExternalSelectedPart());
        return generate(selection, selectionColor, jcpModel);
    }
View Full Code Here

    public IRenderingElement generate(IAtomContainer ac, RendererModel model) {
        JChemPaintRendererModel jcpModel = (JChemPaintRendererModel) model;
        if(model.getExternalSelectedPart()==null)
            return new ElementGroup();
        Color selectionColor = jcpModel.getExternalHighlightColor();
        IChemObjectSelection selection = new AtomContainerSelection(model.getExternalSelectedPart());
        return generate(selection, selectionColor, jcpModel);
    }
View Full Code Here

     */
    public void selectionChanged() {
        updateStatusBar();
       
        if(this.getRenderPanel().getRenderer().getRenderer2DModel().getSelection()!=null) {
          IChemObjectSelection selection = this.getRenderPanel().getRenderer().getRenderer2DModel().getSelection();

          if (selection.getConnectedAtomContainer()!=null && selection.getConnectedAtomContainer().getAtomCount()>0)
                enOrDisableMenus(atomMenu,true);
            else
                enOrDisableMenus(atomMenu,false);

          if (selection.getConnectedAtomContainer()!=null && selection.getConnectedAtomContainer().getBondCount()>0)
                enOrDisableMenus(bondMenu,true);
            else
                enOrDisableMenus(bondMenu,false);

        }
View Full Code Here

    @Override
    public IRenderingElement generate(IAtomContainer ac, RendererModel model) {
        JChemPaintRendererModel jcpModel = (JChemPaintRendererModel) model;
        Color selectionColor = jcpModel.getSelectedPartColor();
        IChemObjectSelection selection = model.getSelection();
        return generate(selection, selectionColor, jcpModel);
    }
View Full Code Here

  public void actionPerformed(ActionEvent event)
  {
    System.out.println("action iz "+type);
    IChemObject eventSource = getSource(event);

    IChemObjectSelection selection = jcpPanel.getRenderPanel().getRenderer().getRenderer2DModel().getSelection();
    if(selection ==null || !selection.isFilled()
        &&(type.equals("setRoot")||type.equals("setSubstitute")) ) {
      JOptionPane.showMessageDialog(jcpPanel, GT.get("You have not selected any atoms or bonds."));
      return;
    }

    IChemModelRelay hub = jcpPanel.get2DHub();
    boolean isNewRgroup=false;
    RGroupHandler rGroupHandler=null;
    Map<IAtom,IAtomContainer> existingAtomDistr = new HashMap<IAtom,IAtomContainer>();
    Map<IBond,IAtomContainer> existingBondDistr= new HashMap<IBond,IAtomContainer>();
    IAtomContainer existingRoot=null;
    Map<IAtom, Map<Integer, IBond>> existingRootAttachmentPoints = null;
    Map<RGroup, Map<Integer,IAtom>> existingRGroupApo=null;
    Map<Integer,RGroupList> existingRgroupLists =null;

    IRGroupQuery rgrpQuery=null;
    IAtomContainer molecule=null;

    /* User action: generate possible configurations for the R-group */
    if(type.equals("rgpGenerate")) {
      if((jcpPanel.get2DHub().getRGroupHandler()==null)) {
        JOptionPane.showMessageDialog(jcpPanel, GT.get("Please define an R-group (root and substituents) first."));
        return;
      }
            try {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(jcpPanel.getCurrentWorkDirectory());
        chooser.setFileView(new JCPFileView());
        chooser.showSaveDialog(jcpPanel);
        File outFile = chooser.getSelectedFile();
        System.out.println(outFile);
        List<IAtomContainer> molecules= jcpPanel.get2DHub().getRGroupHandler().getrGroupQuery().getAllConfigurations();
        if (molecules.size() > 0) {
          IAtomContainerSet molSet = molecules.get(0).getBuilder().newInstance(IAtomContainerSet.class);
          for (IAtomContainer mol : molecules) {
            molSet.addAtomContainer(mol);
          }
          SDFWriter sdfWriter = new SDFWriter(new FileWriter(outFile));
          sdfWriter.write(molSet);
          sdfWriter.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(jcpPanel, GT.get("There was an error generating the configurations {0}", e.getMessage()));
        return;
      }
   
    }
    /* User action: advanced R-group logic */
    else if(type.equals("rgpAdvanced")) {

      if((jcpPanel.get2DHub().getRGroupHandler()==null)) {
        JOptionPane.showMessageDialog(jcpPanel, GT.get("Please define an R-group (root and substituent) first."));
        return;
      }
      jcpPanel.get2DHub().getRGroupHandler().cleanUpRGroup(jcpPanel.get2DHub().getChemModel().getMoleculeSet());
      ChemObjectEditor editor = new RGroupEditor(hub);
      editor.setChemObject((org.openscience.cdk.ChemObject)hub.getRGroupHandler().getrGroupQuery());
      ChemObjectPropertyDialog frame = new ChemObjectPropertyDialog(JOptionPane.getFrameForComponent(editor), jcpPanel.get2DHub(),editor);
      frame.pack();
      frame.setVisible(true);
      jcpPanel.get2DHub().updateView();
    }


    //FOLLOWING actions involve undo/redo

    else
    {

      /* User action: generate possible configurations for the R-group */
      if(type.equals("clearRgroup")) {
        if((jcpPanel.get2DHub().getRGroupHandler()==null)) {
          JOptionPane.showMessageDialog(jcpPanel, GT.get("There is no R-group defined"));
          return;
        }
        rGroupHandler=hub.getRGroupHandler();
        hub.unsetRGroupHandler();
        jcpPanel.get2DHub().updateView();

      }

      /* User has indicated that a certain atom in a substituent needs to become attachment point 1 or 2 */
      else if (type.startsWith("setAtomApoAction")) {
        rGroupHandler=hub.getRGroupHandler();
        IAtom apoAtom = (IAtom) eventSource;
        apoLoop:
          for (Iterator<Integer> rnumItr=rGroupHandler.getrGroupQuery().getRGroupDefinitions().keySet().iterator(); rnumItr.hasNext();) {
            for (RGroup rgrp: rGroupHandler.getrGroupQuery().getRGroupDefinitions().get(rnumItr.next()).getRGroups()) {
              if(rgrp.getGroup().contains(apoAtom)) {
                existingRGroupApo= new HashMap <RGroup,Map<Integer,IAtom>>();
                HashMap<Integer,IAtom> map = new HashMap<Integer,IAtom>();
                map.put(1, rgrp.getFirstAttachmentPoint());
                map.put(2, rgrp.getSecondAttachmentPoint());
                existingRGroupApo.put(rgrp,map);

                boolean firstApo = type.endsWith("1");
                if (firstApo) {
                  rgrp.setFirstAttachmentPoint(apoAtom);
                }
                else {
                  rgrp.setSecondAttachmentPoint(apoAtom);
                }
                break apoLoop;
              }
            }
          }
      }

      /* User action : certain bond in the root needs to become attachment bond 1 or 2 */
      else if (type.startsWith("setBondApoAction")) {
        rGroupHandler=hub.getRGroupHandler();
        IBond apoBond = (IBond) eventSource;
        Map<Integer, IBond> apoBonds=null;

        //Undo/redo business______
        IAtom pseudo=null;
        if (apoBond.getAtom(0) instanceof IPseudoAtom)
          pseudo=apoBond.getAtom(0);
        else
          pseudo=apoBond.getAtom(1);
        Map<Integer, IBond> keepApoBonds = new HashMap<Integer, IBond>();
        if (rGroupHandler.getrGroupQuery().getRootAttachmentPoints()!=null &&
            rGroupHandler.getrGroupQuery().getRootAttachmentPoints().get(pseudo)!=null) {
          apoBonds= rGroupHandler.getrGroupQuery().getRootAttachmentPoints().get(pseudo);
          for (Iterator<Integer> apoItr =apoBonds.keySet().iterator(); apoItr.hasNext();) {
            int apoNum=apoItr.next();
            keepApoBonds.put(apoNum,apoBonds.get(apoNum));
          }
        }
        existingRootAttachmentPoints = new HashMap<IAtom, Map<Integer, IBond>>();
        existingRootAttachmentPoints.put(pseudo, keepApoBonds);
        //________________________


        //Set the new Root APO
        if (rGroupHandler.getrGroupQuery().getRootAttachmentPoints()==null) {
          rGroupHandler.getrGroupQuery().setRootAttachmentPoints(new HashMap<IAtom, Map<Integer, IBond>>());
        }
        Map<IAtom, Map<Integer, IBond>> rootApo=rGroupHandler.getrGroupQuery().getRootAttachmentPoints();
        if (rootApo.get(pseudo)==null) {
          apoBonds=new HashMap<Integer,IBond>();
          rootApo.put(pseudo, apoBonds);
        }
        else
          apoBonds=rGroupHandler.getrGroupQuery().getRootAttachmentPoints().get(pseudo);

        if (type.endsWith("1")) {
          apoBonds.put(1, apoBond);
          if (apoBonds.get(2)!=null && apoBonds.get(2).equals(apoBond))
            apoBonds.remove(2);
        }
        if (type.endsWith("2")) {
          apoBonds.put(2, apoBond);
          if (apoBonds.get(1)!=null && apoBonds.get(1).equals(apoBond))
            apoBonds.remove(1);
        }

      }


      /* User action: certain atom+bond selection is to be the root structure. */
      else if (type.equals("setRoot")) {

        IAtomContainer atc =selection.getConnectedAtomContainer();
        if (!isProperSelection(atc)) {
          JOptionPane.showMessageDialog(jcpPanel, GT.get("Please do not make a fragmented selection."));
          return;
        }

        molecule = createMolecule (atc,existingAtomDistr,existingBondDistr);
        hub.getChemModel().getMoleculeSet().addAtomContainer(molecule);

        if (hub.getRGroupHandler() == null) {
          isNewRgroup=true;
          rgrpQuery = newRGroupQuery(molecule.getBuilder());
          rGroupHandler = new RGroupHandler(rgrpQuery, this.jcpPanel);
          hub.setRGroupHandler(rGroupHandler);
        }
        else {
          rGroupHandler=hub.getRGroupHandler();
          rgrpQuery = hub.getRGroupHandler().getrGroupQuery();
          if(rgrpQuery.getRootStructure()!=null) {
            existingRoot=rgrpQuery.getRootStructure();
            rgrpQuery.getRootStructure().removeProperty(CDKConstants.TITLE);
          }
        }
        molecule.setProperty(CDKConstants.TITLE,RGroup.ROOT_LABEL);
        rgrpQuery.setRootStructure(molecule);

        //Remove old root apo's
        existingRootAttachmentPoints = rgrpQuery.getRootAttachmentPoints();
        rgrpQuery.setRootAttachmentPoints(null);

        //Define new root apo's
        Map<IAtom, Map<Integer, IBond>> apoBonds= new HashMap<IAtom, Map<Integer, IBond>>();
        for (IAtom atom : molecule.atoms()) {
          if (atom instanceof IPseudoAtom) {
            IPseudoAtom pseudo = (IPseudoAtom)atom;
            if (pseudo.getLabel()!=null && RGroupQuery.isValidRgroupQueryLabel(pseudo.getLabel())) {
              chooseRootAttachmentBonds(pseudo,molecule,apoBonds);
            }
          }
        }
        rgrpQuery.setRootAttachmentPoints(apoBonds);

      }

      /* User action: certain atom+bond selection is to be a substituent. */
      else if (type.equals("setSubstitute")) {

        if (hub.getRGroupHandler() == null || hub.getRGroupHandler().getrGroupQuery()==null ||
            hub.getRGroupHandler().getrGroupQuery().getRootStructure()==null) {
          JOptionPane.showMessageDialog(jcpPanel, GT.get("Please define a root structure first."));
          return;
        }

        IAtomContainer atc =selection.getConnectedAtomContainer();
        if (!isProperSelection(atc)) {
          JOptionPane.showMessageDialog(jcpPanel, GT.get("Please do not make a fragmented selection."));
          return;
        }

View Full Code Here

                jcpPanel.get2DHub().removeAtom(atomInRange);
                renderModel.setHighlightedAtom(null);
            } else if (bondInRange != null) {
                jcpPanel.get2DHub().removeBond(bondInRange);
            } else if(renderModel.getSelection()!=null && renderModel.getSelection().getConnectedAtomContainer()!=null){
                IChemObjectSelection selection = renderModel.getSelection();
                IAtomContainer selected = selection.getConnectedAtomContainer();
                jcpPanel.get2DHub().deleteFragment(selected);
                renderModel.setSelection(new LogicalSelection(
                        LogicalSelection.Type.NONE));
                jcpPanel.get2DHub().updateView();
            }
        } else if(type.indexOf("pasteTemplate")>-1){
            //if templates are shown, we extract the tab to show if any
            String templatetab="";
            if(type.indexOf("_")>-1){
                templatetab=type.substring(type.indexOf("_")+1);
            }
            TemplateBrowser templateBrowser = new TemplateBrowser(templatetab);
            if(templateBrowser.getChosenmolecule()!=null){
                scaleStructure(templateBrowser.getChosenmolecule());
                insertStructure(templateBrowser.getChosenmolecule(), renderModel);
                jcpPanel.getRenderPanel().setZoomWide(true);
                jcpPanel.get2DHub().getRenderer().getRenderer2DModel().setZoomFactor(1);
            }
        } else if ("paste".equals(type)) {
            handleSystemClipboard(sysClip);
            Transferable transfer = sysClip.getContents( null );
            ISimpleChemObjectReader reader = null;
            String content=null;
           
            if (supported(transfer, molFlavor) ) {
                StringBuffer sb = new StringBuffer();
              try {
                    //StringBufferInputStream sbis=null;
          //sbis = (StringBufferInputStream) transfer.getTransferData(molFlavor);

                    StringReader sbis=null;
          sbis = (StringReader) transfer.getTransferData(molFlavor);

               
                int x;
                    while((x=sbis.read())!=-1){
                        sb.append((char)x);
                    }
                    reader = new MDLReader(new StringReader(sb.toString()));
        } catch (UnsupportedFlavorException e1) {
          e1.printStackTrace();
        } catch (IOException e1) {
          e1.printStackTrace();
              } catch (Exception e1) {
                  reader = new RGroupQueryReader(new StringReader(sb.toString()));
                }

            } else if (supported(transfer, DataFlavor.stringFlavor) ) {
                try {
                    content = (String) transfer.getTransferData(DataFlavor.stringFlavor);
                    reader = new ReaderFactory().createReader(new StringReader(content));
                    //System.out.println(reader.getClass());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }

            // if looks like CML - InputStream required. Reader throws error.
            if(content!=null && content.indexOf("cml")>-1) {
                reader = new CMLReader(new ByteArrayInputStream(content.getBytes()));
            }

            IAtomContainer toPaste = null;
            boolean rgrpQuery=false;
            if (reader != null) {
                IAtomContainer readMolecule =
                    chemModel.getBuilder().newInstance(IAtomContainer.class);
                try {
                    if (reader.accepts(IAtomContainer.class)) {
                        toPaste = (IAtomContainer) reader.read(readMolecule);
                    } else if (reader.accepts(ChemFile.class)) {
                        toPaste = readMolecule;
                        IChemFile file = (IChemFile) reader.read(new ChemFile());
                        for (IAtomContainer ac :
                            ChemFileManipulator.getAllAtomContainers(file)) {
                            toPaste.add(ac);

                        }
                  } else if (reader.accepts(RGroupQuery.class)) {
                rgrpQuery=true;
                    IRGroupQuery rgroupQuery = (RGroupQuery) reader.read(new RGroupQuery(DefaultChemObjectBuilder.getInstance()));
                chemModel = new ChemModel();
                RGroupHandler rgHandler =  new RGroupHandler(rgroupQuery, this.jcpPanel);
                this.jcpPanel.get2DHub().setRGroupHandler(rgHandler);
                chemModel.setMoleculeSet(rgHandler.getMoleculeSet(chemModel));
                rgHandler.layoutRgroup();
               
                  }

                } catch (CDKException e1) {
                    e1.printStackTrace();
                }
            }

            //Attempt SMILES or InChI if no reader is found for content.
            if(rgrpQuery!=true && toPaste == null &&
                    supported(transfer, DataFlavor.stringFlavor)) {
                try{
                    if (content.toLowerCase().indexOf("inchi")>-1 ) {
                        toPaste = InChITool.parseInChI(content);
                    }
                    else {   
                        SmilesParser sp = new SmilesParser(
                                DefaultChemObjectBuilder.getInstance());
                        toPaste = sp.parseSmiles(
                                ((String) transfer.getTransferData(
                                        DataFlavor.stringFlavor)).trim());
                        toPaste = new FixBondOrdersTool().kekuliseAromaticRings(toPaste);

                        IAtomContainerSet mols = ConnectivityChecker.partitionIntoMolecules(toPaste);
                        for(int i=0;i<mols.getAtomContainerCount();i++)
                        {
                            StructureDiagramGenerator sdg =
                                new StructureDiagramGenerator((IAtomContainer)mols.getAtomContainer(i));

                            sdg.setTemplateHandler(
                                    new TemplateHandler(toPaste.getBuilder())
                            );
                            sdg.generateCoordinates();
                        }
                        //SMILES parser sets valencies, unset
                        for(int i=0;i<toPaste.getAtomCount();i++){
                            toPaste.getAtom(i).setValency(null);
                        }
                    }
                } catch (Exception ex) {
                    jcpPanel.announceError(ex);
                    ex.printStackTrace();
                }
            }
           
            if (toPaste != null || rgrpQuery==true) {
                jcpPanel.getRenderPanel().setZoomWide(true);
                jcpPanel.get2DHub().getRenderer().getRenderer2DModel().setZoomFactor(1);
                if ( rgrpQuery==true) {
                  this.jcpPanel.setChemModel(chemModel);
                }
                else {
                  scaleStructure(toPaste);
                  insertStructure(toPaste, renderModel);
                }
            }
            else{
                JOptionPane.showMessageDialog(jcpPanel, GT.get("The content you tried to copy could not be read to any known format"), GT.get("Could not process content"), JOptionPane.WARNING_MESSAGE);
            }

        } else if (type.equals("cut")) {
            handleSystemClipboard(sysClip);
            IAtom atomInRange = null;
            IBond bondInRange = null;
            IChemObject object = getSource(e);
            logger.debug("Source of call: ", object);
            if (object instanceof IAtom) {
                atomInRange = (IAtom) object;
            } else {
                atomInRange = renderModel.getHighlightedAtom();
            }
            if (object instanceof IBond) {
                bondInRange = (IBond) object;
            } else {
                bondInRange = renderModel.getHighlightedBond();
            }
            IAtomContainer tocopyclone =
                jcpPanel.getChemModel().getBuilder().newInstance(IAtomContainer.class);
            if (atomInRange != null) {
                tocopyclone.addAtom(atomInRange);
                jcpPanel.get2DHub().removeAtom(atomInRange);
                renderModel.setHighlightedAtom(null);
            } else if (bondInRange != null) {
                tocopyclone.addBond(bondInRange);
                jcpPanel.get2DHub().removeBond(bondInRange);
            }else if(renderModel.getSelection()!=null && renderModel.getSelection().getConnectedAtomContainer()!=null){
                IChemObjectSelection selection = renderModel.getSelection();
                IAtomContainer selected = selection.getConnectedAtomContainer();
                tocopyclone.add(selected);
                jcpPanel.get2DHub().deleteFragment(selected);
                renderModel.setSelection(new LogicalSelection(
                        LogicalSelection.Type.NONE));
                jcpPanel.get2DHub().updateView();
            }
      if(tocopyclone.getAtomCount()>0 || tocopyclone.getBondCount()>0)
          addToClipboard(sysClip, tocopyclone);
     
        }else if (type.equals("selectAll")) {
            ControllerHub hub = jcpPanel.get2DHub();
            IChemObjectSelection allSelection =
                new LogicalSelection(LogicalSelection.Type.ALL);

            allSelection.select(hub.getIChemModel());
            renderModel.setSelection(allSelection);
            SelectSquareModule succusorModule = new SelectSquareModule(hub);
            succusorModule.setID("select");
            MoveModule newActiveModule = new MoveModule(hub, succusorModule);
            newActiveModule.setID("move");
View Full Code Here

          return;
        }
        jcpPanel.get2DHub().fireStructureChangedEvent();

        //We select the inserted structure
        IChemObjectSelection selection = new LogicalSelection(LogicalSelection.Type.ALL);
        selection.select(ChemModelManipulator.newChemModel(toPaste));
        renderModel.setSelection(selection);
        SelectSquareModule successorModule = new SelectSquareModule(jcpPanel.get2DHub());
        successorModule.setID("select");
        MoveModule newActiveModule = new MoveModule(jcpPanel.get2DHub(), successorModule);
        newActiveModule.setID("move");
View Full Code Here

    public SelectAtomGenerator() {}

    public IRenderingElement generate(IAtomContainer ac, RendererModel model) {
        JChemPaintRendererModel jcpModel = (JChemPaintRendererModel) model;
        Color selectionColor = jcpModel.getSelectedPartColor();
        IChemObjectSelection selection = jcpModel.getSelection();
        return generate(selection, selectionColor, jcpModel);
    }
View Full Code Here

TOP

Related Classes of org.openscience.cdk.renderer.selection.IChemObjectSelection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.