Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa484435aa824d8d4e3fa1ad4723408013de399c (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
 * Copyright (c) 2000, 2008 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;

import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.team.FileModificationValidationContext;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.client.Command;
import org.eclipse.team.internal.ccvs.ui.actions.EditorsAction;
import org.eclipse.team.internal.ccvs.ui.operations.UpdateOperation;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.ui.progress.IProgressConstants;

/**
 * IFileModificationValidator that is plugged into the CVS Repository Provider
 */
public class FileModificationValidator extends CVSCoreFileModificationValidator {
	
	public FileModificationValidator() {
	}
	
    /* (non-Javadoc)
     * @see org.eclipse.team.internal.ccvs.core.CVSCoreFileModificationValidator#edit(org.eclipse.core.resources.IFile[], org.eclipse.core.resources.team.FileModificationValidationContext)
     */
    @Override
	protected IStatus edit(IFile[] readOnlyFiles, FileModificationValidationContext context) {
        return edit(readOnlyFiles, getShell(context));
    }
    
	private Shell getShell(FileModificationValidationContext context) {
		if (context == null)
			return null;
		if (context.getShell() != null)
			return (Shell)context.getShell();
		return Utils.getShell(null, true);
	}

	private IStatus getStatus(InvocationTargetException e) {
		Throwable target = e.getTargetException();
		if (target instanceof TeamException) {
			return ((TeamException) target).getStatus();
		} else if (target instanceof CoreException) {
			return ((CoreException) target).getStatus();
		}
		return new Status(IStatus.ERROR, CVSUIPlugin.ID, 0, CVSUIMessages.internal, target); 
	}
		
	private IStatus edit(final IFile[] files, final Shell shell) {
		if (isPerformEdit()) {
			try {
				if (shell != null && !promptToEditFiles(files, shell)) {
					// The user didn't want to edit.
					// OK is returned but the file remains read-only
					throw new InterruptedException();
				}
				
                // see if the file is up to date
                if (shell != null && promptToUpdateFiles(files, shell)) {
                    // The user wants to update the file
                    // Run the update in a runnable in order to get a busy cursor.
                    // This runnable is syncExeced in order to get a busy cursor
					IRunnableWithProgress updateRunnable = monitor -> performUpdate(files, monitor);
                    if (isRunningInUIThread()) {
                        // Only show a busy cursor if validate edit is blocking the UI
                        CVSUIPlugin.runWithProgress(shell, false, updateRunnable);
                    } else {
                        // We can't show a busy cursor (i.e., run in the UI thread)
                        // since this thread may hold locks and
                        // running an edit in the UI thread could try to obtain the
                        // same locks, resulting in a deadlock.
                        updateRunnable.run(new NullProgressMonitor());
                    }
                }
                
				// Run the edit in a runnable in order to get a busy cursor.
				// This runnable is syncExeced in order to get a busy cursor
				IRunnableWithProgress editRunnable = monitor -> {
					try {
						performEdit(files, monitor);
					} catch (CVSException e) {
						throw new InvocationTargetException(e);
					}
				};
				if (isRunningInUIThread()) {
				    // Only show a busy cursor if validate edit is blocking the UI
	                CVSUIPlugin.runWithProgress(shell, false, editRunnable);
				} else {
				    // We can't show a busy cursor (i.e., run in the UI thread)
				    // since this thread may hold locks and
				    // running an edit in the UI thread could try to obtain the
				    // same locks, resulting in a deadlock.
				    editRunnable.run(new NullProgressMonitor());
				}
			} catch (InvocationTargetException e) {
				return getStatus(e);
			} catch (InterruptedException e) {
				// Must return an error to indicate that it is not OK to edit the files
				return new Status(IStatus.CANCEL, CVSUIPlugin.ID, 0, CVSUIMessages.FileModificationValidator_vetoMessage, null); //;
			}
        } else if (isPerformEditInBackground()) {
            IStatus status = setWritable(files);
            if (status.isOK())
                performEdit(files);
            return status;
		} else {
			// Allow the files to be edited without notifying the server
			return setWritable(files);
		}

		return Status.OK_STATUS;
		
	}
    
    @Override
	protected void scheduleEditJob(Job job) {
        job.setProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY, Boolean.TRUE);
        job.setProperty(IProgressConstants.ICON_PROPERTY, getOperationIcon());
        super.scheduleEditJob(job);
    }
    
    private URL getOperationIcon() {
        return FileLocator.find(CVSUIPlugin.getPlugin().getBundle(), new Path(ICVSUIConstants.ICON_PATH + ICVSUIConstants.IMG_CVS_PERSPECTIVE), null);
    }
    
    private boolean isRunningInUIThread() {
        return Display.getCurrent() != null;
    }

    private boolean promptToEditFiles(IFile[] files, Shell shell) throws InvocationTargetException, InterruptedException {
		if (files.length == 0)
			return true;		

		if(isNeverPrompt())	
			return true;

		// Contact the server to see if anyone else is editing the files
		EditorsAction editors = fetchEditors(files, shell);
		if (editors.isEmpty()) {
			if (isAlwaysPrompt()) 
				return (promptEdit(shell));
			return true;
		} else {
			return (editors.promptToEdit(shell));
		}
	}
	
    private boolean promptToUpdateFiles(IFile[] files, Shell shell) throws InvocationTargetException, InterruptedException {
        if (files.length == 0)
            return false;
        
        if (isNeverUpdate())
            return false;
        
        // Contact the server to see if the files are up-to-date
        if (needsUpdate(files, new NullProgressMonitor())) {
            if (isPromptUpdate())
                return (promptUpdate(shell));
            return true; // auto update
        }
        
        return false;
    }

	private boolean promptEdit(Shell shell) {
		// Open the dialog using a sync exec (there are no guarantees that we
		// were called from the UI thread
		final boolean[] result = new boolean[] { false };
		int flags = isRunningInUIThread() ? 0 : CVSUIPlugin.PERFORM_SYNC_EXEC;
		CVSUIPlugin
				.openDialog(shell,
						shell1 -> result[0] = MessageDialog.openQuestion(shell1,
								CVSUIMessages.FileModificationValidator_3, CVSUIMessages.FileModificationValidator_4),
						flags);
		return result[0];
	}

    private boolean promptUpdate(Shell shell) {
        // Open the dialog using a sync exec (there are no guarantees that we
        // were called from the UI thread
        final boolean[] result = new boolean[] { false };
        int flags = isRunningInUIThread() ? 0 : CVSUIPlugin.PERFORM_SYNC_EXEC;
		CVSUIPlugin
				.openDialog(shell,
						shell1 -> result[0] = MessageDialog.openQuestion(shell1,
								CVSUIMessages.FileModificationValidator_5, CVSUIMessages.FileModificationValidator_6),
						flags);
        return result[0];
    }

	private boolean isPerformEdit() {
		return ICVSUIConstants.PREF_EDIT_PROMPT_EDIT.equals(CVSUIPlugin.getPlugin().getPreferenceStore().getString(ICVSUIConstants.PREF_EDIT_ACTION));
	}
    
    private boolean isPerformEditInBackground() {
        return ICVSUIConstants.PREF_EDIT_IN_BACKGROUND.equals(CVSUIPlugin.getPlugin().getPreferenceStore().getString(ICVSUIConstants.PREF_EDIT_ACTION));
    }
	
	private EditorsAction fetchEditors(IFile[] files, Shell shell) throws InvocationTargetException, InterruptedException {
		final EditorsAction editors = new EditorsAction(getProvider(files), files);
		IRunnableWithProgress runnable = monitor -> editors.run(monitor);
		if (isRunningInUIThread()) {
		    // Show a busy cursor if we are running in the UI thread
		    CVSUIPlugin.runWithProgress(shell, false, runnable);
		} else {
		    // We can't show a busy cursor (i.e., run in the UI thread)
		    // since this thread may hold locks and
		    // running a CVS operation in the UI thread could try to obtain the
		    // same locks, resulting in a deadlock.
		    runnable.run(new NullProgressMonitor());
		}
		return editors;
	}

	private boolean isNeverPrompt() {
		return ICVSUIConstants.PREF_EDIT_PROMPT_NEVER.equals(CVSUIPlugin.getPlugin().getPreferenceStore().getString(ICVSUIConstants.PREF_EDIT_PROMPT));
	}

	private boolean isAlwaysPrompt() {
		return ICVSUIConstants.PREF_EDIT_PROMPT_ALWAYS.equals(CVSUIPlugin.getPlugin().getPreferenceStore().getString(ICVSUIConstants.PREF_EDIT_PROMPT));
	}
    
    private boolean needsUpdate(IFile[] files, IProgressMonitor monitor) {
        try {
            CVSWorkspaceSubscriber subscriber = CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber();
            subscriber.refresh(files, IResource.DEPTH_ZERO, monitor);
            for (int i = 0; i < files.length; i++) {
                IFile file = files[i];
                SyncInfo info = subscriber.getSyncInfo(file);
                int direction = info.getKind() & SyncInfo.DIRECTION_MASK;
                if (direction == SyncInfo.CONFLICTING || direction == SyncInfo.INCOMING) {
                    return true;
                }
            }
        } catch (TeamException e) {
            // Log the exception and assume we don't need to update it
            CVSProviderPlugin.log(e);
        }
        return false;
    }
    
    private void performUpdate(IFile[] files, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
        // TODO: This obtains the project rule which can cause a rule violation
        new UpdateOperation(null /* no target part */, files, Command.NO_LOCAL_OPTIONS, null /* no tag */).run(monitor);
    }
    
    private boolean isPromptUpdate() {
        return ICVSUIConstants.PREF_UPDATE_PROMPT_IF_OUTDATED.equals(CVSUIPlugin.getPlugin().getPreferenceStore().getString(ICVSUIConstants.PREF_UPDATE_PROMPT));
    }
    
    private boolean isNeverUpdate() {
        return ICVSUIConstants.PREF_UPDATE_PROMPT_NEVER.equals(CVSUIPlugin.getPlugin().getPreferenceStore().getString(ICVSUIConstants.PREF_UPDATE_PROMPT));
    }
    
    @Override
	public ISchedulingRule validateEditRule(CVSResourceRuleFactory factory, IResource[] resources) {
        if (!isNeverUpdate()) {
            // We may need to perform an update so we need to obtain the lock on each project
            Set projects = new HashSet();
            for (int i = 0; i < resources.length; i++) {
                IResource resource = resources[i];
                if (isReadOnly(resource))
                    projects.add(resource.getProject());
            }
            return createSchedulingRule(projects);
        }
        return internalValidateEditRule(factory, resources);
    }
}

Back to the top