Search in sources :

Example 1 with PythonNodeFilter

use of org.python.pydev.navigator.filters.PythonNodeFilter in project Pydev by fabioz.

the class PythonBaseModelProvider method hasChildren.

/**
 * @return whether there are children for the given element. Note that there is
 * an optimization in this method, so that it works correctly for elements that
 * are not python files, and returns true if it is a python file with any content
 * (even if that content does not actually map to a node.
 *
 * @see org.eclipse.ui.model.BaseWorkbenchContentProvider#hasChildren(java.lang.Object)
 */
@Override
public boolean hasChildren(Object element) {
    if (element instanceof PythonFile) {
        // If we're not showing nodes, return false.
        INavigatorContentService contentService = viewer.getNavigatorContentService();
        INavigatorFilterService filterService = contentService.getFilterService();
        ViewerFilter[] visibleFilters = filterService.getVisibleFilters(true);
        for (ViewerFilter viewerFilter : visibleFilters) {
            if (viewerFilter instanceof PythonNodeFilter) {
                return false;
            }
        }
        PythonFile f = (PythonFile) element;
        if (PythonPathHelper.isValidSourceFile(f.getActualObject())) {
            try {
                InputStream contents = f.getContents();
                try {
                    if (contents.read() == -1) {
                        // if there is no content in the file, it has no children
                        return false;
                    } else {
                        // if it has any content, it has children (performance reasons)
                        return true;
                    }
                } finally {
                    contents.close();
                }
            } catch (Exception e) {
                Log.log("Handled error getting contents.", e);
                return false;
            }
        }
        return false;
    }
    if (element instanceof TreeNode<?>) {
        TreeNode<?> treeNode = (TreeNode<?>) element;
        return treeNode.hasChildren();
    }
    return getChildren(element).length > 0;
}
Also used : PythonNodeFilter(org.python.pydev.navigator.filters.PythonNodeFilter) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) InputStream(java.io.InputStream) TreeNode(org.python.pydev.shared_core.structure.TreeNode) INavigatorFilterService(org.eclipse.ui.navigator.INavigatorFilterService) PythonFile(org.python.pydev.navigator.elements.PythonFile) CoreException(org.eclipse.core.runtime.CoreException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) INavigatorContentService(org.eclipse.ui.navigator.INavigatorContentService)

Aggregations

InputStream (java.io.InputStream)1 CoreException (org.eclipse.core.runtime.CoreException)1 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)1 INavigatorContentService (org.eclipse.ui.navigator.INavigatorContentService)1 INavigatorFilterService (org.eclipse.ui.navigator.INavigatorFilterService)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)1 PythonFile (org.python.pydev.navigator.elements.PythonFile)1 PythonNodeFilter (org.python.pydev.navigator.filters.PythonNodeFilter)1 TreeNode (org.python.pydev.shared_core.structure.TreeNode)1