Search in sources :

Example 6 with IWrappedResource

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

the class PythonLabelProvider method getText.

/**
 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
 */
@Override
public String getText(Object element) {
    if (element instanceof PythonNode) {
        PythonNode node = (PythonNode) element;
        return node.entry.toString();
    }
    if (element instanceof PythonSourceFolder) {
        PythonSourceFolder sourceFolder = (PythonSourceFolder) element;
        return provider.getText(sourceFolder.container);
    }
    if (element instanceof IWrappedResource) {
        IWrappedResource resource = (IWrappedResource) element;
        return provider.getText(resource.getActualObject());
    }
    if (element instanceof TreeNode<?>) {
        TreeNode<?> treeNode = (TreeNode<?>) element;
        LabelAndImage data = (LabelAndImage) treeNode.getData();
        return data.label;
    }
    if (element instanceof ProjectConfigError) {
        return ((ProjectConfigError) element).getLabel();
    }
    return provider.getText(element);
}
Also used : TreeNode(org.python.pydev.shared_core.structure.TreeNode) PythonNode(org.python.pydev.navigator.elements.PythonNode) ProjectConfigError(org.python.pydev.navigator.elements.ProjectConfigError) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource)

Example 7 with IWrappedResource

use of org.python.pydev.navigator.elements.IWrappedResource 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)

Example 8 with IWrappedResource

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

the class PythonModelProvider method getPipelinedChildren.

/**
 * This method basically replaces all the elements for other resource elements
 * or for wrapped elements.
 *
 * @see org.eclipse.ui.navigator.IPipelinedTreeContentProvider#getPipelinedChildren(java.lang.Object, java.util.Set)
 */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void getPipelinedChildren(Object parent, Set currentElements) {
    if (DEBUG) {
        System.out.println("getPipelinedChildren for: " + parent);
    }
    final Map<PythonNature, Set<String>> natureToSourcePathSet = new HashMap<>();
    if (parent instanceof IWrappedResource) {
        // Note: It seems that this NEVER happens (IWrappedResources only have getChildren called, not getPipelinedChildren)
        Object[] children = getChildren(parent);
        currentElements.clear();
        currentElements.addAll(Arrays.asList(children));
        if (DEBUG) {
            System.out.println("getPipelinedChildren RETURN: " + currentElements);
        }
        if (parent instanceof PythonProjectSourceFolder) {
            PythonProjectSourceFolder projectSourceFolder = (PythonProjectSourceFolder) parent;
            IProject project = (IProject) projectSourceFolder.getActualObject();
            fillChildrenForProject(currentElements, project, parent);
        }
        return;
    } else if (parent instanceof IWorkspaceRoot) {
        switch(topLevelChoice.getRootMode()) {
            case TopLevelProjectsOrWorkingSetChoice.WORKING_SETS:
                currentElements.clear();
                currentElements.addAll(getWorkingSetsCallback.call((IWorkspaceRoot) parent));
                if (currentElements.size() == 0) {
                    currentElements.add(createErrorNoWorkingSetsDefined(parent));
                }
            case TopLevelProjectsOrWorkingSetChoice.PROJECTS:
        }
    } else if (parent instanceof IWorkingSet) {
        IWorkingSet workingSet = (IWorkingSet) parent;
        currentElements.clear();
        currentElements.addAll(Arrays.asList(workingSet.getElements()));
        if (currentElements.size() == 0) {
            currentElements.add(createErrorNoWorkingSetsDefined(workingSet));
        }
    } else if (parent instanceof TreeNode) {
        TreeNode treeNode = (TreeNode) parent;
        currentElements.addAll(treeNode.getChildren());
    } else if (parent instanceof IProject) {
        IProject project = (IProject) parent;
        fillChildrenForProject(currentElements, project, getResourceInPythonModel(project));
    }
    PipelinedShapeModification modification = new PipelinedShapeModification(parent, currentElements);
    convertToPythonElementsAddOrRemove(modification, true, natureToSourcePathSet);
    if (DEBUG) {
        System.out.println("getPipelinedChildren RETURN: " + modification.getChildren());
    }
}
Also used : HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) IWorkingSet(org.eclipse.ui.IWorkingSet) PythonNature(org.python.pydev.plugin.nature.PythonNature) HashMap(java.util.HashMap) PythonProjectSourceFolder(org.python.pydev.navigator.elements.PythonProjectSourceFolder) IProject(org.eclipse.core.resources.IProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) PipelinedShapeModification(org.eclipse.ui.navigator.PipelinedShapeModification) TreeNode(org.python.pydev.shared_core.structure.TreeNode) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) IWorkingSet(org.eclipse.ui.IWorkingSet)

Example 9 with IWrappedResource

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

the class PythonModelProviderTest method testProjectIsRoot2.

/**
 * Test if setting the project root as a source folder will return an object from the python model.
 */
public void testProjectIsRoot2() throws Exception {
    String pythonpathLoc = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot";
    final HashSet<String> pythonPathSet = new HashSet<String>();
    pythonPathSet.add(pythonpathLoc);
    PythonNature nature = createNature(pythonPathSet);
    WorkspaceRootStub workspaceRootStub = new WorkspaceRootStub();
    project = new ProjectStub(new File(pythonpathLoc), nature);
    provider = new PythonModelProvider();
    FolderStub folder = new FolderStub(project, new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source"));
    workspaceRootStub.addChild(project);
    project.setParent(workspaceRootStub);
    HashSet<Object> folders = new HashSet<Object>();
    folders.add(folder);
    PipelinedShapeModification addModification = new PipelinedShapeModification(project, folders);
    addModification.setParent(project);
    provider.interceptAdd(addModification);
    assertEquals(1, addModification.getChildren().size());
    // it should've been wrapped
    assertTrue(addModification.getChildren().iterator().next() instanceof IWrappedResource);
}
Also used : PythonNature(org.python.pydev.plugin.nature.PythonNature) FolderStub(org.python.pydev.shared_core.resource_stubs.FolderStub) PipelinedShapeModification(org.eclipse.ui.navigator.PipelinedShapeModification) File(java.io.File) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) HashSet(java.util.HashSet)

Example 10 with IWrappedResource

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

the class CustomizationCommons method getResourceFromObject.

public static IResource getResourceFromObject(Object receiver) {
    if (receiver instanceof IWrappedResource) {
        IWrappedResource wrappedResource = (IWrappedResource) receiver;
        Object actualObject = wrappedResource.getActualObject();
        if (actualObject instanceof IResource) {
            return (IResource) actualObject;
        }
    }
    if (receiver instanceof IResource) {
        return (IResource) receiver;
    }
    return null;
}
Also used : IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) IResource(org.eclipse.core.resources.IResource)

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