public void init(IWorkbench workbench, IStructuredSelection selection) {
   if (selection instanceof TreeSelection) {
     try {
       TreeSelection treeSelection = (TreeSelection) selection;
       for (TreePath path : treeSelection.getPaths()) {
         for (int i = 0; i < path.getSegmentCount(); i++) {
           Object obj = path.getSegment(i);
           if (obj instanceof Category<?>) {
             setLighthouseDomain(((Category<?>) obj).getLighthouseDomain());
           }
           if (obj instanceof Operation) {
             addOperation((Operation) obj);
           }
           if (obj instanceof LighthouseDomain) {
             setLighthouseDomain((LighthouseDomain) obj);
           }
           if (obj instanceof Deployment) {
             addDeployment((Deployment) obj);
           }
         }
       }
       if (treeSelection.getPaths().length == 0
           || (selectedDeployments == null && selectedOperations == null)) {
         tryToInitFromActiveEditor();
       }
     } catch (Exception e) {
       OperationsUI.getPlugin()
           .getLog()
           .log(new Status(IStatus.ERROR, OperationsUI.PLUGIN_ID, e.getMessage(), e));
     }
   } else {
     tryToInitFromActiveEditor();
   }
 }
  @SuppressWarnings({"unchecked", "rawtypes"})
  private IWorkingSet[] getSelectedWorkingSet(IStructuredSelection selection) {
    if (!(selection instanceof ITreeSelection)) return EMPTY_WORKING_SET_ARRAY;

    ITreeSelection treeSelection = (ITreeSelection) selection;
    if (treeSelection.isEmpty()) return EMPTY_WORKING_SET_ARRAY;

    List elements = treeSelection.toList();
    if (elements.size() == 1) {
      Object element = elements.get(0);
      TreePath[] paths = treeSelection.getPathsFor(element);
      if (paths.length != 1) return EMPTY_WORKING_SET_ARRAY;

      TreePath path = paths[0];
      if (path.getSegmentCount() == 0) return EMPTY_WORKING_SET_ARRAY;

      Object candidate = path.getSegment(0);
      if (!(candidate instanceof IWorkingSet)) return EMPTY_WORKING_SET_ARRAY;

      IWorkingSet workingSetCandidate = (IWorkingSet) candidate;
      if (isValidWorkingSet(workingSetCandidate)) return new IWorkingSet[] {workingSetCandidate};

      return EMPTY_WORKING_SET_ARRAY;
    }

    ArrayList result = new ArrayList();
    for (Iterator iterator = elements.iterator(); iterator.hasNext(); ) {
      Object element = iterator.next();
      if (element instanceof IWorkingSet && isValidWorkingSet((IWorkingSet) element)) {
        result.add(element);
      }
    }
    return (IWorkingSet[]) result.toArray(new IWorkingSet[result.size()]);
  }
 @Test
 public void headPathShouldReturnTheCorrectSubPath() {
   TreePath path = new TreePath(new Object[] {"a", "b", "c"});
   TreePath expected = path.getParentPath();
   TreePath actual = TreePaths.headPath(path, path.getSegmentCount() - 1);
   assertThat(actual, equalTo(expected));
 }
 /**
  * Recursively searches the VMC for Launch VMC, and returns its ILaunch. Returns null if an
  * ILaunch is not found.
  */
 private ILaunch findLaunch(TreePath path) {
   for (int i = path.getSegmentCount() - 1; i >= 0; i--) {
     if (path.getSegment(i) instanceof ILaunch) {
       return (ILaunch) path.getSegment(i);
     }
   }
   return null;
 }
 @Test
 public void headPathShouldReturnAnEqualPathIfIndexIsEqualToThePathLength() {
   TreePath path = newPath();
   assertThat(TreePaths.headPath(path, path.getSegmentCount()), equalTo(path));
 }
 @Test
 public void headPathShouldThrowAnExceptionIfIndexIsGreaterThanThePathLength() {
   TreePath path = newPath();
   thrown.expect(IllegalArgumentException.class);
   TreePaths.headPath(path, path.getSegmentCount() + 1);
 }