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

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

Introduction

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

Prototype

public abstract 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.google.dart.tools.ui.internal.typehierarchy.TypeHierarchyContentProvider_NEW.java

License:Open Source License

void inputHierarchyChanged(final Viewer viewer) {
    if (items.isEmpty()) {
        return;//from   w  ww.  j ava 2 s  .  co  m
    }
    TypeHierarchyItem target = items.get(0);
    isMemberHierarchy = target.getMemberElement() != null;
    final TypeItem targetItem = new TypeItem(target);
    // fill super hierarchy
    TypeHierarchyItem superType = target;
    while (superType != null) {
        if (!seenItems.add(superType)) {
            break;
        }
        TypeItem superItem = new TypeItem(superType);
        superList.add(0, superItem);
        if (superType == target) {
            superItem = targetItem;
        }
        // try "extends"
        {
            Integer superId = superItem.type.getSuperclass();
            if (superId == null) {
                break;
            }
            superType = items.get(superId);
        }
        // try to use something better than "Object"
        if (superType.getClassElement().getName().equals("Object")) {
            if (superItem.mixins.length != 0) {
                int id = superItem.mixins[0];
                superType = items.get(id);
            } else if (superItem.interfaces.length != 0) {
                int id = superItem.interfaces[0];
                superType = items.get(id);
            }
        }
    }
    // prepare sub types
    fillSubTypes(target, targetItem);
    if (isMemberHierarchy) {
        keepBranchesWithMemberOverride(targetItem);
    }
    // refresh viewer
    Display.getDefault().asyncExec(new Runnable() {
        @Override
        public void run() {
            Control control = viewer.getControl();
            if (control != null && !control.isDisposed()) {
                viewer.refresh();
                if (viewer instanceof TreeViewer) {
                    ((TreeViewer) viewer).expandAll();
                    viewer.setSelection(new StructuredSelection(targetItem), true);
                }
            }
        }
    });
}

From source file:com.symbian.driver.report.actions.ValidationActionWithoutProgress.java

License:Open Source License

protected void handleDiagnostic(Diagnostic diagnostic) {

    if (eclipseResourcesUtilWithLineNumber != null) {
        Resource resource = domain.getResourceSet().getResources().get(0);
        if (resource != null) {
            eclipseResourcesUtilWithLineNumber.deleteMarkers(resource);
            errorURIs.clear();// w  w  w . ja  va 2  s .c o m
        }

        if (true) {
            if (!diagnostic.getChildren().isEmpty()) {
                List<?> data = (diagnostic.getChildren().get(0)).getData();
                if (!data.isEmpty() && data.get(0) instanceof EObject) {
                    Object part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                            .getActivePart();
                    if (part instanceof ISetSelectionTarget) {
                        ((ISetSelectionTarget) part).selectReveal(new StructuredSelection(data.get(0)));
                    } else if (part instanceof IViewerProvider) {
                        Viewer viewer = ((IViewerProvider) part).getViewer();
                        if (viewer != null) {
                            viewer.setSelection(new StructuredSelection(data.get(0)), true);
                        }
                    }
                }
            }

            if (resource != null) {
                for (Diagnostic childDiagnostic : diagnostic.getChildren()) {
                    eclipseResourcesUtilWithLineNumber.createMarkers(resource, childDiagnostic);
                }
            }
        }

        try {
            String MARK_ID = EValidator.MARKER;
            IEditorPart edit = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                    .getActiveEditor();
            IResource iResource = ((IFileEditorInput) edit.getEditorInput()).getFile();
            for (Resource.Diagnostic e : resource.getErrors()) {
                IMarker marker = iResource.createMarker(MARK_ID);
                marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                marker.setAttribute(IMarker.LINE_NUMBER, e.getLine());
                marker.setAttribute(IMarker.LOCATION, "line: " + e.getLine() + ", column: " + e.getColumn());
                marker.setAttribute(IMarker.MESSAGE, e.getMessage());
                marker.setAttribute(EValidator.URI_ATTRIBUTE,
                        EcoreUtil.getURI(resource.getContents().get(0)).toString());
            }

            for (Resource.Diagnostic e : resource.getWarnings()) {
                IMarker marker = iResource.createMarker(MARK_ID);
                marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                marker.setAttribute(IMarker.LINE_NUMBER, e.getLine());
                marker.setAttribute(IMarker.LOCATION, "line: " + e.getLine() + ", column: " + e.getColumn());
                marker.setAttribute(IMarker.MESSAGE, e.getMessage());
            }
        } catch (Exception exception) {
        }
    }
}

From source file:dk.itu.sdg.language.xwpf.resource.xwpf.ui.XwpfHyperlink.java

License:Apache License

/**
 * Opens the resource in <code>linkTarget</code> with the generated editor, if it
 * supports the file extension of this resource, and tries to jump to the
 * definition. Otherwise it tries to open the target with the default editor.
 *///from  w w w .j  a  v a 2s.com
public void open() {
    if (linkTarget == null) {
        return;
    }
    org.eclipse.core.resources.IFile file = getIFileFromResource();
    if (file != null) {
        org.eclipse.ui.IWorkbench workbench = org.eclipse.ui.PlatformUI.getWorkbench();
        org.eclipse.ui.IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
        try {
            org.eclipse.ui.IEditorDescriptor desc = workbench.getEditorRegistry()
                    .getDefaultEditor(file.getName());
            if (desc == null) {
                desc = workbench.getEditorRegistry()
                        .findEditor("org.eclipse.emf.ecore.presentation.ReflectiveEditorID");
            }
            org.eclipse.ui.IEditorPart editorPart = page
                    .openEditor(new org.eclipse.ui.part.FileEditorInput(file), desc.getId());
            if (editorPart instanceof org.eclipse.emf.edit.domain.IEditingDomainProvider) {
                org.eclipse.emf.edit.domain.IEditingDomainProvider editingDomainProvider = (org.eclipse.emf.edit.domain.IEditingDomainProvider) editorPart;
                org.eclipse.emf.edit.domain.EditingDomain editingDomain = editingDomainProvider
                        .getEditingDomain();
                org.eclipse.emf.common.util.URI uri = org.eclipse.emf.ecore.util.EcoreUtil.getURI(linkTarget);
                org.eclipse.emf.ecore.EObject originalObject = editingDomain.getResourceSet().getEObject(uri,
                        true);
                if (editingDomainProvider instanceof org.eclipse.emf.common.ui.viewer.IViewerProvider) {
                    org.eclipse.emf.common.ui.viewer.IViewerProvider viewerProvider = (org.eclipse.emf.common.ui.viewer.IViewerProvider) editingDomainProvider;
                    org.eclipse.jface.viewers.Viewer viewer = viewerProvider.getViewer();
                    viewer.setSelection(new org.eclipse.jface.viewers.StructuredSelection(originalObject),
                            true);
                }
            }
        } catch (org.eclipse.ui.PartInitException e) {
            dk.itu.sdg.language.xwpf.resource.xwpf.mopp.XwpfPlugin
                    .logError("Exception while opening hyperlink target.", e);
        }
    }
}

From source file:fr.labsticc.framework.emf.view.ide.EMFEditorUtil.java

License:Open Source License

public static void selectIntoEditor(final List<? extends EObject> p_modelObjects, final IEditorPart editor) {
    IElementComparer currentComparer = null;
    StructuredViewer structViewer = null;
    final ISelection selection = new StructuredSelection(p_modelObjects);

    if (editor instanceof IViewerProvider) {
        final Viewer viewer = ((IViewerProvider) editor).getViewer();

        if (viewer instanceof StructuredViewer) {
            structViewer = (StructuredViewer) viewer;
            currentComparer = structViewer.getComparer();
            structViewer.setComparer(uriComparer);
        }//from  w w w  .ja  v a  2 s .  c  om

        viewer.setSelection(selection, true);
    } else {
        editor.getEditorSite().getSelectionProvider().setSelection(selection);
    }

    if (structViewer != null) {
        structViewer.setComparer(currentComparer);
    }
}

From source file:gov.nasa.ensemble.core.plan.editor.ValidatePlanHandler.java

License:Open Source License

private void validate(EObject element) {
    Diagnostic diagnostic = null;
    // Validate the contents of the loaded resource.
    ///*from   ww w  .  jav a 2 s .co  m*/
    for (EObject eObject : element.eResource().getContents()) {
        diagnostic = Diagnostician.INSTANCE.validate(eObject);
        //         if (diagnostic.getSeverity() != Diagnostic.OK) {
        //            printDiagnostic(diagnostic, "");
        //         }
    }
    Viewer viewer = null;
    if (diagnostic != null) {
        EclipseResourcesUtil resourcesUtil = new EclipseResourcesUtil();
        resourcesUtil.deleteMarkers(element.eResource());
        for (Diagnostic childDiagnostic : diagnostic.getChildren()) {
            if (!childDiagnostic.getData().isEmpty()) {
                resourcesUtil.createMarkers(element.eResource(), childDiagnostic);
            }
        }

        // select the first object with problem in the viewer if possible
        //
        if (!diagnostic.getChildren().isEmpty()) {
            List<?> data = (diagnostic.getChildren().get(0)).getData();
            if (!data.isEmpty()) {
                Object part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                        .getActivePart();
                Object e = data.get(0);
                if (part instanceof ISetSelectionTarget) {
                    ((ISetSelectionTarget) part).selectReveal(new StructuredSelection(e));
                } else if (part instanceof IViewerProvider) {
                    viewer = ((IViewerProvider) part).getViewer();
                    if (viewer != null) {
                        viewer.setSelection(new StructuredSelection(data.get(0)), true);
                    }
                }
                //               else if (part instanceof IEditorPart) {
                //   
                //                  IEditorPart editor = ((IEditorPart) part);
                //                  if (editor != null) {
                //                     if (e instanceof EObject) {
                //                        
                //                     editor.getEditorSite().getSelectionProvider().setSelection(new StructuredSelection(((EObject)e).eContainer()));
                //                     }
                //                  }
                //               }
            }
        }
    }
    if (viewer == null) {
        Object part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
        if (part instanceof IViewerProvider) {
            viewer = ((IViewerProvider) part).getViewer();
        }
    }

}

From source file:kr.co.apexsoft.stella.modeler.explorer.dialog.ApexNavigatorSearchDialog.java

License:Open Source License

/**
 * Sets a new selection for the associated {@link ISelectionProvider} and optionally makes it visible.
 * <p>//from   ww  w.  ja va2  s .c  o m
 * Subclasses must implement this method.
 * </p>
 *
 * @param selection the new selection
 * @param reveal <code>true</code> if the selection is to be made
 *   visible, and <code>false</code> otherwise
 */
private void fireSetSelection(ISelection selection, boolean reveal) {
    // Note : if we want to force reveal, it is possible to check if 
    // selectionProvider instanceof Viewer, and then call selectionProvider.setSelection(selection, true).
    // By default a TreeViewer reveal the selection.
    if (viewer instanceof CommonViewer) {
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection structured = (IStructuredSelection) selection;
            ModelExplorerView.reveal(Iterables.transform(Lists.newArrayList(structured.iterator()),
                    new Function<Object, EObject>() {

                        public EObject apply(Object arg0) {
                            if (arg0 instanceof IAdaptable) {
                                IAdaptable adapt = (IAdaptable) arg0;
                                return getAdapter(adapt, EObject.class);
                            }
                            return null;
                        }
                    }), (CommonViewer) viewer);
        }
    } else if (viewer instanceof Viewer) {
        Viewer view = (Viewer) viewer;
        view.setSelection(selection, true);
    }
}

From source file:kr.co.apexsoft.stella.modeler.explorer.view.ApexStellaExplorerView.java

License:Open Source License

/**
 * Selects the given ISelection in the given CommonViwer
 * @param structuredSelection The ISelection to select
 * @param commonViewer The ComonViewer to select it in
 */// www. j  av  a 2  s  . c  o  m
public static void selectReveal(ISelection structuredSelection, Viewer commonViewer) {
    commonViewer.setSelection(structuredSelection, true);
}

From source file:org.eclipse.acceleo.internal.ide.ui.editors.template.utils.OpenDeclarationUtils.java

License:Open Source License

/**
 * Select the given EObject in the viewer.
 * /*from  ww  w.  ja  v a 2 s  .com*/
 * @param selectedEObject
 *            is the object to select
 * @param viewer
 *            is the viewer that contains some EMF objects
 */
private static void setSelectionToViewer(final EObject selectedEObject, final Viewer viewer) {
    if (selectedEObject != null && viewer != null) {
        Runnable runnable = new Runnable() {
            public void run() {
                viewer.setSelection(new StructuredSelection(selectedEObject), true);
            }
        };
        viewer.getControl().getDisplay().asyncExec(runnable);
    }
}

From source file:org.eclipse.debug.internal.ui.views.launch.LaunchView.java

License:Open Source License

public boolean show(ShowInContext context) {
    ISelection selection = context.getSelection();
    if (selection != null) {
        if (selection instanceof IStructuredSelection) {
            IStructuredSelection ss = (IStructuredSelection) selection;
            if (ss.size() == 1) {
                Object obj = ss.getFirstElement();
                if (obj instanceof IDebugTarget || obj instanceof IProcess) {
                    Viewer viewer = getViewer();
                    if (viewer instanceof InternalTreeModelViewer) {
                        InternalTreeModelViewer tv = (InternalTreeModelViewer) viewer;
                        tv.setSelection(selection, true, true);
                    } else {
                        viewer.setSelection(selection, true);
                    }//from  www.j  av a 2 s. c o  m
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:org.eclipse.e4mf.edit.ui.action.ValidateAction.java

License:Open Source License

protected void handleDiagnostic(Diagnostic diagnostic) {
    int severity = diagnostic.getSeverity();
    String title = null;//  ww  w. j  a v  a 2 s. c om
    String message = null;

    if (severity == Diagnostic.ERROR || severity == Diagnostic.WARNING) {
        title = EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationProblems_title");
        message = EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationProblems_message");
    } else {
        title = EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationResults_title");
        message = EMFEditUIPlugin.INSTANCE.getString(
                severity == Diagnostic.OK ? "_UI_ValidationOK_message" : "_UI_ValidationResults_message");
    }

    int result = 0;
    if (diagnostic.getSeverity() == Diagnostic.OK) {
        MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), title,
                message);
        result = Window.CANCEL;
    } else {
        result = DiagnosticDialog.open(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), title,
                message, diagnostic);
    }

    ResourceSet resourceSet = domain.getResourceSet();
    Resource resource = eclipseResourcesUtil != null ? resourceSet.getResources().get(0) : null;
    if (resource != null) {
        eclipseResourcesUtil.deleteMarkers(resource);
    }

    if (result == Window.OK) {
        if (!diagnostic.getChildren().isEmpty()) {
            List<?> data = (diagnostic.getChildren().get(0)).getData();
            if (!data.isEmpty() && data.get(0) instanceof EObject) {
                Object part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                        .getActivePart();
                if (part instanceof ISetSelectionTarget) {
                    ((ISetSelectionTarget) part).selectReveal(new StructuredSelection(data.get(0)));
                } else if (part instanceof IViewerProvider) {
                    Viewer viewer = ((IViewerProvider) part).getViewer();
                    if (viewer != null) {
                        viewer.setSelection(new StructuredSelection(data.get(0)), true);
                    }
                }
            }
        }

        if (resource != null) {
            for (Diagnostic childDiagnostic : diagnostic.getChildren()) {
                eclipseResourcesUtil.createMarkers(resource, childDiagnostic);
            }
        }
    } else {
        // Trigger direct updating of decorations, if there are adapters.
        //
        resource = null;
    }

    if (resource == null) {
        // If no markers are produced the decorator won't be able to respond to marker resource deltas, so inform it directly.
        //

        // Create a diagnostic for the resource set as a whole.
        //
        BasicDiagnostic resourceSetDiagnostic = new BasicDiagnostic(EObjectValidator.DIAGNOSTIC_SOURCE, 0, null,
                new Object[] { resourceSet });

        // Create a diagnostic for each resource.
        //
        Map<Resource, BasicDiagnostic> resourceDiagnostics = new LinkedHashMap<Resource, BasicDiagnostic>();
        for (Resource r : resourceSet.getResources()) {
            BasicDiagnostic resourceDiagnostic = new BasicDiagnostic(EObjectValidator.DIAGNOSTIC_SOURCE, 0,
                    null, new Object[] { r });
            resourceDiagnostics.put(r, resourceDiagnostic);
        }

        // Just clean up decorations if the dialog was cancelled.
        //
        if (result == Dialog.OK) {
            // Partition the children among the resource diagnostics.
            //
            for (Diagnostic child : diagnostic.getChildren()) {
                List<?> data = child.getData();
                if (!data.isEmpty()) {
                    Object object = data.get(0);
                    if (object instanceof EObject) {
                        BasicDiagnostic resourceDiagnostic = resourceDiagnostics
                                .get(((EObject) object).eResource());
                        if (resourceDiagnostic != null) {
                            resourceDiagnostic.add(child);
                        }
                    }
                }
            }
        }

        // Add the resource diagnostics to the resource set diagnostic.
        //
        for (Diagnostic resourceDiagnostic : resourceDiagnostics.values()) {
            resourceSetDiagnostic.add(resourceDiagnostic);
        }

        // Inform any decorators.
        //
        DiagnosticDecorator.DiagnosticAdapter.update(resourceSet, resourceSetDiagnostic);
    }
}