Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6d76743ec702167650ab81ee5cdbe97e1e0d6ea9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
 * Copyright (c) 2013 Igor Fedorenko
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Igor Fedorenko - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.ui.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PlatformUI;


/**
 * Helpers to query and manipulate workbench working sets.
 * 
 * @since 1.5
 */
public class WorkingSets {

  /**
   * Returns all visible workbench working sets.
   * 
   * @since 1.5
   */
  public static String[] getWorkingSets() {
    List<String> workingSets = new ArrayList<String>();
    IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
    for(IWorkingSet workingSet : workingSetManager.getWorkingSets()) {
      if(workingSet.isVisible()) {
        workingSets.add(workingSet.getName());
      }
    }
    return workingSets.toArray(new String[workingSets.size()]);
  }

  /**
   * Returns existing or creates new workbench working set with the given name
   * 
   * @since 1.5
   */
  public static IWorkingSet getOrCreateWorkingSet(String workingSetName) {
    IWorkingSetManager wsm = PlatformUI.getWorkbench().getWorkingSetManager();
    IWorkingSet workingSet = wsm.getWorkingSet(workingSetName);
    if(workingSet == null) {
      workingSet = wsm.createWorkingSet(workingSetName, new IAdaptable[0]);
      // TODO is there a constant we should be setting here?
      workingSet.setId("org.eclipse.ui.resourceWorkingSetPage");
      wsm.addWorkingSet(workingSet);
    }
    return workingSet;
  }

  /**
   * Adds given projects to workbench working set with the given name. Creates new working set if workbench working set
   * with given name does not already exist.
   * 
   * @since 1.5
   */
  public static void addToWorkingSet(IProject[] projects, String workingSetName) {
    IWorkingSet[] workingSets = new IWorkingSet[] {getOrCreateWorkingSet(workingSetName)};
    IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
    for(IProject project : projects) {
      manager.addToWorkingSets(project, workingSets);
    }
  }

  /**
   * Adds given projects to given workbench working sets.
   * 
   * @since 1.5
   */
  public static void addToWorkingSets(IProject[] projects, List<IWorkingSet> workingSets) {
    // PlatformUI.getWorkbench().getWorkingSetManager().addToWorkingSets(project, new IWorkingSet[] {workingSet});
    if(projects != null && projects.length > 0 && workingSets != null && !workingSets.isEmpty()) {
      for(IWorkingSet workingSet : workingSets) {
        if(workingSet != null) {
          IAdaptable[] oldElements = workingSet.getElements();
          IAdaptable[] newElements = new IAdaptable[oldElements.length + projects.length];
          System.arraycopy(oldElements, 0, newElements, 0, oldElements.length);
          System.arraycopy(projects, 0, newElements, oldElements.length, projects.length);
          workingSet.setElements(newElements);
        }
      }
    }
  }

  /**
   * Adds given projects to given workbench working sets.
   * 
   * @since 1.5
   */
  public static void addToWorkingSets(Collection<IProject> projects, List<IWorkingSet> workingSets) {
    addToWorkingSets(projects.toArray(new IProject[projects.size()]), workingSets);
  }

  /**
   * Returns all projects that belong to workbench working sets.
   * 
   * @since 1.5
   */
  public static Set<IProject> getProjects() {
    Set<IProject> projects = new HashSet<IProject>();
    IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
    for(IWorkingSet workingSet : manager.getAllWorkingSets()) {
      try {
        for(IAdaptable element : workingSet.getElements()) {
          IProject project = (IProject) element.getAdapter(IProject.class);
          if(project != null) {
            projects.add(project);
          }
        }
      } catch(IllegalStateException ignored) {
        // ignore bad/misconfigured working sets
      }
    }
    return projects;
  }

  /**
   * Returns one of the working sets the element directly belongs to. Returns {@code null} if the element does not
   * belong to any working set.
   * 
   * @since 1.5
   */
  public static IWorkingSet getAssignedWorkingSet(IResource element) {
    IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
    for(IWorkingSet workingSet : workingSetManager.getWorkingSets()) {
      for(IAdaptable adaptable : workingSet.getElements()) {
        if(adaptable.getAdapter(IResource.class) == element) {
          return workingSet;
        }
      }
    }
    return null;
  }

  /**
   * Returns all working sets the element directly belongs to. Returns empty collection if the element does not belong
   * to any working set. The order of returned working sets is not specified.
   * 
   * @since 1.5
   */
  public static List<IWorkingSet> getAssignedWorkingSets(IResource element) {
    List<IWorkingSet> list = new ArrayList<IWorkingSet>();
    IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
    for(IWorkingSet workingSet : workingSetManager.getWorkingSets()) {
      for(IAdaptable adaptable : workingSet.getElements()) {
        if(adaptable.getAdapter(IResource.class) == element) {
          list.add(workingSet);
        }
      }
    }
    return list;
  }

}

Back to the top