|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 30-104
Link Here
|
| 30 |
import org.eclipse.ui.*; |
30 |
import org.eclipse.ui.*; |
| 31 |
|
31 |
|
| 32 |
public class ProjectSetImporter { |
32 |
public class ProjectSetImporter { |
| 33 |
|
33 |
|
| 34 |
public static IProject[] importProjectSet(String filename, Shell shell, IProgressMonitor monitor) throws InvocationTargetException { |
34 |
/** |
|
|
35 |
* Imports a psf file based on a file content. This may be used when psf |
| 36 |
* file is imported from any other location that local filesystem. |
| 37 |
* |
| 38 |
* @param psfContents |
| 39 |
* the content of the psf file. |
| 40 |
* @param filename |
| 41 |
* the name of the source file. This is included in case the |
| 42 |
* provider needs to deduce relative paths |
| 43 |
* @param shell |
| 44 |
* @param monitor |
| 45 |
* @return list of new projects |
| 46 |
* @throws InvocationTargetException |
| 47 |
*/ |
| 48 |
public static IProject[] importProjectSetFromString(String psfContents, |
| 49 |
String filename, Shell shell, IProgressMonitor monitor) |
| 50 |
throws InvocationTargetException { |
| 51 |
XMLMemento xmlMemento = stringToXMLMemento(psfContents); |
| 52 |
return importProjectSet(xmlMemento, filename, shell, monitor); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Imports a psf file. |
| 57 |
* |
| 58 |
* @param filename |
| 59 |
* @param shell |
| 60 |
* @param monitor |
| 61 |
* @return list of new projects |
| 62 |
* @throws InvocationTargetException |
| 63 |
*/ |
| 64 |
public static IProject[] importProjectSet(String filename, Shell shell, |
| 65 |
IProgressMonitor monitor) throws InvocationTargetException { |
| 66 |
XMLMemento xmlMemento = filenameToXMLMemento(filename); |
| 67 |
return importProjectSet(xmlMemento, filename, shell, monitor); |
| 68 |
} |
| 69 |
|
| 70 |
private static IProject[] importProjectSet(XMLMemento xmlMemento, |
| 71 |
String filename, Shell shell, IProgressMonitor monitor) |
| 72 |
throws InvocationTargetException { |
| 35 |
try { |
73 |
try { |
| 36 |
XMLMemento xmlMemento = filenameToXMLMemento(filename); |
74 |
|
| 37 |
String version = xmlMemento.getString("version"); //$NON-NLS-1$ |
75 |
String version = xmlMemento.getString("version"); //$NON-NLS-1$ |
| 38 |
|
76 |
|
| 39 |
List newProjects = new ArrayList(); |
77 |
List newProjects = new ArrayList(); |
| 40 |
if (version.equals("1.0")){ //$NON-NLS-1$ |
78 |
if (version.equals("1.0")) { //$NON-NLS-1$ |
| 41 |
IProjectSetSerializer serializer = Team.getProjectSetSerializer("versionOneSerializer"); //$NON-NLS-1$ |
79 |
IProjectSetSerializer serializer = Team |
|
|
80 |
.getProjectSetSerializer("versionOneSerializer"); //$NON-NLS-1$ |
| 42 |
if (serializer != null) { |
81 |
if (serializer != null) { |
| 43 |
IProject[] projects = serializer.addToWorkspace(new String[0], filename, shell, monitor); |
82 |
IProject[] projects = serializer.addToWorkspace( |
|
|
83 |
new String[0], filename, shell, monitor); |
| 44 |
if (projects != null) |
84 |
if (projects != null) |
| 45 |
newProjects.addAll(Arrays.asList(projects)); |
85 |
newProjects.addAll(Arrays.asList(projects)); |
| 46 |
} |
86 |
} |
| 47 |
} else { |
87 |
} else { |
| 48 |
UIProjectSetSerializationContext context = new UIProjectSetSerializationContext(shell, filename); |
88 |
UIProjectSetSerializationContext context = new UIProjectSetSerializationContext( |
|
|
89 |
shell, filename); |
| 49 |
List errors = new ArrayList(); |
90 |
List errors = new ArrayList(); |
| 50 |
ArrayList referenceStrings = new ArrayList(); |
91 |
ArrayList referenceStrings = new ArrayList(); |
| 51 |
IMemento[] providers = xmlMemento.getChildren("provider"); //$NON-NLS-1$ |
92 |
IMemento[] providers = xmlMemento.getChildren("provider"); //$NON-NLS-1$ |
| 52 |
for (int i = 0; i < providers.length; i++) { |
93 |
for (int i = 0; i < providers.length; i++) { |
| 53 |
IMemento[] projects = providers[i].getChildren("project"); //$NON-NLS-1$ |
94 |
IMemento[] projects = providers[i].getChildren("project"); //$NON-NLS-1$ |
| 54 |
for (int j = 0; j < projects.length; j++) { |
95 |
for (int j = 0; j < projects.length; j++) { |
| 55 |
referenceStrings.add(projects[j].getString("reference")); //$NON-NLS-1$ |
96 |
referenceStrings |
|
|
97 |
.add(projects[j].getString("reference")); //$NON-NLS-1$ |
| 56 |
} |
98 |
} |
| 57 |
try { |
99 |
try { |
| 58 |
String id = providers[i].getString("id"); //$NON-NLS-1$ |
100 |
String id = providers[i].getString("id"); //$NON-NLS-1$ |
| 59 |
TeamCapabilityHelper.getInstance().processRepositoryId(id, |
101 |
TeamCapabilityHelper.getInstance().processRepositoryId( |
| 60 |
PlatformUI.getWorkbench().getActivitySupport()); |
102 |
id, |
| 61 |
RepositoryProviderType providerType = RepositoryProviderType.getProviderType(id); |
103 |
PlatformUI.getWorkbench().getActivitySupport()); |
| 62 |
if (providerType == null) { |
104 |
RepositoryProviderType providerType = RepositoryProviderType |
| 63 |
// The provider type is absent. Perhaps there is another provider that can import this type |
105 |
.getProviderType(id); |
| 64 |
providerType = TeamPlugin.getAliasType(id); |
106 |
if (providerType == null) { |
| 65 |
} |
107 |
// The provider type is absent. Perhaps there is |
| 66 |
if (providerType == null) { |
108 |
// another provider that can import this type |
| 67 |
throw new TeamException(new Status(IStatus.ERROR, TeamUIPlugin.ID, 0, NLS.bind(TeamUIMessages.ProjectSetImportWizard_0, new String[] { id }), null)); |
109 |
providerType = TeamPlugin.getAliasType(id); |
| 68 |
} |
110 |
} |
| 69 |
ProjectSetCapability serializer = providerType.getProjectSetCapability(); |
111 |
if (providerType == null) { |
| 70 |
ProjectSetCapability.ensureBackwardsCompatible(providerType, serializer); |
112 |
throw new TeamException( |
| 71 |
if (serializer != null) { |
113 |
new Status( |
| 72 |
IProject[] allProjects = serializer.addToWorkspace((String[])referenceStrings.toArray(new String[referenceStrings.size()]), context, monitor); |
114 |
IStatus.ERROR, |
| 73 |
if (allProjects != null) |
115 |
TeamUIPlugin.ID, |
| 74 |
newProjects.addAll(Arrays.asList(allProjects)); |
116 |
0, |
| 75 |
} |
117 |
NLS.bind( |
| 76 |
referenceStrings.clear(); |
118 |
TeamUIMessages.ProjectSetImportWizard_0, |
| 77 |
} catch (TeamException e) { |
119 |
new String[] { id }), null)); |
| 78 |
errors.add(e); |
120 |
} |
| 79 |
} |
121 |
ProjectSetCapability serializer = providerType |
| 80 |
} |
122 |
.getProjectSetCapability(); |
| 81 |
if (!errors.isEmpty()) { |
123 |
ProjectSetCapability.ensureBackwardsCompatible( |
| 82 |
if (errors.size() == 1) { |
124 |
providerType, serializer); |
| 83 |
throw (TeamException)errors.get(0); |
125 |
if (serializer != null) { |
| 84 |
} else { |
126 |
IProject[] allProjects = serializer |
| 85 |
TeamException[] exceptions = (TeamException[]) errors.toArray(new TeamException[errors.size()]); |
127 |
.addToWorkspace( |
| 86 |
IStatus[] status = new IStatus[exceptions.length]; |
128 |
(String[]) referenceStrings |
| 87 |
for (int i = 0; i < exceptions.length; i++) { |
129 |
.toArray(new String[referenceStrings |
| 88 |
status[i] = exceptions[i].getStatus(); |
130 |
.size()]), context, |
| 89 |
} |
131 |
monitor); |
| 90 |
throw new TeamException(new MultiStatus(TeamUIPlugin.ID, 0, status, TeamUIMessages.ProjectSetImportWizard_1, null)); |
132 |
if (allProjects != null) |
| 91 |
} |
133 |
newProjects.addAll(Arrays.asList(allProjects)); |
| 92 |
} |
134 |
} |
| 93 |
|
135 |
referenceStrings.clear(); |
| 94 |
//try working sets |
136 |
} catch (TeamException e) { |
| 95 |
IMemento[] sets = xmlMemento.getChildren("workingSets"); //$NON-NLS-1$ |
137 |
errors.add(e); |
| 96 |
IWorkingSetManager wsManager = TeamUIPlugin.getPlugin().getWorkbench().getWorkingSetManager(); |
138 |
} |
| 97 |
boolean replaceAll = false; |
139 |
} |
| 98 |
boolean mergeAll = false; |
140 |
if (!errors.isEmpty()) { |
| 99 |
boolean skipAll = false; |
141 |
if (errors.size() == 1) { |
| 100 |
|
142 |
throw (TeamException) errors.get(0); |
| 101 |
for (int i = 0; i < sets.length; i++) { |
143 |
} else { |
|
|
144 |
TeamException[] exceptions = (TeamException[]) errors |
| 145 |
.toArray(new TeamException[errors.size()]); |
| 146 |
IStatus[] status = new IStatus[exceptions.length]; |
| 147 |
for (int i = 0; i < exceptions.length; i++) { |
| 148 |
status[i] = exceptions[i].getStatus(); |
| 149 |
} |
| 150 |
throw new TeamException(new MultiStatus( |
| 151 |
TeamUIPlugin.ID, 0, status, |
| 152 |
TeamUIMessages.ProjectSetImportWizard_1, null)); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// try working sets |
| 157 |
IMemento[] sets = xmlMemento.getChildren("workingSets"); //$NON-NLS-1$ |
| 158 |
IWorkingSetManager wsManager = TeamUIPlugin.getPlugin() |
| 159 |
.getWorkbench().getWorkingSetManager(); |
| 160 |
boolean replaceAll = false; |
| 161 |
boolean mergeAll = false; |
| 162 |
boolean skipAll = false; |
| 163 |
|
| 164 |
for (int i = 0; i < sets.length; i++) { |
| 102 |
IWorkingSet newWs = wsManager.createWorkingSet(sets[i]); |
165 |
IWorkingSet newWs = wsManager.createWorkingSet(sets[i]); |
| 103 |
if (newWs != null) { |
166 |
if (newWs != null) { |
| 104 |
IWorkingSet oldWs = wsManager.getWorkingSet(newWs |
167 |
IWorkingSet oldWs = wsManager.getWorkingSet(newWs |
|
Lines 113-136
Link Here
|
| 113 |
// a working set with the same name has been found |
176 |
// a working set with the same name has been found |
| 114 |
String title = TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_title; |
177 |
String title = TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_title; |
| 115 |
String msg = NLS |
178 |
String msg = NLS |
| 116 |
.bind( |
179 |
.bind(TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_message, |
| 117 |
TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_message, |
|
|
| 118 |
newWs.getName()); |
180 |
newWs.getName()); |
| 119 |
String[] buttons = new String[] { |
181 |
String[] buttons = new String[] { |
| 120 |
TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_replace, |
182 |
TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_replace, |
| 121 |
TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_merge, |
183 |
TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_merge, |
| 122 |
TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_skip, |
184 |
TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_skip, |
| 123 |
IDialogConstants.CANCEL_LABEL }; |
185 |
IDialogConstants.CANCEL_LABEL }; |
| 124 |
final AdviceDialog dialog = new AdviceDialog( |
186 |
final AdviceDialog dialog = new AdviceDialog(shell, |
| 125 |
shell, title, null, msg, |
187 |
title, null, msg, MessageDialog.QUESTION, |
| 126 |
MessageDialog.QUESTION, buttons, 0); |
188 |
buttons, 0); |
| 127 |
|
189 |
|
| 128 |
shell.getDisplay().syncExec(new Runnable() { |
190 |
shell.getDisplay().syncExec(new Runnable() { |
| 129 |
public void run() { |
191 |
public void run() { |
| 130 |
dialog.open(); |
192 |
dialog.open(); |
| 131 |
} |
193 |
} |
| 132 |
}); |
194 |
}); |
| 133 |
|
195 |
|
| 134 |
switch (dialog.getReturnCode()) { |
196 |
switch (dialog.getReturnCode()) { |
| 135 |
case 0: // overwrite |
197 |
case 0: // overwrite |
| 136 |
replaceWorkingSet(wsManager, newWs, oldWs); |
198 |
replaceWorkingSet(wsManager, newWs, oldWs); |
|
Lines 151-167
Link Here
|
| 151 |
} |
213 |
} |
| 152 |
} |
214 |
} |
| 153 |
} |
215 |
} |
| 154 |
|
216 |
|
| 155 |
return (IProject[]) newProjects.toArray(new IProject[newProjects.size()]); |
217 |
return (IProject[]) newProjects.toArray(new IProject[newProjects |
|
|
218 |
.size()]); |
| 156 |
} catch (TeamException e) { |
219 |
} catch (TeamException e) { |
| 157 |
throw new InvocationTargetException(e); |
220 |
throw new InvocationTargetException(e); |
| 158 |
} |
221 |
} |
| 159 |
} |
222 |
} |
| 160 |
|
223 |
|
| 161 |
private static XMLMemento filenameToXMLMemento(String filename) throws InvocationTargetException { |
224 |
private static XMLMemento filenameToXMLMemento(String filename) |
|
|
225 |
throws InvocationTargetException { |
| 162 |
InputStreamReader reader = null; |
226 |
InputStreamReader reader = null; |
| 163 |
try { |
227 |
try { |
| 164 |
reader = new InputStreamReader(new FileInputStream(filename), "UTF-8"); //$NON-NLS-1$ |
228 |
reader = new InputStreamReader(new FileInputStream(filename), |
|
|
229 |
"UTF-8"); //$NON-NLS-1$ |
| 165 |
return XMLMemento.createReadRoot(reader); |
230 |
return XMLMemento.createReadRoot(reader); |
| 166 |
} catch (UnsupportedEncodingException e) { |
231 |
} catch (UnsupportedEncodingException e) { |
| 167 |
throw new InvocationTargetException(e); |
232 |
throw new InvocationTargetException(e); |
|
Lines 179-185
Link Here
|
| 179 |
} |
244 |
} |
| 180 |
} |
245 |
} |
| 181 |
} |
246 |
} |
| 182 |
|
247 |
|
|
|
248 |
private static XMLMemento stringToXMLMemento(String stringContents) |
| 249 |
throws InvocationTargetException { |
| 250 |
StringReader reader = null; |
| 251 |
try { |
| 252 |
reader = new StringReader(stringContents); |
| 253 |
return XMLMemento.createReadRoot(reader); |
| 254 |
} catch (WorkbenchException e) { |
| 255 |
throw new InvocationTargetException(e); |
| 256 |
} finally { |
| 257 |
if (reader != null) { |
| 258 |
reader.close(); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Check if given file is a valid psf file |
| 265 |
* |
| 266 |
* @param filename |
| 267 |
* @return <code>true</code> is file is a valid psf file |
| 268 |
*/ |
| 183 |
public static boolean isValidProjectSetFile(String filename) { |
269 |
public static boolean isValidProjectSetFile(String filename) { |
| 184 |
try { |
270 |
try { |
| 185 |
return filenameToXMLMemento(filename).getString("version") != null; //$NON-NLS-1$ |
271 |
return filenameToXMLMemento(filename).getString("version") != null; //$NON-NLS-1$ |
|
Lines 187-205
Link Here
|
| 187 |
return false; |
273 |
return false; |
| 188 |
} |
274 |
} |
| 189 |
} |
275 |
} |
| 190 |
|
276 |
|
|
|
277 |
/** |
| 278 |
* Check if given string is a valid project set |
| 279 |
* |
| 280 |
* @param psfContent |
| 281 |
* @return <code>true</code> if psfContent is a valid project set |
| 282 |
*/ |
| 283 |
public static boolean isValidProjectSetString(String psfContent) { |
| 284 |
if (psfContent == null) { |
| 285 |
return false; |
| 286 |
} |
| 287 |
try { |
| 288 |
return stringToXMLMemento(psfContent).getString("version") != null; //$NON-NLS-1$ |
| 289 |
} catch (InvocationTargetException e) { |
| 290 |
return false; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 191 |
private static void mergeWorkingSets(IWorkingSet newWs, IWorkingSet oldWs) { |
294 |
private static void mergeWorkingSets(IWorkingSet newWs, IWorkingSet oldWs) { |
| 192 |
IAdaptable[] oldElements = oldWs.getElements(); |
295 |
IAdaptable[] oldElements = oldWs.getElements(); |
| 193 |
IAdaptable[] newElements = newWs.getElements(); |
296 |
IAdaptable[] newElements = newWs.getElements(); |
| 194 |
|
297 |
|
| 195 |
Set combinedElements = new HashSet(); |
298 |
Set combinedElements = new HashSet(); |
| 196 |
combinedElements.addAll(Arrays.asList(oldElements)); |
299 |
combinedElements.addAll(Arrays.asList(oldElements)); |
| 197 |
combinedElements.addAll(Arrays.asList(newElements)); |
300 |
combinedElements.addAll(Arrays.asList(newElements)); |
| 198 |
|
301 |
|
| 199 |
oldWs.setElements((IAdaptable[]) combinedElements.toArray(new IAdaptable[0])); |
302 |
oldWs.setElements((IAdaptable[]) combinedElements |
|
|
303 |
.toArray(new IAdaptable[0])); |
| 200 |
} |
304 |
} |
| 201 |
|
305 |
|
| 202 |
private static void replaceWorkingSet(IWorkingSetManager wsManager, IWorkingSet newWs, IWorkingSet oldWs) { |
306 |
private static void replaceWorkingSet(IWorkingSetManager wsManager, |
|
|
307 |
IWorkingSet newWs, IWorkingSet oldWs) { |
| 203 |
if (oldWs != null) |
308 |
if (oldWs != null) |
| 204 |
wsManager.removeWorkingSet(oldWs); |
309 |
wsManager.removeWorkingSet(oldWs); |
| 205 |
wsManager.addWorkingSet(newWs); |
310 |
wsManager.addWorkingSet(newWs); |
|
Lines 207-215
Link Here
|
| 207 |
|
312 |
|
| 208 |
private static class AdviceDialog extends MessageDialog { |
313 |
private static class AdviceDialog extends MessageDialog { |
| 209 |
boolean applyToAll; |
314 |
boolean applyToAll; |
| 210 |
public AdviceDialog(Shell parentShell, String dialogTitle, Image dialogTitleImage, String dialogMessage, int dialogImageType, String[] dialogButtonLabels, int defaultIndex) { |
315 |
|
| 211 |
super(parentShell, dialogTitle, dialogTitleImage, dialogMessage, dialogImageType, dialogButtonLabels, defaultIndex); |
316 |
public AdviceDialog(Shell parentShell, String dialogTitle, |
|
|
317 |
Image dialogTitleImage, String dialogMessage, |
| 318 |
int dialogImageType, String[] dialogButtonLabels, |
| 319 |
int defaultIndex) { |
| 320 |
super(parentShell, dialogTitle, dialogTitleImage, dialogMessage, |
| 321 |
dialogImageType, dialogButtonLabels, defaultIndex); |
| 212 |
} |
322 |
} |
|
|
323 |
|
| 213 |
protected Control createCustomArea(Composite parent) { |
324 |
protected Control createCustomArea(Composite parent) { |
| 214 |
final Button checkBox = new Button(parent, SWT.CHECK); |
325 |
final Button checkBox = new Button(parent, SWT.CHECK); |
| 215 |
checkBox.setText(TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_applyToAll); |
326 |
checkBox.setText(TeamUIMessages.ImportProjectSetDialog_duplicatedWorkingSet_applyToAll); |