Search in sources :

Example 1 with InterpreterInfoTreeNodeRoot

use of org.python.pydev.navigator.InterpreterInfoTreeNodeRoot in project Pydev by fabioz.

the class PythonLinkHelper method findExternalFileSelectionGivenTreeSelection.

private IStructuredSelection findExternalFileSelectionGivenTreeSelection(File f, CommonViewer commonViewer, ITreeContentProvider treeContentProvider, Set<IInterpreterInfo> infosSearched, final Object next) {
    if (next instanceof IAdaptable) {
        IAdaptable adaptable = (IAdaptable) next;
        IResource resource = (IResource) adaptable.getAdapter(IResource.class);
        if (resource != null) {
            IProject project = resource.getProject();
            if (project != null) {
                Object[] children = treeContentProvider.getChildren(project);
                for (Object object : children) {
                    if (object instanceof InterpreterInfoTreeNodeRoot) {
                        IStructuredSelection sel = findMatchInTreeNodeRoot(f, commonViewer, (InterpreterInfoTreeNodeRoot) object, infosSearched);
                        if (sel != null) {
                            return sel;
                        }
                    }
                }
                return null;
            }
        }
    // Keep on going to try to find a parent that'll adapt to IResource...
    }
    if (next instanceof TreeNode) {
        TreeNode treeNode = (TreeNode) next;
        while (true) {
            if (treeNode instanceof InterpreterInfoTreeNodeRoot) {
                IStructuredSelection sel = findMatchInTreeNodeRoot(f, commonViewer, (InterpreterInfoTreeNodeRoot) treeNode, infosSearched);
                if (sel != null) {
                    return sel;
                }
                return null;
            }
            if (treeNode instanceof PythonpathTreeNode) {
                PythonpathTreeNode pythonpathTreeNode = (PythonpathTreeNode) treeNode;
                if (f.equals(pythonpathTreeNode.file)) {
                    return new StructuredSelection(treeNode);
                }
            }
            Object parent = treeNode.getParent();
            if (parent instanceof TreeNode) {
                treeNode = (TreeNode) parent;
            } else {
                break;
            }
        }
        // Couldn't find a proper InterpreterInfoTreeNodeRoot already having a TreeNode? Let's log it, as a TreeNode
        // should always map to an InterpreterInfoTreeNodeRoot.
        Log.log("Couldn't find a proper InterpreterInfoTreeNodeRoot already having TreeNode: " + next);
        return null;
    }
    // Some unexpected type... let's get its parent until we find one expected (or just end up trying if we get to the root).
    Object parent = next;
    int i = 200;
    // so, this is likely a problem in the content provider).
    while (i > 0) {
        i--;
        if (i == 0) {
            Log.log("Found a recursion for the element: " + next + " when searching parents. Please report this a a bug!");
        }
        if (parent == null || parent instanceof IWorkspaceRoot || parent instanceof IWorkingSet) {
            break;
        }
        if (parent instanceof TreeNode && parent != next) {
            return findExternalFileSelectionGivenTreeSelection(f, commonViewer, treeContentProvider, infosSearched, parent);
        } else if (parent instanceof IAdaptable) {
            IAdaptable adaptable = (IAdaptable) parent;
            IResource resource = (IResource) adaptable.getAdapter(IResource.class);
            if (resource != null) {
                IProject project = resource.getProject();
                if (project != null && project != next) {
                    return findExternalFileSelectionGivenTreeSelection(f, commonViewer, treeContentProvider, infosSearched, project);
                }
            }
        }
        parent = treeContentProvider.getParent(parent);
    }
    return null;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) InterpreterInfoTreeNodeRoot(org.python.pydev.navigator.InterpreterInfoTreeNodeRoot) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IProject(org.eclipse.core.resources.IProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) PythonpathTreeNode(org.python.pydev.navigator.PythonpathTreeNode) TreeNode(org.python.pydev.shared_core.structure.TreeNode) IResource(org.eclipse.core.resources.IResource) IWorkingSet(org.eclipse.ui.IWorkingSet) PythonpathTreeNode(org.python.pydev.navigator.PythonpathTreeNode)

Aggregations

IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 IWorkingSet (org.eclipse.ui.IWorkingSet)1 InterpreterInfoTreeNodeRoot (org.python.pydev.navigator.InterpreterInfoTreeNodeRoot)1 PythonpathTreeNode (org.python.pydev.navigator.PythonpathTreeNode)1 TreeNode (org.python.pydev.shared_core.structure.TreeNode)1