KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > DefaultFileModificationValidator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.core;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.team.FileModificationValidationContext;
18 import org.eclipse.core.resources.team.FileModificationValidator;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.ITeamStatus;
22 import org.eclipse.team.core.TeamStatus;
23
24 public class DefaultFileModificationValidator extends FileModificationValidator {
25     
26     /*
27      * A validator plugged in the the Team UI that will prompt
28      * the user to make read-only files writable. In the absence of
29      * this validator, edit/save fail on read-only files.
30      */

31     private FileModificationValidator uiValidator;
32
33     protected IStatus getDefaultStatus(IFile file) {
34         return
35             file.isReadOnly()
36             ? new TeamStatus(IStatus.ERROR, TeamPlugin.ID, ITeamStatus.READ_ONLY_LOCAL, NLS.bind(Messages.FileModificationValidator_fileIsReadOnly, new String JavaDoc[] { file.getFullPath().toString() }), null, file)
37                 : Status.OK_STATUS;
38     }
39     
40     public IStatus validateEdit(IFile[] files, FileModificationValidationContext context) {
41         IFile[] readOnlyFiles = getReadOnly(files);
42         if (readOnlyFiles.length == 0)
43             return Status.OK_STATUS;
44         synchronized (this) {
45             if (uiValidator == null)
46                 uiValidator = loadUIValidator();
47         }
48         if (uiValidator != null) {
49             return uiValidator.validateEdit(files, context);
50         }
51         // There was no plugged in validator so fail gracefully
52
return getStatus(files);
53     }
54
55     protected IStatus getStatus(IFile[] files) {
56         if (files.length == 1) {
57             return getDefaultStatus(files[0]);
58         }
59         
60         IStatus[] stati = new Status[files.length];
61         boolean allOK = true;
62         
63         for (int i = 0; i < files.length; i++) {
64             stati[i] = getDefaultStatus(files[i]);
65             if(! stati[i].isOK())
66                 allOK = false;
67         }
68         
69         return new MultiStatus(TeamPlugin.ID,
70             0, stati,
71             allOK
72                     ? Messages.ok
73                     : Messages.FileModificationValidator_someReadOnly,
74             null);
75     }
76
77     private IFile[] getReadOnly(IFile[] files) {
78         List JavaDoc result = new ArrayList JavaDoc(files.length);
79         for (int i = 0; i < files.length; i++) {
80             IFile file = files[i];
81             if (file.isReadOnly()) {
82                 result.add(file);
83             }
84         }
85         return (IFile[]) result.toArray(new IFile[result.size()]);
86     }
87
88     public IStatus validateSave(IFile file) {
89         if (!file.isReadOnly())
90             return Status.OK_STATUS;
91         synchronized (this) {
92             if (uiValidator == null)
93                 uiValidator = loadUIValidator();
94         }
95         if (uiValidator != null) {
96             return uiValidator.validateSave(file);
97         }
98         return getDefaultStatus(file);
99     }
100     
101     private FileModificationValidator loadUIValidator() {
102         IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(TeamPlugin.ID, TeamPlugin.DEFAULT_FILE_MODIFICATION_VALIDATOR_EXTENSION);
103         if (extension != null) {
104             IExtension[] extensions = extension.getExtensions();
105             if (extensions.length > 0) {
106                 IConfigurationElement[] configElements = extensions[0].getConfigurationElements();
107                 if (configElements.length > 0) {
108                     try {
109                         Object JavaDoc o = configElements[0].createExecutableExtension("class"); //$NON-NLS-1$
110
if (o instanceof FileModificationValidator) {
111                             return (FileModificationValidator)o;
112                         }
113                     } catch (CoreException e) {
114                         TeamPlugin.log(e);
115                     }
116                 }
117             }
118         }
119         return null;
120     }
121 }
122
Popular Tags