Skip to main content
summaryrefslogtreecommitdiffstats
blob: f49e2c63c1f2b1170991ffd68ff06d7b839d2fa8 (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
package org.eclipse.search.internal.workingsets;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;

import org.eclipse.jface.util.Assert;

import org.eclipse.search.ui.IWorkingSet;


public class WorkingSet implements IWorkingSet {

	private static Set fgWorkingSets= new HashSet(5);
	
	String fName;
	Set fElements; // of IResources

	WorkingSet(String name, Object[] elements) {
		setName(name);
		setResources(elements);
	}

	void setResources(Object[] elements) {
		Assert.isNotNull(elements, "IPath array must not be null");
		fElements= new HashSet(elements.length);
		for (int i= 0; i < elements.length; i++) {
			Assert.isTrue(elements[i] instanceof IResource);
			Assert.isTrue(!fElements.contains(elements[i]), "elements must only contain each element once");
			fElements.add(elements[i]);
		}
	}

	void setPaths(IPath[] elements) {
		Assert.isNotNull(elements, "IPath array must not be null");
		fElements= new HashSet(elements.length);
		for (int i= 0; i < elements.length; i++) {
			Assert.isTrue(!fElements.contains(elements[i]), "elements must only contain each element once");
			fElements.add(elements[i]);
		}
	}

	/*
	 * @see IWorkingSet#getName()
	 */
	public String getName() {
		return fName;
	}

	void setName(String name) {
		Assert.isNotNull(name, "name must not be null");
		fName= name;
	}

	/*
	 * @see IWorkingSet#getResources()
	 */
	public IResource[] getResources() {
		return (IResource[])fElements.toArray(new IResource[fElements.size()]);
	}

	public boolean equals (Object o) {
		return (o instanceof IWorkingSet) && ((IWorkingSet)o).getName().equals(getName());
	}

	public int hashCode() {
		return fName.hashCode();
	}

	/**
	 * Returns the workbench from which this plugin has been loaded.
	 */	
	public static IWorkingSet[] getWorkingSets() {
		return (IWorkingSet[])fgWorkingSets.toArray(new IWorkingSet[fgWorkingSets.size()]);
	}

	/**
	 * Finds a working set by name.
	 *
	 * This method is for internal use only due to issue below. Once
	 * the issues is solved there will be an official API.
	 * </p>
	 * <p>
	 * [Issue: Working set must be provided by platform.]
	 * </p>
	 * 
	 * @param name the name the working set
	 * @return the working set with the given name or <code>null</code> if not found
	 */
	public static IWorkingSet find(String name) {
		if (name == null || fgWorkingSets == null)
			return null;
		
		Iterator iter= fgWorkingSets.iterator();
		while (iter.hasNext()) {
			IWorkingSet workingSet= (IWorkingSet)iter.next();
			if (name.equals(workingSet.getName()))
				return workingSet;
		}
		return null;
	}

	/**
	 * Removes the working set from the workspace.
	 * This is a NOP if the working set does not exist in the workspace.
	 */	
	static void remove(IWorkingSet workingSet) {
		fgWorkingSets.remove(workingSet);
	}

	/**
	 * Adds the working set to the workspace.
	 * The working set must not exist yet.
	 */	
	static void add(IWorkingSet workingSet) {
		Assert.isTrue(!fgWorkingSets.contains(workingSet), "working set already registered");
		fgWorkingSets.add(workingSet);
	}
}

Back to the top