Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c4f150a087ce58f9b1f52873bb361f9d494cf21 (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
/*****************************************************************************
 * Copyright (c) 2011, 2014 Atos Origin, CEA, and others.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 429826
 *
 *****************************************************************************/
package org.eclipse.papyrus.team.svn;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.team.FileModificationValidationContext;
import org.eclipse.core.resources.team.FileModificationValidator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.papyrus.infra.core.resource.AbstractReadOnlyHandler;
import org.eclipse.papyrus.infra.core.resource.ReadOnlyAxis;
import org.eclipse.team.core.RepositoryProvider;
import com.google.common.base.Optional;
import com.google.common.collect.Sets;

/**
 * The Subversion file read-only handler is permission-based.
 */
public class SVNLockHandler extends AbstractReadOnlyHandler {

	// This is defined by SVNTeamPlugin.NATURE_ID constant, but we don't want a dependency
	// on that class (although, technically, it should be compiled into this class's constant
	// pool, let's be explicit about it)
	private static final String SVN_NATURE_ID = "org.eclipse.team.svn.core.nature"; //$NON-NLS-1$

	FileModificationValidator validator = null;

	public SVNLockHandler(EditingDomain editingDomain) {
		super(editingDomain);
		try {
			validator = new SVNTeamModificationValidator();
		} catch (NoClassDefFoundError e) {
		}
	}

	public Optional<Boolean> anyReadOnly(Set<ReadOnlyAxis> axes, URI[] uris) {

		if ((validator != null) && axes.contains(ReadOnlyAxis.PERMISSION)) {
			IResource[] needsLockResources = FileUtility.filterResources(getIFiles(uris), IStateFilter.SF_NEEDS_LOCK, IResource.DEPTH_ZERO);
			for (IResource needsLockResource : needsLockResources) {
				if (!SVNRemoteStorage.instance().asLocalResource(needsLockResource).isLocked()) {
					return Optional.of(Boolean.TRUE);
				}
			}
		}

		return Optional.absent();
	}

	@Override
	public Optional<Boolean> canMakeWritable(Set<ReadOnlyAxis> axes, URI[] uris) {
		if ((validator != null) && axes.contains(ReadOnlyAxis.PERMISSION)) {
			// get the unique set of projects to check for SVN team provider
			IFile[] files = getIFiles(uris);
			Set<IProject> projects = Sets.newHashSet();
			for (int i = 0; i < files.length; i++) {
				projects.add(files[i].getProject());
			}

			boolean result = false;
			for (Iterator<IProject> iter = projects.iterator(); !result && iter.hasNext();) {
				result = RepositoryProvider.getProvider(iter.next(), SVN_NATURE_ID) != null;
			}

			return Optional.of(result);
		}

		return Optional.absent();
	}

	public Optional<Boolean> makeWritable(Set<ReadOnlyAxis> axes, URI[] uris) {
		if ((validator != null) && axes.contains(ReadOnlyAxis.PERMISSION)) {
			IStatus result = validator.validateEdit(getIFiles(uris), FileModificationValidationContext.VALIDATE_PROMPT);
			return Optional.of(result.isOK());
		}
		return Optional.absent();
	}

	private IFile[] getIFiles(URI[] uris) {
		HashSet<IFile> iFilesSet = new HashSet<IFile>();
		for (URI uri : uris) {
			IFile iFile = getFile(uri);
			if (iFile != null) {
				iFilesSet.add(iFile);
			}
		}
		return iFilesSet.toArray(new IFile[iFilesSet.size()]);
	}

	private static IFile getFile(URI uri) {
		if (uri.isPlatform()) {
			return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true)));
		}
		return null;
	}
}

Back to the top