KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > CVSCoreFileModificationValidator


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.ccvs.core;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.resources.team.FileModificationValidationContext;
17 import org.eclipse.core.resources.team.FileModificationValidator;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.jobs.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.RepositoryProvider;
22 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
23
24 /**
25  * Core validator that will load the UI validator only if a prompt is needed
26  */

27 public class CVSCoreFileModificationValidator extends FileModificationValidator implements ICVSFileModificationValidator {
28     
29     FileModificationValidator uiValidator;
30
31     /* (non-Javadoc)
32      * @see org.eclipse.core.resources.team.FileModificationValidator#validateEdit(org.eclipse.core.resources.IFile[], org.eclipse.core.resources.team.FileModificationValidationContext)
33      */

34     public IStatus validateEdit(IFile[] files, FileModificationValidationContext context) {
35         IFile[] unmanagedReadOnlyFiles = getUnmanagedReadOnlyFiles(files);
36         if (unmanagedReadOnlyFiles.length > 0) {
37             IStatus status = setWritable(unmanagedReadOnlyFiles);
38             if (!status.isOK()) {
39                 return status;
40             }
41         }
42         IFile[] readOnlyFiles = getManagedReadOnlyFiles(files);
43         if (readOnlyFiles.length == 0) return Status.OK_STATUS;
44         return edit(readOnlyFiles, context);
45     }
46
47     /* (non-Javadoc)
48      * @see org.eclipse.core.resources.IFileModificationValidator#validateSave(org.eclipse.core.resources.IFile)
49      */

50     public IStatus validateSave(IFile file) {
51         if (!needsCheckout(file)) {
52             if (file.isReadOnly()) {
53                 setWritable(new IFile[] { file } );
54             }
55             return Status.OK_STATUS;
56         }
57         return edit(new IFile[] {file}, (FileModificationValidationContext)null);
58     }
59
60     /**
61      * Method for editing a set of files. Is overriden by the
62      * UI to prompt the user. Default behavior is to try and load the
63      * UI validator and, failing that, to edit without
64      * prompting.
65      * @param readOnlyFiles
66      * @param context
67      * @return
68      */

69     protected IStatus edit(IFile[] readOnlyFiles, FileModificationValidationContext context) {
70         FileModificationValidator override = getUIValidator();
71         if (override != null) {
72             return override.validateEdit(readOnlyFiles, context);
73         } else {
74             performEdit(readOnlyFiles);
75             return Status.OK_STATUS;
76         }
77     }
78
79     private FileModificationValidator getUIValidator() {
80         synchronized(this) {
81             if (uiValidator == null) {
82                 uiValidator = getPluggedInValidator();
83             }
84         }
85         return uiValidator;
86     }
87     
88     /**
89      * @see org.eclipse.team.internal.ccvs.core.ICVSFileModificationValidator#validateMoveDelete(org.eclipse.core.resources.IFile[], org.eclipse.core.runtime.IProgressMonitor)
90      */

91     public IStatus validateMoveDelete(IFile[] files, IProgressMonitor monitor) {
92         IFile[] readOnlyFiles = getManagedReadOnlyFiles(files);
93         if (readOnlyFiles.length == 0) return Status.OK_STATUS;
94
95         performEdit(readOnlyFiles);
96         return Status.OK_STATUS;
97     }
98     
99     /*
100      * Perform the headless edit check in the background.
101      * The user will be notified of any errors that occurred.
102      */

103     protected void performEdit(final IFile[] readOnlyFiles) {
104         setWritable(readOnlyFiles);
105         Job job = new Job(CVSMessages.CVSCoreFileModificationValidator_editJob) {
106             protected IStatus run(IProgressMonitor monitor) {
107                 try {
108                     performEdit(readOnlyFiles, monitor);
109                 } catch (CVSException e) {
110                     return e.getStatus();
111                 }
112                 return Status.OK_STATUS;
113             }
114         };
115         scheduleEditJob(job);
116     }
117
118     protected void scheduleEditJob(Job job) {
119         job.schedule();
120     }
121
122     protected CVSTeamProvider getProvider(IFile[] files) {
123         CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(files[0].getProject(), CVSProviderPlugin.getTypeId());
124         return provider;
125     }
126     
127     protected void performEdit(IFile[] files, IProgressMonitor monitor) throws CVSException {
128         getProvider(files).edit(files, false /* recurse */, true /* notify server */, true /* notify for writtable files */, ICVSFile.NO_NOTIFICATION, monitor);
129     }
130     
131     private boolean needsCheckout(IFile file) {
132         try {
133             if (file.isReadOnly()) {
134                 ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(file);
135                 boolean managed = cvsFile.isManaged();
136                 return managed;
137             }
138         } catch (CVSException e) {
139             // Log the exception and assume we don't need a checkout
140
CVSProviderPlugin.log(e);
141         }
142         return false;
143     }
144     
145     protected IStatus setWritable(final IFile[] files) {
146         for (int i = 0; i < files.length; i++) {
147             IFile file = files[i];
148             ResourceAttributes attributes = file.getResourceAttributes();
149             if (attributes != null) {
150                 attributes.setReadOnly(false);
151             }
152             try {
153                 file.setResourceAttributes(attributes);
154             } catch (CoreException e) {
155                 return CVSException.wrapException(e).getStatus();
156             }
157         }
158         return Status.OK_STATUS;
159     }
160     
161     private IFile[] getManagedReadOnlyFiles(IFile[] files) {
162         List readOnlys = new ArrayList();
163         for (int i = 0; i < files.length; i++) {
164             IFile iFile = files[i];
165             if (needsCheckout(iFile)) {
166                 readOnlys.add(iFile);
167             }
168         }
169         return (IFile[]) readOnlys.toArray(new IFile[readOnlys.size()]);
170     }
171     
172     protected IFile[] getUnmanagedReadOnlyFiles(IFile[] files) {
173         List readOnlys = new ArrayList();
174         for (int i = 0; i < files.length; i++) {
175             IFile iFile = files[i];
176             if (iFile.isReadOnly() && !needsCheckout(iFile)) {
177                 readOnlys.add(iFile);
178             }
179         }
180         return (IFile[]) readOnlys.toArray(new IFile[readOnlys.size()]);
181     }
182     
183     private static FileModificationValidator getPluggedInValidator() {
184         IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint(CVSProviderPlugin.ID, CVSProviderPlugin.PT_FILE_MODIFICATION_VALIDATOR).getExtensions();
185         if (extensions.length == 0)
186             return null;
187         IExtension extension = extensions[0];
188         IConfigurationElement[] configs = extension.getConfigurationElements();
189         if (configs.length == 0) {
190             CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("The CVS file modification validator is missing from extension {0}", (new Object JavaDoc[] {extension.getUniqueIdentifier()})), null);//$NON-NLS-1$
191
return null;
192         }
193         try {
194             IConfigurationElement config = configs[0];
195             return (FileModificationValidator) config.createExecutableExtension("run");//$NON-NLS-1$
196
} catch (CoreException ex) {
197             CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("The CVS file modification validator registered as ID {0} could not be instantiated", (new Object JavaDoc[] {extension.getUniqueIdentifier()})), ex);//$NON-NLS-1$
198
return null;
199         }
200     }
201     
202     public ISchedulingRule validateEditRule(CVSResourceRuleFactory factory, IResource[] resources) {
203         FileModificationValidator override = getUIValidator();
204         if (override instanceof CVSCoreFileModificationValidator && override != this) {
205             CVSCoreFileModificationValidator ui = (CVSCoreFileModificationValidator) override;
206             return ui.validateEditRule(factory, resources);
207         }
208         return internalValidateEditRule(factory, resources);
209     }
210
211     protected final ISchedulingRule internalValidateEditRule(CVSResourceRuleFactory factory, IResource[] resources) {
212         if (resources.length == 0)
213             return null;
214         //optimize rule for single file
215
if (resources.length == 1)
216             return isReadOnly(resources[0]) ? factory.getParent(resources[0]) : null;
217         //need a lock on the parents of all read-only files
218
HashSet rules = new HashSet();
219         for (int i = 0; i < resources.length; i++)
220             if (isReadOnly(resources[i]))
221                 rules.add(factory.getParent(resources[i]));
222         return createSchedulingRule(rules);
223     }
224
225     protected ISchedulingRule createSchedulingRule(Set rules) {
226         if (rules.isEmpty())
227             return null;
228         if (rules.size() == 1)
229             return (ISchedulingRule) rules.iterator().next();
230         ISchedulingRule[] ruleArray = (ISchedulingRule[]) rules
231                 .toArray(new ISchedulingRule[rules.size()]);
232         return new MultiRule(ruleArray);
233     }
234     
235     protected final boolean isReadOnly(IResource resource) {
236         ResourceAttributes a = resource.getResourceAttributes();
237         if (a != null) {
238             return a.isReadOnly();
239         }
240         return false;
241     }
242 }
243
Popular Tags