Skip to main content
summaryrefslogtreecommitdiffstats
blob: af3be4c6b9e578f8c5834cc97ef3c942bbcb73ee (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
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 * IBM - Initial API and implementation
 ******************************************************************************/
package org.eclipse.team.internal.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
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.swt.widgets.Shell;
import org.eclipse.team.core.IProjectSetSerializer;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.Team;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.TeamPlugin;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;

public class ProjectSetExportWizard extends Wizard implements IExportWizard {
	ExportProjectSetMainPage mainPage;
	IStructuredSelection selection;
	
	public ProjectSetExportWizard() {
		setNeedsProgressMonitor(true);
		setWindowTitle(Policy.bind("ProjectSetExportWizard.Project_Set_1")); //$NON-NLS-1$
	}
	
	public void addPages() {
		mainPage = new ExportProjectSetMainPage("projectSetMainPage", Policy.bind("ProjectSetExportWizard.Export_a_Project_Set_3"), null); //$NON-NLS-1$ //$NON-NLS-2$
		IProject[] projects = (IProject[])selection.toList().toArray(new IProject[0]);
		mainPage.setSelectedProjects(projects);
		addPage(mainPage);
	}
	public boolean performFinish() {
		final boolean[] result = new boolean[] {false};
		try {
			getContainer().run(false, false, new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) throws InvocationTargetException {
					try {
						String filename = mainPage.getFileName();
						File file = new File(filename);
						File parentFile = file.getParentFile();
						if (!parentFile.exists()) {
							boolean r = MessageDialog.openQuestion(getShell(), Policy.bind("ProjectSetExportWizard.Question_4"), Policy.bind("ProjectSetExportWizard.Target_directory_does_not_exist._Would_you_like_to_create_it__5")); //$NON-NLS-1$ //$NON-NLS-2$
							if (!r) {
								result[0] = false;
								return;
							}
							r = parentFile.mkdirs();
							if (!r) {
								MessageDialog.openError(getShell(), Policy.bind("ProjectSetExportWizard.Export_Problems_6"), Policy.bind("ProjectSetExportWizard.An_error_occurred_creating_the_target_directory_7")); //$NON-NLS-1$ //$NON-NLS-2$
								result[0] = false;
								return;
							}
						}
						if (file.exists() && file.isFile()) {
							boolean r = MessageDialog.openQuestion(getShell(), Policy.bind("ProjectSetExportWizard.Question_8"), Policy.bind("ProjectSetExportWizard.Target_already_exists._Would_you_like_to_overwrite_it__9")); //$NON-NLS-1$ //$NON-NLS-2$
							if (!r) {
								result[0] = false;
								return;
							}
						}
						OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); //$NON-NLS-1$
						
						writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //$NON-NLS-1$
						writer.write("<psf version=\"2.0\">\n"); //$NON-NLS-1$
						IProject[] projects = mainPage.getSelectedProjects();
						
						// Hash the projects by provider
						Map map = new HashMap();
						for (int i = 0; i < projects.length; i++) {
							IProject project = projects[i];
							RepositoryProvider provider = RepositoryProvider.getProvider(project);
							String id = provider.getID();
							List list = (List)map.get(id);
							if (list == null) {
								list = new ArrayList();
								map.put(id, list);
							}
							list.add(project);
						}
						
						// For each provider id, do the writing
						Shell shell = getShell();
						Iterator it = map.keySet().iterator();
						monitor.beginTask(null, 1000 * map.keySet().size());
						while (it.hasNext()) {
							String id = (String)it.next();
							writer.write("\t<provider id=\""); //$NON-NLS-1$
							writer.write(id);
							writer.write("\">\n"); //$NON-NLS-1$
							List list = (List)map.get(id);
							IProject[] projectArray = (IProject[])list.toArray(new IProject[list.size()]);
							IProjectSetSerializer serializer = Team.getProjectSetSerializer(id);
							if (serializer != null) {
								String[] references = serializer.asReference(projectArray, shell, new SubProgressMonitor(monitor, 1000));
								for (int i = 0; i < references.length; i++) {
									writer.write("\t\t<project reference=\""); //$NON-NLS-1$
									writer.write(references[i]);
									writer.write("\"/>\n"); //$NON-NLS-1$
								}
							}
							writer.write("\t</provider>\n"); //$NON-NLS-1$
						}
						writer.write("</psf>\n"); //$NON-NLS-1$
						writer.close();
						result[0] = true;
					} catch (FileNotFoundException e) {
						throw new InvocationTargetException(e);
					} catch (IOException e) {
						throw new InvocationTargetException(e);
					} catch (TeamException e) {
						throw new InvocationTargetException(e);
					} finally {
						monitor.done();
					}
				}
			});
		} 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];
	}
	
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
}

Back to the top