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

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

Introduction

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

Prototype

public void expandToLevel(int level, boolean disableRedraw) 

Source Link

Document

Expands the root of the viewer's tree to the given level.

Usage

From source file:ca.uwaterloo.gp.fmp.provider.action.ExpandSubtreeAction.java

License:Open Source License

public void run() {
    if (selectionProvider instanceof FmpEditor) {
        FmpEditor editor = (FmpEditor) selectionProvider;
        TreeViewer viewer = ((TreeViewer) editor.getViewer());
        if (viewer != null)
            viewer.expandToLevel(object, AbstractTreeViewer.ALL_LEVELS);
    } else if (selectionProvider instanceof TreeViewer) {
        TreeViewer viewer = (TreeViewer) selectionProvider;
        if (viewer != null)
            viewer.expandToLevel(object, AbstractTreeViewer.ALL_LEVELS);
    }//from   w  w  w . j a  v a 2 s. c o m

}

From source file:com.amalto.workbench.editors.DataModelMainPage.java

License:Open Source License

private ToolItem createExpandToolItem(ToolBar parentToolBar, final TreeViewer targetTreeViewer) {

    ToolItem expanedToolItem = new ToolItem(parentToolBar, SWT.PUSH);
    expanedToolItem.setImage(ImageCache.getCreatedImage(EImage.EXPAND.getPath()));
    expanedToolItem.setToolTipText(Messages.ExpandText);
    expanedToolItem.setEnabled(!isReadOnly());
    expanedToolItem.addSelectionListener(new SelectionAdapter() {

        @Override/*from   w  w  w.  j  av a2 s .  com*/
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) targetTreeViewer.getSelection();
            for (Object eachSelectedObj : selection.toArray()) {
                targetTreeViewer.expandToLevel(eachSelectedObj, 3);
            }
        }
    });

    return expanedToolItem;
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.gle2.OutlinePage.java

License:Open Source License

private void createOutline(Composite parent) {
    if (AdtUtils.isEclipse4()) {
        // This is a workaround for the focus behavior in Eclipse 4 where
        // the framework ends up calling setFocus() on the first widget in the outline
        // AFTER a mouse click has been received. Specifically, if the user clicks in
        // the embedded property sheet to for example give a Text property editor focus,
        // then after the mouse click, the Outline window activation event is processed,
        // and this event causes setFocus() to be called first on the PageBookView (which
        // ends up calling setFocus on the first control, normally the TreeViewer), and
        // then on the Page itself. We're dealing with the page setFocus() in the override
        // of that method in the class, such that it does nothing.
        // However, we have to also disable the setFocus on the first control in the
        // outline page. To deal with that, we create our *own* first control in the
        // outline, and make its setFocus() a no-op. We also make it invisible, since we
        // don't actually want anything but the tree viewer showing in the outline.
        Text text = new Text(parent, SWT.NONE) {
            @Override/*from   w w  w.j a  va  2s  .  c  om*/
            public boolean setFocus() {
                // Focus no-op
                return true;
            }

            @Override
            protected void checkSubclass() {
                // Disable the check that prevents subclassing of SWT components
            }
        };
        text.setVisible(false);
    }

    super.createControl(parent);

    TreeViewer tv = getTreeViewer();
    tv.setAutoExpandLevel(2);
    tv.setContentProvider(new ContentProvider());
    tv.setLabelProvider(new LabelProvider());
    tv.setInput(mRootWrapper);
    tv.expandToLevel(mRootWrapper.getRoot(), 2);

    int supportedOperations = DND.DROP_COPY | DND.DROP_MOVE;
    Transfer[] transfers = new Transfer[] { SimpleXmlTransfer.getInstance() };

    tv.addDropSupport(supportedOperations, transfers, new OutlineDropListener(this, tv));
    tv.addDragSupport(supportedOperations, transfers, new OutlineDragListener(this, tv));

    // The tree viewer will hold CanvasViewInfo instances, however these
    // change each time the canvas is reloaded. OTOH layoutlib gives us
    // constant UiView keys which we can use to perform tree item comparisons.
    tv.setComparer(new IElementComparer() {
        @Override
        public int hashCode(Object element) {
            if (element instanceof CanvasViewInfo) {
                UiViewElementNode key = ((CanvasViewInfo) element).getUiViewNode();
                if (key != null) {
                    return key.hashCode();
                }
            }
            if (element != null) {
                return element.hashCode();
            }
            return 0;
        }

        @Override
        public boolean equals(Object a, Object b) {
            if (a instanceof CanvasViewInfo && b instanceof CanvasViewInfo) {
                UiViewElementNode keyA = ((CanvasViewInfo) a).getUiViewNode();
                UiViewElementNode keyB = ((CanvasViewInfo) b).getUiViewNode();
                if (keyA != null) {
                    return keyA.equals(keyB);
                }
            }
            if (a != null) {
                return a.equals(b);
            }
            return false;
        }
    });
    tv.addDoubleClickListener(new IDoubleClickListener() {
        @Override
        public void doubleClick(DoubleClickEvent event) {
            // This used to open the property view, but now that properties are docked
            // let's use it for something else -- such as showing the editor source
            /*
            // Front properties panel; its selection is already linked
            IWorkbenchPage page = getSite().getPage();
            try {
            page.showView(IPageLayout.ID_PROP_SHEET, null, IWorkbenchPage.VIEW_ACTIVATE);
            } catch (PartInitException e) {
            AdtPlugin.log(e, "Could not activate property sheet");
            }
            */

            TreeItem[] selection = getTreeViewer().getTree().getSelection();
            if (selection.length > 0) {
                CanvasViewInfo vi = getViewInfo(selection[0].getData());
                if (vi != null) {
                    LayoutCanvas canvas = mGraphicalEditorPart.getCanvasControl();
                    canvas.show(vi);
                }
            }
        }
    });

    setupContextMenu();

    // Listen to selection changes from the layout editor
    getSite().getPage().addSelectionListener(this);
    getControl().addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            dispose();
        }
    });

    Tree tree = tv.getTree();
    tree.addKeyListener(new KeyListener() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.character == '-') {
                if (mMoveUpAction.isEnabled()) {
                    mMoveUpAction.run();
                }
            } else if (e.character == '+') {
                if (mMoveDownAction.isEnabled()) {
                    mMoveDownAction.run();
                }
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });

    setupTooltip();
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.gle2.OutlinePage.java

License:Open Source License

/**
 * Invoked by {@link LayoutCanvas} to set the model (a.k.a. the root view info).
 *
 * @param rootViewInfo The root of the view info hierarchy. Can be null.
 *//*w w w.ja v a  2  s  . c o  m*/
public void setModel(CanvasViewInfo rootViewInfo) {
    if (!mActive) {
        return;
    }

    mRootWrapper.setRoot(rootViewInfo);

    TreeViewer tv = getTreeViewer();
    if (tv != null && !tv.getTree().isDisposed()) {
        Object[] expanded = tv.getExpandedElements();
        tv.refresh();
        tv.setExpandedElements(expanded);
        // Ensure that the root is expanded
        tv.expandToLevel(rootViewInfo, 2);
    }
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.gle2.OutlinePage.java

License:Open Source License

/**
 * Sets the outline selection.//from w  ww.j  a  v  a2 s .co  m
 *
 * @param selection Only {@link ITreeSelection} will be used, otherwise the
 *   selection will be cleared (including a null selection).
 */
@Override
public void setSelection(ISelection selection) {
    // TreeViewer should be able to deal with a null selection, but let's make it safe
    if (selection == null) {
        selection = TreeSelection.EMPTY;
    }
    if (selection.equals(TreeSelection.EMPTY)) {
        return;
    }

    super.setSelection(selection);

    TreeViewer tv = getTreeViewer();
    if (tv == null || !(selection instanceof ITreeSelection) || selection.isEmpty()) {
        return;
    }

    // auto-reveal the selection
    ITreeSelection treeSel = (ITreeSelection) selection;
    for (TreePath p : treeSel.getPaths()) {
        tv.expandToLevel(p, 1);
    }
}

From source file:com.android.ide.eclipse.adt.internal.editors.layout.gle2.OutlinePage2.java

License:Open Source License

/**
 * Sets the outline selection./*w  w w .j  av  a2  s  .c om*/
 *
 * @param selection Only {@link ITreeSelection} will be used, otherwise the
 *   selection will be cleared (including a null selection).
 */
@Override
public void setSelection(ISelection selection) {
    // TreeViewer should be able to deal with a null selection, but let's make it safe
    if (selection == null) {
        selection = TreeSelection.EMPTY;
    }

    super.setSelection(selection);

    TreeViewer tv = getTreeViewer();
    if (tv == null || !(selection instanceof ITreeSelection) || selection.isEmpty()) {
        return;
    }

    // auto-reveal the selection
    ITreeSelection treeSel = (ITreeSelection) selection;
    for (TreePath p : treeSel.getPaths()) {
        tv.expandToLevel(p, 1);
    }
}

From source file:com.aptana.editor.common.outline.CommonOutlinePage.java

License:Open Source License

@Override
public void createControl(Composite parent) {
    fMainControl = new Composite(parent, SWT.NONE);
    fMainControl.setLayout(GridLayoutFactory.fillDefaults().spacing(0, 2).create());
    fMainControl.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());

    fSearchBox = new Text(fMainControl, SWT.SINGLE | SWT.BORDER | SWT.SEARCH);
    fSearchBox.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).indent(0, 3).create());
    fSearchBox.setText(INITIAL_FILTER_TEXT);
    fSearchBox.setForeground(fSearchBox.getDisplay().getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
    fSearchBox.addModifyListener(fSearchModifyListener);
    fSearchBox.addFocusListener(new FocusListener() {

        public void focusLost(FocusEvent e) {
            if (fSearchBox.getText().length() == 0) {
                fSearchBox.removeModifyListener(fSearchModifyListener);
                fSearchBox.setText(INITIAL_FILTER_TEXT);
                fSearchBox.addModifyListener(fSearchModifyListener);
            }/*w  ww .j  a v  a  2s .c om*/
            fSearchBox
                    .setForeground(fSearchBox.getDisplay().getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
        }

        public void focusGained(FocusEvent e) {
            if (fSearchBox.getText().equals(INITIAL_FILTER_TEXT)) {
                fSearchBox.removeModifyListener(fSearchModifyListener);
                fSearchBox.setText(StringUtil.EMPTY);
                fSearchBox.addModifyListener(fSearchModifyListener);
            }
            fSearchBox.setForeground(null);
        }
    });

    fTreeViewer = new TreeViewer(fMainControl, SWT.VIRTUAL | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    fTreeViewer.addSelectionChangedListener(this);
    fTreeViewer.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());

    ((IContextService) getSite().getService(IContextService.class)).activateContext(OUTLINE_CONTEXT);

    final TreeViewer viewer = getTreeViewer();
    viewer.setUseHashlookup(true);
    viewer.setContentProvider(fContentProvider);
    viewer.setLabelProvider(fLabelProvider);
    fInput = new CommonOutlinePageInput(fEditor.getAST());
    // Note: the input remains the same (we change its internal contents with a new ast and call refresh,
    // so that the outline structure is maintained).
    viewer.setInput(fInput);
    viewer.setComparator(isSortingEnabled() ? new ViewerComparator() : null);
    fFilter = new PatternFilter() {

        @Override
        protected boolean isLeafMatch(Viewer viewer, Object element) {
            String label = null;
            if (element instanceof CommonOutlineItem) {
                label = ((CommonOutlineItem) element).getLabel();
            } else if (element instanceof IParseNode) {
                label = ((IParseNode) element).getText();
            }

            if (label == null) {
                return true;
            }
            return wordMatches(label);
        }
    };
    fFilter.setIncludeLeadingWildcard(true);
    viewer.addFilter(fFilter);
    viewer.addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(DoubleClickEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            // expands the selection one level if applicable
            viewer.expandToLevel(selection.getFirstElement(), 1);
            // selects the corresponding text in editor
            if (!isLinkedWithEditor()) {
                setEditorSelection(selection, true);
            }
        }
    });
    viewer.getTree().addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
            if (e.keyCode == '\r' && isLinkedWithEditor()) {
                ISelection selection = viewer.getSelection();
                if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
                    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                    if (page != null) {
                        // brings editor to focus
                        page.activate(fEditor);
                        // deselects the current selection but keeps the cursor position
                        Object widget = fEditor.getAdapter(Control.class);
                        if (widget instanceof StyledText)
                            fEditor.selectAndReveal(((StyledText) widget).getCaretOffset(), 0);
                    }
                }
            }
        }
    });

    hookToThemes();

    IActionBars actionBars = getSite().getActionBars();
    registerActions(actionBars);
    actionBars.updateActionBars();

    fPrefs.addPropertyChangeListener(this);
    fFilterRefreshJob = new WorkbenchJob("Refresh Filter") //$NON-NLS-1$
    {

        @Override
        public IStatus runInUIThread(IProgressMonitor monitor) {
            if (isDisposed()) {
                return Status.CANCEL_STATUS;
            }

            fTreeViewer.refresh();
            String text = fSearchBox.getText();
            if (!StringUtil.isEmpty(text) && !INITIAL_FILTER_TEXT.equals(text)) {
                fTreeViewer.expandAll();
            }
            return Status.OK_STATUS;
        }
    };
    EclipseUtil.setSystemForJob(fFilterRefreshJob);
}

From source file:com.bdaum.zoom.ui.internal.UiUtilities.java

License:Open Source License

public static void installDoubleClickExpansion(TreeViewer viewer) {
    viewer.addDoubleClickListener(new IDoubleClickListener() {
        @Override//from  ww w .j ava  2s.  c  o m
        public void doubleClick(DoubleClickEvent event) {
            Object item = ((IStructuredSelection) event.getSelection()).getFirstElement();
            if (item != null && viewer.isExpandable(item)) {
                if (viewer.getExpandedState(item))
                    viewer.collapseToLevel(item, 1);
                else
                    viewer.expandToLevel(item, 1);
            }
        }
    });
}

From source file:com.bluexml.side.form.presentation.FormEditor.java

License:Open Source License

/**
 * Add double click listener to show the properties view on double click
 * /*from ww w .ja  v a 2 s.co m*/
 * @param viewer
 */
private void addDoubleClickEventListener(StructuredViewer viewer) {

    if (viewer instanceof TreeViewer) {
        final TreeViewer newTreeViewer = (TreeViewer) viewer;
        newTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent event) {
                // Show property view
                try {
                    getEditorSite().getPage().showView("org.eclipse.ui.views.PropertySheet");
                } catch (PartInitException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // Open if is container
                ISelection selection = event.getSelection();
                if (selection instanceof IStructuredSelection) {
                    Object item = ((IStructuredSelection) selection).getFirstElement();
                    if (newTreeViewer.getExpandedState(item)) {
                        newTreeViewer.collapseToLevel(item, 1);
                    } else {
                        newTreeViewer.expandToLevel(item, 5);
                    }
                }
                // If reference change focus to target FormClass
                if ((((IStructuredSelection) selection).getFirstElement() instanceof Reference)
                        && ((IStructuredSelection) selection).size() == 1) {
                    Reference ref = ((Reference) ((IStructuredSelection) selection).getFirstElement());
                    setSelectionToViewer(ref.getTarget());
                } else if ((((IStructuredSelection) selection).getFirstElement() instanceof VirtualField)
                        && ((IStructuredSelection) selection).size() == 1) {
                    VirtualField vf = (VirtualField) ((IStructuredSelection) selection).getFirstElement();
                    ArrayList<Field> list = new ArrayList<Field>(1);
                    list.add(vf.getLink());
                    setSelectionToViewer(list);
                }
            }
        });
    }
}

From source file:com.bluexml.side.view.presentation.ViewEditor.java

License:Open Source License

/**
 * Add double click listener to show the properties view on double click
 *
 * @param viewer//from   ww  w  . j ava 2 s. c  o  m
 */
private void addDoubleClickEventListener(StructuredViewer viewer) {

    if (viewer instanceof TreeViewer) {
        final TreeViewer newTreeViewer = (TreeViewer) viewer;
        newTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
            public void doubleClick(DoubleClickEvent event) {
                // Show property view
                try {
                    getEditorSite().getPage().showView("org.eclipse.ui.views.PropertySheet");
                } catch (PartInitException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // Open if is container
                ISelection selection = event.getSelection();
                if (selection instanceof IStructuredSelection) {
                    Object item = ((IStructuredSelection) selection).getFirstElement();
                    if (newTreeViewer.getExpandedState(item)) {
                        newTreeViewer.collapseToLevel(item, 1);
                    } else {
                        newTreeViewer.expandToLevel(item, 5);
                    }
                }
            }
        });
    }
}