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.schwiebert.cloudio.CloudOptionsComposite.java

License:Open Source License

protected Group addColorButtons(final Composite parent) {
    Group buttons = new Group(parent, SWT.SHADOW_IN);
    buttons.setLayout(new GridLayout(2, false));
    buttons.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    Label l = new Label(buttons, SWT.NONE);
    l.setText("Colors");
    GridData gd = new GridData();
    gd.horizontalSpan = 2;/*from  w w w . j a  v a  2 s. c  om*/
    l.setLayoutData(gd);
    final TreeViewer tv = new TreeViewer(buttons);
    Composite comp = new Composite(buttons, SWT.NONE);
    comp.setLayout(new RowLayout(SWT.VERTICAL));
    comp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true));
    tv.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    ListContentProvider cp = new ListContentProvider();
    tv.setContentProvider(cp);
    tv.setLabelProvider(new ColumnLabelProvider() {

        private Map<Object, Image> images = new HashMap<Object, Image>();

        @Override
        public Image getImage(Object element) {
            Image image = images.get(element);
            if (image == null) {
                Color color = new Color(Display.getDefault(), (RGB) element);
                image = new Image(Display.getDefault(), 24, 24);
                GC gc = new GC(image);
                gc.setBackground(color);
                gc.fillRoundRectangle(0, 0, 24, 24, 3, 3);
                color.dispose();
                gc.dispose();
                images.put(element, image);
            }
            return image;
        }

        @Override
        public void dispose() {
            Collection<Image> images = this.images.values();
            for (Image image : images) {
                image.dispose();
            }
            this.images.clear();
        }

    });
    initColors();
    tv.setInput(colors);
    Button add = new Button(comp, SWT.FLAT);
    add.setImage(Activator.getDefault().getImageRegistry().get(Activator.ADD));
    add.setToolTipText("Add color...");
    add.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            ColorDialog cd = new ColorDialog(parent.getShell());
            RGB color = cd.open();
            if (color != null) {
                colors.add(color);
                tv.setInput(colors);
                updateColors();
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    Button remove = new Button(comp, SWT.FLAT);
    remove.setToolTipText("Remove selected colors");
    remove.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) tv.getSelection();
            colors.removeAll(selection.toList());

            tv.setInput(colors);
            updateColors();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    remove.setImage(Activator.getDefault().getImageRegistry().get(Activator.REMOVE));
    Button toggle = new Button(comp, SWT.FLAT);
    toggle.setToolTipText("Toggle Colors");
    toggle.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            nextColors();
            tv.setInput(colors);
            updateColors();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    toggle.setImage(Activator.getDefault().getImageRegistry().get(Activator.TOGGLE_COLORS));

    comp = new Composite(buttons, SWT.NONE);
    gd = new GridData(SWT.FILL, SWT.FILL, true, false);
    gd.horizontalSpan = 2;
    comp.setLayout(new GridLayout(2, true));
    comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    Button bg = new Button(comp, SWT.NONE);
    bg.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    bg.setText("Background Color");
    bg.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            ColorDialog cd = new ColorDialog(parent.getShell());
            RGB color = cd.open();
            if (color == null)
                return;
            Color old = viewer.getCloud().getBackground();
            Color c = new Color(Display.getDefault(), color);
            viewer.getCloud().setBackground(c);
            old.dispose();
            viewer.getCloud().redrawTextLayerImage();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    Button sel = new Button(comp, SWT.NONE);
    sel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    sel.setText("Selection Color");
    sel.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            ColorDialog cd = new ColorDialog(parent.getShell());
            RGB color = cd.open();
            if (color == null)
                return;
            Color old = viewer.getCloud().getSelectionColor();
            Color c = new Color(Display.getDefault(), color);
            viewer.getCloud().setSelectionColor(c);
            old.dispose();
            viewer.getCloud().redrawTextLayerImage();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    return buttons;
}

From source file:org.splevo.ui.refinementbrowser.RefinementDetailsView.java

License:Open Source License

/**
 * initialize the context menu./*from w  ww. java2s . co m*/
 * 
 * DesignDecision Menu created programaticaly instead of extension point to prevent context menu
 * mess up by other plugins.
 * 
 * DesignDecision Reused command for common look and feel of context menu item for complete
 * application
 * 
 * @param viewer
 *            The viewer to register menu for.
 * @param site
 *            The workbench part to link the selection provider.
 */
private void initContextMenu(final TreeViewer viewer, IWorkbenchPartSite site) {

    MenuManager menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);
    menuManager.addMenuListener(new IMenuListener() {
        @Override
        public void menuAboutToShow(IMenuManager manager) {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            if (!selection.isEmpty() && selection.getFirstElement() instanceof Refinement) {
                Action action = new RenameRefinementAction(viewer);
                manager.add(action);
                action = new RefinementEditDescriptionAction(viewer);
                manager.add(action);
            }
        }
    });
    menuManager.addMenuListener(new CommandActionMenuListener(COMMAND_ID_OPENSOURCELOCATION,
            SPLevoUIPlugin.getImageDescriptor("icons/jcu_obj.gif")));
    menuManager.addMenuListener(new CommandActionMenuListener(COMMAND_ID_OPENSOURCELOCATIONDIFF,
            SPLevoUIPlugin.getImageDescriptor("icons/unifieddiff-editor.gif")));
    menuManager.addMenuListener(new CommandActionMenuListener("org.splevo.ui.commands.argouml.variantscan",
            SPLevoUIPlugin.getImageDescriptor("icons/kopl_circle_only.png")));

    Menu menu = menuManager.createContextMenu(viewer.getTree());
    viewer.getTree().setMenu(menu);
    site.setSelectionProvider(viewer);
}

From source file:org.spotter.eclipse.ui.handlers.DeleteHandler.java

License:Apache License

private Iterator<?> getSelectionIterator() {
    Activator activator = Activator.getDefault();
    TreeViewer viewer = activator.getNavigatorViewer();
    if (viewer == null) {
        return null;
    }//from   www .  j a  v a  2s.c o  m

    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    return selection.isEmpty() ? null : selection.iterator();
}

From source file:org.spotter.eclipse.ui.handlers.DuplicateHandler.java

License:Apache License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Activator activator = Activator.getDefault();
    TreeViewer viewer = activator.getNavigatorViewer();
    if (viewer == null) {
        return null;
    }//  ww w. j a  v a2s  .c  o m

    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    Iterator<?> iter = selection.iterator();
    while (iter.hasNext()) {
        SpotterUtils.duplicateNavigatorElement(iter.next());
    }

    return null;
}

From source file:org.spotter.eclipse.ui.handlers.DuplicateHandler.java

License:Apache License

/**
 * Returns <code>true</code> when exactly one duplicatable is selected.
 * //from  ww  w .  j  a  va 2  s . c o m
 * @return <code>true</code> when exactly one duplicatable is selected
 */
@Override
public boolean isEnabled() {
    Activator activator = Activator.getDefault();
    TreeViewer viewer = activator.getNavigatorViewer();
    if (viewer == null) {
        return false;
    }

    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.size() != 1) {
        return false;
    }

    // check if selected element is duplicatable
    Object element = selection.iterator().next();
    boolean isDuplicatable = (element instanceof IDuplicatable);

    return isDuplicatable;
}

From source file:org.spotter.eclipse.ui.handlers.OpenHandler.java

License:Apache License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Activator activator = Activator.getDefault();
    TreeViewer viewer = activator.getNavigatorViewer();
    if (viewer == null) {
        return null;
    }/* w ww.j a  v  a 2s  .c om*/

    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    Iterator<?> iter = selection.iterator();
    while (iter.hasNext()) {
        SpotterUtils.openNavigatorElement(iter.next());
    }

    return null;
}

From source file:org.spotter.eclipse.ui.handlers.OpenHandler.java

License:Apache License

/**
 * Returns <code>true</code> if only elements are selected that are
 * openable.//from w ww  . j  a  va 2s  .  c o  m
 * 
 * @return <code>true</code> if only elements are selected that are openable
 */
@Override
public boolean isEnabled() {
    Activator activator = Activator.getDefault();
    TreeViewer viewer = activator.getNavigatorViewer();
    if (viewer == null) {
        return false;
    }

    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.isEmpty()) {
        return false;
    }
    Iterator<?> iter = selection.iterator();

    while (iter.hasNext()) {
        if (!(iter.next() instanceof IOpenableProjectElement)) {
            return false;
        }
    }

    return true;
}

From source file:org.springframework.ide.eclipse.boot.dash.cloudfoundry.deployment.DeploymentPropertiesDialog.java

License:Open Source License

private ITreeSelection getStructuredSelection(TreeViewer treeViewer) {
    ISelection selection = treeViewer.getSelection();
    if (selection instanceof ITreeSelection) {
        return (ITreeSelection) selection;
    }/*from   w  w  w . j a  v  a  2 s .  co m*/
    throw new ClassCastException(
            "AbstractTreeViewer should return an instance of ITreeSelection from its getSelection() method."); //$NON-NLS-1$
}

From source file:org.springframework.tooling.ls.eclipse.gotosymbol.dialogs.GotoSymbolDialog.java

License:Open Source License

/**
 * Determine the 'target' for the dialog's action.
 *//*w w w .  j  a va  2 s  . co  m*/
private SymbolInformation getTarget(TreeViewer list) {
    ISelection sel = list.getSelection();
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection ss = (IStructuredSelection) sel;
        Object selected = ss.getFirstElement();
        if (selected instanceof Match) {
            SymbolInformation si = getSymbolInformation((Match<?>) selected);
            if (si != null) {
                return si;
            }
        }
    }
    //No element selected, target the first element in the list instead.
    //This allows user to execute the action without explicitly selecting an element.
    return getFirstElement(list);
}

From source file:org.springframework.tooling.ls.eclipse.gotosymbol.dialogs.GotoSymbolDialog.java

License:Open Source License

private void installWidgetListeners(Text pattern, TreeViewer list) {
    pattern.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.keyCode == SWT.ARROW_DOWN) {
                if (list.getTree().getItemCount() > 0) {
                    list.getTree().setFocus();
                    TreeItem[] items = list.getTree().getItems();
                    if (items != null && items.length > 0) {
                        list.getTree().setSelection(items[0]);
                        //programatic selection may not fire selection events so...
                        list.getTree().notifyListeners(SWT.Selection, new Event());
                    }/*from ww  w .  ja v  a2s.c om*/
                }
            } else if (e.character == '\r') {
                performOk(list);
            }
        }
    });

    list.getTree().addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {

            if (e.keyCode == SWT.ARROW_UP && (e.stateMask & SWT.SHIFT) == 0 && (e.stateMask & SWT.CTRL) == 0) {
                StructuredSelection selection = (StructuredSelection) list.getSelection();

                if (selection.size() == 1) {
                    Object element = selection.getFirstElement();
                    if (element.equals(getFirstElement(list))) {
                        pattern.setFocus();
                        list.setSelection(new StructuredSelection());
                        list.getTree().notifyListeners(SWT.Selection, new Event());
                    }

                }
            } else if (e.character == '\r') {
                performOk(list);
            }

            //            if (e.keyCode == SWT.ARROW_DOWN
            //                  && (e.stateMask & SWT.SHIFT) != 0
            //                  && (e.stateMask & SWT.CTRL) != 0) {
            //
            //               list.getTree().notifyListeners(SWT.Selection, new Event());
            //            }

        }
    });

    list.addDoubleClickListener(e -> performOk(list));
}