Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c62beb40d71af5e94d0cd602303498e179e0382 (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
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.wizards;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.team.core.*;
import org.eclipse.team.internal.ui.*;
import org.eclipse.ui.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ProjectSetExportWizard extends Wizard implements IExportWizard {
	ExportProjectSetMainPage mainPage;
	ExportProjectSetLocationPage locationPage;
	IStructuredSelection selection;

	public ProjectSetExportWizard() {
		setNeedsProgressMonitor(true);
		setWindowTitle(TeamUIMessages.ProjectSetExportWizard_Project_Set_1);
	}

	@Override
	public void addPages() {
		mainPage = new ExportProjectSetMainPage("projectSetMainPage", TeamUIMessages.ProjectSetExportWizard_Export_a_Project_Set_3, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_PROJECTSET_EXPORT_BANNER)); //$NON-NLS-1$
		IProject[] projects = (IProject[])selection.toList().toArray(new IProject[0]);
		addPage(mainPage);
		mainPage.setSelectedProjects(projects);
		locationPage = new ExportProjectSetLocationPage("projectSetLocationPage", TeamUIMessages.ProjectSetExportWizard_Export_a_Project_Set_3, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_PROJECTSET_EXPORT_BANNER)); //$NON-NLS-1$
		addPage(locationPage);
	}

	@Override
	public boolean performFinish() {
		final boolean[] result = new boolean[] {false};
		try {
			getContainer().run(false, false, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException {
					String filename = locationPage.getFileName();
					Path path = new Path(filename);
					if (path.getFileExtension() == null) {
						filename = filename + ".psf"; //$NON-NLS-1$
					}
					PsfFilenameStore.getInstance().remember(filename);
					File file = new File(filename);
					File parentFile = file.getParentFile();
					if (parentFile != null && !parentFile.exists()) {
						boolean r = MessageDialog.openQuestion(getShell(), TeamUIMessages.ProjectSetExportWizard_Question_4, TeamUIMessages.ProjectSetExportWizard_Target_directory_does_not_exist__Would_you_like_to_create_it__5); //
						if (!r) {
							result[0] = false;
							return;
						}
						r = parentFile.mkdirs();
						if (!r) {
							MessageDialog.openError(getShell(), TeamUIMessages.ProjectSetExportWizard_Export_Problems_6, TeamUIMessages.ProjectSetExportWizard_An_error_occurred_creating_the_target_directory_7); //
							result[0] = false;
							return;
						}
					}
					if (file.exists() && file.isFile()) {
						boolean r = MessageDialog.openQuestion(getShell(), TeamUIMessages.ProjectSetExportWizard_Question_8, TeamUIMessages.ProjectSetExportWizard_Target_already_exists__Would_you_like_to_overwrite_it__9); //
						if (!r) {
							result[0] = false;
							return;
						}
					}

					IWorkingSet[] workingSets = null;
					if (mainPage.exportWorkingSets.getSelection()){
						workingSets = mainPage.getSelectedWorkingSets();
					}
					// Hash the projects by provider
					IProject[] projects = mainPage.getSelectedProjects();
					Map<String, Set<IProject>> map = new HashMap<>();
					for (int i = 0; i < projects.length; i++) {
						IProject project = projects[i];
						RepositoryProvider provider = RepositoryProvider.getProvider(project);
						if (provider != null) {
							String id = provider.getID();
							Set<IProject> list = map.get(id);
							if (list == null) {
								list = new TreeSet<>((o1, o2) -> o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase()));
								map.put(id, list);
							}
							list.add(project);
						}
					}


					UIProjectSetSerializationContext context = new UIProjectSetSerializationContext(getShell(), filename);

					BufferedWriter writer = null;
					try {
						// if file was written to the workspace, perform the validateEdit
						if (!locationPage.isSaveToFileSystem())
							locationPage.validateEditWorkspaceFile(getShell());
						writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); //$NON-NLS-1$

						//
						XMLMemento xmlMemento = getXMLMementoRoot();
						Iterator it = map.keySet().iterator();
						monitor.beginTask(null, 1000 * map.keySet().size());
						while (it.hasNext()) {
							String id = (String)it.next();
							IMemento memento = xmlMemento.createChild("provider"); //$NON-NLS-1$
							memento.putString("id", id); //$NON-NLS-1$
							Set<IProject> list = map.get(id);
							IProject[] projectArray = list.toArray(new IProject[list.size()]);
							RepositoryProviderType providerType = RepositoryProviderType.getProviderType(id);
							ProjectSetCapability serializer = providerType.getProjectSetCapability();
							ProjectSetCapability.ensureBackwardsCompatible(providerType, serializer);
							if (serializer != null) {
								String[] references = serializer.asReference(projectArray, context, SubMonitor.convert(monitor, 990));
								for (int i = 0; i < references.length; i++) {
									IMemento proj = memento.createChild("project"); //$NON-NLS-1$
									proj.putString("reference", references[i]); //$NON-NLS-1$
								}
							}
						}
						if (workingSets != null){
							for (int i = 0; i < workingSets.length; i++) {
								IMemento memento =xmlMemento.createChild("workingSets"); //$NON-NLS-1$
								workingSets[i].saveState(memento);
							}
						}
						xmlMemento.save(writer);
						result[0] = true;
					} catch (IOException e) {
						throw new InvocationTargetException(e);
					} catch (TeamException e) {
						throw new InvocationTargetException(e);
					} finally {
						if (writer != null) {
							try {
								writer.close();
							} catch (IOException e) {
								throw new InvocationTargetException(e);
							}
						}
					}

					// if file was written to the workspace, refresh it
					if (!locationPage.isSaveToFileSystem())
						try {
							locationPage.refreshWorkspaceFile(monitor);
						} catch (CoreException e) {
							//throw away
						}

					// notify provider types of the project set write
					for (Iterator iter = map.keySet().iterator();iter.hasNext();) {
						String id = (String) iter.next();
						RepositoryProviderType type = RepositoryProviderType.getProviderType(id);
						if (type != null) {
							ProjectSetCapability capability = type.getProjectSetCapability();
							if (capability != null) {
								capability.projectSetCreated(file, context, SubMonitor.convert(monitor, 10));
							}
						}
					}

					monitor.done();
				}

				private XMLMemento getXMLMementoRoot() {
					Document document;
			        try {
			            document = DocumentBuilderFactory.newInstance()
			                    .newDocumentBuilder().newDocument();
			            Element element = document.createElement("psf"); //$NON-NLS-1$
			            element.setAttribute("version", "2.0"); //$NON-NLS-1$ //$NON-NLS-2$
			            document.appendChild(element);
			            return new XMLMemento(document, element);
			        } catch (ParserConfigurationException e) {
			            throw new Error(e.getMessage());
			        }
				}

			});
		} catch (InterruptedException e) {
			return true;
		} catch (InvocationTargetException e) {
			Throwable target = e.getTargetException();
			if (target instanceof TeamException) {
				ErrorDialog.openError(getShell(), null, null, ((TeamException)target).getStatus());
				return false;
			}
			if (target instanceof RuntimeException) {
				throw (RuntimeException)target;
			}
			if (target instanceof Error) {
				throw (Error)target;
			}
		}
		return result[0];
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
}

Back to the top