Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5228a7dd4b4a59bc92e1402181c4375fc1c124a8 (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
/*******************************************************************************
 * 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.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.*;
import org.eclipse.core.resources.mapping.ResourceTraversal;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.operation.IRunnableWithProgress;
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, new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException,
					InterruptedException {
				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 unadded = new ArrayList();
        PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor) throws InvocationTargetException,
                    InterruptedException {
                final IFileContentManager manager= Team.getFileContentManager();
                for (int i = 0; i < traversals.length; i++) {
                    ResourceTraversal traversal = traversals[i];
                    IResource[] resources = traversal.getResources();
                    for (int j = 0; j < resources.length; j++) {
                        final IResource resource = resources[j];
                        try {
                            resource.accept(new IResourceVisitor() {       
                                public boolean visit(IResource resource) throws CoreException {
                                    if (resource.getType() == IResource.FILE) {
                                        ICVSFile file = CVSWorkspaceRoot.getCVSFileFor((IFile)resource);
                                        if (!file.isManaged()) {
                                            if (!file.isIgnored() || file.equals(resource)) {
                                                final String extension= ((IFile)resource).getFileExtension();
                                                if (manager.getType((IFile)resource) == Team.UNKNOWN) {
                                                    if (extension != null && !manager.isKnownExtension(extension)) {
                                                        unadded.add(resource);
                                                    } else {
                                                        final String name= file.getName();
                                                        if (extension == null && name != null && !manager.isKnownFilename(name))
                                                            unadded.add(resource);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    return true;
                                }
                            }, traversal.getDepth(), false);
                        } catch (CoreException e) {
                            throw new InvocationTargetException(e);
                        }
                    }
                }
            }
        
        });
        return (IFile[]) 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));
    }
    
    public void addPages() {
        
        final Collection names= new HashSet();
        final Collection extensions= new HashSet();
        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 names, Collection extensions) {
        
        final IFileContentManager manager= Team.getFileContentManager();
        
        for (int i = 0; i < files.length; i++) {
            IFile file = files[i];
            
            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);
        }
    }
    
    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