Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 38048 | Differences between
and this patch

Collapse All | Expand All

(-)org/eclipse/team/core/IProjectSetSerializer.java (+4 lines)
Lines 20-25 Link Here
20
 * Given this String, it can create in the workspace an IProject.
20
 * Given this String, it can create in the workspace an IProject.
21
 * 
21
 * 
22
 * @since 2.0
22
 * @since 2.0
23
 * 
24
 * @deprecated 
25
 * 		Use {@link org.eclipse.team.core.RepositoryProviderType#getProjectSetSerializer()}
26
 * 		to obtain an instance of {@link org.eclipse.team.core.ProjectSetSerializer} instead.
23
 */
27
 */
24
28
25
public interface IProjectSetSerializer {
29
public interface IProjectSetSerializer {
(-)org/eclipse/team/core/RepositoryProviderType.java (+29 lines)
Lines 156-159 Link Here
156
	public ProjectSetCapability getProjectSetCapability() {
156
	public ProjectSetCapability getProjectSetCapability() {
157
		return null;
157
		return null;
158
	}
158
	}
159
	
160
	/**
161
	 * Answers an object for serializing and deserializing
162
	 * of references to projects.  Given a project, it can produce a
163
	 * UTF-8 encoded String which can be stored in a file.
164
	 * Given this String, it can load a project into the workspace.
165
	 * <p>
166
	 * Subclasses should override this method to return the appropriate
167
	 * serializer for the associated repository type.
168
	 * It is recommended that serializers not have any references to UI classes
169
	 * so that they can be used in a headless environment.
170
	 * <p>
171
	 * At this time, the default implementation wrappers the <code>IProjectSetSerializer</code>
172
	 * interface if one exists, providing backward compatibility with existing code.
173
	 * At some time in the future, the <code>IProjectSetSerializer</code> interface will be removed
174
	 * and the default implementation will revert to having limited functionality.
175
	 * 
176
	 * @return the serializer (not <code>null</code>)
177
	 * @since 3.0
178
	 */
179
	public ProjectSetSerializer getProjectSetSerializer() {
180
		
181
		// Provide backward compatibility with the old IProjectSetSerializer interface
182
		IProjectSetSerializer oldSerializer = Team.getProjectSetSerializer(getID());
183
		if (oldSerializer != null)
184
			return new DefaultProjectSetSerializer(oldSerializer);
185
		
186
		return new ProjectSetSerializer();
187
	}
159
}
188
}
(-)org/eclipse/team/core/Team.java (+5 lines)
Lines 610-615 Link Here
610
	public static void shutdown() {
610
	public static void shutdown() {
611
		TeamPlugin.getPlugin().savePluginPreferences();
611
		TeamPlugin.getPlugin().savePluginPreferences();
612
	}
612
	}
613
	/**
614
	 * @deprecated 
615
	 * 		Use {@link org.eclipse.team.core.RepositoryProviderType#getProjectSetSerializer()}
616
	 * 		to obtain an instance of {@link org.eclipse.team.core.ProjectSetSerializer} instead.
617
	 */
613
	public static IProjectSetSerializer getProjectSetSerializer(String id) {
618
	public static IProjectSetSerializer getProjectSetSerializer(String id) {
614
		TeamPlugin plugin = TeamPlugin.getPlugin();
619
		TeamPlugin plugin = TeamPlugin.getPlugin();
615
		if (plugin != null) {
620
		if (plugin != null) {
(-)src/org/eclipse/team/core/DefaultProjectSetSerializer.java (+58 lines)
Added Link Here
1
package org.eclipse.team.core;
2
3
import org.eclipse.core.resources.IProject;
4
import org.eclipse.core.runtime.IProgressMonitor;
5
6
/**
7
 * An internal class for backward compatibility with the 
8
 * {@link org.eclipse.team.core.IProjectSetSerializer} interface.
9
 * 
10
 * @since 3.0
11
 */
12
final class DefaultProjectSetSerializer extends ProjectSetSerializer {
13
14
	/**
15
	 * The old serialization interface
16
	 */
17
	private IProjectSetSerializer serializer;
18
19
	/**
20
	 * Create a new instance wrappering the specified serializer.
21
	 * 
22
	 * @param serializer the old serialization interface
23
	 */
24
	public DefaultProjectSetSerializer(IProjectSetSerializer serializer) {
25
		this.serializer = serializer;
26
	}
27
28
	/**
29
	 * Redirect the request to the old serialization interface
30
	 * 
31
	 * @see IProjectSetSerializer
32
	 * @see org.eclipse.team.core.ProjectSetSerializer#asReference(org.eclipse.core.resources.IProject[], org.eclipse.team.core.ProjectSetSerializationContext, org.eclipse.core.runtime.IProgressMonitor)
33
	 */
34
	public String[] asReference(
35
		IProject[] providerProjects,
36
		ProjectSetSerializationContext context,
37
		IProgressMonitor monitor)
38
		throws TeamException {
39
40
		return serializer.asReference(providerProjects, context.getIProjectSetSerializerContext(), monitor);
41
	}
42
43
	/**
44
	 * Redirect the request to the old serialization interface
45
	 * 
46
	 * @see IProjectSetSerializer
47
	 * @see org.eclipse.team.core.ProjectSetSerializer#addToWorkspace(java.lang.String[], org.eclipse.team.core.ProjectSetSerializationContext, org.eclipse.core.runtime.IProgressMonitor)
48
	 */
49
	public IProject[] addToWorkspace(
50
		String[] referenceStrings,
51
		ProjectSetSerializationContext context,
52
		IProgressMonitor monitor)
53
		throws TeamException {
54
			
55
		return serializer.addToWorkspace(referenceStrings, null, context.getIProjectSetSerializerContext(), monitor);
56
	}
57
58
}
(-)src/org/eclipse/team/core/ProjectSetSerializationContext.java (+72 lines)
Added Link Here
1
package org.eclipse.team.core;
2
3
import org.eclipse.core.resources.IProject;
4
import org.eclipse.core.runtime.IPath;
5
6
/**
7
 * The context in which project serialization occurs.
8
 * The class may be subclasses to represent different serialization contexts.
9
 * 
10
 * @since 3.0
11
 */
12
public class ProjectSetSerializationContext {
13
	
14
	/**
15
	 * Given an array of projects that currently exist in the workspace
16
	 * determine which of those projects should be overwritten.
17
	 * <p>
18
	 * This default implementation always returns an empty array
19
	 * indicating that no existing projects should be overwritten.
20
	 * Subclasses may override this as appropriate.
21
	 * 
22
	 * @param projects 
23
	 * 		an array of projects currently existing in the workspace
24
	 * 		that are desired to be overwritten.
25
	 * 		(not <code>null</code>, contains no <code>null</code>s)
26
	 * @return
27
	 * 		an array of zero or more projects that should be overwritten
28
	 * 		or <code>null</code> if the operation is to be canceled
29
	 */
30
	public IProject[] confirmOverwrite(IProject[] projects) throws TeamException {
31
		return new IProject[0];
32
	}
33
	
34
	/**
35
	 * Given an array of projects to be loaded in the workspace
36
	 * determine where those projects should be located.
37
	 * <p>
38
	 * This default implementation always returns an array of <code>null</code>s
39
	 * indicating that all projects should be loaded in their default locations.
40
	 * 
41
	 * @param projects
42
	 * 		An array of projects to be loaded into the workspace.
43
	 * 		A project with this name may or may not already exist in the workspace.
44
	 * 		(not <code>null</code>, contains no <code>null</code>s)
45
	 * @return
46
	 * 		An array containing the same number of elements as the projects array
47
	 * 		or <code>null</code> if the operation should be canceled.
48
	 * 		Each element in the array is either an absolute path 
49
	 * 		indicating the location where the corresponding project should be loaded 
50
	 * 		or a <code>null</code> indicating that the corresponding project 
51
	 * 		should be loaded in its default location in the workspace.
52
	 */
53
	public IPath[] getLocalProjectLocations(IProject[] projects) {
54
		return new IPath[projects.length];
55
	}
56
57
	/**
58
	 * Return a shell if there is a UI context 
59
	 * or <code>null</code> if executing headless.
60
	 *
61
	 * @return the shell or <code>null</code>
62
	 * 
63
	 * @deprecated 
64
	 * 		This method exist solely for backward compatibility
65
	 * 		with the {@link IProjectSetSerializer} interface
66
	 * 		and should not be used for any other purpose.
67
	 */
68
	public Object getIProjectSetSerializerContext() {
69
		return null;
70
	}
71
	
72
}
(-)src/org/eclipse/team/core/ProjectSetSerializer.java (+218 lines)
Added Link Here
1
package org.eclipse.team.core;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.Collection;
6
7
import org.eclipse.core.resources.IProject;
8
import org.eclipse.core.resources.IProjectDescription;
9
import org.eclipse.core.resources.ResourcesPlugin;
10
import org.eclipse.core.runtime.CoreException;
11
import org.eclipse.core.runtime.IPath;
12
import org.eclipse.core.runtime.IProgressMonitor;
13
import org.eclipse.core.runtime.SubProgressMonitor;
14
15
/**
16
 * An object for serializing and deserializing
17
 * of references to projects.  Given a project, it can produce a
18
 * UTF-8 encoded String which can be stored in a file.
19
 * Given this String, it can load a project into the workspace.
20
 * 
21
 * @since 3.0
22
 */
23
public class ProjectSetSerializer {
24
25
	/**
26
	 * For every project in providerProjects, return an opaque
27
	 * UTF-8 encoded String to act as a reference to that project.
28
	 * The format of the String is specific to the provider.
29
	 * The format of the String must be such that
30
	 * {@link #addToWorkspace(String[], ProjectSetSerializationContext, IProgressMonitor)}
31
	 * will be able to consume it and load the corresponding project.
32
	 * <p>
33
	 * This default implementation simply throws an exception
34
	 * indicating that no references can be created.
35
	 * Subclasses are expected to override.
36
	 * 
37
	 * @param providerProjects
38
	 * 		an array of projects for which references are needed
39
	 * 		(not <code>null</code> and contains no <code>null</code>s)
40
	 * @param context
41
	 * 		the context in which the references are created
42
	 * 		(not <code>null</code>)
43
	 * @param monitor
44
	 * 		a progress monitor or <code>null</code> if none
45
	 * @return 
46
	 * 		an array containing exactly the same number of elements 
47
	 * 		as the providerProjects argument 
48
	 * 		where each element is a serialized reference string 
49
	 * 		uniquely identifying the corresponding the project in the providerProjects array
50
	 * 		(not <code>null</code> and contains no <code>null</code>s)
51
	 * @throws TeamException
52
	 * 		thrown if there is a reference string cannot be created for a project
53
	 */
54
	public String[] asReference(
55
		IProject[] providerProjects,
56
		ProjectSetSerializationContext context,
57
		IProgressMonitor monitor)
58
		throws TeamException {
59
			
60
		throw new TeamException("Failed to create project references");
61
	}
62
63
	/**
64
	 * For every String in referenceStrings, load the corresponding project into the workspace.
65
	 * The opaque strings in referenceStrings are guaranteed to have been previously
66
	 * produced by {@link #asReference(IProject[], ProjectSetSerializationContext, IProgressMonitor)}.
67
	 * The confirmOverwrite method is called with an array of projects
68
	 * for which projects of the same name already exists in the workspace.
69
	 * <p>
70
	 * Callers from within a UI context should wrapper a call to this method
71
	 * inside a WorkspaceModifyOperation so that events generated as a result
72
	 * of this operation are deferred until the outermost operation
73
	 * has successfully completed.
74
	 * <p>
75
	 * This default implementation simply throws an exception
76
	 * indicating that no projects can be loaded.
77
	 * Subclasses are expected to override.
78
	 * 
79
	 * @param referenceStrings
80
	 * 		an array of referene strings uniquely identifying the projects
81
	 * 		(not <code>null</code> and contains no <code>null</code>s)
82
	 * @param context
83
	 * 		the context in which the projects are loaded
84
	 * 		(not <code>null</code>)
85
	 * @param monitor
86
	 * 		a progress monitor or <code>null</code> if none
87
	 * @return IProject[]
88
	 * 		an array of projects that were loaded
89
	 * 		excluding those projects already existing and not overwritten
90
	 * 		(not <code>null</code>, contains no <code>null</code>s)
91
	 * @throws TeamException
92
	 * 		thrown if there is a problem loading a project into the workspace.
93
	 * 		If an exception is thrown, then the workspace is left in an unspecified state
94
	 * 		where some of the referenced projects may be loaded or partially loaded, and others may not.
95
	 */
96
	public IProject[] addToWorkspace(
97
		String[] referenceStrings,
98
		ProjectSetSerializationContext context,
99
		IProgressMonitor monitor)
100
		throws TeamException {
101
			
102
		throw new TeamException("Failed to load projects");
103
	}
104
	
105
	////////////////////////////////////////////////////////////////////////////
106
	//
107
	// Internal utility methods for subclasses
108
	//
109
	////////////////////////////////////////////////////////////////////////////
110
111
	/**
112
	 * Determine if any of the projects already exist
113
	 * and confirm which of those projects are to be overwritten.
114
	 * 
115
	 * @param context
116
	 * 		the context in which the projects are loaded
117
	 * 		(not <code>null</code>)
118
	 * @param projects 
119
	 * 		an array of proposed projects to be loaded
120
	 * 		(not <code>null</code>, contains no <code>null</code>s)
121
	 * @return 
122
	 * 		an array of confirmed projects to be loaded
123
	 * 		or <code>null</code> if the operation is to be canceled.
124
	 * @throws TeamException
125
	 */
126
	protected IProject[] confirmOverwrite(
127
		ProjectSetSerializationContext context,
128
		IProject[] projects)
129
		throws TeamException {
130
		
131
		// Build a collection of existing projects
132
		
133
		final Collection existingProjects = new ArrayList();
134
		for (int i = 0; i < projects.length; i++) {
135
			IProject eachProj = projects[i];
136
			if (eachProj.exists())
137
				existingProjects.add(eachProj);
138
		}
139
		if (existingProjects.size() == 0)
140
			return projects;
141
		
142
		// Confirm the overwrite
143
		
144
		IProject[] confirmed =
145
			context.confirmOverwrite(
146
				(IProject[]) existingProjects.toArray(
147
					new IProject[existingProjects.size()]));
148
		if (confirmed == null)
149
			return null;
150
		if (existingProjects.size() == confirmed.length)
151
			return projects;
152
		
153
		// Return the amended list of projects to be loaded
154
		
155
		Collection result = new ArrayList(projects.length);
156
		result.addAll(Arrays.asList(projects));
157
		result.removeAll(existingProjects);
158
		for (int i = 0; i < confirmed.length; i++) {
159
			IProject eachProj = confirmed[i];
160
			if (existingProjects.contains(eachProj))
161
				result.add(eachProj);
162
		}
163
		return (IProject[]) result.toArray(new IProject[result.size()]);
164
	}
165
166
	/**
167
	 * Determine the locations for the projects to be loaded
168
	 * and create any project that does not already exist.
169
	 * 
170
	 * @param context the serialization context
171
	 * @param projects the projects to be created
172
	 * @param monitor the progress monitor (not <code>null</code>)
173
	 * @return 
174
	 * 		an array of projects that were created or already existed
175
	 * 		or <code>null</code> if the operation should be canceled
176
	 */
177
	protected IProject[] createProjects(
178
		ProjectSetSerializationContext context,
179
		IProject[] projects,
180
		IProgressMonitor monitor)
181
		throws TeamException {
182
	
183
		// Retrieve the preferred project locations
184
		IPath[] locations = context.getLocalProjectLocations(projects);
185
		if (locations == null)
186
			return null;
187
		
188
		// Create the projects that do not already exist
189
		monitor.beginTask("", projects.length); //$NON-NLS-1$
190
		for (int i = 0; i < projects.length; i++) {
191
			if (monitor.isCanceled())
192
				return null;
193
			IProject eachProject = projects[i];
194
			IPath eachLocation = locations[i];
195
			if (eachProject.exists()) {
196
				// The project location of an existing project cannot be changed, 
197
				// so if the current location is different than the intended location
198
				// then in the future the project could be deleted and recreated
199
				// ... but do nothing for now
200
			}
201
			else {
202
				IProjectDescription desc =
203
					ResourcesPlugin.getWorkspace().newProjectDescription(
204
						eachProject.getName());
205
				desc.setLocation(eachLocation);
206
				try {
207
					eachProject.create(desc, new SubProgressMonitor(monitor, 1));
208
				}
209
				catch (CoreException e) {
210
					throw new TeamException(e.getMessage(), e);
211
				}
212
			}
213
		}
214
		monitor.done();
215
		return projects;
216
	}
217
218
}

Return to bug 38048