Search in sources :

Example 1 with TreeNode

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

the class SearchIndexLabelProvider method getStyledText.

@Override
public StyledString getStyledText(Object element) {
    if (element instanceof TreeNode) {
        element = ((TreeNode<?>) element).data;
    }
    if (element instanceof ICustomLineElement) {
        return getLineElementLabel((ICustomLineElement) element);
    }
    if (!(element instanceof IResource)) {
        IResource resource = null;
        if (element instanceof IAdaptable) {
            IAdaptable iAdaptable = (IAdaptable) element;
            resource = iAdaptable.getAdapter(IResource.class);
            if (resource != null) {
                if (element instanceof ICustomModule) {
                    return getColoredLabelWithCounts(resource, new StyledString(element.toString()));
                }
                element = resource;
            }
        }
        if (!(element instanceof IResource)) {
            return new StyledString(element.toString());
        }
    }
    IResource resource = (IResource) element;
    if (!resource.exists()) {
        new StyledString("<removed resource>");
    }
    String name = BasicElementLabels.getResourceName(resource);
    return getColoredLabelWithCounts(resource, new StyledString(name));
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) TreeNode(org.python.pydev.shared_core.structure.TreeNode) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) IResource(org.eclipse.core.resources.IResource)

Example 2 with TreeNode

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

the class Select1Dialog method selectElement.

public TreeNode<Object> selectElement(TreeNode<Object> emptyRoot) {
    Shell shell = EditorUtils.getShell();
    final DialogMemento memento = new DialogMemento(shell, "org.python.pydev.ui.dialogs.Select1Dialog.shell");
    TreeSelectionDialog dialog = new TreeSelectionDialog(shell, getLabelProvider(), getContentProvider()) {

        @Override
        public boolean close() {
            memento.writeSettings(getShell());
            return super.close();
        }

        @Override
        public Control createDialogArea(Composite parent) {
            memento.readSettings();
            Control ret = super.createDialogArea(parent);
            ret.addTraverseListener(new TraverseListener() {

                @Override
                public void keyTraversed(TraverseEvent e) {
                    if (e.detail == SWT.TRAVERSE_RETURN) {
                        okPressed();
                    }
                }
            });
            return ret;
        }

        /* (non-Javadoc)
             * @see org.python.pydev.ui.dialogs.TreeSelectionDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
             */
        @Override
        protected Control createButtonBar(Composite parent) {
            Composite buttonBar = new Composite(parent, 0);
            GridLayout layout = new GridLayout();
            layout.numColumns = 2;
            buttonBar.setLayout(layout);
            afterCreateButtonBar(parent, buttonBar);
            GridData data = new GridData();
            data.horizontalAlignment = SWT.FILL;
            data.grabExcessHorizontalSpace = true;
            buttonBar.setLayoutData(data);
            return buttonBar;
        }

        @Override
        protected Point getInitialSize() {
            return memento.getInitialSize(super.getInitialSize(), getShell());
        }

        @Override
        protected Point getInitialLocation(Point initialSize) {
            return memento.getInitialLocation(initialSize, super.getInitialLocation(initialSize), getShell());
        }

        /*
             * @see SelectionStatusDialog#computeResult()
             */
        @Override
        @SuppressWarnings("unchecked")
        protected void computeResult() {
            doFinalUpdateBeforeComputeResult();
            IStructuredSelection selection = (IStructuredSelection) getTreeViewer().getSelection();
            List<Object> list = selection.toList();
            if (list.size() == 1) {
                setResult(list);
            } else {
                Tree tree = getTreeViewer().getTree();
                TreeItem[] items = tree.getItems();
                list = new ArrayList<Object>();
                // Now, if he didn't select anything, let's create tests with all that is currently filtered
                // in the interface
                createListWithLeafs(items, list);
                if (list.size() == 1) {
                    setResult(list);
                }
            }
        }

        private void createListWithLeafs(TreeItem[] items, List<Object> leafObjectsList) {
            for (TreeItem item : items) {
                TreeItem[] children = item.getItems();
                if (children.length == 0) {
                    leafObjectsList.add(item.getData());
                } else {
                    createListWithLeafs(children, leafObjectsList);
                }
            }
        }
    };
    dialog.setTitle("PyDev: Select entry");
    dialog.setMessage("Select entry");
    dialog.setInitialFilter(getInitialFilter());
    dialog.setAllowMultiple(false);
    dialog.setInput(emptyRoot);
    int open = dialog.open();
    if (open != Window.OK) {
        return null;
    }
    Object[] result = dialog.getResult();
    if (result != null && result.length == 1) {
        return (TreeNode<Object>) result[0];
    }
    return null;
}
Also used : TraverseEvent(org.eclipse.swt.events.TraverseEvent) Composite(org.eclipse.swt.widgets.Composite) TraverseListener(org.eclipse.swt.events.TraverseListener) TreeItem(org.eclipse.swt.widgets.TreeItem) Point(org.eclipse.swt.graphics.Point) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DialogMemento(org.python.pydev.shared_ui.dialogs.DialogMemento) Point(org.eclipse.swt.graphics.Point) Shell(org.eclipse.swt.widgets.Shell) Control(org.eclipse.swt.widgets.Control) GridLayout(org.eclipse.swt.layout.GridLayout) TreeNode(org.python.pydev.shared_core.structure.TreeNode) GridData(org.eclipse.swt.layout.GridData) Tree(org.eclipse.swt.widgets.Tree) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with TreeNode

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

the class SelectNDialog method selectElements.

/**
 * @param labelProvider: usually a TreeNodeLabelProvider
 * @param title: message in shell
 * @param message: message above tree
 * @param checkElapsedBeforeClose: if true we'll only accept a click after some secs (to prevent an accidental click
 * in the case of an unrequested dialog).
 */
public static List<TreeNode> selectElements(TreeNode root, ILabelProvider labelProvider, String title, String message, final boolean checkElapsedBeforeClose, List initialSelection) {
    Shell shell = EditorUtils.getShell();
    CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(shell, labelProvider, new TreeNodeContentProvider()) {

        private final DialogButtonEnablementHelper helper = new DialogButtonEnablementHelper(!checkElapsedBeforeClose);

        @Override
        public boolean close() {
            if (!helper.areButtonsEnabled()) {
                return false;
            }
            return super.close();
        }

        @Override
        protected void cancelPressed() {
            if (!helper.areButtonsEnabled()) {
                return;
            }
            super.cancelPressed();
        }

        @Override
        protected void okPressed() {
            if (!helper.areButtonsEnabled()) {
                return;
            }
            super.okPressed();
        }

        @Override
        protected void constrainShellSize() {
            helper.onConstrainShellSize();
            super.constrainShellSize();
        }

        @Override
        protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
            Button button = super.createButton(parent, id, label, defaultButton);
            helper.onCreateButton(button, id);
            return button;
        }

        @Override
        protected void createButtonsForButtonBar(Composite parent) {
            // create OK and Cancel buttons by default
            createButton(parent, IDialogConstants.OK_ID, "Apply selected changes (Ignore unselected)", true);
            createButton(parent, IDialogConstants.CANCEL_ID, "Don't ask again (Ignore all)", false);
        }
    };
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setInput(root);
    dialog.setInitialElementSelections(initialSelection);
    dialog.setExpandedElements(initialSelection.toArray());
    if (dialog.open() == CheckedTreeSelectionDialog.OK) {
        Object[] result = dialog.getResult();
        if (result != null) {
            ArrayList<TreeNode> ret = new ArrayList<TreeNode>(result.length);
            for (Object o : result) {
                ret.add((TreeNode) o);
            }
            return ret;
        }
    }
    return null;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Composite(org.eclipse.swt.widgets.Composite) CheckedTreeSelectionDialog(org.eclipse.ui.dialogs.CheckedTreeSelectionDialog) Button(org.eclipse.swt.widgets.Button) TreeNode(org.python.pydev.shared_core.structure.TreeNode) ArrayList(java.util.ArrayList) TreeNodeContentProvider(org.python.pydev.shared_ui.tree.TreeNodeContentProvider)

Example 4 with TreeNode

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

the class PythonBaseModelProvider method createErrorNoWorkingSetsDefined.

public TreeNode<LabelAndImage> createErrorNoWorkingSetsDefined(Object parentElement) {
    IImageHandle img = SharedUiPlugin.getImageCache().get(UIConstants.WARNING);
    TreeNode<LabelAndImage> root = new TreeNode<LabelAndImage>(parentElement, new LabelAndImage("Warning: Top level elements set to working sets but no working sets are defined.", img));
    new TreeNode<>(root, new LabelAndImage("Access the menu (Ctrl+F10) to change to show projects or create a working set.", null));
    return root;
}
Also used : IImageHandle(org.python.pydev.shared_core.image.IImageHandle) TreeNode(org.python.pydev.shared_core.structure.TreeNode)

Example 5 with TreeNode

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

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