Search in sources :

Example 1 with PythonProjectSourceFolder

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

the class PythonBaseModelProvider method getChildrenForIResourceOrWorkingSet.

/**
 * @param parentElement an IResource from where we want to get the children (or a working set)
 *
 * @return as we're not below a source folder here, we have still not entered the 'python' domain,
 * and as the starting point for the 'python' domain is always a source folder, the things
 * that can be returned are IResources and PythonSourceFolders.
 */
private Object[] getChildrenForIResourceOrWorkingSet(Object parentElement) {
    PythonNature nature = null;
    IProject project = null;
    if (parentElement instanceof IResource) {
        project = ((IResource) parentElement).getProject();
    }
    final Map<PythonNature, Set<String>> natureToSourcePathSet = new HashMap<>();
    // we can only get the nature if the project is open
    if (project != null && project.isOpen()) {
        nature = PythonNature.getPythonNature(project);
    }
    // replace folders -> source folders (we should only get here on a path that's not below a source folder)
    Object[] childrenToReturn = super.getChildren(parentElement);
    // if we don't have a python nature in this project, there is no way we can have a PythonSourceFolder
    List<Object> ret = new ArrayList<Object>(childrenToReturn.length);
    for (int i = 0; i < childrenToReturn.length; i++) {
        PythonNature localNature = nature;
        IProject localProject = project;
        // now, first we have to try to get it (because it might already be created)
        Object child = childrenToReturn[i];
        if (child == null) {
            continue;
        }
        // only add it if it wasn't null
        ret.add(child);
        if (!(child instanceof IResource)) {
            // not an element that we can treat in pydev (but still, it was already added)
            continue;
        }
        child = getResourceInPythonModel((IResource) child);
        if (child == null) {
            // ok, it was not in the python model (but it was already added with the original representation, so, that's ok)
            continue;
        } else {
            // replace the element added for the one in the python model
            ret.set(ret.size() - 1, child);
        }
        // if it is a folder (that is not already a PythonSourceFolder, it might be that we have to create a PythonSourceFolder)
        if (child instanceof IContainer && !(child instanceof PythonSourceFolder)) {
            IContainer container = (IContainer) child;
            // check if it is a source folder (and if it is, create it)
            if (localNature == null) {
                if (container instanceof IProject) {
                    localProject = (IProject) container;
                    if (localProject.isOpen() == false) {
                        continue;
                    } else {
                        localNature = PythonNature.getPythonNature(localProject);
                    }
                } else {
                    continue;
                }
            }
            // if it's a python project, the nature can't be null
            if (localNature == null) {
                continue;
            }
            // If possible, don't recalculate the source paths for each new folder (it can be costly).
            Set<String> sourcePathSet = this.getSourcePathSet(natureToSourcePathSet, localNature);
            IPath fullPath = container.getFullPath();
            if (sourcePathSet.contains(fullPath.toString())) {
                PythonSourceFolder createdSourceFolder;
                if (container instanceof IFolder) {
                    createdSourceFolder = new PythonSourceFolder(parentElement, (IFolder) container);
                } else if (container instanceof IProject) {
                    createdSourceFolder = new PythonProjectSourceFolder(parentElement, (IProject) container);
                } else {
                    throw new RuntimeException("Should not get here.");
                }
                // replace the element added for the one in the python model
                ret.set(ret.size() - 1, createdSourceFolder);
                Set<PythonSourceFolder> sourceFolders = getProjectSourceFolders(localProject);
                sourceFolders.add(createdSourceFolder);
            }
        }
    }
    return ret.toArray();
}
Also used : Set(java.util.Set) IWorkingSet(org.eclipse.ui.IWorkingSet) HashSet(java.util.HashSet) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PythonProjectSourceFolder(org.python.pydev.navigator.elements.PythonProjectSourceFolder) 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) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with PythonProjectSourceFolder

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

the class PythonBaseModelProvider method internalDoNotifyPythonPathRebuilt.

/**
 * This is the actual implementation of the rebuild.
 *
 * @return the element that should be refreshed or null if the project location can't be determined!
 */
/*default*/
IResource internalDoNotifyPythonPathRebuilt(IProject project, List<String> projectPythonpath) {
    IResource refreshObject = project;
    IPath location = project.getLocation();
    if (location == null) {
        return null;
    }
    if (DEBUG) {
        System.out.println("\n\nRebuilding pythonpath: " + project + " - " + projectPythonpath);
    }
    HashSet<Path> projectPythonpathSet = new HashSet<Path>();
    for (String string : projectPythonpath) {
        Path newPath = new Path(string);
        if (location.equals(newPath)) {
            refreshObject = project.getParent();
        }
        projectPythonpathSet.add(newPath);
    }
    ProjectInfoForPackageExplorer projectInfo = ProjectInfoForPackageExplorer.getProjectInfo(project);
    if (projectInfo != null) {
        projectInfo.recreateInfo(project);
        Set<PythonSourceFolder> existingSourceFolders = projectInfo.sourceFolders;
        if (existingSourceFolders != null) {
            // iterate in a copy
            for (PythonSourceFolder pythonSourceFolder : new HashSet<PythonSourceFolder>(existingSourceFolders)) {
                IPath fullPath = pythonSourceFolder.container.getLocation();
                if (!projectPythonpathSet.contains(fullPath)) {
                    if (pythonSourceFolder instanceof PythonProjectSourceFolder) {
                        refreshObject = project.getParent();
                    }
                    // it's not a valid source folder anymore...
                    existingSourceFolders.remove(pythonSourceFolder);
                    if (DEBUG) {
                        System.out.println("Removing:" + pythonSourceFolder + " - " + fullPath);
                    }
                }
            }
        }
    }
    Runnable refreshRunnable = getRefreshRunnable(refreshObject);
    final Collection<Runnable> runnables = new ArrayList<Runnable>();
    runnables.add(refreshRunnable);
    processRunnables(runnables);
    return refreshObject;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) PythonProjectSourceFolder(org.python.pydev.navigator.elements.PythonProjectSourceFolder) PythonSourceFolder(org.python.pydev.navigator.elements.PythonSourceFolder) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 3 with PythonProjectSourceFolder

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

the class PyPropertyTester method test.

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    if (expectedValue == null) {
        return false;
    }
    if ("open".equals(property)) {
        if (receiver instanceof PythonProjectSourceFolder) {
            PythonProjectSourceFolder pythonProjectSourceFolder = (PythonProjectSourceFolder) receiver;
            IResource actualObject = pythonProjectSourceFolder.getActualObject();
            if (actualObject instanceof IProject) {
                return ((IProject) actualObject).isOpen() == toBoolean(expectedValue);
            }
        }
    } else if ("name".equals(property)) {
        if (receiver instanceof IWrappedResource) {
            IWrappedResource wrappedResource = (IWrappedResource) receiver;
            IResource resource = (IResource) wrappedResource.getAdapter(IResource.class);
            if (resource != null) {
                return expectedValue.toString().equals(resource.getName());
            }
        }
    }
    return false;
}
Also used : PythonProjectSourceFolder(org.python.pydev.navigator.elements.PythonProjectSourceFolder) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) IWrappedResource(org.python.pydev.navigator.elements.IWrappedResource)

Example 4 with PythonProjectSourceFolder

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

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

Aggregations

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