1 11 package org.eclipse.ui.internal.ide.model; 12 13 import org.eclipse.core.expressions.PropertyTester; 14 import org.eclipse.core.internal.resources.mapping.ResourceMapping; 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.QualifiedName; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.jface.resource.ImageDescriptor; 22 import org.eclipse.ui.IActionFilter; 23 import org.eclipse.ui.IResourceActionFilter; 24 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 25 26 32 public class ProjectPersistentPropertyTester extends PropertyTester { 33 34 private static final String ALLOW_UNSET_PROJECTS = "allowUnsetProjects"; 36 private static final IActionFilter filter = new WorkbenchResource() { 37 38 41 protected ImageDescriptor getBaseImage(IResource resource) { 42 return null; 43 } 44 }; 45 46 49 public boolean test(Object receiver, String property, Object [] args, Object expectedValue) { 50 if (receiver instanceof ResourceMapping) { 51 if (property.equals(IResourceActionFilter.PROJECT_PERSISTENT_PROPERTY)) { 52 if(args == null) return false; 53 String persitentPropertyEntry = (String )args[0]; 54 boolean allowUnsetProjects = false; 55 if (args.length > 1) 56 allowUnsetProjects = args[1].equals(ALLOW_UNSET_PROJECTS); 57 IProject[] projects = ((ResourceMapping)receiver).getProjects(); 58 boolean atLeastOne = false; 59 for (int i = 0; i < projects.length; i++) { 60 IProject project = projects[i]; 61 if (filter.testAttribute(project, property, persitentPropertyEntry)) { 62 atLeastOne = true; 63 } else if (!allowUnsetProjects) { 64 return false; 65 } else { 66 try { 69 if (project != null && project.isAccessible() && project.getPersistentProperty(getPropertyKey(persitentPropertyEntry)) != null) 70 return false; 71 } catch (CoreException e) { 72 final String message = "Core exception while testing project persistent property"; IDEWorkbenchPlugin.log(message, 74 new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 75 IStatus.ERROR, message, e)); 76 } 78 } 79 } 80 return atLeastOne; 81 } 82 } else if (receiver instanceof IResource) { 83 if (property.equals(IResourceActionFilter.PROJECT_PERSISTENT_PROPERTY)) { 84 if(args == null) return false; 85 String persitentPropertyEntry = (String )args[0]; 86 IProject project = ((IResource)receiver).getProject(); 87 return filter.testAttribute(project, property, persitentPropertyEntry); 88 } 89 } 90 91 return false; 92 } 93 94 private QualifiedName getPropertyKey(String value) { 95 String propertyName; 96 int i = value.indexOf('='); 97 if (i != -1) { 98 propertyName = value.substring(0, i).trim(); 99 } else { 100 propertyName = value.trim(); 101 } 102 QualifiedName key; 103 int dot = propertyName.lastIndexOf('.'); 104 if (dot != -1) { 105 key = new QualifiedName(propertyName.substring(0, dot), 106 propertyName.substring(dot + 1)); 107 } else { 108 key = new QualifiedName(null, propertyName); 109 } 110 return key; 111 } 112 113 } 114 | Popular Tags |