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:org.eclipse.mylyn.internal.java.PackageExplorerManager.java

License:Open Source License

public void selectionChanged(IWorkbenchPart part, ISelection changedSelection) {
    if (!(part instanceof PackageExplorerPart)) {
        return;/*from  ww  w .ja  v a2  s .co m*/
    }
    AbstractFocusViewAction applyAction = AbstractFocusViewAction.getActionForPart((PackageExplorerPart) part);
    if (!ContextCorePlugin.getContextManager().isContextActive()
            || (applyAction != null && !applyAction.isChecked())) {
        return;
    }
    try {
        Object elementToSelect = null;
        if (changedSelection instanceof TextSelection && part instanceof JavaEditor) {
            TextSelection textSelection = (TextSelection) changedSelection;
            IJavaElement javaElement = SelectionConverter.resolveEnclosingElement((JavaEditor) part,
                    textSelection);
            if (javaElement != null)
                elementToSelect = javaElement;
        } else if (changedSelection instanceof TextSelection) {
            //            if (part instanceof EditorPart) {
            //               elementToSelect = ((EditorPart) part).getEditorInput().getAdapter(IResource.class);
            //            }
        } else {
            return;
        }

        if (elementToSelect != null) {
            PackageExplorerPart packageExplorer = PackageExplorerPart.getFromActivePerspective();
            if (packageExplorer != null) {
                TreeViewer viewer = packageExplorer.getTreeViewer();
                StructuredSelection currentSelection = (StructuredSelection) viewer.getSelection();
                if (currentSelection.size() <= 1) {
                    boolean membersFilteredMode = false;
                    for (ViewerFilter filter : Arrays.asList(viewer.getFilters())) {
                        if (filter instanceof JavaDeclarationsFilter)
                            membersFilteredMode = true;
                    }
                    if (membersFilteredMode) {
                        if (elementToSelect instanceof IMember) {
                            ICompilationUnit toSelect = ((IMember) elementToSelect).getCompilationUnit();
                            if (toSelect != null) {
                                viewer.setSelection(new StructuredSelection(toSelect), true);
                            }
                        }
                    } else if (elementToSelect != null) {
                        if (!elementToSelect.equals(currentSelection.getFirstElement())) {
                            viewer.setSelection(new StructuredSelection(elementToSelect), true);
                        }
                    }
                }
                //                  if (elementToSelect != null
                //                        && MylarJavaPlugin.getDefault().getPluginPreferences().getBoolean(
                //                              MylarJavaPrefConstants.PACKAGE_EXPLORER_AUTO_EXPAND)) {
                //                     viewer.expandAll();
                //                  }
            }
        }
    } catch (Throwable t) {
        MylarStatusHandler.log(t, "Could not update package explorer");
    }
}

From source file:org.eclipse.mylyn.internal.sandbox.ui.planner.TaskActivityEditorPart.java

License:Open Source License

private void fillContextMenu(TreeViewer viewer, IMenuManager manager) {
    if (!viewer.getSelection().isEmpty()) {
        manager.add(new OpenTaskEditorAction(viewer));
        manager.add(new RemoveTaskAction(viewer));
        manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    } else {/*  ww w  .j a v a 2s  .  c  o m*/
        manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    }
}

From source file:org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal.java

License:Open Source License

public static void preservingSelection(final TreeViewer viewer, Runnable runnable) {
    final ISelection selection = viewer.getSelection();

    runnable.run();//from w ww.  jav  a  2 s.  c  o m

    if (selection != null) {
        ISelection newSelection = viewer.getSelection();
        if ((newSelection == null || newSelection.isEmpty()) && !(selection == null || selection.isEmpty())) {
            // delay execution to ensure that any delayed tree updates such as expand all have been processed and the selection is revealed properly
            Display.getDefault().asyncExec(new Runnable() {
                public void run() {
                    viewer.setSelection(selection, true);
                }
            });
        } else if (newSelection instanceof ITreeSelection && !newSelection.isEmpty()) {
            viewer.reveal(((ITreeSelection) newSelection).getFirstElement());
        }
    }
}

From source file:org.eclipse.nebula.treemapper.TreeMapper.java

License:Open Source License

/**
 * @param sourceTreeViewer//  w  w  w . ja v a 2s . co m
 * @param targetTreeViewer
 * @param direction
 */
private void bindTreeForDND(final TreeViewer sourceTreeViewer, final TreeViewer targetTreeViewer,
        final int direction) {
    final LocalSelectionTransfer sourceTransfer = LocalSelectionTransfer.getTransfer();
    final LocalSelectionTransfer targetTransfer = LocalSelectionTransfer.getTransfer();
    sourceTreeViewer.addDragSupport(DND.DROP_LINK, new Transfer[] { sourceTransfer },
            new TreeDragSourceEffect(sourceTreeViewer.getTree()) {
                @Override
                public void dragStart(DragSourceEvent event) {
                    event.doit = !sourceTreeViewer.getSelection().isEmpty();
                }
            });
    targetTreeViewer.addDropSupport(DND.DROP_LINK, new Transfer[] { targetTransfer },
            new TreeDropTargetEffect(targetTreeViewer.getTree()) {
                @Override
                public void dragEnter(DropTargetEvent event) {
                    event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL | DND.FEEDBACK_SELECT;
                    event.detail = DND.DROP_LINK;
                    super.dragEnter(event);
                }

                @Override
                public void drop(DropTargetEvent event) {
                    performMappingByDrop(sourceTreeViewer, sourceTreeViewer.getSelection(), targetTreeViewer,
                            (TreeItem) getItem(event.x, event.y), direction);
                }
            });
}

From source file:org.eclipse.oomph.setup.ui.actions.AbstractContainerAction.java

License:Open Source License

protected final void expandItem(final EObject object) {
    if (selectionProvider instanceof IViewerProvider) {
        IViewerProvider viewerProvider = (IViewerProvider) selectionProvider;
        final Viewer viewer = viewerProvider.getViewer();
        if (viewer instanceof TreeViewer) {
            UIUtil.getDisplay().asyncExec(new Runnable() {
                public void run() {
                    TreeViewer treeViewer = (TreeViewer) viewer;
                    expand(treeViewer, object);

                    IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();

                    @SuppressWarnings("unchecked")
                    List<Object> list = selection.toList();
                    list = new ArrayList<Object>(list);
                    list.add(object);//ww w.j a  v a  2 s  .  c o m

                    treeViewer.setSelection(new StructuredSelection(list));
                }

                private void expand(TreeViewer treeViewer, EObject object) {
                    treeViewer.setExpandedState(object, true);

                    EObject eContainer = object.eContainer();
                    if (eContainer != null) {
                        expand(treeViewer, eContainer);
                    }
                }
            });
        }
    }
}

From source file:org.eclipse.oomph.setup.ui.wizards.ConfirmationPage.java

License:Open Source License

private void connectMasterDetail(final TreeViewer master, final Viewer detail) {
    master.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            if (detail != null) {
                Object selection = ((IStructuredSelection) master.getSelection()).getFirstElement();
                Control control = detail.getControl();
                try {
                    control.setRedraw(false);
                    detail.setInput(selection);
                    columnResizer.controlResized(null);
                } finally {
                    control.setRedraw(true);
                }/*from  w  ww. j  av  a2  s.com*/
            }
        }
    });
}

From source file:org.eclipse.osee.framework.ui.swt.TreeViewerUtility.java

License:Open Source License

@SuppressWarnings("unchecked")
private static <A extends Object> void populateSelectionHash(TreeViewer tree, Map<A, Object> selectionHash) {
    Iterator<?> iterator = ((IStructuredSelection) tree.getSelection()).iterator();

    while (iterator.hasNext()) {
        selectionHash.put((A) iterator.next(), dummy);
    }//ww  w  . ja  v  a 2s. c o  m
}

From source file:org.eclipse.papyrus.diagram.common.service.palette.StereotypePostAction.java

License:Open Source License

/**
 * Action done when the add button is pressed
 * // w  ww. j  a  v a2s.c  om
 * @param tree
 * 
 */
protected void performAddButtonPressed(TreeViewer tree) {
    TreeSelection selection = (TreeSelection) tree.getSelection();
    Object selectedElement = selection.getFirstElement();
    if (selection != null && selection.size() == 1 && !(selectedElement instanceof StereotypeRepresentation)) {
        if (selectedElement instanceof PropertyRepresentation) {
            //We want add a value to a multi-valued property or edit a value to a mono-valued property
            PropertyRepresentation prop = (PropertyRepresentation) selectedElement;
            if (prop.getUpperMultiplicity() != 1) {
                editMe(prop, null);//we add a value
            } else if (!prop.getValues().isEmpty()) { //mono-valued property and the valu exists
                editMe(prop, prop.getValues().get(0));
            } else {//monovalued property, the value doesn't exist
                editMe(prop, null);
            }
            tree.refresh(selectedElement);
        }
        return;
    }

    // pops-up a dialog window where users can select the additionnal stereotypes to apply
    // it needs the metaclass information...
    // for this, entry Proxy should be a Palette entry proxy
    if (!(entryProxy instanceof IPaletteAspectToolEntryProxy)) {
        return;
    }

    PaletteEntry entry = ((IPaletteAspectToolEntryProxy) entryProxy).getEntry();
    if (!(entry instanceof ToolEntry)) {
        return;
    }

    // this is a PaletteAspectToolEntryProxy
    // try to find the metaclass on which the element is working
    EClass metaClass = PaletteUtil.getToolMetaclass((ToolEntry) entry);

    if (metaClass == null) {
        Activator.log.error("Impossible to find metaclass", null); //$NON-NLS-1$
        return;
    }

    // retrieve the original stereotype list from the qualified names
    List<Stereotype> stereotypeList = config.getUMLStereotypes();//retrieveStereotypesFromQualifiedNames();

    // we have all applied profiles, the list of already applied stereotypes, the metaclass of the created element...
    // just have to open a selection dialog
    final CheckedTreeSelectionDialog selectionDialog = new CheckedTreeSelectionDialog(
            DisplayUtils.getDisplay().getActiveShell(), editorLabelProvider,
            new ProfileContentProvider(metaClass));
    selectionDialog.setTitle(Messages.StereotypePostAction_StereotypeSelectionTitle);
    selectionDialog.setMessage(Messages.StereotypePostAction_StereotypeSelectionMessage);
    selectionDialog.setContainerMode(true);
    selectionDialog.setInitialElementSelections(stereotypeList);
    selectionDialog.setInput(config.getAppliedProfiles());
    selectionDialog.setExpandedElements(config.getAppliedProfiles().toArray());

    ViewerComparator comparator = new ViewerComparator();
    selectionDialog.setComparator(comparator);

    if (CheckedTreeSelectionDialog.OK == selectionDialog.open()) {
        // update the list of stereotype to apply
        Object[] result = selectionDialog.getResult();
        ArrayList<String> stereotypesToApply = new ArrayList<String>();
        for (int i = 0; i < result.length; i++) {
            if (result[i] instanceof Stereotype) {
                stereotypesToApply.add(((Stereotype) result[i]).getQualifiedName());

            }
        }
        config.setStereotypesRepresentations(stereotypesToApply);
        //it's not possible to use refresh or update here!
        updateStereotypeViewer();
    }

}

From source file:org.eclipse.papyrus.diagram.common.service.palette.StereotypePostAction.java

License:Open Source License

/**
 * Action done when the remove button is pressed
 * //from  w w w .  ja  v a2 s. c o  m
 * @param viewer
 *        the viewer on which the remove action is applied
 */
protected void performRemoveAction(TreeViewer viewer) {

    ITreeSelection selection = (ITreeSelection) viewer.getSelection();
    if (selection != null) {
        List<?> selectedElements = ((TreeSelection) selection).toList();
        if (sameLevelForFullSelection(selection)) {
            //all the element have the same type 
            if (selection.getFirstElement() instanceof StereotypeRepresentation) {
                for (Object object : selectedElements) {
                    if (object instanceof StereotypeRepresentation) {
                        config.removeStereotype((StereotypeRepresentation) object);
                    }
                }

            } else if (selection.getFirstElement() instanceof PropertyRepresentation) {
                //we delete the value for mono-valued property
                for (Object object : selectedElements) {
                    if (object instanceof PropertyRepresentation) {
                        if (!((PropertyRepresentation) object).getUMLProperty().isMultivalued()) {
                            ((PropertyRepresentation) object).getValues().remove(0);
                        }
                    }
                }
            } else if (selection.getFirstElement() instanceof Value) {
                //we remove value from a multivalued property
                for (Object object : selectedElements) {
                    if (object instanceof Value) {
                        ((Value) object).getPropertyRepresentation().removeValue((Value) object);
                    }
                }
            }
            viewer.refresh();
        }
    }
}

From source file:org.eclipse.papyrus.diagram.common.service.palette.StereotypePostAction.java

License:Open Source License

/**
 * Action to do when the F2 key is pressed
 * <ul>/*from  w  w  w . jav a2 s.c  o m*/
 * <li>open a CellEditor for the PrimitiveType and DataType</li>
 * <li>open a popup editor for others types</li>
 * </ul>
 * 
 * @param viewer
 *        the viewer on which the F2 action is applied
 */
protected void performF2Action(TreeViewer viewer) {

    TreeSelection selection = (TreeSelection) viewer.getSelection();
    if ((selection != null) && (selection.size() == 1)) {
        Object objectToEdit = selection.getFirstElement();
        if (objectToEdit instanceof PropertyRepresentation) {
            Property prop = ((PropertyRepresentation) objectToEdit).getUMLProperty();
            if (prop.isMultivalued() == false) {
                if ((prop.getType() instanceof PrimitiveType) || (prop.getType() instanceof DataType)) {
                    viewer.editElement(prop, 0);
                } else {
                    //TODO
                }
            }
        } else if (objectToEdit instanceof Value) {
            PropertyRepresentation propRep = ((Value) objectToEdit).getPropertyRepresentation();
            Type type = propRep.getType();
            if (type != null) {
                if (type instanceof PrimitiveType || type instanceof DataType) {
                    viewer.editElement(objectToEdit, 0);

                    //the refresh is do by the cellEditor! (if the resfresh is do here, there is a bug!)
                    //viewer.refresh(property);
                } else {//popupEditor
                    editMe(propRep, (Value) objectToEdit);
                    viewer.refresh(propRep);
                }
            }
        }
    }

}