private IWorkingSet[] getSelectedWorkingSet(
      IStructuredSelection selection, IWorkbenchPart activePart) {
    IWorkingSet[] selected = getSelectedWorkingSet(selection);
    if (selected != null && selected.length > 0) {
      for (int i = 0; i < selected.length; i++) {
        if (!isValidWorkingSet(selected[i])) return EMPTY_WORKING_SET_ARRAY;
      }
      return selected;
    }

    if (!(activePart instanceof PackageExplorerPart)) return EMPTY_WORKING_SET_ARRAY;

    PackageExplorerPart explorerPart = (PackageExplorerPart) activePart;
    if (explorerPart.getRootMode() == PackageExplorerPart.PROJECTS_AS_ROOTS) {
      // Get active filter
      IWorkingSet filterWorkingSet = explorerPart.getFilterWorkingSet();
      if (filterWorkingSet == null) return EMPTY_WORKING_SET_ARRAY;

      if (!isValidWorkingSet(filterWorkingSet)) return EMPTY_WORKING_SET_ARRAY;

      return new IWorkingSet[] {filterWorkingSet};
    } else {
      // If we have been gone into a working set return the working set
      Object input = explorerPart.getViewPartInput();
      if (!(input instanceof IWorkingSet)) return EMPTY_WORKING_SET_ARRAY;

      IWorkingSet workingSet = (IWorkingSet) input;
      if (!isValidWorkingSet(workingSet)) return EMPTY_WORKING_SET_ARRAY;

      return new IWorkingSet[] {workingSet};
    }
  }
  @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);
      }
    }
  }
  protected void onRun(IAction action) throws Exception {
    IEditorPart editor = getActiveEditor();

    if (editor != null) {
      IEditorInput input = editor.getEditorInput();

      Object resource = input.getAdapter(IJavaElement.class);

      if (resource == null) {
        resource = input.getAdapter(IFile.class);
      }

      if (resource != null) {
        PackageExplorerPart.openInActivePerspective().selectAndReveal(resource);
      }
    }
  }