Search in sources :

Example 16 with IWrappedResource

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

the class PythonLabelProvider method checkIfValidPackageFolder.

/**
 * Checks if the given folder is a valid package.
 */
private final boolean checkIfValidPackageFolder(final PythonFolder pythonFolder) {
    String name = pythonFolder.getActualObject().getName();
    if (!PythonPathHelper.isValidModuleLastPart(name)) {
        return false;
    }
    IWrappedResource parentElement = pythonFolder.getParentElement();
    while (parentElement != null) {
        if (parentElement instanceof PythonSourceFolder) {
            // gotten to the source folder: this one doesn't need to have an __init__.py
            return true;
        }
        Object actualObject = parentElement.getActualObject();
        if (!(actualObject instanceof IContainer)) {
            return false;
        }
        IContainer iContainer = (IContainer) actualObject;
        if (!PythonPathHelper.isValidModuleLastPart(iContainer.getName())) {
            return false;
        }
        Object tempParent = parentElement.getParentElement();
        if (!(tempParent instanceof IWrappedResource)) {
            break;
        }
        parentElement = (IWrappedResource) tempParent;
    }
    return true;
}
Also used : IContainer(org.eclipse.core.resources.IContainer) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder)

Example 17 with IWrappedResource

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

the class PythonModelProvider method convertToPythonElementsAddOrRemove.

/**
 * Converts the shape modification to use Python elements.
 * @param natureToSourcePathSet
 *
 * @param modification: the shape modification to convert
 * @param isAdd: boolean indicating whether this convertion is happening in an add operation
 */
@SuppressWarnings("unchecked")
private void convertToPythonElementsAddOrRemove(PipelinedShapeModification modification, boolean isAdd, Map<PythonNature, Set<String>> natureToSourcePathSet) {
    if (DEBUG) {
        debug("Before", modification);
    }
    Object parent = modification.getParent();
    if (parent instanceof IContainer) {
        IContainer parentContainer = (IContainer) parent;
        Object pythonParent = getResourceInPythonModel(parentContainer, true);
        if (pythonParent instanceof IWrappedResource) {
            IWrappedResource parentResource = (IWrappedResource) pythonParent;
            modification.setParent(parentResource);
            wrapChildren(parentResource, parentResource.getSourceFolder(), modification.getChildren(), isAdd, natureToSourcePathSet);
        } else if (pythonParent == null) {
            Object parentInWrap = parentContainer;
            PythonSourceFolder sourceFolderInWrap = null;
            // this may happen when a source folder is added or some element that still doesn't have it's parent in the model...
            // so, we have to get the parent's parent until we actually 'know' that it is not in the model (or until we run
            // out of parents to try)
            // the case in which we reproduce this is Test 1 (described in the class)
            FastStack<Object> found = new FastStack<Object>(20);
            while (true) {
                // add the current to the found
                if (parentContainer == null) {
                    break;
                }
                found.push(parentContainer);
                if (parentContainer instanceof IProject) {
                    // we got to the project without finding any part of a python model already there, so, let's see
                    // if any of the parts was actually a source folder (that was still not added)
                    tryCreateModelFromProject((IProject) parentContainer, found, natureToSourcePathSet);
                    // and now, if it was created, try to convert it to the python model (without any further add)
                    convertToPythonElementsUpdateOrRefresh(modification.getChildren());
                    return;
                }
                Object p = getResourceInPythonModel(parentContainer, true);
                if (p instanceof IWrappedResource) {
                    IWrappedResource wrappedResource = (IWrappedResource) p;
                    sourceFolderInWrap = wrappedResource.getSourceFolder();
                    while (found.size() > 0) {
                        Object f = found.pop();
                        if (f instanceof IResource) {
                            // no need to create it if it's already in the model!
                            Object child = sourceFolderInWrap.getChild((IResource) f);
                            if (child != null && child instanceof IWrappedResource) {
                                wrappedResource = (IWrappedResource) child;
                                continue;
                            }
                        }
                        // creating is enough to add it to the model
                        if (f instanceof IFile) {
                            wrappedResource = new PythonFile(wrappedResource, (IFile) f, sourceFolderInWrap);
                        } else if (f instanceof IFolder) {
                            wrappedResource = new PythonFolder(wrappedResource, (IFolder) f, sourceFolderInWrap);
                        }
                    }
                    parentInWrap = wrappedResource;
                    break;
                }
                parentContainer = parentContainer.getParent();
            }
            wrapChildren(parentInWrap, sourceFolderInWrap, modification.getChildren(), isAdd, natureToSourcePathSet);
        }
    } else if (parent == null) {
        wrapChildren(null, null, modification.getChildren(), isAdd, natureToSourcePathSet);
    }
    if (DEBUG) {
        debug("After", modification);
    }
}
Also used : FastStack(org.python.pydev.shared_core.structure.FastStack) IFile(org.eclipse.core.resources.IFile) PythonFolder(org.python.pydev.navigator.elements.PythonFolder) IContainer(org.eclipse.core.resources.IContainer) PythonFile(org.python.pydev.navigator.elements.PythonFile) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 18 with IWrappedResource

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

the class PythonModelProvider method tryCreateModelFromProject.

/**
 * Given a Path from the 1st child of the project, will try to create that path in the python model.
 * @param project the project
 * @param found a stack so that the last element added is the leaf of the path we want to discover
 * @param natureToSourcePathSet
 */
private void tryCreateModelFromProject(IProject project, FastStack<Object> found, Map<PythonNature, Set<String>> natureToSourcePathSet) {
    PythonNature nature = PythonNature.getPythonNature(project);
    if (nature == null) {
        // if the python nature is not available, we won't have any python elements here
        return;
    }
    Set<String> sourcePathSet = this.getSourcePathSet(natureToSourcePathSet, nature);
    Object currentParent = project;
    PythonSourceFolder pythonSourceFolder = null;
    for (Iterator<Object> it = found.topDownIterator(); it.hasNext(); ) {
        Object child = it.next();
        if (child instanceof IFolder || child instanceof IProject) {
            if (pythonSourceFolder == null) {
                pythonSourceFolder = tryWrapSourceFolder(currentParent, (IContainer) child, sourcePathSet);
                if (pythonSourceFolder != null) {
                    currentParent = pythonSourceFolder;
                } else if (child instanceof IContainer) {
                    currentParent = child;
                }
                // we didn't, then the children will not be in the python model anyway)
                continue;
            }
        }
        if (pythonSourceFolder != null) {
            IWrappedResource r = doWrap(currentParent, pythonSourceFolder, child, natureToSourcePathSet);
            if (r != null) {
                child = r;
            }
        }
        currentParent = child;
    }
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) IContainer(org.eclipse.core.resources.IContainer) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) IProject(org.eclipse.core.resources.IProject) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) IFolder(org.eclipse.core.resources.IFolder)

Example 19 with IWrappedResource

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

the class PythonModelProvider method wrapChildren.

/**
 * Actually wraps some resource into a wrapped resource.
 *
 * @param parent this is the parent
 *        it may be null -- in the case of a remove
 *        it may be a wrapped resource (if it is in the python model)
 *        it may be a resource (if it is a source folder)
 *
 * @param pythonSourceFolder this is the python source folder for the resource (it may be null if the resource itself is a source folder
 *        or if it is actually a resource that has already been removed)
 * @param currentChildren those are the children that should be wrapped
 * @param isAdd whether this is an add operation or not
 * @param natureToSourcePathSet
 * @return
 */
private boolean wrapChildren(Object parent, PythonSourceFolder pythonSourceFolder, Set<Object> currentChildren, boolean isAdd, Map<PythonNature, Set<String>> natureToSourcePathSet) {
    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;
        }
        // yeap, it may be an object that's not an actual resource (created by some other plugin... just continue)
        if (!(child instanceof IResource)) {
            continue;
        }
        Object existing = getResourceInPythonModel((IResource) child, true);
        if (existing == null) {
            if (isAdd) {
                // add
                IWrappedResource w = doWrap(parent, pythonSourceFolder, child, natureToSourcePathSet);
                if (w != null) {
                    // if it is null, it is not below a python source folder
                    childrenItr.remove();
                    convertedChildren.add(w);
                }
            } else {
                // it has already been removed
                continue;
            }
        } else {
            // existing != null
            childrenItr.remove();
            convertedChildren.add(existing);
            if (!isAdd) {
                // also remove it from the model
                IWrappedResource wrapped = (IWrappedResource) existing;
                wrapped.getSourceFolder().removeChild((IResource) child);
            }
        }
    }
    // if we did have some wrapping... go on and add them to the out list (and return true)
    if (!convertedChildren.isEmpty()) {
        currentChildren.addAll(convertedChildren);
        return true;
    }
    // nothing happened, so, just say it
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IResource(org.eclipse.core.resources.IResource) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource)

Aggregations

IWrappedResource (org.python.pydev.navigator.elements.IWrappedResource)19 IProject (org.eclipse.core.resources.IProject)8 IResource (org.eclipse.core.resources.IResource)7 IFile (org.eclipse.core.resources.IFile)6 PythonSourceFolder (org.python.pydev.navigator.elements.PythonSourceFolder)6 IContainer (org.eclipse.core.resources.IContainer)5 IWorkingSet (org.eclipse.ui.IWorkingSet)5 PythonNature (org.python.pydev.plugin.nature.PythonNature)5 HashSet (java.util.HashSet)4 IFolder (org.eclipse.core.resources.IFolder)4 PipelinedShapeModification (org.eclipse.ui.navigator.PipelinedShapeModification)4 LinkedHashSet (java.util.LinkedHashSet)3 PythonFolder (org.python.pydev.navigator.elements.PythonFolder)3 PythonProjectSourceFolder (org.python.pydev.navigator.elements.PythonProjectSourceFolder)3 TreeNode (org.python.pydev.shared_core.structure.TreeNode)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 IAdaptable (org.eclipse.core.runtime.IAdaptable)2