Skip to main content
summaryrefslogtreecommitdiffstats
blob: 70588b69f60affc96325cc9b75886fede67d7a4d (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 * IBM - Initial API and implementation
 ******************************************************************************/

package org.eclipse.team.internal.ccvs.ui;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.team.IMoveDeleteHook;
import org.eclipse.core.resources.team.IResourceTree;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.ccvs.core.ICVSFile;
import org.eclipse.team.ccvs.core.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.ui.IWorkbenchWindow;

public class CVSMoveDeleteHook implements IMoveDeleteHook {

	public interface IRunnableWithShell {
		public void run(Shell shell);
	}
	
	public class MoveDeleteMessageDialog extends MessageDialog {
		Button dontShowAgain;
		boolean dontShow;
		boolean showOption;
		
		public MoveDeleteMessageDialog(Shell shell, String dialogTitle, Image dialogTitleImage, String dialogMessage, int dialogImageType, String[] dialogButtonLabels, int defaultIndex) {
			this(shell, dialogTitle, dialogTitleImage, dialogMessage, dialogImageType, dialogButtonLabels, defaultIndex, true);
		}
		
		public MoveDeleteMessageDialog(Shell shell, String dialogTitle, Image dialogTitleImage, String dialogMessage, int dialogImageType, String[] dialogButtonLabels, int defaultIndex, boolean showOption) {
			super(shell, dialogTitle, dialogTitleImage, dialogMessage, dialogImageType, dialogButtonLabels, defaultIndex);
			this.showOption = showOption;
		}
		
		protected Control createCustomArea(Composite composite) {
			if ( ! showOption) return null;
			dontShow = false;
			dontShowAgain = new Button(composite, SWT.CHECK);
			GridData data = new GridData();
			data.horizontalIndent = 50;
			dontShowAgain.setLayoutData(data);
			dontShowAgain.setText(Policy.bind("CVSMoveDeleteHook.dontShowAgain")); //$NON-NLS-1$
			dontShowAgain.setSelection(dontShow);
			dontShowAgain.addSelectionListener(new SelectionListener() {
				public void widgetSelected(SelectionEvent e) {
					dontShow = dontShowAgain.getSelection();
				}
				public void widgetDefaultSelected(SelectionEvent e) {
					widgetSelected(e);
				}

			});
			return dontShowAgain;
		}
		
		public boolean isDontShowAgain() {
			return dontShow;
		}
	}

	private void showDialog(final IRunnableWithShell runnable) {
		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				IWorkbenchWindow window = CVSUIPlugin.getPlugin().getWorkbench().getActiveWorkbenchWindow();
				if (window != null) {
					runnable.run(window.getShell());
				} else {
					Display display = Display.getCurrent();
					Shell shell = new Shell(display);
					runnable.run(shell);
				}
			}
		});
	}
	
	private void makeFileOutgoingDeletion(IResourceTree tree, IFile source, IFile destination, int updateFlags, IProgressMonitor monitor) throws CoreException {
		if (destination == null) {
			tree.standardDeleteFile(source, updateFlags, monitor);
		} else {
			tree.standardMoveFile(source, destination, updateFlags, monitor);
		}
	}
	
	/*
	 * Delete the files contained in the folder structure rooted at source.
	 * Return true if at least one file was deleted
	 */
	private boolean makeFolderOutgoingDeletion(final IResourceTree tree, final IFolder source, final IFolder destination, final int updateFlags, final IProgressMonitor monitor) throws CoreException {
		final boolean[] fileFound = new boolean[] {false};
		source.accept(new IResourceVisitor() {
			public boolean visit(IResource resource) throws CoreException {
				if (resource.isTeamPrivateMember()) {
					return false;
				}
				// XXX Compensate for above not working
				if (resource.getName().equals("CVS")) {
					return false;
				}
				if (resource.getType() == IResource.FOLDER) {
					if (destination != null) {
						IFolder destFolder = destination.getFolder(resource.getFullPath().removeFirstSegments(source.getFullPath().segmentCount()));
						destFolder.create(false, true, monitor);
					}
				} else if (resource.getType() == IResource.FILE) {
					fileFound[0] = true;
					IFile destFile = null;
					if (destination != null) {
						destFile = destination.getFile(resource.getFullPath().removeFirstSegments(source.getFullPath().segmentCount()));
					}
					makeFileOutgoingDeletion(tree, (IFile)resource, destFile, updateFlags, monitor);
					return false;
				}
				return true;
			}

		}, IResource.DEPTH_INFINITE, false);
		
		return fileFound[0];
	}
	
	private boolean checkForTeamPrivate(IResource resource) {
		if (resource.isTeamPrivateMember()) {
			showDialog(new IRunnableWithShell() {
				public void run(Shell shell) {
					ErrorDialog.openError(shell, "Team Private Resource", "Deletion of team private resources is not permitted", null);
				}
			});
			return true;
		} 
		return false;
	}
	
	private boolean deleteFile(
		final IResourceTree tree,
		final IFile source,
		final IFile destination,
		final int updateFlags,
		final String title,
		final String message,
		final IProgressMonitor monitor) {
		
		if (checkForTeamPrivate(source)) {
			return true;
		}
		
		final ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(source);
		if (cvsFile.isManaged()) {
			// prompt the user for choices: Mark as outgoing deletion or cancel
			final boolean[] performDelete = new boolean[] { ! CVSProviderPlugin.getPlugin().getPromptOnFileDelete()};
			if ( ! performDelete[0]){
				showDialog(new IRunnableWithShell() {
					public void run(Shell shell) {
						MoveDeleteMessageDialog dialog = new MoveDeleteMessageDialog(
							shell,
							title, 
							null,	// accept the default window icon
							message, 
							MessageDialog.QUESTION, 
							new String[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL}, 
							0); 	// yes is the default
						performDelete[0] = dialog.open() == 0;
						if (performDelete[0]) {
							// The CVS core delta listener will mark the file as an outgoing deletion
							// so we just need to say that we didn't handle it. Core will delete the
							// file and the delta listener will do it's thing.
							CVSProviderPlugin.getPlugin().setPromptOnFileDelete( ! dialog.isDontShowAgain());
							CVSUIPlugin.getPlugin().getPreferenceStore().setValue(ICVSUIConstants.PREF_PROMPT_ON_FILE_DELETE, CVSProviderPlugin.getPlugin().getPromptOnFileDelete());
						}
					}
				});
			}
			if (performDelete[0]) {
				// Issue a delete for all child files
				// The CVS core delta listener will mark each file as an outgoing deletion
				// so we just need to delete each file and let the delta listener do its thing
				try {
					makeFileOutgoingDeletion(tree, source, destination, updateFlags, monitor);
				} catch (final CoreException e) {
					showDialog(new IRunnableWithShell() {
						public void run(Shell shell) {
							ErrorDialog.openError(shell, null, null, e.getStatus());
						}
					});
				}
			}
			return true;
		} else // XXX Until team-private resources are hiddent
			if (source.getParent().getName().equals("CVS")) {
				return true;
			}
		return false;
	}
	
	private boolean deleteFolder(
		final IResourceTree tree,
		final IFolder source,
		final IFolder destination,
		final int updateFlags,
		final String title,
		final String message,
		final IProgressMonitor monitor) {
		
		if (checkForTeamPrivate(source)) {
			return true;
		}
		
		final ICVSFolder cvsFolder = CVSWorkspaceRoot.getCVSFolderFor(source);
		if (cvsFolder.isManaged()) {
			// prompt the user for choices: Mark as outgoing deletion or cancel
			final boolean[] performDelete = new boolean[] { ! CVSProviderPlugin.getPlugin().getPromptOnFolderDelete()};
			boolean dialogShown = false;
			if ( ! performDelete[0]) {
				dialogShown = true;
				showDialog(new IRunnableWithShell() {
					public void run(Shell shell) {
						MoveDeleteMessageDialog dialog = new MoveDeleteMessageDialog(
							shell,
							title, 
							null,	// accept the default window icon
							message, 
							MessageDialog.QUESTION, 
							new String[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL}, 
							0); 	// yes is the default
						performDelete[0] = dialog.open() == 0;
						if (performDelete[0]) {
							CVSProviderPlugin.getPlugin().setPromptOnFolderDelete( ! dialog.isDontShowAgain());
							CVSUIPlugin.getPlugin().getPreferenceStore().setValue(ICVSUIConstants.PREF_PROMPT_ON_FOLDER_DELETE, CVSProviderPlugin.getPlugin().getPromptOnFolderDelete());
						}
					}
				});
			}
			if (performDelete[0]) {
				// Issue a delete for all child files
				// The CVS core delta listener will mark each file as an outgoing deletion
				// so we just need to delete each file and let the delta listener do its thing
				try {
					if ( ! makeFolderOutgoingDeletion(tree, source, destination, updateFlags, monitor) && ! dialogShown && destination == null) {
						showDialog(new IRunnableWithShell() {
							public void run(Shell shell) {
								MoveDeleteMessageDialog dialog = new MoveDeleteMessageDialog(
									shell,
									title, 
									null,	// accept the default window icon
									message, 
									MessageDialog.QUESTION, 
									new String[] {IDialogConstants.OK_LABEL}, 
									0, false); 	// yes is the default
								performDelete[0] = dialog.open() == 0;
								if (performDelete[0]) {
									CVSProviderPlugin.getPlugin().setPromptOnFolderDelete( ! dialog.isDontShowAgain());
									CVSUIPlugin.getPlugin().getPreferenceStore().setValue(ICVSUIConstants.PREF_PROMPT_ON_FOLDER_DELETE, CVSProviderPlugin.getPlugin().getPromptOnFolderDelete());
								}
							}
						});
					}
				} catch (final CoreException e) {
					showDialog(new IRunnableWithShell() {
						public void run(Shell shell) {
							ErrorDialog.openError(shell, null, null, e.getStatus());
						}
					});
				}
			}
			return true;
		} else // XXX Until team-private resources are hidden
			if (source.getName().equals("CVS")) {
				return true;
			}
		return false;
	}
	
	/**
	 * @see IMoveDeleteHook#deleteFile(IResourceTree, IFile, int, IProgressMonitor)
	 */
	public boolean deleteFile(
		final IResourceTree tree,
		final IFile file,
		int updateFlags,
		final IProgressMonitor monitor) {
		
		return deleteFile(tree, file, null, updateFlags, 
			Policy.bind("CVSMoveDeleteHook.deleteFileTitle"), //$NON-NLS-1$
			Policy.bind("CVSMoveDeleteHook.deleteFileMessage", file.getName()), //$NON-NLS-1$
			monitor);
	}

	/**
	 * @see IMoveDeleteHook#deleteFolder(IResourceTree, IFolder, int, IProgressMonitor)
	 */
	public boolean deleteFolder(
		final IResourceTree tree,
		final IFolder folder,
		final int updateFlags,
		final IProgressMonitor monitor) {
		
		return deleteFolder(tree, folder, null, updateFlags,
			Policy.bind("CVSMoveDeleteHook.deleteFolderTitle"), //$NON-NLS-1$
			Policy.bind("CVSMoveDeleteHook.deleteFolderMessage", folder.getName()), //$NON-NLS-1$
			monitor);
	}

	/**
	 * @see IMoveDeleteHook#deleteProject(IResourceTree, IProject, int, IProgressMonitor)
	 */
	public boolean deleteProject(
		IResourceTree tree,
		IProject project,
		int updateFlags,
		IProgressMonitor monitor) {
			
		// No special action needed
		return false;
	}

	/**
	 * @see IMoveDeleteHook#moveFile(IResourceTree, IFile, IFile, int, IProgressMonitor)
	 */
	public boolean moveFile(
		IResourceTree tree,
		IFile source,
		IFile destination,
		int updateFlags,
		IProgressMonitor monitor) {
			
		return deleteFile(tree, source, destination, updateFlags,
			Policy.bind("CVSMoveDeleteHook.moveFileTitle"), //$NON-NLS-1$
			Policy.bind("CVSMoveDeleteHook.moveFileMessage", source.getName()), //$NON-NLS-1$
			monitor);
	}

	/**
	 * @see IMoveDeleteHook#moveFolder(IResourceTree, IFolder, IFolder, int, IProgressMonitor)
	 */
	public boolean moveFolder(
		IResourceTree tree,
		IFolder source,
		IFolder destination,
		int updateFlags,
		IProgressMonitor monitor) {
			
		return deleteFolder(tree, source, destination, updateFlags,
			Policy.bind("CVSMoveDeleteHook.moveFolderTitle"), //$NON-NLS-1$
			Policy.bind("CVSMoveDeleteHook.moveFolderMessage", source.getName()), //$NON-NLS-1$
			monitor);
	}

	/**
	 * @see IMoveDeleteHook#moveProject(IResourceTree, IProject, IProjectDescription, int, IProgressMonitor)
	 */
	public boolean moveProject(
		IResourceTree tree,
		IProject source,
		IProjectDescription description,
		int updateFlags,
		IProgressMonitor monitor) {
			
		// No special action
		return false;
	}
}

Back to the top