Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8d5bb633027c70db1df5c6f27996505a5f25c7c4 (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
/*******************************************************************************
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.wizards;

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

import org.eclipse.core.resources.*;
import org.eclipse.core.resources.mapping.ResourceTraversal;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.IFileContentManager;
import org.eclipse.team.core.Team;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.ICVSFile;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.ui.*;
import org.eclipse.team.internal.ccvs.ui.operations.AddOperation;
import org.eclipse.ui.PlatformUI;

public class AddWizard extends ResizableWizard {

	private final AddOperation op;
	private final IFile[] unknowns;
	private CommitWizardFileTypePage fFileTypePage;

	public static void run(Shell shell, final AddOperation op) throws InvocationTargetException, InterruptedException {
		// Prompt if there are files of unknown type being added
		PlatformUI.getWorkbench().getProgressService().run(true, true, monitor -> {
			try {
				op.buildScope(monitor);
			} catch (CVSException e) {
				throw new InvocationTargetException(e);
			}
		});
		
		IFile[] unknowns = getUnaddedWithUnknownFileType(op.getTraversals());
		if (unknowns.length == 0) {
			op.run();
		} else {
			AddWizard wizard = new AddWizard(op, unknowns);
			ResizableWizard.open(shell, wizard);
		}
	}
	
	private static IFile[] getUnaddedWithUnknownFileType(final ResourceTraversal[] traversals) throws InvocationTargetException, InterruptedException {
		final List<IResource> unadded = new ArrayList<>();
		PlatformUI.getWorkbench().getProgressService().busyCursorWhile(monitor -> {
			final IFileContentManager manager = Team.getFileContentManager();
			for (ResourceTraversal traversal : traversals) {
				IResource[] resources = traversal.getResources();
				for (IResource resource : resources) {
					try {
						resource.accept((IResourceVisitor) resource1 -> {
							if (resource1.getType() == IResource.FILE) {
								ICVSFile file = CVSWorkspaceRoot.getCVSFileFor((IFile) resource1);
								if (!file.isManaged()) {
									if (!file.isIgnored() || file.equals(resource1)) {
										final String extension = ((IFile) resource1).getFileExtension();
										if (manager.getType((IFile) resource1) == Team.UNKNOWN) {
											if (extension != null && !manager.isKnownExtension(extension)) {
												unadded.add(resource1);
											} else {
												final String name = file.getName();
												if (extension == null && name != null && !manager.isKnownFilename(name))
													unadded.add(resource1);
											}
										}
									}
								}
							}
							return true;
						}, traversal.getDepth(), false);
					} catch (CoreException e) {
						throw new InvocationTargetException(e);
					}
				}
			}
		});
		return unadded.toArray(new IFile[unadded.size()]);
	}
	
	public AddWizard(AddOperation op, IFile[] unknowns) {
		super("AddWizard", CVSUIPlugin.getPlugin().getDialogSettings()); //$NON-NLS-1$
		this.op = op;
		this.unknowns = unknowns;
		setWindowTitle(CVSUIMessages.AddWizard_0); 
		setDefaultPageImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_NEW_LOCATION));
	}
	
	@Override
	public void addPages() {
		
		final Collection<String> names= new HashSet<String>();
		final Collection<String> extensions= new HashSet<String>();
		getUnknownNamesAndExtension(unknowns, names, extensions);
		
		if (names.size() + extensions.size() > 0) {
			fFileTypePage= new CommitWizardFileTypePage(extensions, names); 
			addPage(fFileTypePage);
		}
		
		super.addPages();
	}
	
	private static void getUnknownNamesAndExtension(IFile[] files, Collection<String> names, Collection<String> extensions) {
		
		final IFileContentManager manager= Team.getFileContentManager();
		
		for (IFile file : files) {
			final String extension= file.getFileExtension();
			if (extension != null && !manager.isKnownExtension(extension)) {
				extensions.add(extension);
			}
			
			final String name= file.getName();
			if (extension == null && name != null && !manager.isKnownFilename(name))
				names.add(name);
		}
	}
	
	@Override
	public boolean performFinish() {
		final Map extensionsToSave= new HashMap();
		final Map extensionsNotToSave= new HashMap();
		
		fFileTypePage.getModesForExtensions(extensionsToSave, extensionsNotToSave);
		CommitWizardFileTypePage.saveExtensionMappings(extensionsToSave);
		op.addModesForExtensions(extensionsNotToSave);
		
		final Map namesToSave= new HashMap();
		final Map namesNotToSave= new HashMap();
		
		fFileTypePage.getModesForNames(namesToSave, namesNotToSave);
		CommitWizardFileTypePage.saveNameMappings(namesToSave);
		op.addModesForNames(namesNotToSave);
		
		try {
			op.run();
		} catch (InvocationTargetException e) {
			return false;
		} catch (InterruptedException e) {
			return false;
		}
		
		return super.performFinish();
	}
	
}

Back to the top