1 11 package org.eclipse.ui.internal.ide.model; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IProject; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IAdaptable; 18 import org.eclipse.core.runtime.QualifiedName; 19 import org.eclipse.core.runtime.content.IContentDescription; 20 import org.eclipse.core.runtime.content.IContentType; 21 import org.eclipse.jface.resource.ImageDescriptor; 22 import org.eclipse.ui.IResourceActionFilter; 23 import org.eclipse.ui.actions.SimpleWildcardTester; 24 import org.eclipse.ui.model.WorkbenchAdapter; 25 26 29 public abstract class WorkbenchResource extends WorkbenchAdapter implements 30 IResourceActionFilter { 31 32 35 protected abstract ImageDescriptor getBaseImage(IResource resource); 36 37 40 public ImageDescriptor getImageDescriptor(Object o) { 41 IResource resource = getResource(o); 42 return resource == null ? null : getBaseImage(resource); 43 } 44 45 48 public String getLabel(Object o) { 49 IResource resource = getResource(o); 50 return resource == null ? null : resource.getName(); 51 } 52 53 57 public Object getParent(Object o) { 58 IResource resource = getResource(o); 59 return resource == null ? null : resource.getParent(); 60 } 61 62 66 protected IResource getResource(Object o) { 67 if (o instanceof IResource) { 68 return (IResource) o; 69 } 70 if (o instanceof IAdaptable) { 71 return (IResource) ((IAdaptable) o).getAdapter(IResource.class); 72 } 73 return null; 74 } 75 76 85 public boolean testAttribute(Object target, String name, String value) { 86 if (!(target instanceof IResource)) { 87 return false; 88 } 89 IResource res = (IResource) target; 90 if (name.equals(NAME)) { 91 return SimpleWildcardTester.testWildcardIgnoreCase(value, res 92 .getName()); 93 } else if (name.equals(PATH)) { 94 return SimpleWildcardTester.testWildcardIgnoreCase(value, res 95 .getFullPath().toString()); 96 } else if (name.equals(EXTENSION)) { 97 return SimpleWildcardTester.testWildcardIgnoreCase(value, res 98 .getFileExtension()); 99 } else if (name.equals(READ_ONLY)) { 100 return (res.isReadOnly() == value.equalsIgnoreCase("true")); } else if (name.equals(PROJECT_NATURE)) { 102 try { 103 IProject proj = res.getProject(); 104 return proj.isAccessible() && proj.hasNature(value); 105 } catch (CoreException e) { 106 return false; 107 } 108 } else if (name.equals(PERSISTENT_PROPERTY)) { 109 return testProperty(res, true, false, value); 110 } else if (name.equals(PROJECT_PERSISTENT_PROPERTY)) { 111 return testProperty(res, true, true, value); 112 } else if (name.equals(SESSION_PROPERTY)) { 113 return testProperty(res, false, false, value); 114 } else if (name.equals(PROJECT_SESSION_PROPERTY)) { 115 return testProperty(res, false, true, value); 116 } else if (name.equals(CONTENT_TYPE_ID)) { 117 return testContentTypeProperty(res, value); 118 } 119 return false; 120 } 121 122 137 private final boolean testContentTypeProperty(final IResource resource, 138 final String contentTypeId) { 139 final String expectedValue = contentTypeId.trim(); 140 141 if (!(resource instanceof IFile)) { 142 return false; 143 } 144 145 final IFile file = (IFile) resource; 146 String actualValue = null; 147 148 try { 149 final IContentDescription contentDescription = file 150 .getContentDescription(); 151 152 if (contentDescription != null) { 153 final IContentType contentType = contentDescription 154 .getContentType(); 155 actualValue = contentType.getId(); 156 } 157 } catch (CoreException e) { 158 } 160 161 return expectedValue == null || expectedValue.equals(actualValue); 162 } 163 164 181 private boolean testProperty(IResource resource, boolean persistentFlag, 182 boolean projectFlag, String value) { 183 String propertyName; 184 String expectedVal; 185 int i = value.indexOf('='); 186 if (i != -1) { 187 propertyName = value.substring(0, i).trim(); 188 expectedVal = value.substring(i + 1).trim(); 189 } else { 190 propertyName = value.trim(); 191 expectedVal = null; 192 } 193 try { 194 QualifiedName key; 195 int dot = propertyName.lastIndexOf('.'); 196 if (dot != -1) { 197 key = new QualifiedName(propertyName.substring(0, dot), 198 propertyName.substring(dot + 1)); 199 } else { 200 key = new QualifiedName(null, propertyName); 201 } 202 IResource resToCheck = projectFlag ? resource.getProject() 203 : resource; 204 if (resToCheck == null) { 206 return false; 207 } 208 if (persistentFlag) { 209 String actualVal = resToCheck.getPersistentProperty(key); 210 if (actualVal == null) { 211 return false; 212 } 213 return expectedVal == null || expectedVal.equals(actualVal); 214 } 215 216 Object actualVal = resToCheck.getSessionProperty(key); 217 if (actualVal == null) { 218 return false; 219 } 220 221 return expectedVal == null 222 || expectedVal.equals(actualVal.toString()); 223 224 } catch (CoreException e) { 225 } 227 return false; 228 } 229 230 } 231 | Popular Tags |