Example usage for org.eclipse.jface.viewers TreeSelection getPathsFor

List of usage examples for org.eclipse.jface.viewers TreeSelection getPathsFor

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers TreeSelection getPathsFor.

Prototype

@Override
    public TreePath[] getPathsFor(Object element) 

Source Link

Usage

From source file:es.cv.gvcase.emf.ui.common.dialogs.ChooseDialog.java

License:Open Source License

protected void modifySelectionPath(TreeSelection ts) {
    paths.clear();// w w  w.ja  va 2 s  .co  m

    for (Object o : ts.toList()) {
        DataPath dp = new DataPath(o, ts.getPathsFor(o)[0], getAllowedTypesInThePath());
        paths.add(dp);
    }
}

From source file:es.cv.gvcase.fefem.common.composites.EMFContainedHierarchicalCollectionEditionComposite.java

License:Open Source License

/**
 * Creates the a SelectionListener which will invoke the code to remove/delete
 * a selected element from the tree viewer.
 *     /*from  w ww  .ja v a 2  s. co  m*/
 * @return SelectionListener the {@link SelectionListener} which will
 * remove/delete a selected element from the viewer.  
 */
protected SelectionListener getRemoveButtonSelectionListener() {
    SelectionAdapter adapter = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {

            TreeViewer viewer = getViewer();
            if (viewer.getSelection() instanceof TreeSelection) {
                TreeSelection selection = (TreeSelection) viewer.getSelection();
                List<?> elementsToDelete = selection.toList();
                if (elementsToDelete.size() > 0) {
                    // We prepare the next selection, based on the element to delete
                    EObject eObject = (EObject) elementsToDelete.get(0);
                    TreePath path = selection.getPathsFor(eObject)[0];
                    TreeSelection nextSelection = TreeSelection.EMPTY;
                    ;
                    if (path.getSegmentCount() == 1) { // If it is a first level element, we will select a sibling element
                        if (modelObservable.size() > 1) { // If we have more than one element 
                            int pos = modelObservable.indexOf(eObject);
                            nextSelection = (pos == modelObservable.size() - 1) // If it's the last first level element, we will select the previous sibling
                                    ? new TreeSelection(
                                            new TreePath(new Object[] { modelObservable.get(pos - 1) }))
                                    : new TreeSelection(
                                            new TreePath(new Object[] { modelObservable.get(pos + 1) })); // otherwise, we will select the next one
                        }
                    } else { // If it is not a first level element, we will select its parent element
                        nextSelection = new TreeSelection(path.getParentPath());
                    }

                    EditingDomain domain = getEditingDomain();
                    domain.getCommandStack().execute(DeleteCommand.create(domain, elementsToDelete));
                    getPage().setDirty(true);
                    viewer.setSelection(nextSelection);
                }
            }
        }
    };
    return adapter;
}

From source file:org.eclipse.emf.henshin.presentation.HenshinEditor.java

License:Open Source License

/**
 * Calculates the order the selected items are selected in. To do so,
 * variable {@link #editorSelection}, i.e. the last selection, is taken into
 * account.<br>/*  w  w w.  j  a  va2s.c om*/
 * This procedure may be useful to present (e.g.) a context menu with
 * entries according to the selection order. An example scenario here would
 * be the selection of two nodes of which a validly typed directed edge
 * could be created instantly.
 * 
 * @param selection
 * @return
 */
@SuppressWarnings("unchecked")
private ISelection remainSelectionOrder(ISelection selection) {
    /*
     * A calculation is only meaningful if both the given selection and the
     * previous selection are of type TreeSelection, i.e. selections within
     * (our) tree viewer, and if both are non-empty.
     */
    if ((selection instanceof TreeSelection) && (editorSelection instanceof TreeSelection)
            && (!editorSelection.isEmpty())) {

        TreeSelection currSelection = (TreeSelection) selection;
        if (currSelection.size() > 1) {

            TreeSelection prevSelection = (TreeSelection) editorSelection;

            // Filter and sort selected items
            @SuppressWarnings("rawtypes")
            LinkedHashSet sList1 = new LinkedHashSet(prevSelection.toList());
            sList1.retainAll(currSelection.toList());
            sList1.addAll(currSelection.toList());

            // create new selection container
            Object[] objects = sList1.toArray();
            TreePath[] treePaths = new TreePath[sList1.size()];
            for (int i = 0; i < sList1.size(); i++)
                treePaths[i] = currSelection.getPathsFor(objects[i])[0];
            selection = new TreeSelection(treePaths);
        } // if

    } // if
    return selection;
}

From source file:org.eclipse.stardust.modeling.transformation.debug.views.TransformationMappingDebugView.java

License:Open Source License

public void highlightOutMessageNode(String fieldPath) {
    // This counter should be incremented on each iter.next() call which succeeds.
    int usedFieldPathSegmentCounter = 0;
    Iterator iter = StringUtils.split(fieldPath, "/"); //$NON-NLS-1$
    // if no path at all -> return
    if (!iter.hasNext()) {
        // TODO: log this situation
        return;/*from ww  w .j a  v  a2  s . c  o m*/
    }

    TreeViewer target = controlsManager.getTargetMessageTreeViewer();
    MultipleAccessPathBrowserContentProvider contentProvider = (MultipleAccessPathBrowserContentProvider) target
            .getContentProvider();

    TreeItem[] items = target.getTree().getItems();

    String rootItemName = (String) iter.next();
    ++usedFieldPathSegmentCounter;

    TreeItem rootItem = null;
    for (int idx = 0; idx < items.length; idx++) {
        rootItem = items[idx];
        if (rootItemName.equals(rootItem.getText())) {
            break;
        }
    }

    // if no root item with this name exists -> return
    if (null == rootItem) {
        // TODO: log this situation
        return;
    }

    List itemData = CollectionUtils.newArrayList();
    StructAccessPointType sap = (StructAccessPointType) rootItem.getData();
    itemData.add(sap);

    // search for all children selected by field path elements
    boolean hasChildren = contentProvider.hasChildren(sap);
    while (hasChildren && iter.hasNext()) {
        hasChildren = false;
        String nextItemName = (String) iter.next();
        ++usedFieldPathSegmentCounter;

        Object[] children = contentProvider.getChildren(sap);
        for (int idx = 0; idx < children.length; idx++) {
            sap = (StructAccessPointType) children[idx];
            if (sap.getId().equals(nextItemName)) {
                itemData.add(sap);
                hasChildren = contentProvider.hasChildren(sap);
                break;
            }
        }
    }

    if (usedFieldPathSegmentCounter != itemData.size()) {
        // TODO: log this situation
        return;
    }

    // only set new selection if current selection is the same.
    TreeSelection selection = (TreeSelection) target.getSelection();
    TreePath[] selectedPath = selection.getPathsFor(itemData.get(itemData.size() - 1));
    if (selectedPath.length == 0) {
        target.setSelection(new TreeSelection(new TreePath(itemData.toArray())), true);
    }
}

From source file:org.fusesource.ide.jmx.ui.internal.actions.DoubleClickAction.java

License:Open Source License

protected IConnectionWrapper findParent(IStructuredSelection sel) {
    if (sel instanceof TreeSelection) {
        TreeSelection sel2 = ((TreeSelection) sel);
        TreePath[] paths = sel2.getPathsFor(sel.getFirstElement());
        if (paths != null && paths.length == 1) {
            if (paths[0].getFirstSegment() instanceof IConnectionWrapper)
                return (IConnectionWrapper) paths[0].getFirstSegment();
        }/*  w  w  w .j a  v  a2s  . co m*/
    }
    return null;
}

From source file:org.gemoc.executionframework.engine.ui.debug.AbstractGemocDebugger.java

License:Open Source License

private void selectLastStackframe() {
    final IWorkbench workbench = PlatformUI.getWorkbench();
    workbench.getDisplay().asyncExec(() -> {
        final IWorkbenchPage workbenchPage = workbench.getActiveWorkbenchWindow().getActivePage();
        final IViewPart view = workbenchPage.findView("org.eclipse.debug.ui.DebugView");
        view.setFocus();/*from   w  w w.j  a  va  2s . c  om*/
        final ISelectionProvider selectionProvider = view.getSite().getSelectionProvider();
        selectionProvider.setSelection(StructuredSelection.EMPTY);
        if (view instanceof LaunchView) {
            final LaunchView launchView = (LaunchView) view;
            final Viewer viewer = launchView.getViewer();
            final Tree tree = ((TreeModelViewer) viewer).getTree();
            final TreeItem[] items = tree.getItems();
            final List<TreeItem> allItems = flatten(Arrays.asList(items), t -> Arrays.asList(t.getItems()));
            final List<TreeItem> leafItems = allItems.stream()
                    .filter(i -> i.getData() instanceof DSLStackFrameAdapter)
                    .filter(i -> ((DSLStackFrameAdapter) i.getData()).getTarget() instanceof StackFrame)
                    .collect(Collectors.toList());
            for (TreeItem item : leafItems) {
                final DSLStackFrameAdapter stackFrameAdapter = (DSLStackFrameAdapter) item.getData();
                final StackFrame s = (StackFrame) stackFrameAdapter.getTarget();
                if (s.getName().startsWith("Global context :")) {
                    tree.showItem(item);
                    tree.select(item);
                    final TreeSelection selection = (TreeSelection) viewer.getSelection();
                    final TreePath[] paths = selection.getPathsFor(stackFrameAdapter);
                    selectionProvider.setSelection(new TreeSelection(paths));
                    break;
                }
            }
        }
    });
}