Search in sources :

Example 1 with PythonSourceFolder

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

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

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

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

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

PythonSourceFolder (org.python.pydev.navigator.elements.PythonSourceFolder)17 PythonNature (org.python.pydev.plugin.nature.PythonNature)10 HashSet (java.util.HashSet)9 IProject (org.eclipse.core.resources.IProject)8 IFolder (org.eclipse.core.resources.IFolder)6 IWorkingSet (org.eclipse.ui.IWorkingSet)6 IWrappedResource (org.python.pydev.navigator.elements.IWrappedResource)6 File (java.io.File)5 Set (java.util.Set)5 IContainer (org.eclipse.core.resources.IContainer)5 IResource (org.eclipse.core.resources.IResource)5 PythonFolder (org.python.pydev.navigator.elements.PythonFolder)5 IFile (org.eclipse.core.resources.IFile)4 PythonProjectSourceFolder (org.python.pydev.navigator.elements.PythonProjectSourceFolder)4 IPath (org.eclipse.core.runtime.IPath)3 PipelinedShapeModification (org.eclipse.ui.navigator.PipelinedShapeModification)3 FolderStub (org.python.pydev.shared_core.resource_stubs.FolderStub)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IPythonNature (org.python.pydev.core.IPythonNature)2