Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9827bf90befa7f92a70e5e14479c4edcf2117c6e (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
package org.eclipse.search.internal.workingsets;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.eclipse.core.resources.IResource;

import org.eclipse.jface.util.Assert;

import org.xml.sax.SAXException;

import org.eclipse.search.ui.IWorkingSet;
import org.eclipse.search.ui.SearchUI;

import org.eclipse.search.internal.ui.SearchMessages;
import org.eclipse.search.internal.ui.SearchPlugin;
import org.eclipse.search.internal.ui.util.ExceptionHandler;

public class WorkingSet implements IWorkingSet {

	private static SortedSet fgWorkingSets= new TreeSet(new WorkingSetComparator());
	

	// XML tags
	static String TAG_WORKINGSETS= "workingsets"; //$NON-NLS-1$
	static String TAG_WORKINGSET= "workingset"; //$NON-NLS-1$
	static String TAG_NAME= "name"; //$NON-NLS-1$
	static String TAG_CONTENTS= "contents"; //$NON-NLS-1$
	static String TAG_FILE= "file"; //$NON-NLS-1$
	static String TAG_FOLDER= "folder"; //$NON-NLS-1$
	static String TAG_PATH= "path"; //$NON-NLS-1$
	static String TAG_PROJECT= "project"; //$NON-NLS-1$

	// Persistency
	static String STORE_NAME= "workingsets.xml"; //$NON-NLS-1$
	static {
		restore();
	}
	
	String fName;
	Set fElements; // of IResources

	WorkingSet(String name, Object[] elements) {
		Assert.isNotNull(name, "name must not be null");
		fName= name;
		setResources(elements, true);
	}

	void setResources(Object[] elements) {
		setResources(elements, false);
	}

	private void setResources(Object[] elements, boolean internal) {
		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]);
		}
		if (!internal)
			saveWorkingSets();
	}

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

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

	/*
	 * @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 all working sets for the workspace.
	 *
	 * 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>
	 * 
	 * @return an array of IWorkingSet
	 */
	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);
		saveWorkingSets();
	}

	// Persistency

	private static void restore() {
		WorkingSetReader reader= null;
		IWorkingSet[] workingSets= null;		
		try {
			File file= SearchPlugin.getDefault().getStateLocation().append(STORE_NAME).toFile();
			if (!file.exists())
				return;
			reader= new WorkingSetReader(new BufferedInputStream(new FileInputStream(file)));
			workingSets= reader.readXML();
		} catch (IOException ex) {
			String message= SearchMessages.getFormattedString("WorkingSet.error.readingFile", ex.getMessage()); //$NON-NLS-1$
			ExceptionHandler.log(ex, message);
		} catch (SAXException ex) {
			String message= SearchMessages.getFormattedString("WorkingSet.error.badXmlFormat", ex.getMessage()); //$NON-NLS-1$
			ExceptionHandler.log(ex, message);
		} finally {
			try {
				if (reader != null)
					reader.close();
			}
			catch (IOException ex) {
				String message= SearchMessages.getFormattedString("WorkingSet.error.close", ex.getMessage()); //$NON-NLS-1$
				ExceptionHandler.log(ex, message);
			}
		}
		if (workingSets != null)
			for (int i= 0; i < workingSets.length; i++)
				WorkingSet.add(workingSets[i]);
	}

	private static void saveWorkingSets() {
		WorkingSetWriter writer= null;
		try {
			File file= SearchPlugin.getDefault().getStateLocation().append(STORE_NAME).toFile();
			writer= new WorkingSetWriter(new BufferedOutputStream(new FileOutputStream(file)));
			writer.writeXML(SearchUI.getWorkingSets());
		} catch (IOException ex) {
			String message= SearchMessages.getFormattedString("WorkingSet.error.readingFile", ex.getMessage()); //$NON-NLS-1$
			ExceptionHandler.log(ex, message);
		} finally {
			if (writer != null)
				try {
					writer.close();
				} catch (IOException ex) {
					String message= SearchMessages.getFormattedString("WorkingSet.error.readingFile", ex.getMessage()); //$NON-NLS-1$
					ExceptionHandler.log(ex, message);
				}
		}
	}
}

Back to the top