Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 180fb6353fb5d2b344c4bb5a9590ba0cb48bc134 (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
/*******************************************************************************
 * Copyright (c) 2000, 2007 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.examples.filesystem;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.team.FileModificationValidationContext;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;

/**
 * This class models a sentry that verifies whether resources are available for editing or overwriting.
 * This has been made a separate class for illustration purposes. It may have been more appropriate
 * to have FileSystemProvider implement IFileModificationValidator itself since the interface
 * only has two methods and their implementation is straight forward.
 */
public final class FileModificationValidator extends org.eclipse.core.resources.team.FileModificationValidator {
	
	private FileSystemOperations operations;

	/**
	 * Constructor for FileModificationValidator.
	 */
	public FileModificationValidator(RepositoryProvider provider) {
		operations = ((FileSystemProvider)provider).getOperations();
	}

	/**
	 * This method will convert any exceptions thrown by the SimpleAccessOperations.checkout() to a Status.
	 * @param resources the resources that are to be checked out
	 * @return IStatus a status indicator that reports whether the operation went smoothly or not.
	 */
	private IStatus checkout(IResource[] resources) {
		try {
			operations.checkout(resources, IResource.DEPTH_INFINITE, null);
		} catch (TeamException e) {
			return new Status(IStatus.ERROR, FileSystemPlugin.ID, 0, e.getLocalizedMessage(), e);
		}
		return Status.OK_STATUS;
	}

	/**
	 * This method will be called by the workbench/editor before it tries to edit one or more files.
	 * The idea is to prevent anyone from accidentally working on a file that they won't be able to check in changes to.
	 * @see org.eclipse.core.resources.IFileModificationValidator#validateEdit(IFile[], Object)
	 */
	public IStatus validateEdit(IFile[] files, FileModificationValidationContext context) {
		Collection toBeCheckedOut = new ArrayList();

		//Make a list of all the files that need to be checked out:
		for (int i = 0; i < files.length; i++) {
			if (!operations.isCheckedOut(files[i])) {
				toBeCheckedOut.add(files[i]);
			}
		}
		
		return checkout((IResource[]) toBeCheckedOut.toArray(new IResource[toBeCheckedOut.size()]));
	}

	/**
	 * This method will be called by the workbench before it tries to save a file.
	 * It should not attempt to save any files that don't receive an OK status here.
	 * @see org.eclipse.core.resources.IFileModificationValidator#validateSave(IFile)
	 */
	public IStatus validateSave(IFile file) {
		if (file.isReadOnly()) {
			return checkout(new IResource[] { file });
		}
		return Status.OK_STATUS;
	}

}

Back to the top