Example usage for org.eclipse.jface.viewers IStructuredSelection toList

List of usage examples for org.eclipse.jface.viewers IStructuredSelection toList

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers IStructuredSelection toList.

Prototype

public List toList();

Source Link

Document

Returns the elements in this selection as a List.

Usage

From source file:net.bioclipse.cdk.ui.handlers.KabschAlignHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection sel = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
    if (!sel.isEmpty() && sel instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) sel;
        ICDKManager cdk = Activator.getDefault().getJavaCDKManager();
        IUIManager ui = net.bioclipse.ui.business.Activator.getDefault().getUIManager();
        List<IMolecule> molecules = new ArrayList<IMolecule>(ssel.size());
        for (Object file : ssel.toList())
            molecules.add(cdk.loadMolecule((IFile) file));
        String path = "/Virtual/aligned.sdf";
        while (ui.fileExists(path))
            path = "/Virtual/aligned" + UUID.randomUUID() + ".sdf";
        try {//from  w w  w.  j a  va  2s .  co  m
            List<ICDKMolecule> aligned = cdk.kabsch(molecules);
            List<IMolecule> alignedMols = new ArrayList<IMolecule>();
            for (IMolecule mol : aligned)
                alignedMols.add(mol);
            cdk.saveSDFile(path, alignedMols);
            ui.open(path, "net.bioclipse.jmol.editors.JmolEditor");
        } catch (BioclipseException cause) {
            throw new ExecutionException("Error while calculating RMSD matrix...", cause);
        } catch (InvocationTargetException cause) {
            throw new ExecutionException("Error while calculating RMSD matrix...", cause);
        }
    }
    return null;
}

From source file:net.bioclipse.cdk.ui.sdfeditor.editor.MoleculesEditor.java

License:Open Source License

protected void setupDragSource() {
    int operations = DND.DROP_COPY | DND.DROP_MOVE;
    CompositeTable viewer = null;//from w  w w  .ja  v  a2s .  c  o  m
    DragSource dragSource = new DragSource(viewer, operations);
    Transfer[] transferTypes = new Transfer[] { LocalSelectionTransfer.getTransfer() };
    dragSource.setTransfer(transferTypes);

    dragSource.addDragListener(new DragSourceListener() {

        public void dragStart(DragSourceEvent event) {
            if (!getSelectedRows().isEmpty()) {
                LocalSelectionTransfer.getTransfer().setSelection(getSelectedRows());
                event.doit = true;
            } else
                event.doit = false;
        }

        public void dragSetData(DragSourceEvent event) {
            ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();

            if (LocalSelectionTransfer.getTransfer().isSupportedType(event.dataType)) {

                event.data = selection;

            } else {
                IStructuredSelection selection1 = (IStructuredSelection) getSelectedRows();
                List<EditorInputData> data = new ArrayList<EditorInputData>();
                for (Object o : selection1.toList()) {
                    MoleculesIndexEditorInput input = new MoleculesIndexEditorInput((SDFElement) o);
                    data.add(EditorInputTransfer
                            .createEditorInputData("net.bioclipse.cdk.ui.editors.jchempaint", input));
                }
                event.data = data.toArray(new EditorInputData[0]);
            }

        }

        public void dragFinished(DragSourceEvent event) {
        }

    });
}

From source file:net.bioclipse.chemoinformatics.dialogs.PickMoleculeDialog.java

License:Open Source License

/**
 * React on treeviewer changes to be able to return the selected resources
 *///ww w  .  j a  va2 s . c  o m
public void selectionChanged(SelectionChangedEvent event) {

    IStructuredSelection selection = (IStructuredSelection) event.getSelection();
    selectedFiles = new ArrayList<IFile>();
    for (Object obj : selection.toList()) {
        if (obj instanceof IFile) {
            IFile file = (IFile) obj;
            selectedFiles.add(file);
        }
    }
}

From source file:net.bioclipse.core.internal.filesystem.memory.MemoryTreeSelectionDialog.java

License:Open Source License

protected void okPressed() {
    IStructuredSelection selection = (IStructuredSelection) tree.getSelection();
    setResult(selection.toList());
    super.okPressed();
}

From source file:net.bioclipse.ds.ui.views.DSView.java

License:Open Source License

protected void doIncludeSelectedTests() {

    IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
    for (Object obj : sel.toList()) {
        if (obj instanceof IDSTest) {
            IDSTest dstest = (IDSTest) obj;
            dstest.setExcluded(false);//from w  w  w  .  ja  v a2 s. co  m
            viewer.refresh(dstest);
        } else if (obj instanceof TestRun) {
            TestRun testrun = (TestRun) obj;
            testrun.setStatus(TestRun.NOT_STARTED);
            testrun.getTest().setExcluded(false);
            viewer.refresh(testrun);
        }
    }

}

From source file:net.bioclipse.ds.ui.views.DSView.java

License:Open Source License

protected void doExcludeSelectedTests() {

    IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
    for (Object obj : sel.toList()) {
        if (obj instanceof IDSTest) {
            IDSTest dstest = (IDSTest) obj;
            dstest.setExcluded(true);//from  w ww. j a  v a  2 s . c o  m
            viewer.refresh(dstest);
        } else if (obj instanceof TestRun) {
            TestRun testrun = (TestRun) obj;
            testrun.setStatus(TestRun.EXCLUDED);
            testrun.getTest().setExcluded(true);
            if (testrun.getMatches() != null)
                testrun.getMatches().clear();
            viewer.refresh(testrun);
        }
    }

}

From source file:net.bioclipse.jmol.cdk.views.JmolView.java

License:Open Source License

/**
 * /* w  w  w  .j a  va2s.  c  o m*/
 * @param selection
 */
private void reactOnSelection(ISelection selection) {

    if (!(selection instanceof IStructuredSelection)) {
        clearView();
        return;
    }
    IStructuredSelection eclipseSelection = (IStructuredSelection) selection;

    /*
     * Get info from selections directly or via adapter in this order:
     * 
     * 1. Collect (or generate from IMolecule) ICDKMolecules
     * 2. Collect ChemModels to visualize
     * 3. Collect atoms/bonds to highlight (IChemicalSelection)
     * 4. Collect scripts to run
     */

    //Store selected CDKMols in List
    List<ICDKMolecule> collectedCDKMols = new ArrayList<ICDKMolecule>();

    //Store chemical selections in List
    List<IChemicalSelection> chemicalSelections = new ArrayList<IChemicalSelection>();

    //Store pharmacophpreProviders in List
    List<IPharmacophoreProvider> pcoreProviders = new ArrayList<IPharmacophoreProvider>();

    //See if list is the same as previously selected
    boolean newSelection = false;
    for (Object obj : eclipseSelection.toList()) {

        if (!(lastSelected.contains(obj))) {
            newSelection = true;
        }
    }
    if (newSelection == false) {
        //            System.out.println("Omitting selection!");
        return;
    }

    //We have a new selection
    lastSelected.clear();

    //Extract molecules and chemical selection from the eclipseSelection
    //Store extracted info in lists
    extractFromSelection(eclipseSelection, collectedCDKMols, chemicalSelections, pcoreProviders);

    //Extract JmolModelSet from selection.
    //This happens when JmolEditor provides input via adapter
    JmolModelString jms = extractJmolModelsetFromSelection(eclipseSelection);
    //        System.out.println("** jms: " + jms);
    if (jms != null) {
        //            System.out.println("** jms string: " + jms.getModelString());
        //This overrides other ICDKMolecules (for now at least)
        runScript(jms.getModelString());
        return;
    }

    //TODO: continue here

    //We have now collected everything we are interested in.
    //If no fun, return
    if ((collectedCDKMols.size() <= 0) && chemicalSelections.size() <= 0) {

        return;

    }

    /*
     * If extracted molecules: Set up Chemfile and and send it to jmol
     */
    if (collectedCDKMols != null) {

        logger.debug("Extracted the following molecules from selection:");
        for (ICDKMolecule cmol : collectedCDKMols) {
            logger.debug("  * " + cmol.getName());
        }

        processMolecules(collectedCDKMols);
    }

    /*
     * Process any pharmacophoreProviders
    * This means e.g. to draw spheres and lines
    */
    if (pcoreProviders.size() > 0) {

        logger.debug("Extracted the following pcoreProviders:");
        for (IChemicalSelection csel : chemicalSelections) {
            logger.debug("  * " + csel.getSelection());
        }

        processPcoreProviders(pcoreProviders);
    }

    /*
     * Process any chemicalSelections
    * This means to show/filter/add commands to the already shown molecules
    * For example, display one or more models, highlight atoms, run scripts
    */
    if (chemicalSelections.size() > 0) {

        logger.debug("Extracted the following chemical selections:");
        for (IChemicalSelection csel : chemicalSelections) {
            logger.debug("  * " + csel.getSelection());
        }

        processChemicalSelections(chemicalSelections);
    }

}

From source file:net.bioclipse.jmol.cdk.views.JmolView.java

License:Open Source License

private JmolModelString extractJmolModelsetFromSelection(IStructuredSelection eclipseSelection) {

    for (Object obj : eclipseSelection.toList()) {
        if (obj instanceof JmolModelString) {
            return (JmolModelString) obj;
        }//from   w ww .  j  a v a  2  s . c  o  m

        if (obj instanceof IAdaptable) {
            IAdaptable adaptable = (IAdaptable) obj;
            JmolModelString jms = (JmolModelString) adaptable.getAdapter(JmolModelString.class);
            return jms;
        }
    }

    //Default is null
    return null;
}

From source file:net.bioclipse.jmol.cdk.views.JmolView.java

License:Open Source License

private void extractFromSelection(IStructuredSelection ssel, List<ICDKMolecule> collectedCDKMols,
        List<IChemicalSelection> chemicalSelections, List<IPharmacophoreProvider> pcoreProviders) {

    //Loop all selections; if they can provide AC: add to moleculeSet
    for (Object obj : ssel.toList()) {

        boolean storeSelection = false;

        //If we have an ICDKMolecule, just get the AC directly
        if (obj instanceof ICDKMolecule) {
            ICDKMolecule cdkmol = (ICDKMolecule) obj;
            collectedCDKMols.add(cdkmol);
            storeSelection = true;/*from  ww  w  .j  a  v a  2 s.  c  o  m*/

            //                if (cdkmol.getAtomContainer()==null){
            //                    logger.debug("CDKMolecule but can't get AtomContainer.");
            //                }
            //                //Only add if have 3D coords
            //                IAtomContainer ac=cdkmol.getAtomContainer();
            //                if (GeometryTools.has3DCoordinates(ac)){
            //                    addAtomContainer( displayedModels, ac );
            //                }
        }

        //Else try to get the different adapters
        else if (obj instanceof IAdaptable) {
            IAdaptable ada = (IAdaptable) obj;

            //Handle case where Iadaptable can return a molecule
            Object molobj = ada.getAdapter(net.bioclipse.core.domain.IMolecule.class);
            if (molobj != null) {
                //If adaptable returns a cdkmolecule, add it directly 
                if (molobj instanceof ICDKMolecule) {
                    ICDKMolecule cdkmol = (ICDKMolecule) molobj;
                    collectedCDKMols.add(cdkmol);
                }

                //If adaptable at least returns an IMolecule
                //we can create CDKMolecule from it (this is costly though)
                else if (molobj instanceof net.bioclipse.core.domain.IMolecule) {
                    net.bioclipse.core.domain.IMolecule bcmol = (net.bioclipse.core.domain.IMolecule) molobj;
                    try {
                        //Lengthy operation, as via CML or SMILES
                        ICDKMolecule cdkmol = cdk.asCDKMolecule(bcmol);
                        collectedCDKMols.add(cdkmol);
                    } catch (BioclipseException e) {
                        e.printStackTrace();
                    }
                }
                storeSelection = true;
            }

            //Handle case where Iadaptable can return atoms to be highlighted
            Object chemSelectionObj = ada.getAdapter(IChemicalSelection.class);
            if (chemSelectionObj != null) {
                chemicalSelections.add((IChemicalSelection) chemSelectionObj);
                storeSelection = true;
            }

            //Handle case where Iadaptable can return models to be shown
            Object chemModelSel = ada.getAdapter(ModelSelection.class);
            if (chemModelSel != null) {
                chemicalSelections.add((IChemicalSelection) chemModelSel);
                storeSelection = true;
            }

            //Handle case where Iadaptable can return a script
            Object scriptSelection = ada.getAdapter(ScriptSelection.class);
            if (scriptSelection != null) {
                chemicalSelections.add((ScriptSelection) scriptSelection);
                storeSelection = true;
            }

            //Handle case where Iadaptable can return a pharmacophoreProvider
            Object pcoreProvider = ada.getAdapter(IPharmacophoreProvider.class);
            if (pcoreProvider != null) {
                pcoreProviders.add((IPharmacophoreProvider) pcoreProvider);
                storeSelection = true;
            }

        } //End of adaptable collection

        if (storeSelection == true) {
            System.out.println("Storing selection in jmol");
            lastSelected.add(obj);
        }

    } //End of loop over selections
}

From source file:net.bioclipse.metaprint2d.ui.actions.MetaPrint2DBatchHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {

    ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
            .getSelection();//w  w w  .ja v  a 2 s  . c o  m
    if (selection == null) {
        showError("Selection is empty");
        return null;
    }

    if (!(selection instanceof IStructuredSelection)) {
        showError("Selection does not contain an SDF or CML file.");
        return null;
    }

    IStructuredSelection ssel = (IStructuredSelection) selection;

    IMetaPrint2DManager m2d = Activator.getDefault().getMetaPrint2DManager();

    List<String> errors = new ArrayList<String>();

    for (Object obj : ssel.toList()) {
        if (obj instanceof IFile) {
            IFile file = (IFile) obj;
            try {
                m2d.calculate(file, true);
            } catch (Exception e) {
                e.printStackTrace();
                errors.add(" Error calculating file: '" + file + "': " + e.getMessage());
            }
        }
    }

    if (errors.size() > 0) {
        String emsg = "Metaprint2D calculation experienced the following errors:\n";
        for (String er : errors) {
            emsg = emsg + "\n - " + er;
        }
        showError(emsg);
    }

    return null;

}