Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37f0f525b14ef012deb331fb8aa1d3583627a9f0 (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
/*******************************************************************************
 * Copyright (c) 2000, 2017 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.core;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.team.FileModificationValidationContext;
import org.eclipse.core.resources.team.FileModificationValidator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.ITeamStatus;
import org.eclipse.team.core.TeamStatus;

public class DefaultFileModificationValidator extends FileModificationValidator {

	/*
	 * A validator plugged in the the Team UI that will prompt
	 * the user to make read-only files writable. In the absence of
	 * this validator, edit/save fail on read-only files.
	 */
	private FileModificationValidator uiValidator;

	protected IStatus getDefaultStatus(IFile file) {
		return
			file.isReadOnly()
			? new TeamStatus(IStatus.ERROR, TeamPlugin.ID, ITeamStatus.READ_ONLY_LOCAL, NLS.bind(Messages.FileModificationValidator_fileIsReadOnly, new String[] { file.getFullPath().toString() }), null, file)
				: Status.OK_STATUS;
	}

	@Override
	public IStatus validateEdit(IFile[] files, FileModificationValidationContext context) {
	    IFile[] readOnlyFiles = getReadOnly(files);
	    if (readOnlyFiles.length == 0)
	        return Status.OK_STATUS;
	    synchronized (this) {
	        if (uiValidator == null)
	            uiValidator = loadUIValidator();
	    }
	    if (uiValidator != null) {
	        return uiValidator.validateEdit(files, context);
	    }
	    // There was no plugged in validator so fail gracefully
		return getStatus(files);
	}

    protected IStatus getStatus(IFile[] files) {
        if (files.length == 1) {
			return getDefaultStatus(files[0]);
		}

		IStatus[] stati = new Status[files.length];
		boolean allOK = true;

		for (int i = 0; i < files.length; i++) {
			stati[i] = getDefaultStatus(files[i]);
			if(! stati[i].isOK())
				allOK = false;
		}

		return new MultiStatus(TeamPlugin.ID,
			0, stati,
			allOK
					? Messages.ok
					: Messages.FileModificationValidator_someReadOnly,
			null);
    }

    private IFile[] getReadOnly(IFile[] files) {
        List<IFile> result = new ArrayList<>(files.length);
        for (int i = 0; i < files.length; i++) {
            IFile file = files[i];
            if (file.isReadOnly()) {
                result.add(file);
            }
        }
        return result.toArray(new IFile[result.size()]);
    }

	@Override
	public IStatus validateSave(IFile file) {
	    if (!file.isReadOnly())
	        return Status.OK_STATUS;
	    synchronized (this) {
	        if (uiValidator == null)
	            uiValidator = loadUIValidator();
	    }
	    if (uiValidator != null) {
	        return uiValidator.validateSave(file);
	    }
		return getDefaultStatus(file);
	}

    private FileModificationValidator loadUIValidator() {
        IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(TeamPlugin.ID, TeamPlugin.DEFAULT_FILE_MODIFICATION_VALIDATOR_EXTENSION);
		if (extension != null) {
			IExtension[] extensions =  extension.getExtensions();
			if (extensions.length > 0) {
				IConfigurationElement[] configElements = extensions[0].getConfigurationElements();
				if (configElements.length > 0) {
					try {
                        Object o = configElements[0].createExecutableExtension("class"); //$NON-NLS-1$
                        if (o instanceof FileModificationValidator) {
                            return (FileModificationValidator)o;
                        }
                    } catch (CoreException e) {
                        TeamPlugin.log(e);
                    }
				}
			}
		}
		return null;
    }
}

Back to the top