Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c7e55a2c51653ffb608f44ea21493840b56ae2d (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*******************************************************************************
 * Copyright (c) 2000, 2005 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.runtime.*;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.ccvs.ui.Policy;

/**
 * A wizard for changing the keyword substitution mode of files.
 * 
 * 1.  Ask the user select to select the desired keyword substitution mode.
 * 2.  Compute the set of possibly affected resources
 * 3.  If the affected resources include existing committed files, warn the user
 *     and provide an option to include them in the operation anyways.
 * 4.  If the affected resources include dirty files, warn the user and provide
 *     an option to include them in the operation anyways.
 * 5.  Perform the operation on Finish.
 */
public class ModeWizard extends ResizableWizard {
    
    public static class ModeChange {
        
        private final IFile fFile;
        private final KSubstOption fMode;
        
        private KSubstOption fNewMode;
        
        public ModeChange(IFile file, KSubstOption mode) {
            fFile = file;
            fMode= mode;
            fNewMode= mode;
        }
        
        public IFile getFile() {
            return fFile;
        }
        
        public KSubstOption getMode() {
            return fMode;
        }
        
        public KSubstOption getNewMode() {
            return fNewMode;
        }
        
        public boolean hasChanged() {
            return !fMode.equals(fNewMode);
        }
        
        public void setNewMode(KSubstOption mode) {
            fNewMode= mode;
        }
        
        public int compareTo(Object o) {
            return fFile.getName().compareTo(((ModeChange)o).getFile().getName());
        }
    }
    
    protected List fChanges;
    final ModeWizardSelectionPage fPage;
    
    public static ModeWizard run(final Shell shell, final IResource [] resources) {
        
        final ModeWizard [] wizard= new ModeWizard[1];

		BusyIndicator.showWhile(shell.getDisplay(), () -> wizard[0] = new ModeWizard(shell, resources));
        
        open(shell, wizard[0]);
        return wizard[0];
    }
    
    /**
     * Creates a wizard to set the keyword substitution mode for the specified resources.
     * 
     * @param resources the resources to alter
     * @param depth the recursion depth
     * @param defaultOption the keyword substitution option to select by default
     */
    
    protected ModeWizard(Shell shell, final IResource[] resources) {
        super(CVSUIMessages.ModeWizard_0, CVSUIPlugin.getPlugin().getDialogSettings(), 700, 480); 
        setWindowTitle(CVSUIMessages.ModeWizard_1); 
        
        fChanges= getModeChanges(shell, resources);
        fPage= new ModeWizardSelectionPage(fChanges);
//            Workbench.getInstance().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
                    
    }
    
    @Override
	public void addPages() {
        addPage(fPage);
    }
    
    /* (Non-javadoc)
     * Method declared on IWizard.
     */
    @Override
	public boolean needsProgressMonitor() {
        return true;
    }
    
    protected static List getModeChanges(Shell shell, IResource [] resources) {
        
        final ArrayList changes= new ArrayList();
        final HashSet visited= new HashSet();
        
        for (int i = 0; i < resources.length; i++) {
            final IResource currentResource = resources[i];
            try {
				currentResource.accept((IResourceVisitor) resource -> {
					try {
						if (visited.contains(resource) || resource.getType() != IResource.FILE || !resource.exists())
							return true;
						visited.add(resource);
						IFile file = (IFile) resource;
						ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(file);
						if (!cvsFile.isManaged())
							return true;
						final ResourceSyncInfo info = cvsFile.getSyncInfo();
						final KSubstOption mode = info.getKeywordMode();

						changes.add(new ModeChange(file, mode));

					} catch (TeamException e) {
						throw new CoreException(e.getStatus());
					}
					// always return true and let the depth determine if children are visited
					return true;
				}, IResource.DEPTH_INFINITE, false);
            } catch (CoreException e) {
                CVSUIPlugin.openError(shell, CVSUIMessages.ModeWizard_2, null, e); 
            }
        }
        return changes;
    }
    
    @Override
	public boolean performFinish() {
        try {
            final List messages = new ArrayList();
            final List changes= fPage.getChanges();
            if (changes.size() == 0)
                return true;
            
            final String comment = fPage.getComment(getShell());
            if (comment == null)
                return false;
            
            getContainer().run(false /*fork*/, true /*cancelable*/, new IRunnableWithProgress() {
                @Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        final int totalWork= 10000;
                        monitor.beginTask(CVSUIMessages.ModeWizard_3, totalWork); 
                        
                        final Map changesPerProvider= getProviderMapping(changes);
                        
                        final int initialWork= totalWork / 10;
                        monitor.worked(initialWork);
                        
                        final int workPerProvider = (totalWork - initialWork) / changesPerProvider.size();

                        for (Iterator iter = changesPerProvider.entrySet().iterator(); iter.hasNext();) {
                            final Map.Entry entry = (Map.Entry) iter.next();
                            final CVSTeamProvider provider = (CVSTeamProvider)entry.getKey();
                            final Map providerFiles = (Map) entry.getValue();

                            final IStatus status = provider.setKeywordSubstitution(providerFiles, comment, Policy.subMonitorFor(monitor, workPerProvider));
                            if (status.getCode() != IStatus.OK) {
                                messages.add(status);
                            }
                        }
                        // Broadcast a decorator change so all interested parties will update their labels.
                        // This is done in particular because the syncview will not see this change
                        // as a change in state for the resources involved
                        CVSUIPlugin.broadcastPropertyChange(new PropertyChangeEvent(this, CVSUIPlugin.P_DECORATORS_CHANGED, null, null));
                    } catch (TeamException e) {
                        throw new InvocationTargetException(e);
                    } finally {
                        monitor.done();
                    }
                }
            });
            // Check for any status messages and display them
            if (!messages.isEmpty()) {
                boolean error = false;
                final MultiStatus combinedStatus = new MultiStatus(CVSUIPlugin.ID, 0, CVSUIMessages.ModeWizard_4, null); 
                for (int i = 0; i < messages.size(); i++) {
                    final IStatus status = (IStatus)messages.get(i);
                    if (status.getSeverity() == IStatus.ERROR || status.getCode() == CVSStatus.SERVER_ERROR) {
                        error = true;
                    }
                    combinedStatus.merge(status);
                }
                String message = null;
                IStatus statusToDisplay;
                if (combinedStatus.getChildren().length == 1) {
                    message = combinedStatus.getMessage();
                    statusToDisplay = combinedStatus.getChildren()[0];
                } else {
                    statusToDisplay = combinedStatus;
                }
                final String title= error ? CVSUIMessages.ModeWizard_5 : CVSUIMessages.ModeWizard_6; // 
                CVSUIPlugin.openError(getShell(), title, message, new CVSException(statusToDisplay));
            }
            return super.performFinish();
        } catch (InterruptedException e) {
            return true;
        } catch (InvocationTargetException e) {
            CVSUIPlugin.openError(getShell(), CVSUIMessages.ModeWizard_4, null, e); 
            return false;
        }
    }
    
    /**
     * Get a map 
     * @param changes
     * @return
     */
    static Map getProviderMapping(Collection changes) {
        
        final Map table = new HashMap();
        
        for (Iterator iter = changes.iterator(); iter.hasNext();) {
            final ModeChange change= (ModeChange)iter.next();
            
            if (!change.hasChanged())
                continue;
            
            final IFile file = change.getFile();
            final RepositoryProvider provider = RepositoryProvider.getProvider(file.getProject(), CVSProviderPlugin.getTypeId());
            
            if (!table.containsKey(provider)) {
                table.put(provider, new HashMap());
            }
            final Map providerMap = (Map)table.get(provider);
            providerMap.put(file, change.getNewMode());
        }
        return table;
    }


}

Back to the top