Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4b940cdd65578b50c571811c11c93631243100f0 (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 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:
 *     Dan Rubel - initial API and implementation
 *     IBM Corporation - ongoing maintenance
 *******************************************************************************/

package org.eclipse.team.internal.ui;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.ProjectSetSerializationContext;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ui.dialogs.IPromptCondition;
import org.eclipse.team.internal.ui.dialogs.PromptingDialog;

/**
 * The UI based context in which project serialization occurs.
 * The class may be subclasses to represent different UI based serialization contexts.
 * It is recommended that all UI based serialization contexts
 * use this class directly or indirectly as their superclass.
 *
 * @since 3.0
 */
public class UIProjectSetSerializationContext extends ProjectSetSerializationContext {
	/**
	 * The parent shell for this UI context
	 */
	private final Shell shell;

	/**
	 * Constructs a new instance.
	 *
	 * @param shell The parent shell for this UI context
	 * @param filename a filename or {@code null}
	 */
	public UIProjectSetSerializationContext(Shell shell, String filename) {
		super(filename);
		Assert.isNotNull(shell);
		this.shell = shell;
	}

	/**
	 * Answer the shell associated with this UI context.
	 *
	 * @return the shell (not <code>null</code>)
	 */
	@Override
	public Object getShell() {
		return shell;
	}

	/**
	 * Given an array of projects that currently exist in the workspace
	 * prompt the user to determine which of those projects should be overwritten.
	 * <p>
	 * This default implementation prompts the user
	 * to determine which projects should be overwritten.
	 * Subclasses may override this as appropriate.
	 *
	 * @param projects
	 * 		an array of projects currently existing in the workspace
	 * 		that are desired to be overwritten.
	 * 		(not <code>null</code>, contains no <code>null</code>s)
	 * @return
	 * 		an array of zero or more projects that should be overwritten
	 * 		or <code>null</code> if the operation is to be canceled
	 *
	 * @see org.eclipse.team.core.ProjectSetSerializationContext#confirmOverwrite(org.eclipse.core.resources.IProject[])
	 */
	@Override
	public IProject[] confirmOverwrite(final IProject[] projects) throws TeamException {
		IPromptCondition prompt = new IPromptCondition() {
			List resources = Arrays.asList(projects);
			@Override
			public boolean needsPrompt(IResource resource) {
				if (resource instanceof IProject) {
					IProject project = (IProject) resource;
					return (project.exists() || getTargetFile(project).exists()) && resources.contains(resource);
				}
				return false;
			}
			@Override
			public String promptMessage(IResource resource) {
				if (resource.exists())
					return NLS.bind(TeamUIMessages.UIProjectSetSerializationContext_0, new String[] { resource.getName() });
				return NLS.bind(TeamUIMessages.UIProjectSetSerializationContext_2, new String[] { resource.getName(), getTargetFile((IProject)resource).getAbsolutePath() });
			}
			public File getTargetFile(IProject project) {
				return new File(project.getParent().getLocation().toFile(), project.getName());
			}
		};
		PromptingDialog dialog =
			new PromptingDialog(
				(Shell) getShell(),
				projects,
				prompt,
				TeamUIMessages.UIProjectSetSerializationContext_1);
		IResource[] resourcesToOverwrite;
		try {
			resourcesToOverwrite = dialog.promptForMultiple();
		} catch (InterruptedException e) {
			// Return null indicating that the user canceled the operation
			return null;
		}
		IProject[] projectsToOverwrite = new IProject[resourcesToOverwrite.length];
		System.arraycopy(resourcesToOverwrite, 0, projectsToOverwrite, 0, resourcesToOverwrite.length);
		return projectsToOverwrite;
	}

}

Back to the top