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

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

Introduction

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

Prototype

@Override
    protected void setSelection(List<Item> items) 

Source Link

Usage

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

License:Open Source License

private void selectInOutline(ISelection selection) {
    TreeViewer viewer = getTreeViewer();
    lastSelection = selection;/*from   w ww .j  ava2  s .c  o  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.editors.DataModelMainPage.java

License:Open Source License

private void activeMarkerItem(TreeViewer v, Element dom) {
    ITreeContentProvider provider = (ITreeContentProvider) v.getContentProvider();
    for (Object data : provider.getElements(xsdSchema)) {
        Object[] foundData = findMarkerData(provider, data, dom, new Object[0]);
        if (foundData != null) {
            TreePath treePath = new TreePath(foundData);
            v.setSelection(new TreeSelection(treePath));
            return;
        }/*w w  w.j a v  a  2  s  .c o m*/
    }
}

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 w ww  .  ja va2s .com
            }
        }
    }
}

From source file:com.atlassian.connector.eclipse.internal.crucible.ui.wizards.SelectChangesetsFromCruciblePage.java

License:Open Source License

@SuppressWarnings("unchecked")
private TreeSelection validateTreeSelection(TreeViewer treeViewer) {
    TreeSelection selection = getTreeSelection(treeViewer);
    if (selection != null) {
        ArrayList<TreePath> validSelections = new ArrayList<TreePath>();
        Iterator<Object> iterator = (selection).iterator();
        while (iterator.hasNext()) {
            Object element = iterator.next();
            if (element instanceof Change) {
                validSelections.add((selection).getPathsFor(element)[0]);
            } else if (element instanceof Repository) {
                validSelections.add((selection).getPathsFor(element)[0]);
            }//w w w  .j a  v a2 s. c o m
        }
        //set new selection
        TreeSelection newSelection = new TreeSelection(
                validSelections.toArray(new TreePath[validSelections.size()]),
                (selection).getElementComparer());
        treeViewer.setSelection(newSelection);
    } else {
        treeViewer.setSelection(new TreeSelection());
    }
    return getTreeSelection(treeViewer);
}

From source file:com.atlassian.connector.eclipse.internal.crucible.ui.wizards.SelectScmChangesetsPage.java

License:Open Source License

@SuppressWarnings("unchecked")
private TreeSelection validateTreeSelection(TreeViewer treeViewer, boolean allowChangesetsSelection) {
    TreeSelection selection = getTreeSelection(treeViewer);
    if (selection != null) {
        ArrayList<TreePath> validSelections = new ArrayList<TreePath>();
        Iterator<Object> iterator = (selection).iterator();
        while (iterator.hasNext()) {
            Object element = iterator.next();
            if (element instanceof ICustomChangesetLogEntry) {
                validSelections.add((selection).getPathsFor(element)[0]);
            } else if (allowChangesetsSelection && element instanceof ScmRepository) {
                validSelections.add((selection).getPathsFor(element)[0]);
            }/*from ww w  .  j  a  v a 2 s.  co  m*/
        }
        //set new selection
        TreeSelection newSelection = new TreeSelection(
                validSelections.toArray(new TreePath[validSelections.size()]),
                (selection).getElementComparer());
        treeViewer.setSelection(newSelection);
    } else {
        treeViewer.setSelection(new TreeSelection());
    }
    return getTreeSelection(treeViewer);
}

From source file:com.clustercontrol.monitor.view.action.EventFilterAction.java

License:Open Source License

/**
 * [??]?????????//  w w  w .  j  a v  a  2 s.c o m
 * ???
 * <p>
 * <ol>
 * <li>[??]???</li>
 * <li>????????</li>
 * <li>[]??????</li>
 * <li>[]???</li>
 * </ol>
 *
 * @see org.eclipse.core.commands.IHandler#execute
 * @see com.clustercontrol.monitor.dialog.EventFilterDialog
 * @see com.clustercontrol.monitor.view.EventView#setCondition(Property)
 * @see com.clustercontrol.monitor.view.EventView#update()
 */
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    this.window = HandlerUtil.getActiveWorkbenchWindow(event);
    // ???
    this.viewPart = HandlerUtil.getActivePart(event);

    EventView view = null;
    try {
        view = (EventView) this.viewPart.getAdapter(EventView.class);
    } catch (Exception e) {
        m_log.info("execute " + e.getMessage());
        return null;
    }

    if (view == null) {
        m_log.info("execute: view is null");
        return null;
    }

    ICommandService commandService = (ICommandService) window.getService(ICommandService.class);
    Command command = commandService.getCommand(ID);
    boolean isChecked = !HandlerUtil.toggleCommandState(command);

    if (isChecked) {
        // ?
        EventFilterDialog dialog = new EventFilterDialog(this.viewPart.getSite().getShell());

        // ??????????
        if (dialog.open() == IDialogConstants.OK_ID) {

            Property condition = dialog.getInputData();

            view.setCondition(condition);
            view.update(false);
        } else {
            State state = command.getState(RegistryToggleState.STATE_ID);
            state.setValue(false);
        }
    } else {
        // ?
        view.setCondition(null);
        // ???(?????)
        TreeViewer tree = view.getScopeTreeComposite().getTreeViewer();

        // ????????
        if (((StructuredSelection) tree.getSelection()).getFirstElement() != null) {
            tree.setSelection(tree.getSelection()); // ??
        }
        // ?????????
        else {
            view.update(false);
        }
    }
    return null;
}

From source file:com.clustercontrol.monitor.view.action.StatusFilterAction.java

License:Open Source License

/**
 * [??]?????????//  w  ww  .java  2  s  .c  om
 * ???
 * <p>
 * <ol>
 * <li>[??]???</li>
 * <li>????????</li>
 * <li>[]??????</li>
 * <li>[]???</li>
 * </ol>
 *
 * @see org.eclipse.core.commands.IHandler#execute
 * @see com.clustercontrol.monitor.dialog.StatusFilterDialog
 * @see com.clustercontrol.monitor.view.StatusView#setCondition(Property)
 * @see com.clustercontrol.monitor.view.StatusView#update()
 */
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    this.window = HandlerUtil.getActiveWorkbenchWindow(event);
    // In case this action has been disposed
    if (null == this.window || !isEnabled()) {
        return null;
    }

    // ???
    this.viewPart = HandlerUtil.getActivePart(event);
    StatusView statusView = null;
    try {
        statusView = (StatusView) this.viewPart.getAdapter(StatusView.class);
    } catch (Exception e) {
        m_log.info("execute " + e.getMessage());
        return null;
    }

    if (statusView == null) {
        m_log.info("execute: view is null");
        return null;
    }

    ICommandService commandService = (ICommandService) window.getService(ICommandService.class);
    Command command = commandService.getCommand(ID);
    boolean isChecked = !HandlerUtil.toggleCommandState(command);

    if (isChecked) {
        // ?
        StatusFilterDialog dialog = new StatusFilterDialog(this.viewPart.getSite().getShell());

        // ??????????
        if (dialog.open() == IDialogConstants.OK_ID) {
            Property condition = dialog.getInputData();

            statusView.setCondition(condition);
            statusView.update(false);
        } else {
            State state = command.getState(RegistryToggleState.STATE_ID);
            state.setValue(false);
        }
    } else {
        // ?
        statusView.setCondition(null);

        // ???(?????)
        TreeViewer tree = statusView.getScopeTreeComposite().getTreeViewer();

        // ????????
        if (((StructuredSelection) tree.getSelection()).getFirstElement() != null) {
            tree.setSelection(tree.getSelection()); // ??
        }
        // ?????????
        else {
            statusView.update(false);
        }
    }
    return null;
}

From source file:com.gg.eclipse.closure.ui.properties.FolderSelectionDialog.java

License:Open Source License

protected void newFolderButtonPressed() {
    NewFolderDialog dialog = new NewFolderDialog(getShell(), fSelectedContainer) {
        protected Control createContents(Composite parent) {
            //PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IJavaHelpContextIds.BP_CREATE_NEW_FOLDER);
            return super.createContents(parent);
        }//from w  w  w. j  a va2  s .com
    };
    if (dialog.open() == Window.OK) {
        TreeViewer treeViewer = getTreeViewer();
        treeViewer.refresh(fSelectedContainer);
        Object createdFolder = dialog.getResult()[0];
        treeViewer.reveal(createdFolder);
        treeViewer.setSelection(new StructuredSelection(createdFolder));
    }
}

From source file:com.ibm.research.tours.dnd.TourDragNDropAdapter.java

License:Open Source License

@Override
public boolean performDrop(Object data) {
    externalMove = false;//from  w  ww  .ja  v a  2s .c om

    // Get the current drop target
    Object currentTarget = getCurrentTarget();
    ITour tour = fEditor.getTour();
    int dropIndex = 0;

    // If the drop target is null then default to the last element 
    if (currentTarget == null) {
        ITourElement[] elements = fEditor.getTour().getElements();

        if (elements.length == 0)
            currentTarget = fEditor.getTourTreeAdapter().getTourElements();
        else {
            currentTarget = elements[fEditor.getTour().getElementCount() - 1];
            dropIndex = fEditor.getTour().getElementCount();
        }
    }

    if (!(currentTarget instanceof ITourElement) && !(currentTarget instanceof TourElements))
        return false;

    // We are droping onto an existing tour element or the root tour elements container
    if (currentTarget instanceof ITourElement) {
        if (fDropTarget != null) {
            TreeItem parentItem = fDropTarget.getParentItem();
            int location = getCurrentLocation();

            for (int i = 0; i < parentItem.getItemCount(); i++) {
                if (fDropTarget == parentItem.getItem(i)) {
                    if (location == LOCATION_BEFORE)
                        dropIndex = i;
                    else if (location == LOCATION_ON)
                        dropIndex = i + 1;
                    else if (location == LOCATION_AFTER)
                        dropIndex = i + 1;

                    break;
                }
            }
        }
    }

    // At this point we have the target tour and the drop index
    // Now handle the elements
    ITourElement[] elements = null;

    // This transfer is coming from the 
    if (isPaletteTransfer) {
        IPaletteEntry[] entries = (IPaletteEntry[]) data;
        List<ITourElement> elementList = new ArrayList<ITourElement>();

        for (IPaletteEntry entry : entries) {
            ITourElement[] elementArray = entry.getProvider().createElements();

            if (elementArray != null) {
                for (ITourElement element : elementArray)
                    elementList.add(element);
            }
        }

        elements = elementList.toArray(new ITourElement[0]);
    } else if (isTourElementTransfer)
        elements = (ITourElement[]) data;
    else if (fAcceptingAdapter != null) {
        if (fAcceptingAdapter instanceof ITourElementDropAdapterExtension) {
            elements = ((ITourElementDropAdapterExtension) fAcceptingAdapter).convertDropData(data, tour);
        } else {
            elements = fAcceptingAdapter.convertDropData(data);
        }
    }

    if (elements == null || (elements != null && elements.length <= 0))
        return false;

    if (isTourElementTransfer) {
        if (getCurrentOperation() == DND.DROP_MOVE) {
            // Check if this is an internal drop, if so the elements must be removed more carefully
            // For external drops removal is handled in the drag listener
            if (fEditor.getTour().contins(elements[0])) {
                int earliestDragIndex = fEditor.getTour().getIndex(elements[0]);

                if (earliestDragIndex < dropIndex)
                // Awww here it goes! We need to correct the drop index based on the 
                // removal of dragged elements
                {
                    for (ITourElement e : elements) {
                        int index = fEditor.getTour().getIndex(e);

                        if (index < dropIndex)
                            dropIndex--;
                    }
                }
                fEditor.getTour().removeElements(elements);
            } else
                externalMove = true;
        } else if (getCurrentOperation() == DND.DROP_COPY) {
            Vector<ITourElement> clones = new Vector<ITourElement>();

            // For copies we replace the dragged elements with clones
            for (ITourElement e : elements) {
                clones.add(e.createClone());
            }

            elements = clones.toArray(new ITourElement[0]);
        }
    }

    tour.addElements(dropIndex, elements);

    TreeViewer viewer = (TreeViewer) getViewer();
    viewer.setExpandedState(fEditor.getTourTreeAdapter().getTourElements(), true);
    viewer.setSelection(new StructuredSelection(elements));

    fAcceptingAdapter = null;
    isTourElementTransfer = false;
    isPaletteTransfer = false;
    return true;
}

From source file:com.javadude.antxr.eclipse.ui.editor.outline.AntxrOutlinePage.java

License:Open Source License

/**
 * Select part of the grammar/*from  w ww  .  j  a  va2 s . c om*/
 * @param aLine the line to select
 * @param aForceSelect should we force the selection?
 */
public void selectSegment(int aLine, boolean aForceSelect) {
    if (aLine > 0) {
        TreeViewer viewer = getTreeViewer();
        ISegment segment = fEditor.getSegment(aLine);
        viewer.removeSelectionChangedListener(this);
        if (segment == null) {
            if (fSelectedSegmentID != null) {
                viewer.setSelection(new StructuredSelection());
                fEditor.resetHighlightRange();
                fSelectedSegmentID = null;
            }
        } else {
            if (aForceSelect || isDifferentSegment(segment)) {
                viewer.setSelection(new StructuredSelection(segment));
                fEditor.highlightSegment(segment, false);
                fSelectedSegmentID = segment.getUniqueID();
            }
            viewer.reveal(segment);
        }
        viewer.addSelectionChangedListener(this);
    }
}