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

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

Introduction

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

Prototype

@Override
    public void setSelection(ISelection selection, boolean reveal) 

Source Link

Usage

From source file:com.aptana.ide.debug.internal.ui.dialogs.AddExceptionBreakpointDialog.java

License:Open Source License

private boolean createBreakpoint() {
    final Object[] selected = getSelectedElements();
    if (selected.length != 1) {
        return false;
    }/*from  www .j  av a 2  s.  c o  m*/

    new Job(Messages.AddExceptionBreakpointDialog_AddJavaScriptExceptionBreakpoint) {

        protected IStatus run(IProgressMonitor monitor) {
            try {
                IResource resource = null;
                if (resource == null) {
                    resource = ResourcesPlugin.getWorkspace().getRoot();
                }
                IJSExceptionBreakpoint breakpoint = JSDebugModel.createExceptionBreakpoint(resource,
                        (String) selected[0]);
                final List<IBreakpoint> list = new ArrayList<IBreakpoint>(1);
                list.add(breakpoint);
                Runnable r = new Runnable() {
                    public void run() {
                        IViewPart part = DebugUiPlugin.getActivePage()
                                .findView(IDebugUIConstants.ID_BREAKPOINT_VIEW);
                        if (part instanceof IDebugView) {
                            Viewer viewer = ((IDebugView) part).getViewer();
                            if (viewer instanceof StructuredViewer) {
                                StructuredViewer sv = (StructuredViewer) viewer;
                                sv.setSelection(new StructuredSelection(list), true);
                            }
                        }
                    }
                };
                DebugUiPlugin.getStandardDisplay().asyncExec(r);
                return Status.OK_STATUS;
            } catch (CoreException e) {
                updateStatus(e.getStatus());
                return Status.CANCEL_STATUS;
            }
        }

    }.schedule();
    return true;
}

From source file:com.aptana.js.debug.ui.internal.dialogs.AddExceptionBreakpointDialog.java

License:Open Source License

private boolean createBreakpoint() {
    final Object[] selected = getSelectedElements();
    if (selected.length != 1) {
        return false;
    }/*w  ww. ja va 2 s . c o  m*/

    new Job(Messages.AddExceptionBreakpointDialog_AddJavaScriptExceptionBreakpoint) {

        protected IStatus run(IProgressMonitor monitor) {
            try {
                IResource resource = null;
                if (resource == null) {
                    resource = ResourcesPlugin.getWorkspace().getRoot();
                }
                IJSExceptionBreakpoint breakpoint = JSDebugModel.createExceptionBreakpoint(resource,
                        (String) selected[0]);
                final List<IBreakpoint> list = new ArrayList<IBreakpoint>(1);
                list.add(breakpoint);
                Runnable r = new Runnable() {
                    public void run() {
                        IViewPart part = UIUtils.getActivePage().findView(IDebugUIConstants.ID_BREAKPOINT_VIEW);
                        if (part instanceof IDebugView) {
                            Viewer viewer = ((IDebugView) part).getViewer();
                            if (viewer instanceof StructuredViewer) {
                                StructuredViewer sv = (StructuredViewer) viewer;
                                sv.setSelection(new StructuredSelection(list), true);
                            }
                        }
                    }
                };
                DebugUiPlugin.getStandardDisplay().asyncExec(r);
                return Status.OK_STATUS;
            } catch (CoreException e) {
                updateStatus(e.getStatus());
                return Status.CANCEL_STATUS;
            }
        }

    }.schedule();
    return true;
}

From source file:com.netxforge.netxstudio.screens.editing.util.CDOMementoUtil.java

License:Open Source License

/**
 * Retrieve a Combo viewer./*  w ww  .  j  a v  a 2  s  . co m*/
 * 
 * @param memento
 * @param viewer
 * @param key
 */
public static void retrieveStructuredViewerSelection(IMemento memento, StructuredViewer viewer, String key,
        CDOView view) {
    IStructuredSelection retrieveSelection = retrieveSelection(memento, key, view);
    if (retrieveSelection != null) {
        viewer.setSelection(retrieveSelection, true);
    }
}

From source file:com.nokia.tools.variant.confml.ui.editors.CompositeSelectionProvider.java

License:Open Source License

public void setSelection(ISelection selection) {
    setting = true;/*from   ww  w.j  a  v  a 2  s.co m*/
    for (StructuredViewer provider : selectionProviders) {
        provider.setSelection(selection, true);
    }
    setting = false;
}

From source file:it.rcpvision.emf.components.editors.EmfAbstractEditor.java

License:Open Source License

public void setSelectionOnRoot(StructuredViewer viewer) {
    if (!editingDomain.getResourceSet().getResources().isEmpty()) {
        // Select the root object in the view.
        viewer.setSelection(new StructuredSelection(editingDomain.getResourceSet().getResources().get(0)),
                true);/*from  w  w  w  .j a  v a2 s  . co  m*/
    }
}

From source file:org.eclipse.datatools.sqltools.sqlbuilder.views.TableNavigator.java

License:Open Source License

public TableNavigator(Table table, StructuredViewer viewer) {
    super(table, SWT.NONE);
    this.table = table;
    final Table currentTable = table;
    final StructuredViewer sViewer = viewer;

    // Linux index out of bounds fix.  See defect 253429, 253433, and more
    setVisible(false);//  w  w  w  .  j a  va 2  s  .  c  o m

    addPaintListener(viewer);
    addKeyListeners(viewer);
    addMouseListeners(viewer);
    addSelectionListener(new SelectionAdapter() {
        /**
        * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(SelectionEvent)
        */
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            currentTable.setSelection(new TableItem[] { getRow() });
            if (sViewer instanceof TableTreeViewer) {
                TableTreeItem tableTreeItem = (TableTreeItem) getRow().getData(TABLETREEITEM_ID);
                StructuredSelection selection = new StructuredSelection(tableTreeItem.getData());
                sViewer.setSelection(selection, true);
            }
        }

    });
    addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent e) {
            // if e.source is not a child of the table then set selection - this is for tab into viewer
            Object eventSource = e.getSource();
            if (eventSource instanceof Control) {
                if (!isChild(currentTable, (Control) eventSource)) {
                    if (currentTable.getItemCount() > 0 && currentTable.getSelectionCount() <= 0) {
                        if (sViewer instanceof TableTreeViewer) {
                            TableTreeItem tableTreeItem = (TableTreeItem) getRow().getData(TABLETREEITEM_ID);
                            StructuredSelection selection = new StructuredSelection(tableTreeItem.getData());
                            sViewer.setSelection(selection, true);
                        } else {
                            currentTable.setSelection(0);
                            setSelection(0, 0);
                        }
                        // Since TableCusor.setSelection() doesn't notify listeners, we
                        // need to do it so listeners are notified of selection
                        notifyListeners(SWT.Selection, new Event());

                    }
                } else {
                    if (currentTable.getItems().length > 0) {
                        // cursor can end up on a non-existent table row 
                        //   currently no way to get the current table cursor row 
                        //   so for now just catch the exception since it doesn't
                        //   cause any side effects.
                        try {
                            setVisible(true);
                        } catch (Exception ee) {
                            currentTable.setSelection(0);
                            setSelection(0, 0);
                        }
                    } else // do not show table cursor if there are no elements in the table - avoid repaint
                    {
                        setVisible(false);
                    }
                }
            }
        }

        protected boolean isChild(Control parent, Control child) {
            Control tempChild = child;
            while (tempChild != null) {
                if (tempChild == parent) {
                    return true;
                }
                tempChild = tempChild.getParent();
            }
            return false;
        }

        /**
         * @see org.eclipse.swt.events.FocusAdapter#focusLost(FocusEvent)
         */
        public void focusLost(FocusEvent e) {
            // Set the table navigator to be not visible if the the table
            // is not in focus and a child of the table is not in focus
            // note that we do this asynchronously so we don't mess up the
            // current focus handling.
            Display.getDefault().asyncExec(new Runnable() {
                /**
                 * @see java.lang.Runnable#run()
                 */
                public void run() {
                    if (currentTable != null && !currentTable.isDisposed() && !currentTable.isFocusControl()
                            && !isChild(currentTable, Display.getDefault().getFocusControl())) {
                        setVisible(false);
                    }
                }
            });
        }

    });

    table.addFocusListener(new FocusAdapter() {
        /**
         * @see org.eclipse.swt.events.FocusListener#focusGained(FocusEvent)
         */
        public void focusGained(FocusEvent e) {
            // only display navigator if there are items in the table 
            // and if the focus wasn't gained from our own table navigator
            // (ie focus came from outside)
            if (currentTable.getItemCount() > 0 && (Display.getDefault().getFocusControl() != null)
                    && !Display.getDefault().getFocusControl().equals(TableNavigator.this)) {
                // note that we do this asynchronously so we don't mess up the
                // current focus handling.
                Display.getDefault().asyncExec(new Runnable() {
                    /**
                     * @see java.lang.Runnable#run()
                     */
                    public void run() {
                        if (!isVisible()) {
                            // If no row has been selected, make sure there is one
                            // Bugzilla bug 3116
                            if (TableNavigator.this.getRow() == null && currentTable.getItemCount() > 0) {
                                currentTable.setSelection(0);
                                setSelection(0, 0);
                                // Since TableCusor.setSelection() doesn't notify listeners, we
                                // need to do it so listeners are notified of selection
                                notifyListeners(SWT.Selection, new Event());
                            }

                            try {
                                setVisible(true);
                                setFocus();
                            } catch (Exception e) {
                                // catch IllegalArgumentExceptions here - index out of bounds on tableviewer
                                if (currentTable.getItemCount() > 0) {
                                    currentTable.setSelection(0);
                                    setSelection(0, 0);
                                } else // do not show table cursor if there are no elements in the table - avoid repaint
                                {
                                    setVisible(false);
                                }
                            }
                        }
                    }
                });
            }
        }
    });
}

From source file:org.eclipse.mylyn.context.ui.AbstractFocusViewAction.java

License:Open Source License

protected void select(StructuredViewer viewer, ISelection selection) {
    viewer.setSelection(selection, true);
}

From source file:org.eclipse.net4j.util.ui.views.MultiViewersView.java

License:Open Source License

public void selectReveal(ISelection selection) {
    StructuredViewer viewer = getCurrentViewer();
    if (viewer != null) {
        viewer.setSelection(selection, true);
    }//from  ww w .ja va  2  s .c  o m
}

From source file:org.eclipse.oomph.internal.ui.FindAndReplaceTarget.java

License:Open Source License

/**
 * This records a match either initially or as a result of a find.
 *///w  w w. j  a v  a  2s . c  o  m
protected void setSelection(boolean preserve, StructuredViewer viewer, Data.Item item, final int start,
        Pattern pattern) {
    Object selectedObjectToUpdate = selectedItem != null && selectedItem.itemPropertyDescriptor == null
            ? selectedItem.data.object
            : null;

    // Remember the information about the item, pattern, and offset within the item of the match.
    selectedItem = item;
    selectedItemPattern = pattern;
    selectedItemStart = start;

    if (selectedObjectToUpdate != null) {
        viewer.update(selectedObjectToUpdate, null);
    }

    // There is no special tree item anymore.
    specialTreeItem = null;

    // If we haven't already done so, hook up the special label provider for providing selection feedback.
    hookLabelProvider();

    // Clean any previous stuff we did to decorate the properties view.
    propertiesCleanup();

    // In replace all mode, we don't want to provide any further feedback.
    if (replaceAllCommand == null) {
        // Select the item in the viewer, unless we're preserving the selection, i.e., during the initial feedback.
        StructuredSelection selection = new StructuredSelection(new TreePath(item.data.getPath()));
        if (!preserve) {
            viewer.setSelection(selection, true);
        }

        // If there is an active property page, update it's selection immediately.
        PropertySheetPage activePropertySheetPage = getActivePropertySheetPage();
        if (activePropertySheetPage != null) {
            activePropertySheetPage.selectionChanged(workbenchPart, selection);
        }

        // Make the properties view visible, creating it if necessary.
        IWorkbenchPartSite site = workbenchPart.getSite();
        IWorkbenchPage page = site.getPage();
        IViewPart viewPart = page.findView("org.eclipse.ui.views.PropertySheet");

        // Sometimes showing the properties view gives it focus, e.g., when the editor is maximized.
        Display display = site.getShell().getDisplay();
        Control oldFocusControl = display.getFocusControl();
        try {
            // Ignore scope changes while showing the properties view.
            suspendScopeChanges = true;
            if (item.itemPropertyDescriptor != null) {
                viewPart = page.showView("org.eclipse.ui.views.PropertySheet", null,
                        IWorkbenchPage.VIEW_VISIBLE);
                if (viewPart == null) {
                    viewPart = page.showView("org.eclipse.ui.views.PropertySheet", null,
                            IWorkbenchPage.VIEW_CREATE);
                }
            }

            // Restore the focus.
            Control newFocusControl = display.getFocusControl();
            if (oldFocusControl != newFocusControl) {
                oldFocusControl.setFocus();
            }
        } catch (PartInitException ex) {
            UIPlugin.INSTANCE.log(ex);
        } finally {
            suspendScopeChanges = false;
        }

        // If it is a property sheet, as expected...
        if (viewPart instanceof PropertySheet) {
            // And the current page is a property sheet page as expected...
            final PropertySheet propertySheet = (PropertySheet) viewPart;
            IPage currentPage = propertySheet.getCurrentPage();
            if (currentPage instanceof PropertySheetPage) {
                // And the control is a tree...
                Control control = currentPage.getControl();
                if (control instanceof Tree) {
                    final Tree tree = (Tree) control;

                    if (item.itemPropertyDescriptor != null) {
                        // Remember the filter action that we needed to check it to be able to show an advanced property.
                        Action filterAction = null;

                        // If the property has filter flags...
                        String[] filterFlags = item.itemPropertyDescriptor.getFilterFlags(item.data.object);
                        if (filterFlags != null) {
                            for (String filterFlag : filterFlags) {
                                // If the filter is one for expert property...
                                if ("org.eclipse.ui.views.properties.expert".equals(filterFlag)) {
                                    Action action = ReflectUtil.getValue(FILTER_ACTION_FIELD, currentPage);
                                    if (!action.isChecked()) {
                                        // Run the action to show advanced properties, and remember that.
                                        action.setChecked(true);
                                        action.run();
                                        filterAction = action;
                                    }
                                }
                            }
                        }

                        // Walk the tree items.
                        for (final TreeItem treeItem : tree.getItems()) {
                            // If there is an EMF property descriptor with a feature for the selected item...
                            PropertyDescriptor propertyDescriptor = getPropertyDescriptor(treeItem);
                            if (propertyDescriptor != null
                                    && propertyDescriptor.getFeature() == item.getFeature()) {
                                // Consider the label shown in the tree verses the value of the selected item...
                                String treeItemText = treeItem.getText(1);
                                String itemValue = item.value;

                                // We might need to replace the tree item's text with a special representation...
                                specialStart = -1;

                                // If they are're identical....
                                if (!treeItemText.equals(itemValue)) {
                                    // Find the match, which really must be there, do we can determine the length of the match.
                                    Matcher matcher = pattern.matcher(itemValue);
                                    if (matcher.find(start)) {
                                        // Remember this special item, because we'll want to update it after we do a replace to show the replaced text.
                                        specialTreeItem = treeItem;

                                        // If the end of the match is after the end of the tree item's text, or the strings up until the end of the match are not
                                        // identical...
                                        int end = matcher.end();
                                        if (treeItemText.length() < end || !treeItemText.substring(0, end)
                                                .equals(itemValue.substring(0, end))) {
                                            // Consider the starting point of the match, and work our way backward for 20 characters or until the preceding control
                                            // character.
                                            int begin = matcher.start();
                                            specialStart = 2;
                                            while (begin >= 0 && specialStart < 20
                                                    && !Character.isISOControl(itemValue.charAt(begin))) {
                                                ++specialStart;
                                                --begin;
                                            }

                                            // Work our way forward until the end of the string or until we hit a control character.
                                            int itemValueLength = itemValue.length();
                                            while (end < itemValueLength
                                                    && !Character.isISOControl(itemValue.charAt(end))) {
                                                ++end;
                                            }

                                            // Create a special string with ellipses at both ends.
                                            String specialText = "..." + itemValue.substring(begin + 1, end)
                                                    + "...";

                                            // But that back into the item.
                                            treeItem.setText(1, specialText);

                                            // Get the tree to redraw itself.
                                            tree.redraw();
                                        }
                                    }
                                }

                                // Create a paint listener to select the match.
                                final Listener paintItemListener = new Listener() {
                                    private void paintItem(Event event, TreeItem item, int matchStart) {
                                        String text = item.getText(1);
                                        Matcher matcher = selectedItemPattern.matcher(text);
                                        if (matchStart < text.length() && matcher.find(matchStart)) {
                                            // Compute the offset of the start of the matching, relative to the start of the text.
                                            int start = matcher.start();
                                            int x = event.gc.textExtent(text.substring(0, start)).x
                                                    + item.getTextBounds(1).x - treeItem.getBounds(1).x;

                                            // Compute the offset at the end of the match, taking into account the width of the matching text.
                                            int width = event.gc.textExtent(matcher.group()).x;
                                            event.gc.drawRectangle(event.x + x + 1, event.y, width + 1,
                                                    event.height - 1);
                                        } else if (text.endsWith("...")) {
                                            int x = event.gc.textExtent(text.substring(0, text.length() - 3)).x
                                                    + treeItem.getTextBounds(1).x - treeItem.getBounds(1).x;
                                            int width = event.gc.textExtent("...").x;
                                            event.gc.drawRectangle(event.x + x + 1, event.y, width + 1,
                                                    event.height - 1);
                                        }
                                    }

                                    public void handleEvent(Event event) {
                                        // If we're painting or special item...
                                        TreeItem item = (TreeItem) event.item;
                                        if (item == treeItem && event.index == 1) {
                                            paintItem(event, item, specialStart == -1 ? start : specialStart);
                                        }
                                    }
                                };

                                // Add the listener.
                                tree.addListener(SWT.PaintItem, paintItemListener);

                                // Set up the runnable to clean up what we've done here.
                                final PropertySheetPage propertySheetPage = (PropertySheetPage) currentPage;
                                final Action finalFilterAction = filterAction;
                                propertiesCleanup = new Runnable() {
                                    public void run() {
                                        // Remove the listener.
                                        tree.removeListener(SWT.PaintItem, paintItemListener);

                                        // If there is a filter action we toggled...
                                        if (finalFilterAction != null) {
                                            // Toggle it back, which will refresh the view.
                                            finalFilterAction.setChecked(false);
                                            finalFilterAction.run();
                                        } else {
                                            // Otherwise refresh the view.
                                            propertySheetPage.refresh();
                                        }
                                    }
                                };

                                // Select the item, and force a repaint.
                                tree.setSelection(treeItem);
                                tree.redraw();

                                // We're done.
                                return;
                            }
                        }
                    }

                    // If we didn't find it at all, clear out the selection.
                    tree.setSelection(new TreeItem[0]);
                }
            }
        }
    }
}

From source file:org.eclipse.wst.common.ui.internal.viewers.TableNavigator.java

License:Open Source License

public TableNavigator(Table table, StructuredViewer viewer) {
    super(table, SWT.NONE);
    this.table = table;
    final Table currentTable = table;
    final StructuredViewer sViewer = viewer;

    // Linux index out of bounds fix.  See defect 253429, 253433, and more
    setVisible(false);//from www  . j  a va2  s.c  o m

    addPaintListener(viewer);
    addKeyListeners(viewer);
    addMouseListeners(viewer);
    addSelectionListener(new SelectionAdapter() {
        /**
        * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(SelectionEvent)
        */
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            if (sViewer instanceof TableTreeViewer) {
                TableTreeItem tableTreeItem = (TableTreeItem) getRow().getData(TABLETREEITEM_ID);
                StructuredSelection selection = new StructuredSelection(tableTreeItem.getData());
                sViewer.setSelection(selection, true);
            }
        }

    });
    addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent e) {
            // if e.source is not a child of the table then set selection - this is for tab into viewer
            Object eventSource = e.getSource();
            if (eventSource instanceof Control) {
                if (!isChild(currentTable, (Control) eventSource)) {
                    if (currentTable.getItemCount() > 0 && currentTable.getSelectionCount() <= 0) {
                        if (sViewer instanceof TableTreeViewer) {
                            TableTreeItem tableTreeItem = (TableTreeItem) getRow().getData(TABLETREEITEM_ID);
                            StructuredSelection selection = new StructuredSelection(tableTreeItem.getData());
                            sViewer.setSelection(selection, true);
                        } else {
                            currentTable.setSelection(0);
                            setSelection(0, 0);
                        }
                    }
                } else {
                    if (currentTable.getItems().length > 0) {
                        // cursor can end up on a non-existent table row 
                        //   currently no way to get the current table cursor row 
                        //   so for now just catch the exception since it doesn't
                        //   cause any side effects.
                        try {
                            setVisible(true);
                        } catch (Exception ee) {
                            currentTable.setSelection(0);
                            setSelection(0, 0);
                        }
                    } else // do not show table cursor if there are no elements in the table - avoid repaint
                    {
                        setVisible(false);
                    }
                }
            }
        }

        protected boolean isChild(Control parent, Control child) {
            Control tempChild = child;
            while (tempChild != null) {
                if (tempChild == parent) {
                    return true;
                }
                tempChild = tempChild.getParent();
            }
            return false;
        }

        /**
         * @see org.eclipse.swt.events.FocusAdapter#focusLost(FocusEvent)
         */
        public void focusLost(FocusEvent e) {
            // Set the table navigator to be not visible if the the table
            // is not in focus and a child of the table is not in focus
            // note that we do this asynchronously so we don't mess up the
            // current focus handling.
            Display.getDefault().asyncExec(new Runnable() {
                /**
                 * @see java.lang.Runnable#run()
                 */
                public void run() {
                    if (currentTable != null && !currentTable.isDisposed() && !currentTable.isFocusControl()
                            && !isChild(currentTable, Display.getDefault().getFocusControl())) {
                        setVisible(false);
                    }
                }
            });
        }

    });

    table.addFocusListener(new FocusAdapter() {
        /**
         * @see org.eclipse.swt.events.FocusListener#focusGained(FocusEvent)
         */
        public void focusGained(FocusEvent e) {
            // only display navigator if there are items in the table 
            // and if the focus wasn't gained from our own table navigator
            // (ie focus came from outside)
            if (currentTable.getItemCount() > 0 && (Display.getDefault().getFocusControl() != null)
                    && !Display.getDefault().getFocusControl().equals(TableNavigator.this)) {
                // note that we do this asynchronously so we don't mess up the
                // current focus handling.
                Display.getDefault().asyncExec(new Runnable() {
                    /**
                     * @see java.lang.Runnable#run()
                     */
                    public void run() {
                        if (!isVisible()) {
                            try {
                                setVisible(true);
                                setFocus();
                            } catch (Exception e) {
                                // catch IllegalArgumentExceptions here - index out of bounds on tableviewer
                                if (currentTable.getItemCount() > 0) {
                                    currentTable.setSelection(0);
                                    setSelection(0, 0);
                                } else // do not show table cursor if there are no elements in the table - avoid repaint
                                {
                                    setVisible(false);
                                }
                            }
                        }
                    }
                });
            }
        }
    });
}