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

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

Introduction

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

Prototype

@Override
public ISelection getSelection() 

Source Link

Document

The AbstractTreeViewer implementation of this method returns the result as an ITreeSelection.

Usage

From source file:ccw.editors.outline.ClojureOutlinePage.java

License:Open Source License

protected void setInputInUiThread(List<List> forms) {
    if (sort) {/*from   w  w  w.  j  av  a  2  s .  c  o m*/
        List<List> sorted = new ArrayList(forms);
        Collections.sort(sorted, new Comparator<List>() {
            public int compare(List o1, List o2) {
                Symbol s1 = symbol(RT.first(o1)), s2 = symbol(RT.first(o2));

                if (s1 == null && s2 == null)
                    return 0; // mandatory for Comparator contract
                if (s1 == null)
                    return 1;
                if (s2 == null)
                    return -1;

                if (s1.getName().equals("ns") && s2.getName().equals("ns")) {
                    // fall through to order ns decls correctly
                } else {
                    if (s1.getName().equals("ns"))
                        return -1;
                    if (s2.getName().equals("ns"))
                        return 1;
                }

                if (isPrivate(o1) != isPrivate(o2)) {
                    return isPrivate(o1) ? -1 : 1;
                }

                s1 = symbol(RT.second(o1));
                s2 = symbol(RT.second(o2));

                if (s1 == null && s2 == null)
                    return 0; // mandatory for Comparator contract
                if (s1 == null)
                    return 1;
                if (s2 == null)
                    return -1;

                return s1.getName().compareToIgnoreCase(s2.getName());
            }
        });
        forms = sorted;
    }
    final List<List> theForms = forms;

    if (getControl() != null) {
        getControl().getDisplay().asyncExec(new Runnable() {
            public void run() {
                if (getControl().isDisposed())
                    return;

                TreeViewer treeViewer = getTreeViewer();
                if (treeViewer != null) {
                    treeViewer.getTree().setRedraw(false);
                    treeViewer.setInput(theForms);
                    ISelection treeSelection = treeViewer.getSelection();
                    if (treeSelection == null || treeSelection.isEmpty()) {
                        selectInOutline(lastSelection);
                    }
                    treeViewer.getTree().setRedraw(true);
                }
            }
        });
    }
}

From source file:ccw.editors.outline.ClojureOutlinePage.java

License:Open Source License

private void selectInOutline(ISelection selection) {
    TreeViewer viewer = getTreeViewer();
    lastSelection = selection;/*from   w  w  w .  ja v a2  s.co m*/
    if (viewer != null && selection instanceof TextSelection) {
        TextSelection textSelection = (TextSelection) selection;
        int line = textSelection.getStartLine();
        StructuredSelection newSelection = findClosest(line + 1);
        ISelection oldSelection = viewer.getSelection();
        if (!newSelection.equals(oldSelection)) {
            viewer.setSelection(newSelection);
        }
    }
}

From source file:com.amalto.workbench.actions.XSDSkipToFKAction.java

License:Open Source License

@Override
public void run() {
    TreeViewer treeViewer = page.getTreeViewer();
    ISelection selection = treeViewer.getSelection();

    Object selObj = ((IStructuredSelection) selection).getFirstElement();
    if (selObj instanceof XSDParticle) {
        XSDTerm term = ((XSDParticle) selObj).getTerm();
        if (term instanceof XSDElementDeclaration) {
            XSDElementDeclaration element = (XSDElementDeclaration) term;

            String fkPath = getFKInfo(element);
            if (fkPath == null) {
                MessageDialog.openInformation(null, Messages.XSDSkipToFKAction_actionTitle,
                        Messages.XSDSkipToFKAction_NotFoundFkInfo);
                return;
            }/*  www . j a  v a  2 s  . co  m*/
            String entityName = getEntityName(fkPath);

            EList<XSDElementDeclaration> elementDeclarations = page.getXSDSchema().getElementDeclarations();
            for (XSDElementDeclaration elementDeclaration : elementDeclarations) {
                String name = elementDeclaration.getName();
                if (entityName.equals(name)) {
                    StructuredSelection fkSelection = new StructuredSelection(elementDeclaration);
                    page.getElementsViewer().setSelection(fkSelection);
                    break;
                }
            }

        }
    }

}

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/*  ww w  .j ava2s. 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.amalto.workbench.editors.DataModelMainPage.java

License:Open Source License

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

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

        @Override/*  w ww.jav a  2  s  . c o m*/
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) targetTreeViewer.getSelection();
            for (Object eachSelectedObj : selection.toArray()) {
                targetTreeViewer.collapseToLevel(eachSelectedObj, 3);
            }
        }
    });

    return collapseToolItem;
}

From source file:com.amalto.workbench.editors.xsdeditor.XSDEditor.java

License:Open Source License

private void resetTreeSelection(int newPageIndex) {
    DataModelMainPage dataModelEditorPage = getDataModelEditorPage();
    if (dataModelEditorPage != null) {
        TreeViewer treeViewer = dataModelEditorPage.getTreeViewer();
        if (newPageIndex == MODEL_PAGE_INDEX) {
            treeViewer.setSelection(null);
        } else if (newPageIndex == SOURCE_PAGE_INDEX) {
            IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
            if (!selection.isEmpty()) {
                Object firstElement = selection.getFirstElement();
                if ((firstElement instanceof XSDIdentityConstraintDefinition)) {
                    XSDIdentityConstraintDefinition cdf = (XSDIdentityConstraintDefinition) firstElement;
                    XSDConcreteComponent container = cdf.getContainer();
                    treeViewer.setSelection(new StructuredSelection(container));
                } else if ((firstElement instanceof XSDXPathDefinition)) {
                    XSDXPathDefinition pathdef = (XSDXPathDefinition) firstElement;
                    XSDConcreteComponent container = pathdef.getContainer().getContainer();
                    treeViewer.setSelection(new StructuredSelection(container));
                } else if (firstElement instanceof XSDAnnotation) {
                    XSDAnnotation annotation = (XSDAnnotation) firstElement;
                    XSDConcreteComponent container = annotation.getContainer();
                    treeViewer.setSelection(new StructuredSelection(container));
                }/*from   ww w.ja va  2 s .  c om*/
            }
        }
    }
}

From source file:com.aptana.deploy.wizard.DeployWizardPage.java

License:Open Source License

protected void updateMessage() {
    setMessage(com.aptana.deploy.wizard.WorkbenchMessages.DeployWizardPage_SelectYourDesiredDeploymentOption);
    TreeViewer viewer = getTreeViewer();
    if (viewer != null) {
        ISelection selection = viewer.getSelection();
        IStructuredSelection ss = (IStructuredSelection) selection;
        Object sel = ss.getFirstElement();
        if (sel instanceof WorkbenchWizardElement) {
            updateSelectedNode((WorkbenchWizardElement) sel);
        } else {/*from w w  w .ja va  2s  .  co  m*/
            setSelectedNode(null);
        }
    } else {
        descriptionLabel.setText(null);
    }
}

From source file:com.aptana.deploy.wizard.DeployWizardPage.java

License:Open Source License

/**
 * Stores the currently-selected element in this page's dialog store, in order to recreate this page's state in the
 * next instance of this page./*from  w w w. jav  a  2  s .  c  om*/
 */
protected void storeSelectedCategoryAndWizard(String setting, TreeViewer viewer) {
    Object selected = ((IStructuredSelection) viewer.getSelection()).getFirstElement();

    if (selected != null) {
        if (selected instanceof IWizardCategory) {
            getDialogSettings().put(setting, ((IWizardCategory) selected).getPath().toString());
        } else {
            // else its a wizard
            getDialogSettings().put(setting, ((IWizardDescriptor) selected).getId());
        }
    }
}

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  a2s. co m*/
            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.aptana.editor.common.outline.CommonOutlinePage.java

License:Open Source License

public void propertyChange(PropertyChangeEvent event) {
    String property = event.getProperty();

    if (property.equals(IPreferenceConstants.LINK_OUTLINE_WITH_EDITOR)) {
        boolean isLinked = Boolean.parseBoolean(StringUtil.getStringValue(event.getNewValue()));

        fToggleLinkingAction.setChecked(isLinked);
        TreeViewer viewer = getTreeViewer();
        if (isLinked) {
            setEditorSelection((IStructuredSelection) viewer.getSelection(), false);
        }//ww w . j av a  2 s  .  c o  m
    } else if (property.equals(IPreferenceConstants.SORT_OUTLINE_ALPHABETIC)) {
        boolean sort = Boolean.parseBoolean(StringUtil.getStringValue(event.getNewValue()));
        getTreeViewer().setComparator(sort ? new ViewerComparator() : null);
    }
}