Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3d5e11bdfd8f78a9151cd82ad3daf12eb183ca38 (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
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.examples.filesystem.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.IProjectSetSerializer;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.examples.filesystem.FileSystemPlugin;
import org.eclipse.team.examples.filesystem.FileSystemProvider;
import org.eclipse.team.examples.filesystem.Policy;

/**
 * This is an old-style (pre-3.0) project set serializer used to test backwards compatibility
 */
public class ProjectSetSerializer implements IProjectSetSerializer {

	@Override
	public String[] asReference(IProject[] providerProjects, Object context, IProgressMonitor monitor) {
		Assert.isTrue(context instanceof Shell);
		List<String> refs = new ArrayList<>();
		for (IProject project : providerProjects) {
			FileSystemProvider provider = (FileSystemProvider)RepositoryProvider.getProvider(project, FileSystemPlugin.PROVIDER_ID);
			if (provider != null) {
				refs.add(asReference(provider));
			}
		}
		return refs.toArray(new String[refs.size()]);
	}

	@Override
	public IProject[] addToWorkspace(String[] referenceStrings, String filename, Object context, IProgressMonitor monitor) {
		Assert.isTrue(context instanceof Shell);
		List<IProject> projects = new ArrayList<>();
		for (String string : referenceStrings) {
			String projectName = getProjectName(string);
			String path = getPath(string);
			if (projectName != null && path != null) {
				try {
					IProject project = createProject(projectName, monitor);
					RepositoryProvider.map(project, FileSystemPlugin.PROVIDER_ID);
					FileSystemProvider provider = (FileSystemProvider) RepositoryProvider.getProvider(project);
					provider.setTargetLocation(path);
					projects.add(project);
				} catch (CoreException e) {
					ErrorDialog.openError(
							(Shell)context,
							Policy.bind("ConfigurationWizard.errorMapping"), //$NON-NLS-1$
							Policy.bind("ConfigurationWizard.error"), //$NON-NLS-1$
							e.getStatus());
				}
			}
		}
		return projects.toArray(new IProject[projects.size()]);
	}

	/**
	 * @param provider
	 * @return
	 */
	private String asReference(FileSystemProvider provider) {
		return provider.getProject().getName() + "," + provider.getRoot().toString(); //$NON-NLS-1$
	}

	/**
	 * @param string
	 * @return
	 */
	private String getProjectName(String string) {
		int i = string.indexOf(',');
		if (i == -1) return null;
		return string.substring(0, i);
	}

	/**
	 * @param string
	 * @return
	 */
	private String getPath(String string) {
		int i = string.indexOf(',');
		if (i == -1) return null;
		return string.substring(i + 1);
	}

	/**
	 * @param projectName
	 * @return
	 * @throws CoreException
	 */
	private IProject createProject(String projectName, IProgressMonitor monitor) throws CoreException {
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		if (!project.exists()) {
			project.create(monitor);
		}
		if (!project.isOpen()) {
			project.open(monitor);
		}
		return project;
	}
}

Back to the top