Search in sources :

Example 6 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode 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 7 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode 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 8 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode 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 TreeNode

use of org.python.pydev.shared_core.structure.TreeNode in project Pydev by fabioz.

the class SyncSystemModulesManagerTest method testUpdateAndApply.

public void testUpdateAndApply() throws Exception {
    setupEnv();
    SyncSystemModulesManager synchManager = new SyncSystemModulesManager();
    final DataAndImageTreeNode root = new DataAndImageTreeNode(null, null, null);
    Map<IInterpreterManager, Map<String, IInterpreterInfo>> managerToNameToInfoMap = InterpreterManagersAPI.getInterpreterManagerToInterpreterNameToInfo();
    ManagerInfoToUpdate managerToNameToInfo = new ManagerInfoToUpdate(managerToNameToInfoMap);
    checkUpdateStructures(synchManager, root, managerToNameToInfo);
    checkSynchronize(synchManager, root, managerToNameToInfo);
    // Ok, the interpreter should be synchronized with the pythonpath which is currently set.
    // Now, check a different scenario: create a new path and add it to the interpreter pythonpath.
    // In this situation, the sync manager should ask the user if that path should actually be added
    // to this interpreter.
    root.clear();
    managerToNameToInfo = new ManagerInfoToUpdate(InterpreterManagersAPI.getInterpreterManagerToInterpreterNameToInfo());
    synchManager.updateStructures(null, root, managerToNameToInfo, new SyncSystemModulesManager.CreateInterpreterInfoCallback() {

        @Override
        public IInterpreterInfo createInterpreterInfo(IInterpreterManager manager, String executable, IProgressMonitor monitor) {
            Collection<String> pythonpath = new ArrayList<String>();
            pythonpath.add(libDir.toString());
            pythonpath.add(libDir2.toString());
            final InterpreterInfo info = new InterpreterInfo("2.6", TestDependent.PYTHON2_EXE, pythonpath);
            return info;
        }
    });
    assertTrue(root.hasChildren());
    List<TreeNode> selectElements = new ArrayList<>();
    selectElements.addAll(root.flattenChildren());
    synchManager.applySelectedChangesToInterpreterInfosPythonpath(root, selectElements, null);
    List<IInterpreterInfo> allInterpreterInfos = InterpreterManagersAPI.getAllInterpreterInfos();
    for (IInterpreterInfo interpreterInfo : allInterpreterInfos) {
        assertEquals(5, interpreterInfo.getModulesManager().getSize(false));
    }
}
Also used : ArrayList(java.util.ArrayList) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) IInterpreterManager(org.python.pydev.core.IInterpreterManager) SyncSystemModulesManager(org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManager) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) TreeNode(org.python.pydev.shared_core.structure.TreeNode) Collection(java.util.Collection) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) AdditionalSystemInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) HashMap(java.util.HashMap) Map(java.util.Map) ManagerInfoToUpdate(org.python.pydev.ast.codecompletion.revisited.ManagerInfoToUpdate)

Example 10 with TreeNode

use of org.python.pydev.shared_core.structure.TreeNode in project Pydev by fabioz.

the class SyncSystemModulesManagerTest method testSaveUserChoicesAfterSelection.

public void testSaveUserChoicesAfterSelection() throws Exception {
    setupEnv(false);
    IEclipsePreferences preferences = createPreferenceStore();
    SyncSystemModulesManager synchManager = new SyncSystemModulesManager();
    final DataAndImageTreeNode root = new DataAndImageTreeNode(null, null, null);
    Map<IInterpreterManager, Map<String, IInterpreterInfo>> managerToNameToInfo = InterpreterManagersAPI.getInterpreterManagerToInterpreterNameToInfo();
    synchManager.updateStructures(null, root, new ManagerInfoToUpdate(managerToNameToInfo), new SyncSystemModulesManager.CreateInterpreterInfoCallback() {

        @Override
        public IInterpreterInfo createInterpreterInfo(IInterpreterManager manager, String executable, IProgressMonitor monitor) {
            Collection<String> pythonpath = new ArrayList<>();
            pythonpath.add(libDir.toString());
            pythonpath.add(libDir2.toString());
            pythonpath.add(libDir3.toString());
            pythonpath.add(libZipFile.toString());
            final InterpreterInfo info = new InterpreterInfo("2.6", TestDependent.PYTHON2_EXE, pythonpath);
            return info;
        }
    });
    assertTrue(root.hasChildren());
    List<TreeNode> selectedElements = new ArrayList<>();
    TreeNode interpreterNode = (TreeNode) root.getChildren().get(0);
    selectedElements.add(interpreterNode);
    List<TreeNode> children = interpreterNode.getChildren();
    for (TreeNode<PythonpathChange> treeNode : children) {
        if (treeNode.getData().path.equals(libDir2.toString())) {
            selectedElements.add(treeNode);
        }
    }
    synchManager.saveUnselected(root, selectedElements, preferences);
    // Check that we ignored libDir3 and libZipFile
    String key = SyncSystemModulesManager.createKeyForInfo((IInterpreterInfo) ((TreeNode) root.getChildren().get(0)).getData());
    String entry = preferences.get(key, "");
    List<String> entries = StringUtils.split(entry, "|||");
    assertEquals(2, entries.size());
    HashSet<String> entriesSet = new HashSet<>(entries);
    assertEquals(new HashSet(entries), new HashSet(Arrays.asList(libDir3.toString(), libZipFile.toString())));
    // Check that only libDir2 is initially selected.
    List<TreeNode> initialSelection = synchManager.createInitialSelectionForDialogConsideringPreviouslyIgnored(root, preferences);
    assertEquals(2, initialSelection.size());
    TreeNode treeNode = initialSelection.get(0);
    TreeNode treeNode1 = initialSelection.get(1);
    TreeNode interpreterInfoNode;
    TreeNode pythonpathNode;
    if (treeNode.getData() instanceof IInterpreterInfo) {
        interpreterNode = treeNode;
        pythonpathNode = treeNode1;
    } else {
        interpreterNode = treeNode1;
        pythonpathNode = treeNode;
    }
    assertEquals(((PythonpathChange) pythonpathNode.getData()).path, libDir2.toString());
}
Also used : IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) ArrayList(java.util.ArrayList) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) IInterpreterManager(org.python.pydev.core.IInterpreterManager) SyncSystemModulesManager(org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManager) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) DataAndImageTreeNode(org.python.pydev.shared_core.structure.DataAndImageTreeNode) TreeNode(org.python.pydev.shared_core.structure.TreeNode) Collection(java.util.Collection) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) AdditionalSystemInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) HashMap(java.util.HashMap) Map(java.util.Map) PythonpathChange(org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManager.PythonpathChange) ManagerInfoToUpdate(org.python.pydev.ast.codecompletion.revisited.ManagerInfoToUpdate) HashSet(java.util.HashSet)

Aggregations

TreeNode (org.python.pydev.shared_core.structure.TreeNode)23 ArrayList (java.util.ArrayList)9 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)8 DataAndImageTreeNode (org.python.pydev.shared_core.structure.DataAndImageTreeNode)7 HashSet (java.util.HashSet)6 IResource (org.eclipse.core.resources.IResource)5 IAdaptable (org.eclipse.core.runtime.IAdaptable)5 InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)5 HashMap (java.util.HashMap)4 List (java.util.List)4 IProject (org.eclipse.core.resources.IProject)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 SyncSystemModulesManager (org.python.pydev.ast.codecompletion.revisited.SyncSystemModulesManager)4 IInterpreterManager (org.python.pydev.core.IInterpreterManager)4 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)3 Collection (java.util.Collection)3 Map (java.util.Map)3 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)3 IWrappedResource (org.python.pydev.navigator.elements.IWrappedResource)3