/**
   * Asserts that when a selection is set on the viewer:
   *
   * <ul>
   *   <li>the selection is available in the observable
   *   <li>Value change events are fired with appropriate diff values
   * </ul>
   */
  public void testGetSetValue() {
    SelectionProviderSingleSelectionObservableValue observable =
        new SelectionProviderSingleSelectionObservableValue(
            SWTObservables.getRealm(Display.getDefault()), selectionProvider);
    ValueChangeEventTracker listener = new ValueChangeEventTracker();
    observable.addValueChangeListener(listener);
    assertNull(observable.getValue());

    selectionProvider.setSelection(new StructuredSelection(model[0]));
    assertEquals(1, listener.count);
    assertNull(listener.event.diff.getOldValue());
    assertEquals(model[0], listener.event.diff.getNewValue());
    assertEquals(observable, listener.event.getObservableValue());
    assertEquals(model[0], observable.getValue());

    selectionProvider.setSelection(new StructuredSelection(model[1]));
    assertEquals(2, listener.count);
    assertEquals(model[0], listener.event.diff.getOldValue());
    assertEquals(model[1], listener.event.diff.getNewValue());
    assertEquals(observable, listener.event.getObservableValue());
    assertEquals(model[1], observable.getValue());

    selectionProvider.setSelection(StructuredSelection.EMPTY);
    assertEquals(3, listener.count);
    assertEquals(model[1], listener.event.diff.getOldValue());
    assertNull(listener.event.diff.getNewValue());
    assertEquals(observable, listener.event.getObservableValue());
    assertEquals(null, observable.getValue());
  }
Ejemplo n.º 2
0
  /** Selects and reveals the given offset and length in the given editor part. */
  public static void revealInEditor(IEditorPart editor, final int offset, final int length) {
    if (editor instanceof ITextEditor) {
      ((ITextEditor) editor).selectAndReveal(offset, length);
      return;
    }

    // Support for non-text editor - try IGotoMarker interface
    if (editor instanceof IGotoMarker) {
      final IEditorInput input = editor.getEditorInput();
      if (input instanceof IFileEditorInput) {
        final IGotoMarker gotoMarkerTarget = (IGotoMarker) editor;
        WorkspaceModifyOperation op =
            new WorkspaceModifyOperation() {
              @Override
              protected void execute(IProgressMonitor monitor) throws CoreException {
                IMarker marker = null;
                try {
                  marker = ((IFileEditorInput) input).getFile().createMarker(IMarker.TEXT);
                  marker.setAttribute(IMarker.CHAR_START, offset);
                  marker.setAttribute(IMarker.CHAR_END, offset + length);

                  gotoMarkerTarget.gotoMarker(marker);

                } finally {
                  if (marker != null) {
                    marker.delete();
                  }
                }
              }
            };

        try {
          op.run(null);
        } catch (InvocationTargetException ex) {
          // reveal failed
        } catch (InterruptedException e) {
          Assert.isTrue(false, "this operation can not be canceled"); // $NON-NLS-1$
        }
      }
      return;
    }

    /*
     * Workaround: send out a text selection XXX: Needs to be improved, see
     * https://bugs.eclipse.org/bugs/show_bug.cgi?id=32214
     */
    if (editor != null && editor.getEditorSite().getSelectionProvider() != null) {
      IEditorSite site = editor.getEditorSite();
      if (site == null) {
        return;
      }

      ISelectionProvider provider = editor.getEditorSite().getSelectionProvider();
      if (provider == null) {
        return;
      }

      provider.setSelection(new TextSelection(offset, length));
    }
  }
  private void selectFiles(IProject project, List<? extends IResource> createdFiles) {
    // Attempt to select the newly created files in the Package Explorer
    IWorkbench workbench = AdtPlugin.getDefault().getWorkbench();
    IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
    IViewPart viewPart = page.findView(JavaUI.ID_PACKAGES);
    if (viewPart != null) {
      IWorkbenchPartSite site = viewPart.getSite();
      IJavaProject javaProject = null;
      try {
        javaProject = BaseProjectHelper.getJavaProject(project);
      } catch (CoreException e) {
        AdtPlugin.log(e, null);
      }
      final ISelectionProvider provider = site.getSelectionProvider();
      if (provider != null) {
        List<TreePath> pathList = new ArrayList<TreePath>();
        for (IResource file : createdFiles) {
          // Create a TreePath for the given file,
          // which should be the JavaProject, followed by the folders down to
          // the final file.
          List<Object> segments = new ArrayList<Object>();
          segments.add(file);
          IContainer folder = file.getParent();
          if (folder != null && !(folder instanceof IProject)) {
            segments.add(folder);
            // res folder
            folder = folder.getParent();
            if (folder != null && !(folder instanceof IProject)) {
              segments.add(folder);
            }
          }
          // project
          segments.add(javaProject);

          Collections.reverse(segments);
          TreePath path = new TreePath(segments.toArray());
          pathList.add(path);

          // IDEA: Maybe normalize the files backwards (IFile objects aren't unique)
          // by maybe using the package explorer icons instead
        }

        TreePath[] paths = pathList.toArray(new TreePath[pathList.size()]);
        final TreeSelection selection = new TreeSelection(paths);

        provider.setSelection(selection);

        // Workaround: The above doesn't always work; it will frequently select
        // some siblings of the real files. I've tried a number of workarounds:
        // normalizing the IFile objects by looking up the canonical ones via
        // their relative paths from the project; deferring execution with
        // Display.asyncRun; first calling select on the parents, etc.
        // However, it turns out a simple workaround works best: Calling this
        // method TWICE. The first call seems to expand all the necessary parents,
        // and the second call ensures that the correct children are selected!
        provider.setSelection(selection);

        viewPart.setFocus();
      }
    }
  }
 public void setSelection(ISelection selection) {
   if (delegate != null) {
     delegate.setSelection(selection);
   }
 }
 @Override
 protected void doSetList(Object source, List list) {
   ((ISelectionProvider) source).setSelection(new StructuredSelection(list));
 }
Ejemplo n.º 6
0
  @Override
  public void widgetSelected(SelectionEvent e) {

    ISelection selection = selectionProvider.getSelection();
    if (selection != null) selectionProvider.setSelection(selection);
  }