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
public void setSelection(ISelection selection, boolean reveal) 

Source Link

Document

Sets a new selection for this viewer and optionally makes it visible.

Usage

From source file:com.mentor.nucleus.bp.test.common.ExplorerUtil.java

License:Open Source License

static public void checkTreeItemDeletion(NonRootModelElement modelElement) {
    //  the tree must be focused for the selection to be
    // be reported to the model explorer 
    TreeViewer viewer = explorer.getTreeViewer();
    final Tree tree = viewer.getTree();
    boolean focused = tree.setFocus();
    TestCase.assertTrue("Could not focus model explorer tree", focused);

    // select the node in the tree; note that we must specify
    // the model element, not its tree-item 
    viewer.setSelection(new StructuredSelection(new Object[] { modelElement }), false);

    // since this test is running on the event-dispatch thread, we
    // have to fire the event pump manually to get the 
    // selection event reported, before proceeding
    Display display = Display.getCurrent();
    while (display.readAndDispatch())
        ;// ww  w  . ja  v a2  s  . c  o  m
    TestCase.assertTrue("Tree Item still exist after deletion ", tree.getSelectionCount() == 0);
}

From source file:com.mentor.nucleus.bp.test.common.ExplorerUtil.java

License:Open Source License

static public void checkTreeItemExistance(NonRootModelElement modelElement, String name) {
    // the tree must be focused for the selection to be
    // be reported to the model explorer 
    TreeViewer viewer = explorer.getTreeViewer();
    final Tree tree = viewer.getTree();
    boolean focused = tree.setFocus();
    TestCase.assertTrue("Could not focus model explorer tree", focused);

    // select the node in the tree; note that we must specify
    // the model element, not its tree-item 
    viewer.setSelection(new StructuredSelection(new Object[] { modelElement }), false);

    // since this test is running on the event-dispatch thread, we
    // have to fire the event pump manually to get the 
    // selection event reported, before proceeding
    Display display = Display.getCurrent();
    while (display.readAndDispatch())
        ;//from   w ww  .  j  a  v  a2s  .  co  m
    TestCase.assertTrue("Tree Item with text '" + name + "' dz not exist",
            tree.getSelection()[0].getText().equals(name));
}

From source file:com.mentor.nucleus.bp.test.common.ExplorerUtil.java

License:Open Source License

/**
  * Select the NonRootModelElement from Model Explorer
 * @return /* w  ww . j a v  a2 s.c  om*/
  */
public static TreeItem selectItem(Object item) {
    // the tree must be focused for the selection to be
    // be reported to the model explorer 
    TreeViewer viewer = explorer.getTreeViewer();
    final Tree tree = viewer.getTree();
    boolean focused = tree.setFocus();
    TestCase.assertTrue("Could not focus model explorer tree", focused);

    // select the node in the tree; note that we must specify
    // the model element, not its tree-item 
    viewer.setSelection(new StructuredSelection(new Object[] { item }), false);

    // since this test is running on the event-dispatch thread, we
    // have to fire the event pump manually to get the 
    // selection event reported, before proceeding
    Display display = Display.getCurrent();
    while (display.readAndDispatch())
        ;
    TestCase.assertTrue("Tree Item not selected ", tree.getSelectionCount() > 0);
    return tree.getSelection()[0];
}

From source file:com.mentor.nucleus.bp.test.common.ExplorerUtil.java

License:Open Source License

/**
 * Selects the given item in the model explorer tree.
 *///from  w w  w  . j a va 2  s  .co  m
public static void selectItem(TreeItem item) {
    // the tree must be focused for the selection to be
    // be reported to the model explorer 
    TreeViewer viewer = explorer.getTreeViewer();
    final Tree tree = viewer.getTree();
    boolean focused = tree.setFocus();
    TestCase.assertTrue("Could not focus model explorer tree", focused);

    // select the node in the tree; note that we must specify
    // the model element, not its tree-item 
    viewer.setSelection(new StructuredSelection(new Object[] { item.getData() }), false);

    // since this test is running on the event-dispatch thread, we
    // have to fire the event pump manually to get the 
    // selection event reported, before proceeding
    Display display = Display.getCurrent();
    while (display.readAndDispatch())
        ;
}

From source file:com.mentor.nucleus.bp.ui.explorer.test.ExplorerTest.java

License:Open Source License

static public void checkTreeItemExistance(NonRootModelElement modelElement, String name) {
    // the tree must be focused for the selection to be
    // be reported to the model explorer 
    TreeViewer viewer = ExplorerUtil.getTreeViewer();
    final Tree tree = viewer.getTree();
    boolean focused = tree.setFocus();
    assertTrue("Could not focus model explorer tree", focused);

    // select the node in the tree; note that we must specify
    // the model element, not its tree-item 
    viewer.setSelection(new StructuredSelection(new Object[] { modelElement }), false);

    // since this test is running on the event-dispatch thread, we
    // have to fire the event pump manually to get the 
    // selection event reported, before proceeding
    BaseTest.dispatchEvents(0);/* ww w . ja v  a  2s  .  c  om*/
    waitForDecorator();
    assertTrue("Tree Item with text '" + name + "' dz not exist",
            tree.getSelection()[0].getText().startsWith(name));
}

From source file:com.mentor.nucleus.bp.ui.explorer.test.ExplorerTest.java

License:Open Source License

static public void checkTreeItemDeletion(NonRootModelElement modelElement) {
    //  the tree must be focused for the selection to be
    // be reported to the model explorer 
    TreeViewer viewer = ExplorerUtil.getTreeViewer();
    final Tree tree = viewer.getTree();
    boolean focused = tree.setFocus();
    assertTrue("Could not focus model explorer tree", focused);

    // select the node in the tree; note that we must specify
    // the model element, not its tree-item 
    viewer.setSelection(new StructuredSelection(new Object[] { modelElement }), false);

    // since this test is running on the event-dispatch thread, we
    // have to fire the event pump manually to get the 
    // selection event reported, before proceeding
    BaseTest.dispatchEvents(0);//w w w .  j a v  a 2  s. co  m
    assertTrue("Tree Item still exist after deletion ", tree.getSelectionCount() == 0);
}

From source file:com.twinsoft.convertigo.eclipse.property_editors.XmlQNameEditorComposite.java

License:Open Source License

public XmlQNameEditorComposite(final Composite parent, int style, AbstractDialogCellEditor cellEditor) {
    super(parent, style, cellEditor);

    try {//from w w  w  .  j  av  a  2s .c  o m
        String propertyName = "" + cellEditor.propertyDescriptor.getId();
        DatabaseObject dbo = cellEditor.databaseObjectTreeObject.getObject();
        Project project = dbo.getProject();
        collection = Engine.theApp.schemaManager.getSchemasForProject(project.getName());
        currentNamespace = project.getTargetNamespace();
        if ("xmlTypeAffectation".equals(propertyName)) {
            // useComplexType = true; // TODO: add complex type support for input variables 
            useSimpleType = true;
        } else {
            useComplexType = "xmlComplexTypeAffectation".equals(propertyName);
            useSimpleType = "xmlSimpleTypeAffectation".equals(propertyName);
        }
        useType = useComplexType || useSimpleType;
        useRef = "xmlElementRefAffectation".equals(propertyName);
    } catch (Exception e) {
        e.printStackTrace();
    }

    this.setLayoutData(new GridData(GridData.FILL_BOTH));
    this.setLayout(new GridLayout(1, false));

    new Label(this, style).setText("Existing objects");

    final TreeViewer treeViewer = new TreeViewer(this);
    treeViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
    treeViewer.setContentProvider(new SchemaViewContentProvider() {

        @Override
        protected void filter(XmlSchemaObject xso, List<XmlSchemaObject> children, XmlSchemaObject subObject) {
            if (xso instanceof XmlSchema) {
                if ((useSimpleType && subObject instanceof XmlSchemaSimpleType)
                        || useComplexType && subObject instanceof XmlSchemaComplexType
                        || useRef && subObject instanceof XmlSchemaElement) {
                    super.filter(xso, children, subObject);
                }
            } else {
                super.filter(xso, children, subObject);
            }
        }

        @Override
        public Object[] getChildren(Object object) {
            Object[] children = super.getChildren(object);
            for (Object child : children) {
                if (child instanceof NamedList) {
                    if (useType && "Types".equals(((NamedList) child).getName())) {
                        return new Object[] { child };
                    } else if (useRef && "Elements".equals(((NamedList) child).getName())) {
                        return new Object[] { child };
                    }
                } else {
                    return children;
                }
            }
            return children;
        }

    });

    treeViewer.setComparer(new IElementComparer() {

        public int hashCode(Object element) {
            return element.hashCode();
        }

        public boolean equals(Object a, Object b) {
            return a == b;
        }

    });

    DecoratingLabelProvider dlp = new DecoratingLabelProvider(new SchemaViewLabelProvider(),
            new SchemaViewLabelDecorator());
    treeViewer.setLabelProvider(dlp);
    treeViewer.setInput(collection);
    treeViewer.expandToLevel(3);

    new Label(this, SWT.NONE).setText("Namespace");

    tNamespace = new Text(this, SWT.NONE);
    tNamespace.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    tNamespace.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    tNamespace.setEditable(false);

    new Label(this, SWT.NONE).setText("Local name");

    tLocalName = new Text(this, SWT.NONE);
    tLocalName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    final Button bNone = new Button(this, SWT.NONE);
    bNone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    bNone.setText("No " + (useComplexType ? "type" : (useRef ? "element" : "object")));

    new Label(this, SWT.NONE).setText("Summary");

    lSummary = new Label(this, SWT.WRAP);
    lSummary.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    lSummary.setText("No change.");
    lSummary.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND));

    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            TreePath[] path = ((ITreeSelection) event.getSelection()).getPaths();

            XmlSchemaObject object = null;
            QName qName = null;
            if (path.length > 0 && path[0].getSegmentCount() > 2
                    && path[0].getSegment(2) instanceof XmlSchemaType) {
                object = (XmlSchemaType) path[0].getSegment(2);
                qName = ((XmlSchemaType) object).getQName();
            }
            if (path.length > 0 && path[0].getSegmentCount() > 2
                    && path[0].getSegment(2) instanceof XmlSchemaElement) {
                object = (XmlSchemaElement) path[0].getSegment(2);
                qName = ((XmlSchemaElement) object).getQName();
            }

            if (object != null) {
                String obText = (useType ? "type" : (useRef ? "element" : "object"));
                tLocalName.setText(qName.getLocalPart());
                tNamespace.setText(qName.getNamespaceURI());
                updateLabel(obText, qName, object);
                XmlQNameEditorComposite.this.layout(true);
            }
        }
    });

    bNone.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            tNamespace.setText("");
            tLocalName.setText("");
            lSummary.setText("No " + (useType ? "type" : (useRef ? "element" : "object")) + " set.");
            lSummary.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
        }
    });

    tLocalName.addVerifyListener(new VerifyListener() {
        public void verifyText(VerifyEvent e) {
            if (e.text.contains(" ")) {
                e.doit = false;
            }
        }
    });

    tLocalName.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
            String txt = tLocalName.getText();
            if (txt.length() == 0) {
                //bNone.notifyListeners(SWT.Selection, null); // commented to prevent stack over flow
            } else {
                tNamespace.setText(currentNamespace);
                QName qName = new QName(currentNamespace, txt);
                XmlSchemaType type = collection.getTypeByQName(qName);
                XmlSchemaElement element = collection.getElementByQName(qName);

                XmlSchemaObject object = type == null ? element : type;
                String obText = (useType ? "type" : (useRef ? "element" : "object"));
                if (object == null) {
                    lSummary.setText("Create the dynamic " + obText + " : \n" + qName.toString());
                    lSummary.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_MAGENTA));
                    XmlQNameEditorComposite.this.layout(true);
                } else {
                    updateLabel(obText, qName, object);
                }
            }
        }
    });

    if (useSimpleType) {
        tLocalName.setEnabled(false);
        bNone.setEnabled(false);
    }

    XmlQName schemaDefinition = (XmlQName) cellEditor.getValue();

    if (schemaDefinition != null) {
        QName qName = schemaDefinition.getQName();

        if (useType) {
            XmlSchemaType type = collection.getTypeByQName(qName);
            if (type != null) {
                treeViewer.setSelection(new StructuredSelection(type), true);
            }
        }

        if (useRef) {
            XmlSchemaElement element = collection.getElementByQName(qName);
            if (element != null) {
                treeViewer.setSelection(new StructuredSelection(element), true);
            }
        }
    }
}

From source file:edu.harvard.i2b2.eclipse.plugins.ontology.views.TreeNode.java

License:Open Source License

public Thread expandFindTree(TreeViewer viewer, String[] parts, String parent) {
    final TreeNode theNode = this;
    final TreeViewer theViewer = viewer;
    final String[] theParts = parts;
    final Display theDisplay = PlatformUI.getWorkbench().getDisplay();

    return new Thread() {
        @Override/*w  w  w.j  av  a  2 s  . c o  m*/
        public void run() {
            try {
                //   String parent = theParent;
                TreeNode treeNode = theNode;
                String compare = "\\";
                for (int i = 1; i < theParts.length; i++) {
                    compare += theParts[i] + "\\";
                    List<TreeNode> nodes = treeNode.getChildren();
                    Iterator<TreeNode> it = nodes.iterator();
                    while (it.hasNext()) {
                        TreeNode node = (TreeNode) it.next();

                        if (StringUtil.getPath(node.getData().getFullName()).equals(compare)) {
                            treeNode = node;
                            RefreshNode.getInstance().setRefreshNode(treeNode);
                            RefreshNode.getInstance().setLevel(i);
                            if ((node.getChildren().isEmpty())
                                    || (node.getChildren().get(0).getData().getName().equals("working..."))) {
                                //   node.setOpen(true);
                                node.getChildren().clear();
                                treeNode.updateModifiers(theDisplay, theViewer);
                                treeNode.updateChildren(theDisplay, theViewer);
                            }
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //            System.setProperty("statusMessage", e.getMessage());               
            }
            theDisplay.syncExec(new Runnable() {
                public void run() {
                    theViewer.collapseAll();
                    theViewer.expandToLevel(RefreshNode.getInstance().getRefreshNode(), 1);
                    theViewer.setSelection(new StructuredSelection(RefreshNode.getInstance().getRefreshNode()),
                            true);
                    theViewer.getTree().setEnabled(true);
                }

            });
        }
    };
}

From source file:ext.org.eclipse.jdt.internal.ui.packageview.GotoResourceAction.java

License:Open Source License

@Override
public void run() {
    TreeViewer viewer = fPackageExplorer.getTreeViewer();
    GotoResourceDialog dialog = new GotoResourceDialog(fPackageExplorer.getSite().getShell(),
            ResourcesPlugin.getWorkspace().getRoot(), viewer);
    dialog.open();/*from w w  w  .  j ava 2  s. c  o m*/
    Object[] result = dialog.getResult();
    if (result == null || result.length == 0 || !(result[0] instanceof IResource))
        return;
    StructuredSelection selection = null;
    IJavaElement element = JavaCore.create((IResource) result[0]);
    if (element != null && element.exists())
        selection = new StructuredSelection(element);
    else
        selection = new StructuredSelection(result[0]);
    viewer.setSelection(selection, true);
}

From source file:gov.nasa.arc.spife.core.plan.editor.timeline.ui.tooltips.SolitaryScaleTimelineMarkerTooltip.java

License:Open Source License

protected SolitaryScaleTimelineMarkerTooltip(Display display, int style,
        final ScaleTimelineMarkerEditPart scaleTimelineMarkerEditPart, ViolationTracker violationTracker) {
    super(display, style, scaleTimelineMarkerEditPart, violationTracker);

    /*//ww  w.  jav  a  2s.  c o  m
     *  mouse listener to listen for clicks on the title area. select
     *  the appropriate violation in the plan advisor.
     */
    mouseListener = new MouseAdapter() {
        @Override
        public void mouseUp(MouseEvent e) {
            Timeline timeline = scaleTimelineMarkerEditPart.getTimeline();
            IWorkbenchPage page = timeline.getSite().getPage();
            IViewReference[] viewReferences = page.getViewReferences();
            IViewPart viewPart = null;
            for (IViewReference viewReference : viewReferences) {
                viewPart = viewReference.getView(false);
                if (viewPart != null) {
                    Object adapter = viewPart.getAdapter(TreeViewer.class);
                    if (adapter instanceof TreeViewer) {
                        TreeViewer treeViewer = (TreeViewer) adapter;
                        ViolationTracker violationTracker = getViolationTracker();
                        treeViewer.setSelection(new StructuredSelection(violationTracker), true);
                        try {
                            page.activate(viewPart);
                        } catch (Exception e1) {
                            // ignore failures in activation
                        }
                    }
                }
            }

        }
    };

    // add the mouse listener to the title
    Control[] controls = titleComposite.getChildren();
    for (Control control : controls) {
        control.addMouseListener(mouseListener);
    }
}