KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import java.util.*;
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.IStatus;
20 import org.eclipse.core.runtime.MultiStatus;
21 import org.eclipse.team.core.RepositoryProvider;
22
23 public class FileModificationValidatorManager extends FileModificationValidator {
24     private FileModificationValidator defaultValidator;
25     
26     /*
27      * @see IFileModificationValidator#validateEdit(IFile[], Object)
28      * For all files, determine which provider.
29      * Ask each provider once for its files.
30      * Collect the resulting status' and return a MultiStatus.
31      */

32     public IStatus validateEdit(IFile[] files, FileModificationValidationContext context) {
33         ArrayList returnStati = new ArrayList();
34         
35         //map provider to the files under that provider's control
36
Map providersToFiles = new HashMap(files.length);
37         
38         //for each file, determine which provider, map providers to files
39
for (int i = 0; i < files.length; i++) {
40             IFile file = files[i];
41             RepositoryProvider provider = RepositoryProvider.getProvider(file.getProject());
42             
43             if (!providersToFiles.containsKey(provider)) {
44                 providersToFiles.put(provider, new ArrayList());
45             }
46             
47             ((ArrayList)providersToFiles.get(provider)).add(file);
48         }
49         
50         Iterator providersIterator = providersToFiles.keySet().iterator();
51         
52         boolean allOK = true;
53         
54         //for each provider, validate its files
55
while(providersIterator.hasNext()) {
56             RepositoryProvider provider = (RepositoryProvider)providersIterator.next();
57             ArrayList filesList = (ArrayList)providersToFiles.get(provider);
58             IFile[] filesArray = (IFile[])filesList.toArray(new IFile[filesList.size()]);
59             FileModificationValidator validator = getDefaultValidator();
60
61             //if no provider or no validator use the default validator
62
if (provider != null) {
63                 FileModificationValidator v = provider.getFileModificationValidator2();
64                 if (v != null) validator = v;
65             }
66             
67             IStatus status = validator.validateEdit(filesArray, context);
68             if(!status.isOK())
69                 allOK = false;
70
71             returnStati.add(status);
72         }
73
74         if (returnStati.size() == 1) {
75             return (IStatus)returnStati.get(0);
76         }
77         
78         return new MultiStatus(TeamPlugin.ID,
79             0,
80             (IStatus[])returnStati.toArray(new IStatus[returnStati.size()]),
81                 allOK
82                     ? Messages.ok
83                     : Messages.FileModificationValidator_editFailed,
84             null);
85     }
86
87     /*
88      * @see IFileModificationValidator#validateSave(IFile)
89      */

90     public IStatus validateSave(IFile file) {
91         RepositoryProvider provider = RepositoryProvider.getProvider(file.getProject());
92         FileModificationValidator validator = getDefaultValidator();
93
94         //if no provider or no validator use the default validator
95
if (provider != null) {
96             FileModificationValidator v = provider.getFileModificationValidator2();
97             if (v != null) validator = v;
98         }
99
100         return validator.validateSave(file);
101     }
102     
103     private synchronized FileModificationValidator getDefaultValidator() {
104         if (defaultValidator == null) {
105             defaultValidator = new DefaultFileModificationValidator();
106         }
107         return defaultValidator;
108     }
109 }
110
Popular Tags