Search in sources :

Example 1 with PythonFolder

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

the class PythonLabelProvider method getImage.

/**
 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
 */
@Override
public Image getImage(Object element) {
    if (element instanceof PythonProjectSourceFolder) {
        return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.PROJECT_SOURCE_FOLDER_ICON));
    }
    if (element instanceof PythonSourceFolder) {
        return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.SOURCE_FOLDER_ICON));
    }
    if (element instanceof PythonFolder) {
        PythonFolder folder = (PythonFolder) element;
        IFolder actualObject = folder.getActualObject();
        if (actualObject != null) {
            if (checkIfValidPackageFolder(folder)) {
                return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.FOLDER_PACKAGE_ICON));
            }
        }
        return provider.getImage(actualObject);
    }
    if (element instanceof PythonNode) {
        PythonNode node = (PythonNode) element;
        return node.entry.getImage();
    }
    if (element instanceof IWrappedResource) {
        IWrappedResource resource = (IWrappedResource) element;
        Object actualObject = resource.getActualObject();
        if (actualObject instanceof IFile) {
            IFile iFile = (IFile) actualObject;
            final String name = iFile.getName();
            if (name.indexOf('.') == -1) {
                try {
                    if (CorePlugin.markAsPyDevFileIfDetected(iFile)) {
                        if (FileTypesPreferences.isCythonFile(name)) {
                            return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.CYTHON_FILE_ICON));
                        }
                        return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.PY_FILE_ICON));
                    }
                } catch (Exception e) {
                // Ignore
                }
            }
            if (FileTypesPreferences.isCythonFile(name)) {
                return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.CYTHON_FILE_ICON));
            }
            if (name.startsWith("__init__.") && PythonPathHelper.isValidSourceFile(name)) {
                return ImageCache.asImage(PyTitlePreferencesPage.getInitIcon());
            } else {
                IProject project = iFile.getProject();
                try {
                    if (project.hasNature(PythonNature.DJANGO_NATURE_ID)) {
                        String djangoModulesHandling = PyTitlePreferencesPage.getDjangoModulesHandling();
                        if (djangoModulesHandling == PyTitlePreferencesPage.TITLE_EDITOR_DJANGO_MODULES_SHOW_PARENT_AND_DECORATE || djangoModulesHandling == PyTitlePreferencesPage.TITLE_EDITOR_DJANGO_MODULES_DECORATE) {
                            if (PyTitlePreferencesPage.isDjangoModuleToDecorate(name)) {
                                return ImageCache.asImage(PyTitlePreferencesPage.getDjangoModuleIcon(name));
                            }
                        }
                    }
                } catch (CoreException e) {
                    Log.log(e);
                }
            }
        }
        return provider.getImage(actualObject);
    }
    if (element instanceof ProjectConfigError) {
        return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.ERROR));
    }
    if (element instanceof TreeNode<?>) {
        TreeNode<?> treeNode = (TreeNode<?>) element;
        LabelAndImage data = (LabelAndImage) treeNode.getData();
        return ImageCache.asImage(data.image);
    }
    if (element instanceof IFile) {
        IFile iFile = (IFile) element;
        String name = iFile.getName();
        if (FileTypesPreferences.isCythonFile(name)) {
            return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.CYTHON_FILE_ICON));
        }
    }
    if (element instanceof IProject) {
        IProject project = (IProject) element;
        if (!project.isOpen()) {
            return null;
        }
        IMarker[] markers;
        try {
            markers = project.findMarkers(PythonBaseModelProvider.PYDEV_PACKAGE_EXPORER_PROBLEM_MARKER, true, 0);
        } catch (CoreException e1) {
            Log.log(e1);
            return null;
        }
        if (markers == null || markers.length == 0) {
            return null;
        }
        // We have errors: make them explicit.
        if (projectWithError == null) {
            synchronized (lock) {
                // the other enters the lock, it does not need to recalculated).
                if (projectWithError == null) {
                    // Note on double-checked locking idiom: http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html.
                    // (would not work as expected on java 1.4)
                    Image image = provider.getImage(element);
                    try {
                        DecorationOverlayIcon decorationOverlayIcon = new DecorationOverlayIcon(image, ImageCache.asImageDescriptor(SharedUiPlugin.getImageCache().getDescriptor(UIConstants.ERROR_SMALL)), IDecoration.BOTTOM_LEFT);
                        projectWithError = decorationOverlayIcon.createImage();
                    } catch (Exception e) {
                        Log.log("Unable to create error decoration for project icon.", e);
                        projectWithError = image;
                    }
                }
            }
        }
        return projectWithError;
    }
    if (element instanceof IWorkingSet) {
        return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.WORKING_SET));
    }
    return null;
}
Also used : DecorationOverlayIcon(org.eclipse.jface.viewers.DecorationOverlayIcon) IFile(org.eclipse.core.resources.IFile) PythonNode(org.python.pydev.navigator.elements.PythonNode) PythonFolder(org.python.pydev.navigator.elements.PythonFolder) ProjectConfigError(org.python.pydev.navigator.elements.ProjectConfigError) PythonProjectSourceFolder(org.python.pydev.navigator.elements.PythonProjectSourceFolder) Image(org.eclipse.swt.graphics.Image) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) CoreException(org.eclipse.core.runtime.CoreException) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) TreeNode(org.python.pydev.shared_core.structure.TreeNode) IMarker(org.eclipse.core.resources.IMarker) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource) IWorkingSet(org.eclipse.ui.IWorkingSet) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with PythonFolder

use of org.python.pydev.navigator.elements.PythonFolder 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 3 with PythonFolder

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

the class PythonModelProviderTest method testFolderToSourceFolder2.

public void testFolderToSourceFolder2() throws Exception {
    final HashSet<String> pythonPathSet = new HashSet<String>();
    pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source");
    String source2Folder = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source2";
    File f = new File(source2Folder);
    File f1 = new File(f, "childFolder");
    File f2 = new File(f1, "rechildFolder");
    if (f2.exists()) {
        f2.delete();
    }
    if (f1.exists()) {
        f1.delete();
    }
    if (f.exists()) {
        f.delete();
    }
    // still not created!
    pythonPathSet.add(source2Folder);
    PythonNature nature = createNature(pythonPathSet);
    project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
    provider = new PythonModelProvider();
    Object[] children1 = provider.getChildren(project);
    assertEquals(1, children1.length);
    assertTrue("Expected source folder. Received: " + children1[0], children1[0] instanceof PythonSourceFolder);
    f.mkdir();
    f1.mkdir();
    f2.mkdir();
    try {
        FolderStub source2FolderFile = new FolderStub(project, f);
        FolderStub source2FolderChild = new FolderStub(project, source2FolderFile, f1);
        FolderStub source2FolderReChild = new FolderStub(project, source2FolderChild, f2);
        Set set = new HashSet();
        set.add(source2FolderReChild);
        provider.interceptAdd(new PipelinedShapeModification(source2FolderChild, set));
        assertEquals(1, set.size());
        PythonFolder c = (PythonFolder) set.iterator().next();
        PythonSourceFolder sourceFolder = c.getSourceFolder();
        assertTrue(sourceFolder instanceof PythonSourceFolder);
        set.clear();
        set.add(source2FolderChild);
        provider.interceptRemove(new PipelinedShapeModification(source2FolderFile, set));
        assertTrue(set.iterator().next() instanceof PythonFolder);
        // System.out.println(set);
        set.clear();
        set.add(source2FolderReChild);
        provider.interceptAdd(new PipelinedShapeModification(source2FolderChild, set));
        assertTrue(set.iterator().next() instanceof PythonFolder);
        // System.out.println(set);
        set.clear();
        set.add(source2FolderChild);
        provider.interceptRemove(new PipelinedShapeModification(source2FolderFile, set));
        assertTrue(set.iterator().next() instanceof PythonFolder);
        // System.out.println(set);
        set.clear();
        set.add(source2FolderReChild);
        provider.interceptAdd(new PipelinedShapeModification(source2FolderChild, set));
        assertTrue(set.iterator().next() instanceof PythonFolder);
    // System.out.println(set);
    } finally {
        f2.delete();
        f1.delete();
        f.delete();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IWorkingSet(org.eclipse.ui.IWorkingSet) PythonNature(org.python.pydev.plugin.nature.PythonNature) PythonFolder(org.python.pydev.navigator.elements.PythonFolder) FolderStub(org.python.pydev.shared_core.resource_stubs.FolderStub) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) PipelinedShapeModification(org.eclipse.ui.navigator.PipelinedShapeModification) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with PythonFolder

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

the class PythonModelProviderTest method testFolderToSourceFolder.

public void testFolderToSourceFolder() throws Exception {
    final HashSet<String> pythonPathSet = new HashSet<String>();
    pythonPathSet.add(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source");
    String source2Folder = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source2";
    File f = new File(source2Folder);
    File f1 = new File(f, "childFolder");
    if (f1.exists()) {
        f1.delete();
    }
    if (f.exists()) {
        f.delete();
    }
    // still not created!
    pythonPathSet.add(source2Folder);
    PythonNature nature = createNature(pythonPathSet);
    project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
    provider = new PythonModelProvider();
    Object[] children1 = provider.getChildren(project);
    assertEquals(1, children1.length);
    assertTrue("Found: " + children1[0], children1[0] instanceof PythonSourceFolder);
    f.mkdir();
    f1.mkdir();
    try {
        FolderStub source2FolderFile = new FolderStub(project, f);
        FolderStub source2FolderChild = new FolderStub(project, source2FolderFile, f1);
        Set set = new HashSet();
        set.add(source2FolderChild);
        provider.interceptAdd(new PipelinedShapeModification(source2FolderFile, set));
        assertEquals(1, set.size());
        PythonFolder c = (PythonFolder) set.iterator().next();
        PythonSourceFolder sourceFolder = c.getSourceFolder();
        assertTrue(sourceFolder instanceof PythonSourceFolder);
        set.clear();
        set.add(source2FolderChild);
        provider.interceptAdd(new PipelinedShapeModification(source2FolderFile, set));
    } finally {
        f1.delete();
        f.delete();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IWorkingSet(org.eclipse.ui.IWorkingSet) PythonNature(org.python.pydev.plugin.nature.PythonNature) PythonFolder(org.python.pydev.navigator.elements.PythonFolder) FolderStub(org.python.pydev.shared_core.resource_stubs.FolderStub) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) PipelinedShapeModification(org.eclipse.ui.navigator.PipelinedShapeModification) File(java.io.File) HashSet(java.util.HashSet)

Example 5 with PythonFolder

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

Aggregations

PythonFolder (org.python.pydev.navigator.elements.PythonFolder)5 PythonSourceFolder (org.python.pydev.navigator.elements.PythonSourceFolder)5 IWorkingSet (org.eclipse.ui.IWorkingSet)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 IFile (org.eclipse.core.resources.IFile)3 IFolder (org.eclipse.core.resources.IFolder)3 IProject (org.eclipse.core.resources.IProject)3 IWrappedResource (org.python.pydev.navigator.elements.IWrappedResource)3 PythonNature (org.python.pydev.plugin.nature.PythonNature)3 File (java.io.File)2 IContainer (org.eclipse.core.resources.IContainer)2 IResource (org.eclipse.core.resources.IResource)2 PipelinedShapeModification (org.eclipse.ui.navigator.PipelinedShapeModification)2 PythonFile (org.python.pydev.navigator.elements.PythonFile)2 FolderStub (org.python.pydev.shared_core.resource_stubs.FolderStub)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 IMarker (org.eclipse.core.resources.IMarker)1 CoreException (org.eclipse.core.runtime.CoreException)1