1 11 package org.eclipse.team.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 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 [] { 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 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 result = new ArrayList (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 o = configElements[0].createExecutableExtension("class"); 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 |