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

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

Introduction

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

Prototype

TreeSelection EMPTY

To view the source code for org.eclipse.jface.viewers TreeSelection EMPTY.

Click Source Link

Document

The canonical empty selection.

Usage

From source file:bndtools.jareditor.internal.JARContentTreePart.java

License:Open Source License

private void refreshSelectedPath() {
    if (selectedPath != null) {
        TreePath treePath = contentProvider.findPath(selectedPath);
        if (treePath != null) {
            viewer.setSelection(new TreeSelection(treePath), true);
        } else {//from  w w w .j av  a2  s  .c o m
            viewer.setSelection(TreeSelection.EMPTY);
        }
    }
}

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

License:Open Source License

/**
 * Called on both dragEnter and dragMove.
 * Generates the onDropEnter/Move/Leave events depending on the currently
 * selected target node./*from   w w  w .j a  v  a 2  s.c o  m*/
 */
private void processDropEvent(DropTargetEvent event) {
    if (!mCanvas.getViewHierarchy().isValid()) {
        // We don't allow drop on an invalid layout, even if we have some obsolete
        // layout info for it.
        event.detail = DND.DROP_NONE;
        clearDropInfo();
        return;
    }

    LayoutPoint p = getDropLocation(event).toLayout();

    // Is the mouse currently captured by a DropFeedback.captureArea?
    boolean isCaptured = false;
    if (mFeedback != null) {
        Rect r = mFeedback.captureArea;
        isCaptured = r != null && r.contains(p.x, p.y);
    }

    // We can't switch views/nodes when the mouse is captured
    CanvasViewInfo vi;
    if (isCaptured) {
        vi = mCurrentView;
    } else {
        vi = mCanvas.getViewHierarchy().findViewInfoAt(p);

        // When dragging into the canvas, if you are not over any other view, target
        // the root element (since it may not "fill" the screen, e.g. if you have a linear
        // layout but have layout_height wrap_content, then the layout will only extend
        // to cover the children in the layout, not the whole visible screen area, which
        // may be surprising
        if (vi == null) {
            vi = mCanvas.getViewHierarchy().getRoot();
        }
    }

    boolean isMove = true;
    boolean needRedraw = false;

    if (vi != mCurrentView) {
        // Current view has changed. Does that also change the target node?
        // Note that either mCurrentView or vi can be null.

        if (vi == null) {
            // vi is null but mCurrentView is not, no view is a target anymore
            // We don't need onDropMove in this case
            isMove = false;
            needRedraw = true;
            event.detail = DND.DROP_NONE;
            clearDropInfo(); // this will call callDropLeave.

        } else {
            // vi is a new current view.
            // Query GRE for onDropEnter on the ViewInfo hierarchy, starting from the child
            // towards its parent, till we find one that returns a non-null drop feedback.

            DropFeedback df = null;
            NodeProxy targetNode = null;

            for (CanvasViewInfo targetVi = vi; targetVi != null
                    && df == null; targetVi = targetVi.getParent()) {
                targetNode = mCanvas.getNodeFactory().create(targetVi);
                df = mCanvas.getRulesEngine().callOnDropEnter(targetNode, targetVi.getViewObject(),
                        mCurrentDragElements);

                if (df != null) {
                    // We should also dispatch an onDropMove() call to the initial enter
                    // position, such that the view is notified of the position where
                    // we are within the node immediately (before we for example attempt
                    // to draw feedback). This is necessary since most views perform the
                    // guideline computations in onDropMove (since only onDropMove is handed
                    // the -position- of the mouse), and we want this computation to happen
                    // before we ask the view to draw its feedback.
                    updateDropFeedback(df, event);
                    df = mCanvas.getRulesEngine().callOnDropMove(targetNode, mCurrentDragElements, df,
                            new Point(p.x, p.y));
                }

                if (df != null && event.detail == DND.DROP_MOVE
                        && mCanvas == mGlobalDragInfo.getSourceCanvas()) {
                    // You can't move an object into itself in the same canvas.
                    // E.g. case of moving a layout and the node under the mouse is the
                    // layout itself: a copy would be ok but not a move operation of the
                    // layout into himself.

                    SelectionItem[] selection = mGlobalDragInfo.getCurrentSelection();
                    if (selection != null) {
                        for (SelectionItem cs : selection) {
                            if (cs.getViewInfo() == targetVi) {
                                // The node that responded is one of the selection roots.
                                // Simply invalidate the drop feedback and move on the
                                // parent in the ViewInfo chain.

                                updateDropFeedback(df, event);
                                mCanvas.getRulesEngine().callOnDropLeave(targetNode, mCurrentDragElements, df);
                                df = null;
                                targetNode = null;
                            }
                        }
                    }
                }
            }

            if (df == null) {
                // Provide visual feedback that we are refusing the drop
                event.detail = DND.DROP_NONE;
                clearDropInfo();

            } else if (targetNode != mTargetNode) {
                // We found a new target node for the drag'n'drop.
                // Release the previous one, if any.
                callDropLeave();

                // And assign the new one
                mTargetNode = targetNode;
                mFeedback = df;

                // We don't need onDropMove in this case
                isMove = false;
            }
        }

        mCurrentView = vi;
    }

    if (isMove && mTargetNode != null && mFeedback != null) {
        // this is a move inside the same view
        com.android.ide.common.api.Point p2 = new com.android.ide.common.api.Point(p.x, p.y);
        updateDropFeedback(mFeedback, event);
        DropFeedback df = mCanvas.getRulesEngine().callOnDropMove(mTargetNode, mCurrentDragElements, mFeedback,
                p2);
        mCanvas.getGestureManager().updateMessage(mFeedback);

        if (df == null) {
            // The target is no longer interested in the drop move.
            event.detail = DND.DROP_NONE;
            callDropLeave();

        } else if (df != mFeedback) {
            mFeedback = df;
        }
    }

    if (mFeedback != null) {
        if (event.detail == DND.DROP_NONE && !mFeedback.invalidTarget) {
            // If we previously provided visual feedback that we were refusing
            // the drop, we now need to change it to mean we're accepting it.
            event.detail = DND.DROP_DEFAULT;
            recomputeDragType(event);

        } else if (mFeedback.invalidTarget) {
            // Provide visual feedback that we are refusing the drop
            event.detail = DND.DROP_NONE;
        }
    }

    if (needRedraw || (mFeedback != null && mFeedback.requestPaint)) {
        mCanvas.redraw();
    }

    // Update outline to show the target node there
    OutlinePage outline = mCanvas.getOutlinePage();
    TreeSelection newSelection = TreeSelection.EMPTY;
    if (mCurrentView != null && mTargetNode != null) {
        // Find the view corresponding to the target node. The current view can be a leaf
        // view whereas the target node is always a parent layout.
        if (mCurrentView.getUiViewNode() != mTargetNode.getNode()) {
            mCurrentView = mCurrentView.getParent();
        }
        if (mCurrentView != null && mCurrentView.getUiViewNode() == mTargetNode.getNode()) {
            TreePath treePath = SelectionManager.getTreePath(mCurrentView);
            newSelection = new TreeSelection(treePath);
        }
    }

    ISelection currentSelection = outline.getSelection();
    if (currentSelection == null || !currentSelection.equals(newSelection)) {
        outline.setSelection(newSelection);
    }
}

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

License:Open Source License

void setActive(boolean active) {
    if (active != mActive) {
        mActive = active;/*from w ww .j  a  v  a  2s  . c om*/

        // Outlines are by default active when they are created; this is intended
        // for deactivating a hidden outline and later reactivating it
        assert mControl != null;
        if (active) {
            getSite().getPage().addSelectionListener(this);
            setModel(mGraphicalEditorPart.getCanvasControl().getViewHierarchy().getRoot());
        } else {
            getSite().getPage().removeSelectionListener(this);
            mRootWrapper.setRoot(null);
            if (mPropertySheet != null) {
                mPropertySheet.selectionChanged(null, TreeSelection.EMPTY);
            }
        }
    }
}

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

License:Open Source License

/**
 * Sets the outline selection./*from   w w w.  j  a va  2 s  .c  o  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./*from  w  w w .j a v  a  2  s. com*/
 *
 * @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.android.ide.eclipse.adt.internal.editors.layout.gle2.SelectionManager.java

License:Open Source License

/**
 * Returns a {@link TreeSelection} where each {@link TreePath} item is
 * actually a {@link CanvasViewInfo}.// w ww  .  ja v a  2  s  . com
 */
@Override
public ISelection getSelection() {
    if (mSelections.isEmpty()) {
        return TreeSelection.EMPTY;
    }

    ArrayList<TreePath> paths = new ArrayList<TreePath>();

    for (SelectionItem cs : mSelections) {
        CanvasViewInfo vi = cs.getViewInfo();
        if (vi != null) {
            paths.add(getTreePath(vi));
        }
    }

    return new TreeSelection(paths.toArray(new TreePath[paths.size()]));
}

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

License:Open Source License

/**
 * Sets the selection. It must be an {@link ITreeSelection} where each segment
 * of the tree path is a {@link CanvasViewInfo}. A null selection is considered
 * as an empty selection.//from www . jav a 2 s  . c  o  m
 * <p/>
 * This method is invoked by {@link LayoutCanvasViewer#setSelection(ISelection)}
 * in response to an <em>outside</em> selection (compatible with ours) that has
 * changed. Typically it means the outline selection has changed and we're
 * synchronizing ours to match.
 */
@Override
public void setSelection(ISelection selection) {
    if (mInsideUpdateSelection) {
        return;
    }

    boolean changed = false;
    try {
        mInsideUpdateSelection = true;

        if (selection == null) {
            selection = TreeSelection.EMPTY;
        }

        if (selection instanceof ITreeSelection) {
            ITreeSelection treeSel = (ITreeSelection) selection;

            if (treeSel.isEmpty()) {
                // Clear existing selection, if any
                if (!mSelections.isEmpty()) {
                    mSelections.clear();
                    mAltSelection = null;
                    updateActionsFromSelection();
                    redraw();
                }
                return;
            }

            boolean redoLayout = false;

            // Create a list of all currently selected view infos
            Set<CanvasViewInfo> oldSelected = new HashSet<CanvasViewInfo>();
            for (SelectionItem cs : mSelections) {
                oldSelected.add(cs.getViewInfo());
            }

            // Go thru new selection and take care of selecting new items
            // or marking those which are the same as in the current selection
            for (TreePath path : treeSel.getPaths()) {
                Object seg = path.getLastSegment();
                if (seg instanceof CanvasViewInfo) {
                    CanvasViewInfo newVi = (CanvasViewInfo) seg;
                    if (oldSelected.contains(newVi)) {
                        // This view info is already selected. Remove it from the
                        // oldSelected list so that we don't deselect it later.
                        oldSelected.remove(newVi);
                    } else {
                        // This view info is not already selected. Select it now.

                        // reset alternate selection if any
                        mAltSelection = null;
                        // otherwise add it.
                        mSelections.add(createSelection(newVi));
                        changed = true;
                    }
                    if (newVi.isInvisible()) {
                        redoLayout = true;
                    }
                } else {
                    // Unrelated selection (e.g. user clicked in the Project Explorer
                    // or something) -- just ignore these
                    return;
                }
            }

            // Deselect old selected items that are not in the new one
            for (CanvasViewInfo vi : oldSelected) {
                if (vi.isExploded()) {
                    redoLayout = true;
                }
                deselect(vi);
                changed = true;
            }

            if (redoLayout) {
                mCanvas.getEditorDelegate().recomputeLayout();
            }
        }
    } finally {
        mInsideUpdateSelection = false;
    }

    if (changed) {
        redraw();
        fireSelectionChanged();
        updateActionsFromSelection();
    }
}

From source file:com.android.ide.eclipse.adt.internal.editors.ui.tree.UiTreeBlock.java

License:Open Source License

/**
 * Creates the buttons next to the tree.
 *///  w ww.  j  a v  a2 s. c om
private void createButtons(FormToolkit toolkit, Composite grid) {

    mUiTreeActions = new UiTreeActions();

    Composite button_grid = SectionHelper.createGridLayout(grid, toolkit, 1);
    button_grid.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
    mAddButton = toolkit.createButton(button_grid, "Add...", SWT.PUSH);
    SectionHelper.addControlTooltip(mAddButton, "Adds a new element.");
    mAddButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));

    mAddButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            doTreeAdd();
        }
    });

    mRemoveButton = toolkit.createButton(button_grid, "Remove...", SWT.PUSH);
    SectionHelper.addControlTooltip(mRemoveButton, "Removes an existing selected element.");
    mRemoveButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    mRemoveButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            doTreeRemove();
        }
    });

    mUpButton = toolkit.createButton(button_grid, "Up", SWT.PUSH);
    SectionHelper.addControlTooltip(mRemoveButton, "Moves the selected element up.");
    mUpButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    mUpButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            doTreeUp();
        }
    });

    mDownButton = toolkit.createButton(button_grid, "Down", SWT.PUSH);
    SectionHelper.addControlTooltip(mRemoveButton, "Moves the selected element down.");
    mDownButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    mDownButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            doTreeDown();
        }
    });

    adjustTreeButtons(TreeSelection.EMPTY);
}

From source file:com.jointlogic.breadcrumbs.sampleapp.api.BreadcrumbViewer.java

License:Open Source License

/**
 * This implementation of getSelection() returns an instance of
 * ITreeSelection.//from  w w  w  . j a  va2s.c om
 */
@Override
public ISelection getSelection() {
    final Control control = getControl();
    if (control == null || control.isDisposed()) {
        return TreeSelection.EMPTY;
    }
    if (this.fSelectedItem != null) {
        final TreePath path = getTreePathFromItem(this.fSelectedItem);
        if (path != null) {
            return new TreeSelection(new TreePath[] { path });
        }
    }
    return TreeSelection.EMPTY;
}

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 w w. j  ava  2s. com*/
 * @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;
}