Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e18a4ed014028454833440b2b43b2954a2c8941e (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*******************************************************************************
 * Copyright (c) 2009, 2012 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     QNX Software Systems - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.ui.workingsets;

import static org.eclipse.cdt.internal.ui.workingsets.WorkingSetConfigurationManager.ATTR_NAME;
import static org.eclipse.cdt.internal.ui.workingsets.WorkingSetConfigurationManager.KEY_PROJECT;

import java.util.List;
import java.util.Map;

import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IWorkingSet;

/**
 * Default implementation of the {@link IWorkingSetConfiguration} interface.
 *
 * @noextend This class is not intended to be subclassed by clients.
 *
 * @author Christian W. Damus (cdamus)
 *
 * @since 6.0
 *
 */
public class WorkingSetConfiguration implements IWorkingSetConfiguration {
	private final IWorkingSetProxy workingSet;
	private String name;

	private Map<String, IWorkingSetProjectConfiguration> projects;

	/**
	 * Initializes me with my parent working set.
	 *
	 * @param workingSet
	 *            my parent working set
	 */
	protected WorkingSetConfiguration(IWorkingSetProxy workingSet) {
		this.workingSet = workingSet;
	}

	/**
	 * Obtains my parent working set.
	 *
	 * @return my parent
	 */
	@Override
	public IWorkingSetProxy getWorkingSet() {
		return workingSet;
	}

	/**
	 * Obtains my name.
	 *
	 * @return my name
	 */
	@Override
	public String getName() {
		return name;
	}

	/**
	 * Sets my name.
	 *
	 * @param name
	 *            my new name
	 *
	 * @throws IllegalArgumentException
	 *             if the specified name is <code>null</code> or empty, or if it is already used by another
	 *             configuration in my warking set
	 */
	void setName(String name) {
		if ((name == null) || (name.length() == 0)) {
			throw new IllegalArgumentException("name is empty"); //$NON-NLS-1$
		}

		if (!name.equals(getName())) {
			if (getWorkingSet().getConfiguration(name) != null) {
				throw new IllegalArgumentException("name is already in use"); //$NON-NLS-1$
			}

			basicSetName(name);
		}
	}

	/**
	 * Provides simple access to the name for setting it.
	 *
	 * @param name
	 *            my new name
	 */
	protected void basicSetName(String name) {
		this.name = name;
	}

	private Map<String, IWorkingSetProjectConfiguration> getProjects() {
		if (projects == null) {
			projects = new java.util.HashMap<>();

			for (IProject next : workingSet.resolveProjects()) {
				IWorkingSetProjectConfiguration child = createProjectConfiguration(next);

				// the project may not be a C/C++ project
				if (child != null) {
					basicAddProjectConfiguration(child);
				}
			}
		}

		return projects;
	}

	protected void basicAddProjectConfiguration(IWorkingSetProjectConfiguration projectConfig) {
		if (projects == null) {
			projects = new java.util.HashMap<>();
		}

		projects.put(projectConfig.getProjectName(), projectConfig);
	}

	@Override
	public IWorkingSetProjectConfiguration getProjectConfiguration(String projectName) {
		return getProjects().get(projectName);
	}

	@Override
	public java.util.Collection<IWorkingSetProjectConfiguration> getProjectConfigurations() {
		return getProjects().values();
	}

	@Override
	public boolean isActive() {
		boolean result = !getProjects().isEmpty();

		if (result) {
			for (IWorkingSetProjectConfiguration next : getProjectConfigurations()) {
				if (!next.isActive()) {
					result = false;
					break;
				}
			}
		}

		return result;
	}

	@Override
	public void activate() {
		if (!isActive()) {
			for (IWorkingSetProjectConfiguration next : getProjectConfigurations()) {
				next.activate();
			}
		}

		// this is a "recently used" working set
		IWorkingSet ws = getWorkingSet().resolve();
		if (ws != null) {
			WorkingSetConfigurationManager.WS_MGR.addRecentWorkingSet(ws);
		}
	}

	@Override
	public IStatus build(IProgressMonitor monitor) {
		MultiStatus result = new MultiStatus(CUIPlugin.PLUGIN_ID, 0, WorkingSetMessages.WSConfig_build_problems, null);

		List<IWorkingSetProjectConfiguration> toBuild = new java.util.ArrayList<>(
				getProjectConfigurations().size());
		for (IWorkingSetProjectConfiguration next : getProjectConfigurations()) {
			IProject project = next.resolveProject();

			if ((project != null) && (project.isAccessible())) {
				toBuild.add(next);
			}
		}

		SubMonitor progress = SubMonitor.convert(monitor,
				NLS.bind(WorkingSetMessages.WSConfig_build_task, getWorkingSet().getName()), toBuild.size());

		try {
			for (IWorkingSetProjectConfiguration next : toBuild) {
				if (progress.isCanceled()) {
					result.add(Status.CANCEL_STATUS);
					break;
				}

				IStatus status = next.build(progress.newChild(1));

				if ((status != null && !status.isOK())) {
					result.add(status);
				}
			}
		} finally {
			if (monitor != null) {
				monitor.done();
			}
		}

		return result.isOK() ? Status.OK_STATUS : result;
	}

	@Override
	public void saveState(IMemento memento) {
		memento.putString(ATTR_NAME, getName());

		for (IWorkingSetProjectConfiguration next : getProjectConfigurations()) {
			next.saveState(memento.createChild(KEY_PROJECT));
		}
	}

	@Override
	public void loadState(IMemento memento) {
		setName(memento.getString(ATTR_NAME));

		Map<String, IMemento> projectMementos = new java.util.HashMap<>();
		for (IMemento next : memento.getChildren(KEY_PROJECT)) {
			projectMementos.put(next.getString(ATTR_NAME), next);
		}

		for (IWorkingSetProjectConfiguration next : getProjectConfigurations()) {
			IMemento state = projectMementos.get(next.getProjectName());
			if (state != null) {
				next.loadState(state);
			}
		}
	}

	/**
	 * Creates a new project configuration for the specified project. May be overridden by subclasses to
	 * create a different implementation.
	 *
	 * @param project
	 *            a workspace project
	 * @return a new project configuration element for it
	 */
	protected IWorkingSetProjectConfiguration createProjectConfiguration(IProject project) {
		IWorkingSetProjectConfiguration result = null;

		IWorkingSetProjectConfigurationFactory factory = IWorkingSetProjectConfigurationFactory.Registry.INSTANCE
				.getFactory(project);
		if (factory != null) {
			result = factory.createProjectConfiguration(this, project);
		}

		return result;
	}

	@Override
	public ISnapshot createSnapshot(IWorkingSetProxy.ISnapshot workingSet, WorkspaceSnapshot workspace) {

		return new Snapshot(workingSet, this, workspace);
	}

	/**
	 * Utility method to query whether the specified configuration is a read-only snapshot.
	 *
	 * @param config
	 *            a working set configuration
	 * @return whether it is a read-only snapshot
	 *
	 * @see IWorkingSetConfiguration.ISnapshot#isReadOnly()
	 */
	static boolean isReadOnly(IWorkingSetConfiguration config) {
		return (config instanceof WorkingSetConfiguration.Snapshot)
				&& ((WorkingSetConfiguration.Snapshot) config).isReadOnly();
	}

	//
	// Nested classes
	//

	/**
	 * Default implementation of the mutable working set configuration snapshot.
	 *
	 * @author Christian W. Damus (cdamus)
	 *
	 * @noextend This class is not intended to be subclassed by clients.
	 *
	 * @since 6.0
	 */
	public static class Snapshot extends WorkingSetConfiguration implements IWorkingSetConfiguration.ISnapshot {

		private final boolean readOnly;
		private final WorkspaceSnapshot workspace;

		/**
		 * Initializes me with the current workspace snapshot.
		 *
		 * @param workingSet
		 *            my parent working set
		 * @param workspace
		 *            the current workspace snapshot
		 */
		protected Snapshot(IWorkingSetProxy workingSet, WorkspaceSnapshot workspace) {
			this(workingSet, workspace, false);
		}

		/**
		 * Initializes me as a special read-only configuration that shows what is the current active
		 * configuration of the projects in a working set when none of its named configurations is active.
		 *
		 * @param workingSet
		 *            my parent working set
		 * @param workspace
		 *            the current workspace snapshot
		 * @param readOnly
		 *            whether I am read-only. A read-only configuration cannot be modified in the dialog
		 */
		protected Snapshot(IWorkingSetProxy workingSet, WorkspaceSnapshot workspace, boolean readOnly) {
			super(workingSet);

			this.readOnly = readOnly;
			this.workspace = workspace;
		}

		/**
		 * Initializes me with the current workspace snapshot.
		 *
		 * @param workingSet
		 *            my parent working set
		 * @param config
		 *            the working set configuration that I copy
		 * @param workspace
		 *            the current workspace snapshot
		 */
		protected Snapshot(IWorkingSetProxy workingSet, IWorkingSetConfiguration config, WorkspaceSnapshot workspace) {
			this(workingSet, workspace);

			setName(config.getName());

			for (IWorkingSetProjectConfiguration next : config.getProjectConfigurations()) {
				basicAddProjectConfiguration(next.createSnapshot(this, workspace));
			}
		}

		@Override
		public final IWorkingSetProxy.ISnapshot getWorkingSet() {
			return (IWorkingSetProxy.ISnapshot) super.getWorkingSet();
		}

		@Override
		public final WorkspaceSnapshot getWorkspaceSnapshot() {
			return workspace;
		}

		/**
		 * Queries whether I am a read-only view of the current active configurations of my working set's
		 * projects.
		 *
		 * @return whether I am read-only
		 */
		@Override
		public final boolean isReadOnly() {
			return readOnly;
		}

		@Override
		public void setName(String name) {
			super.setName(name);
		}

		/**
		 * I create project configurations that are mutable, as I am.
		 */
		@Override
		protected IWorkingSetProjectConfiguration createProjectConfiguration(IProject project) {
			IWorkingSetProjectConfiguration result = null;

			IWorkingSetProjectConfigurationFactory factory = IWorkingSetProjectConfigurationFactory.Registry.INSTANCE
					.getFactory(project);
			if (factory != null) {
				result = factory.createProjectConfiguration(this, project).createSnapshot(this, workspace);
			}

			return result;
		}
	}
}

Back to the top