@Override
  public void selectionChanged(SelectionChangedEvent event) {
    ISelection selection = event.getSelection();
    if (selection instanceof IStructuredSelection) {

      // If the selection event source is different from the active part,
      // that means the selection change has not been triggered by a user
      // action. In that case we do nothing to avoid loops.
      if (event.getSource() != getActivePartSource()) {
        return;
      }

      // If the selection come from the navigator, we select the
      // corresponding representations elements.
      if (event.getSource().equals(navigator.getCommonViewer())) {
        IWorkbenchPage page = EclipseUIUtil.getActivePage();
        IEditorPart activeEditor = page.getActiveEditor();
        if (activeEditor instanceof DialectEditor) {
          DialectEditor dialectEditor = (DialectEditor) activeEditor;
          page.bringToTop(dialectEditor);
          selectRepresentationElements(selection, dialectEditor);
        }
      } else {
        Set<EObject> targets = getTargetsFromSelection((IStructuredSelection) selection);
        if (!targets.isEmpty()) {
          navigator.selectReveal(new StructuredSelection(targets.toArray()));
        }
      }
    }
  }
 public void hookOnViewer(final String viewerId) {
   IWorkbench workbench = PlatformUI.getWorkbench();
   if (viewerId != null && workbench != null && workbench.getDisplay() != null) {
     Display display = workbench.getDisplay();
     Thread displayThread = display.getThread();
     if (workbench.isStarting() || !Thread.currentThread().equals(displayThread)) {
       // while workbench is starting defer hooking until later
       UIJob job =
           new UIJob(display, "viewer hooker") {
             @Override
             public IStatus runInUIThread(IProgressMonitor monitor) {
               hookOnViewer(viewerId);
               return Status.OK_STATUS;
             }
           };
       job.schedule(250);
     } else if (viewerId != null) {
       CommonNavigator navigator =
           (CommonNavigator) eu.fittest.eclipse.gui.utils.Viewer.getView(viewerId);
       if (navigator != null) {
         CommonViewer viewer = navigator.getCommonViewer();
         if (viewer != null) {
           if (this.viewer != null) {
             this.viewer.removeSelectionChangedListener(this);
           }
           requestRefresh();
           viewer.addSelectionChangedListener(this);
           this.viewer = viewer;
         }
       }
     }
   }
 }
 /**
  * Removes this instance from the {@link IPartListener2}s and from all {@link DialectEditor}
  * selectionListeners.
  */
 private void disableSelectionListener() {
   if (partService != null) {
     navigator.getCommonViewer().removeSelectionChangedListener(this);
     partService.removePartListener(this);
     for (DialectEditor dialectEditor : registeredEditors) {
       removeSelectionListener(dialectEditor);
     }
     registeredEditors.clear();
   }
 }
Example #4
0
 public void run(IAction action) {
   if (navigator == null) {
     return;
   }
   Grouping grouping = getGrouping(action.getId());
   if (grouping == null) {
     return;
   }
   BugContentProvider provider =
       BugContentProvider.getProvider(navigator.getNavigatorContentService());
   provider.setGrouping(grouping);
   CommonViewer viewer = navigator.getCommonViewer();
   Object[] expandedElements = viewer.getExpandedElements();
   provider.reSetInput();
   viewer.setExpandedElements(expandedElements);
 }
  public static IFolder getFolder() {

    CommonNavigator navigator = (CommonNavigator) Viewer.getView(IFITTESTFolderConstants.NAVIGATOR);

    if (navigator != null) {
      CommonViewer viewer = navigator.getCommonViewer();
      if (viewer != null) {
        ISelection selection = viewer.getSelection();
        if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
          IStructuredSelection sel = (IStructuredSelection) selection;
          Object object = sel.getFirstElement();
          return (object instanceof IFolder && !(object instanceof IProject))
              ? (IFolder) object
              : null;
        }
      }
    }
    return null;
  }
 /**
  * Registers this instance as a {@link IPartListener2} and as a {@link ISelectionChangedListener}
  * on each opened {@link DialectEditor}.
  */
 private void enableSelectionListener() {
   if (partService != null) {
     // We register this listener on the current viewer to have a
     // bidirectional selection.
     navigator.getCommonViewer().addSelectionChangedListener(this);
     partService.addPartListener(this);
     IEditorReference[] editorReferences =
         PlatformUI.getWorkbench()
             .getActiveWorkbenchWindow()
             .getActivePage()
             .getEditorReferences();
     for (IEditorReference editorReference : editorReferences) {
       IEditorPart editorPart = editorReference.getEditor(false);
       if (editorPart instanceof DialectEditor) {
         addSelectionListener((DialectEditor) editorPart);
         registeredEditors.add((DialectEditor) editorPart);
       }
     }
   }
 }
Example #7
0
  /*
   * (non-Javadoc)
   * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
   */
  @Override
  protected IContributionItem[] getContributionItems() {
    CommonNavigator navigator = (CommonNavigator) ViewsUtil.getPart(IUIConstants.ID_EXPLORER);
    if (navigator == null) return new IContributionItem[0];

    INavigatorContentService contentService = navigator.getNavigatorContentService();
    List<IContributionItem> items = new ArrayList<IContributionItem>();
    List<String> extensionSet = new MRUList(IPreferenceKeys.PREF_CONTENT_MRU_LIST);
    CommonViewer commonViewer = navigator.getCommonViewer();
    for (int i = 0; i < extensionSet.size(); i++) {
      String extensionId = extensionSet.get(i);
      INavigatorContentDescriptor contentDescriptor =
          contentService.getContentDescriptorById(extensionId);
      if (contentDescriptor != null) {
        items.add(
            new ActionContributionItem(
                new ContentMRUAction((i + 1), contentDescriptor, contentService, commonViewer)));
      }
    }
    return items.toArray(new IContributionItem[items.size()]);
  }
  @Test
  public void testFilter() throws CoreException {
    IPerspectiveDescriptor persDescription =
        PlatformUI.getWorkbench()
            .getPerspectiveRegistry()
            .findPerspectiveWithId(JavaUI.ID_PERSPECTIVE);
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    page.setPerspective(persDescription);
    IViewPart arquilliaView = page.showView(ArquillianView.ID);
    try {
      assertTrue(arquilliaView instanceof CommonNavigator);
      PackageExplorerPart packageView = (PackageExplorerPart) page.showView(JavaUI.ID_PACKAGES);
      IProject project = getProject(TEST_PROJECT_NAME);
      packageView.selectAndReveal(project);

      CommonNavigator navigator = (CommonNavigator) arquilliaView;
      CommonViewer viewer = navigator.getCommonViewer();
      TreeItem[] items = viewer.getTree().getItems();
      assertTrue(items.length == 1);
      ViewerFilter[] filters = viewer.getFilters();
      assertTrue(filters.length > 0);
      WorkingSetFilter activeFilter = null;
      List<ViewerFilter> newFilters = new ArrayList<ViewerFilter>();
      int i = 0;
      for (ViewerFilter filter : filters) {
        if (!(filter instanceof ActiveProjectFilter)) {
          newFilters.add(filter);
        }
      }
      newFilters.add(new WorkingSetFilter());
      viewer.setFilters(newFilters.toArray(new ViewerFilter[0]));

      items = viewer.getTree().getItems();
      assertTrue("Invalid filter", items.length == 2);
    } finally {
      if (arquilliaView != null) {
        page.hideView(arquilliaView);
      }
    }
  }
Example #9
0
  public void run(IAction action) {
    if (navigator != null) {
      CommonViewer viewer = navigator.getCommonViewer();
      BugContentProvider provider =
          BugContentProvider.getProvider(navigator.getNavigatorContentService());
      // provider.setRefreshRequested(true);

      Object[] expandedElements = viewer.getExpandedElements();
      Object input = viewer.getInput();
      if (provider.getChildren(input).length == 0) {
        viewer.setInput(null);
        if (input instanceof IWorkingSet || input instanceof IWorkspaceRoot) {
          viewer.setInput(input);
        } else {
          viewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
        }
        viewer.refresh(true);
      } else {
        provider.reSetInput();
      }
      viewer.setExpandedElements(expandedElements);
    }
  }
  @Override
  protected ISelection resolveSelection(
      IEditorPart editor, ITextSelection changedSelection, StructuredViewer viewer)
      throws CoreException {
    if (resolveFailed) {
      return null;
    }
    if (linkServiceMethod == null) {
      // TODO e3.5 replace with call to CommonNavigator.getLinkHelperService()
      try {
        try {
          // e3.5: get helper from common navigator
          Method method =
              CommonNavigator.class.getDeclaredMethod("getLinkHelperService"); // $NON-NLS-1$
          method.setAccessible(true);
          linkService = method.invoke(commonNavigator);
        } catch (NoSuchMethodException e) {
          // e3.3, e3.4: instantiate helper
          Class<?> clazz =
              Class.forName(
                  "org.eclipse.ui.internal.navigator.extensions.LinkHelperService"); //$NON-NLS-1$
          Constructor<?> constructor = clazz.getConstructor(NavigatorContentService.class);
          linkService =
              constructor.newInstance(
                  (NavigatorContentService)
                      commonNavigator.getCommonViewer().getNavigatorContentService());
        }
        linkServiceMethod =
            linkService
                .getClass()
                .getDeclaredMethod("getLinkHelpersFor", IEditorInput.class); // $NON-NLS-1$
      } catch (Throwable e) {
        resolveFailed = true;
        StatusHandler.log(
            new Status(
                IStatus.ERROR,
                ResourcesUiBridgePlugin.ID_PLUGIN,
                "Initialization of LinkHelperService failed",
                e)); //$NON-NLS-1$
      }
    }

    IEditorInput input = editor.getEditorInput();
    // TODO e3.5 replace with call to linkService.getLinkHelpersFor(editor.getEditorInput());
    ILinkHelper[] helpers;
    try {
      helpers = (ILinkHelper[]) linkServiceMethod.invoke(linkService, editor.getEditorInput());
    } catch (Exception e) {
      return null;
    }

    IStructuredSelection selection = StructuredSelection.EMPTY;
    IStructuredSelection newSelection = StructuredSelection.EMPTY;

    for (ILinkHelper helper : helpers) {
      selection = helper.findSelection(input);
      if (selection != null && !selection.isEmpty()) {
        newSelection = mergeSelection(newSelection, selection);
      }
    }
    if (!newSelection.isEmpty()) {
      return newSelection;
    }
    return null;
  }