|
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 |
} |