Search in sources :

Example 1 with PythonResource

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

the class PythonModelProvider method convertToPythonElementsUpdateOrRefresh.

/**
 * Converts elements to the python model -- but only creates it if it's parent is found in the python model
 */
protected boolean convertToPythonElementsUpdateOrRefresh(Set<Object> currentChildren) {
    final Map<PythonNature, Set<String>> natureToSourcePathSet = new HashMap<>();
    LinkedHashSet<Object> convertedChildren = new LinkedHashSet<>();
    for (Iterator<Object> childrenItr = currentChildren.iterator(); childrenItr.hasNext(); ) {
        Object child = childrenItr.next();
        if (child == null) {
            // only case when a child is removed and another one is not added (null)
            childrenItr.remove();
            continue;
        }
        if (child instanceof IResource && !(child instanceof IWrappedResource)) {
            IResource res = (IResource) child;
            Object resourceInPythonModel = getResourceInPythonModel(res, true);
            if (resourceInPythonModel != null) {
                // if it is in the python model, just go on
                childrenItr.remove();
                convertedChildren.add(resourceInPythonModel);
            } else {
                // now, if it's not but its parent is, go on and create it
                IContainer p = res.getParent();
                if (p == null) {
                    continue;
                }
                Object pythonParent = getResourceInPythonModel(p, true);
                if (pythonParent instanceof IWrappedResource) {
                    IWrappedResource parent = (IWrappedResource) pythonParent;
                    if (res instanceof IProject) {
                        throw new RuntimeException("A project's parent should never be an IWrappedResource!");
                    } else if (res instanceof IFolder) {
                        childrenItr.remove();
                        convertedChildren.add(new PythonFolder(parent, (IFolder) res, parent.getSourceFolder()));
                    } else if (res instanceof IFile) {
                        childrenItr.remove();
                        convertedChildren.add(new PythonFile(parent, (IFile) res, parent.getSourceFolder()));
                    } else if (child instanceof IResource) {
                        childrenItr.remove();
                        convertedChildren.add(new PythonResource(parent, (IResource) child, parent.getSourceFolder()));
                    }
                } else if (res instanceof IFolder) {
                    // ok, still not in the model... could it be a PythonSourceFolder
                    IFolder folder = (IFolder) res;
                    IProject project = folder.getProject();
                    if (project == null) {
                        continue;
                    }
                    PythonNature nature = PythonNature.getPythonNature(project);
                    if (nature == null) {
                        continue;
                    }
                    Set<String> sourcePathSet = this.getSourcePathSet(natureToSourcePathSet, nature);
                    PythonSourceFolder wrapped = tryWrapSourceFolder(p, folder, sourcePathSet);
                    if (wrapped != null) {
                        childrenItr.remove();
                        convertedChildren.add(wrapped);
                    }
                }
            }
        }
    }
    if (!convertedChildren.isEmpty()) {
        currentChildren.addAll(convertedChildren);
        return true;
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PythonResource(org.python.pydev.navigator.elements.PythonResource) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) IWorkingSet(org.eclipse.ui.IWorkingSet) IFile(org.eclipse.core.resources.IFile) PythonNature(org.python.pydev.plugin.nature.PythonNature) HashMap(java.util.HashMap) PythonFolder(org.python.pydev.navigator.elements.PythonFolder) PythonFile(org.python.pydev.navigator.elements.PythonFile) IProject(org.eclipse.core.resources.IProject) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 IContainer (org.eclipse.core.resources.IContainer)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 IWorkingSet (org.eclipse.ui.IWorkingSet)1 IWrappedResource (org.python.pydev.navigator.elements.IWrappedResource)1 PythonFile (org.python.pydev.navigator.elements.PythonFile)1 PythonFolder (org.python.pydev.navigator.elements.PythonFolder)1 PythonResource (org.python.pydev.navigator.elements.PythonResource)1 PythonSourceFolder (org.python.pydev.navigator.elements.PythonSourceFolder)1 PythonNature (org.python.pydev.plugin.nature.PythonNature)1