Example usage for org.eclipse.jface.viewers TreeViewer getExpandedState

List of usage examples for org.eclipse.jface.viewers TreeViewer getExpandedState

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers TreeViewer getExpandedState.

Prototype

public boolean getExpandedState(Object elementOrTreePath) 

Source Link

Document

Returns whether the node corresponding to the given element or tree path is expanded or collapsed.

Usage

From source file:org.eclipse.tcf.te.ui.views.handler.OpenEditorHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    // The selection is the Target Explorer tree selection
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    // The active part is the Target Explorer view instance
    IWorkbenchPart part = HandlerUtil.getActivePart(event);
    // Get the currently active workbench window
    // In Eclipse 4.x, the HandlerUtil.getActiveWorkbenchWindow(event) may return null
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
    if (window == null)
        window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    // ALT - Key pressed?
    Object ctrlPressed = HandlerUtil.getVariable(event, "ctrlPressed"); //$NON-NLS-1$

    boolean expand = ctrlPressed instanceof Boolean ? ((Boolean) ctrlPressed).booleanValue() : false;

    if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
        // If the tree node is expandable, expand or collapse it
        TreeViewer viewer = ((CommonNavigator) part).getCommonViewer();
        Object element = ((IStructuredSelection) selection).getFirstElement();
        if (selection instanceof TreeSelection) {
            TreePath[] path = ((TreeSelection) selection).getPaths();
            if (path != null && path.length > 0) {
                element = path[0].getLastSegment();
            }/*from ww  w.  j  av a2s. c  om*/
        }
        if (viewer.isExpandable(element) && expand) {
            viewer.setExpandedState(element, !viewer.getExpandedState(element));
        } else {
            openEditorOnSelection(window, selection);
        }
    }

    return null;
}

From source file:org.eclipse.tcf.te.ui.views.internal.quickfilter.QuickFilterTester.java

License:Open Source License

/**
 * Test if the root for the tree viewer is eligible as a root path 
 * of a quick filter.// w  w w. ja v a 2s.  c  om
 * 
 * @param root The root path to be tested.
 * @param viewer The tree viewer to be filtered.
 * @return true if it is eligible as a root path or else false.
 */
private boolean isEligible(TreePath root, TreeViewer viewer) {
    if (viewer.getExpandedState(root)) {
        return hasChildren(root, viewer);
    }
    return false;
}

From source file:org.eclipse.tcf.te.ui.wizards.newWizard.NewWizardSelectionPage.java

License:Open Source License

@Override
public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout());
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));

    Label label = new Label(composite, SWT.NONE);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    label.setText(Messages.NewWizardSelectionPage_wizards);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    filteredTreeFilter = new WizardPatternFilter();
    filteredTree = new FilteredTree(composite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER,
            filteredTreeFilter, true);/*from   w w w . j  a  v a 2  s  .  c  o  m*/
    filteredTree.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    GridData layoutData = new GridData(GridData.FILL_BOTH);
    layoutData.heightHint = 325;
    layoutData.widthHint = 450;
    filteredTree.setLayoutData(layoutData);

    final TreeViewer treeViewer = filteredTree.getViewer();
    treeViewer.setContentProvider(new NewWizardContentProvider());
    treeViewer.setLabelProvider(WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider());
    treeViewer.setComparator(new NewWizardViewerComparator());

    treeViewer.addFilter(new NewWizardViewerFilter());

    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            onSelectionChanged();
        }
    });
    treeViewer.addDoubleClickListener(new IDoubleClickListener() {
        @Override
        public void doubleClick(DoubleClickEvent event) {
            // Double-click on a connection type is triggering the sub wizard
            if (event.getSelection() instanceof IStructuredSelection) {
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                // The tree is single selection, so look for the first element only.
                Object element = selection.getFirstElement();
                if (element instanceof IWizardDescriptor) {
                    // Double-click on a connection type is triggering the sub wizard
                    if (canFlipToNextPage()) {
                        getWizard().getContainer().showPage(getNextPage());
                    }
                } else if (event.getViewer() instanceof TreeViewer) {
                    TreeViewer viewer = (TreeViewer) event.getViewer();
                    if (viewer.isExpandable(element)) {
                        viewer.setExpandedState(element, !viewer.getExpandedState(element));
                    }
                }
            }
        }
    });

    treeViewer.setInput(category != null ? category : wizardRegistry);

    // apply the standard dialog font
    Dialog.applyDialogFont(composite);

    setControl(composite);

    // Restore the tree action
    restoreWidgetValues();

    // Initialize the context help id
    PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
            IUIConstants.HELP_NEW_WIZARD_SELECTION_PAGE);
}

From source file:org.eclipse.team.svn.ui.history.HistoryActionManager.java

License:Open Source License

protected void handleDoubleClick(TreeViewer viewer, ILogNode item, boolean doubleClick) {
    int type = item.getType();
    if (type == ILogNode.TYPE_CATEGORY) {
        if (viewer.getExpandedState(item)) {
            viewer.collapseToLevel(item, TreeViewer.ALL_LEVELS);
        } else {//from   ww  w . j  av  a  2s.co m
            viewer.expandToLevel(item, TreeViewer.ALL_LEVELS);
        }
        return;
    }
    boolean isCompareAllowed = CoreExtensionsManager.instance().getSVNConnectorFactory()
            .getSVNAPIVersion() >= ISVNConnectorFactory.APICompatibility.SVNAPI_1_5_x
            || this.view.getRepositoryResource() instanceof IRepositoryFile;
    if ((this.view.getOptions() & ISVNHistoryView.COMPARE_MODE) != 0 && doubleClick && isCompareAllowed) {
        if (type == ILogNode.TYPE_SVN && !(item.getParent() instanceof SVNLogNode)) {
            this.compareWithCurrent((SVNLogEntry) item.getEntity());
        } else if (type == ILogNode.TYPE_LOCAL) {
            this.runCompareForLocal(new Object[] { item });
        }
    } else if (type == ILogNode.TYPE_LOCAL) {
        try {
            SVNLocalFileRevision selected = (SVNLocalFileRevision) item.getEntity();
            Utils.openEditor(UIMonitorUtility.getActivePage(), selected, new NullProgressMonitor());
        } catch (CoreException ex) {
            UILoggedOperation.reportError("Open Editor", ex);
        }
    } else if (!(this.view.getRepositoryResource() instanceof IRepositoryContainer) && type == ILogNode.TYPE_SVN
            && !(item.getParent() instanceof SVNLogNode)) {
        UIMonitorUtility.doTaskScheduledActive(new OpenRemoteFileOperation(
                new IRepositoryFile[] {
                        (IRepositoryFile) this.getResourceForSelectedRevision((SVNLogEntry) item.getEntity()) },
                OpenRemoteFileOperation.OPEN_DEFAULT));
    }
}

From source file:org.eclipse.tm.te.ui.views.handler.OpenCommandHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    // The selection is the Target Explorer tree selection
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    // The active part is the Target Explorer view instance
    IWorkbenchPart part = HandlerUtil.getActivePart(event);

    if (selection instanceof IStructuredSelection && !selection.isEmpty() && part instanceof CommonNavigator) {
        // If the tree node is expandable, expand or collapse it
        TreeViewer viewer = ((CommonNavigator) part).getCommonViewer();
        Object element = ((IStructuredSelection) selection).getFirstElement();
        if (viewer.isExpandable(element)) {
            viewer.setExpandedState(element, !viewer.getExpandedState(element));
        } else {//from w  w  w.j av  a 2s  .co  m
            // Node is not an expandable node, forward to the properties action.
            ICommandService service = (ICommandService) PlatformUI.getWorkbench()
                    .getService(ICommandService.class);
            Command command = service != null ? service.getCommand(ActionFactory.PROPERTIES.getCommandId())
                    : null;
            if (command != null && command.isDefined() && command.isEnabled()) {
                try {
                    command.executeWithChecks(event);
                } catch (Exception e) {
                    // If the platform is in debug mode, we print the exception to the log view
                    if (Platform.inDebugMode()) {
                        IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
                                e.getLocalizedMessage(), e);
                        UIPlugin.getDefault().getLog().log(status);
                    }
                }
            }
        }
    }

    return null;
}

From source file:org.eclipse.tm.te.ui.wizards.newWizard.NewWizardSelectionPage.java

License:Open Source License

@Override
public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout());
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));

    Label label = new Label(composite, SWT.NONE);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    label.setText(Messages.NewWizardSelectionPage_wizards);
    label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    filteredTreeFilter = new WizardPatternFilter();
    filteredTree = new FilteredTree(composite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER,
            filteredTreeFilter, true);//  ww  w .j  a  v a 2s. c  o  m
    filteredTree.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    GridData layoutData = new GridData(GridData.FILL_BOTH);
    layoutData.heightHint = 325;
    layoutData.widthHint = 450;
    filteredTree.setLayoutData(layoutData);

    final TreeViewer treeViewer = filteredTree.getViewer();
    treeViewer.setContentProvider(new WizardContentProvider());
    treeViewer.setLabelProvider(new WorkbenchLabelProvider());
    treeViewer.setComparator(new NewWizardViewerComparator());

    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            onSelectionChanged();
        }
    });
    treeViewer.addDoubleClickListener(new IDoubleClickListener() {
        @Override
        public void doubleClick(DoubleClickEvent event) {
            // Double-click on a connection type is triggering the sub wizard
            if (event.getSelection() instanceof IStructuredSelection) {
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                // The tree is single selection, so look for the first element only.
                Object element = selection.getFirstElement();
                if (element instanceof IWizardDescriptor) {
                    // Double-click on a connection type is triggering the sub wizard
                    if (canFlipToNextPage())
                        getWizard().getContainer().showPage(getNextPage());
                } else if (event.getViewer() instanceof TreeViewer) {
                    TreeViewer viewer = (TreeViewer) event.getViewer();
                    if (viewer.isExpandable(element)) {
                        viewer.setExpandedState(element, !viewer.getExpandedState(element));
                    }
                }
            }
        }
    });

    treeViewer.setInput(wizardRegistry.getRootCategory());

    // apply the standard dialog font
    Dialog.applyDialogFont(composite);

    setControl(composite);

    // Restore the tree state
    restoreWidgetValues();

    // Initialize the context help id
    PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
            IUIConstants.HELP_NEW_WIZARD_SELECTION_PAGE);
}

From source file:org.eclipse.ui.internal.dialogs.NewWizardNewPage.java

License:Open Source License

/**
 * Create a new FilteredTree in the parent.
 * /*w  w w .j  a va  2  s  .c  om*/
 * @param parent the parent <code>Composite</code>.
 * @since 3.0
 */
protected FilteredTree createFilteredTree(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    composite.setLayout(layout);

    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    data.widthHint = SIZING_VIEWER_WIDTH;
    data.horizontalSpan = 2;
    data.grabExcessHorizontalSpace = true;
    data.grabExcessVerticalSpace = true;

    boolean needsHint = DialogUtil.inRegularFontMode(parent);

    //Only give a height hint if the dialog is going to be too small
    if (needsHint) {
        data.heightHint = SIZING_LISTS_HEIGHT;
    }
    composite.setLayoutData(data);

    filteredTreeFilter = new WizardPatternFilter();
    FilteredTree filterTree = new FilteredTree(composite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER,
            filteredTreeFilter, true);
    filterTree.setQuickSelectionMode(true);

    final TreeViewer treeViewer = filterTree.getViewer();
    treeViewer.setContentProvider(new WizardContentProvider());
    treeViewer.setLabelProvider(new DelegatingLabelProviderWithTooltip(new WorkbenchLabelProvider(),
            PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator(ContributingPluginDecorator.ID)) {
        protected Object unwrapElement(Object element) {
            if (element instanceof WorkbenchWizardElement) {
                element = ((WorkbenchWizardElement) element).getConfigurationElement();
            }
            if (element instanceof WizardCollectionElement) {
                element = ((WizardCollectionElement) element).getConfigurationElement();
            }
            return element;
        }
    });
    treeViewer.setComparator(NewWizardCollectionComparator.INSTANCE);
    treeViewer.addSelectionChangedListener(this);

    ArrayList inputArray = new ArrayList();

    for (int i = 0; i < primaryWizards.length; i++) {
        inputArray.add(primaryWizards[i]);
    }

    boolean expandTop = false;

    if (wizardCategories != null) {
        if (wizardCategories.getParent() == null) {
            IWizardCategory[] children = wizardCategories.getCategories();
            for (int i = 0; i < children.length; i++) {
                inputArray.add(children[i]);
            }
        } else {
            expandTop = true;
            inputArray.add(wizardCategories);
        }
    }

    // ensure the category is expanded.  If there is a remembered expansion it will be set later.
    if (expandTop) {
        treeViewer.setAutoExpandLevel(2);
    }

    AdaptableList input = new AdaptableList(inputArray);

    treeViewer.setInput(input);

    filterTree.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

    treeViewer.getTree().setFont(parent.getFont());

    treeViewer.addDoubleClickListener(new IDoubleClickListener() {
        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
         */
        public void doubleClick(DoubleClickEvent event) {
            IStructuredSelection s = (IStructuredSelection) event.getSelection();
            selectionChanged(new SelectionChangedEvent(event.getViewer(), s));

            Object element = s.getFirstElement();
            if (treeViewer.isExpandable(element)) {
                treeViewer.setExpandedState(element, !treeViewer.getExpandedState(element));
            } else if (element instanceof WorkbenchWizardElement) {
                page.advanceToNextPageOrFinish();
            }
        }
    });

    treeViewer.addFilter(filter);

    if (projectsOnly) {
        treeViewer.addFilter(projectFilter);
    }

    Dialog.applyDialogFont(filterTree);
    return filterTree;
}

From source file:org.eclipse.ui.navigator.CommonNavigator.java

License:Open Source License

/**
 * <p>//from  w ww. ja  va 2 s.co m
 * Note: This method is for internal use only. Clients should not call this
 * method.
 * </p>
 * 
 * @param anEvent
 *            Supplied by the DoubleClick listener.
 */
protected void handleDoubleClick(DoubleClickEvent anEvent) {

    IAction openHandler = getViewSite().getActionBars().getGlobalActionHandler(ICommonActionConstants.OPEN);

    if (openHandler == null) {
        IStructuredSelection selection = (IStructuredSelection) anEvent.getSelection();
        Object element = selection.getFirstElement();

        TreeViewer viewer = getCommonViewer();
        if (viewer.isExpandable(element)) {
            viewer.setExpandedState(element, !viewer.getExpandedState(element));
        }
    }
}

From source file:org.eclipse.ui.trace.internal.TracingDoubleClickListener.java

License:Open Source License

public void doubleClick(final DoubleClickEvent event) {
    // auto-expand or collapse the selected node
    TreeViewer traceComponentViewer = (TreeViewer) event.getViewer();
    final Object selectedItem = ((IStructuredSelection) traceComponentViewer.getSelection()).getFirstElement();
    final boolean expandedState = traceComponentViewer.getExpandedState(selectedItem);
    if (selectedItem instanceof TracingNode) {
        traceComponentViewer.setExpandedState(selectedItem, !expandedState);
    }/* w  ww. j a  v a2 s.co m*/
}

From source file:org.eclipse.viatra.query.tooling.ui.queryexplorer.content.matcher.CompositeContent.java

License:Open Source License

/**
 * Call this method if the element is already present in the tree viewer, but it is not expanded yet and the
 * children list has changed. In this case, even if the children are updated properly, the tree viewer will not show
 * that the element has children. Calling this method only has effect if the element is not expanded yet (because in
 * these cases the observable list propagates the updates properly).
 *///from w w w.  j a v a  2s  .  c  om
public void updateHasChildren() {
    // only perform it for items which are not expanded, ClassCastException will be thrown otherwise
    // due to the lazy tree content provider
    if (QueryExplorer.getInstance() != null) {
        TreeViewer viewer = QueryExplorer.getInstance().getMatcherTreeViewer();
        if (!viewer.getExpandedState(this)) {
            viewer.setHasChildren(this, getChildren().size() > 0);
        }
        //Bug 491506: This explicit refresh avoid display issues on Linux
        viewer.refresh(this, true);
    }
}