001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide.model;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.core.runtime.QualifiedName;
018: import org.eclipse.core.runtime.content.IContentDescription;
019: import org.eclipse.core.runtime.content.IContentType;
020: import org.eclipse.jface.resource.ImageDescriptor;
021: import org.eclipse.ui.IResourceActionFilter;
022: import org.eclipse.ui.actions.SimpleWildcardTester;
023: import org.eclipse.ui.model.WorkbenchAdapter;
024:
025: /**
026: * An IWorkbenchAdapter that represents IResources.
027: */
028: public abstract class WorkbenchResource extends WorkbenchAdapter
029: implements IResourceActionFilter {
030:
031: /**
032: * Answer the appropriate base image to use for the resource.
033: */
034: protected abstract ImageDescriptor getBaseImage(IResource resource);
035:
036: /**
037: * Returns an image descriptor for this object.
038: */
039: public ImageDescriptor getImageDescriptor(Object o) {
040: IResource resource = getResource(o);
041: return resource == null ? null : getBaseImage(resource);
042: }
043:
044: /**
045: * getLabel method comment.
046: */
047: public String getLabel(Object o) {
048: IResource resource = getResource(o);
049: return resource == null ? null : resource.getName();
050: }
051:
052: /**
053: * Returns the parent of the given object. Returns null if the
054: * parent is not available.
055: */
056: public Object getParent(Object o) {
057: IResource resource = getResource(o);
058: return resource == null ? null : resource.getParent();
059: }
060:
061: /**
062: * Returns the resource corresponding to this object,
063: * or null if there is none.
064: */
065: protected IResource getResource(Object o) {
066: if (o instanceof IResource) {
067: return (IResource) o;
068: }
069: if (o instanceof IAdaptable) {
070: return (IResource) ((IAdaptable) o)
071: .getAdapter(IResource.class);
072: }
073: return null;
074: }
075:
076: /**
077: * Returns whether the specific attribute matches the state of the target
078: * object.
079: *
080: * @param target the target object
081: * @param name the attribute name
082: * @param value the attribute value
083: * @return <code>true</code> if the attribute matches; <code>false</code> otherwise
084: */
085: public boolean testAttribute(Object target, String name,
086: String value) {
087: if (!(target instanceof IResource)) {
088: return false;
089: }
090: IResource res = (IResource) target;
091: if (name.equals(NAME)) {
092: return SimpleWildcardTester.testWildcardIgnoreCase(value,
093: res.getName());
094: } else if (name.equals(PATH)) {
095: return SimpleWildcardTester.testWildcardIgnoreCase(value,
096: res.getFullPath().toString());
097: } else if (name.equals(EXTENSION)) {
098: return SimpleWildcardTester.testWildcardIgnoreCase(value,
099: res.getFileExtension());
100: } else if (name.equals(READ_ONLY)) {
101: return (res.isReadOnly() == value.equalsIgnoreCase("true"));//$NON-NLS-1$
102: } else if (name.equals(PROJECT_NATURE)) {
103: try {
104: IProject proj = res.getProject();
105: return proj.isAccessible() && proj.hasNature(value);
106: } catch (CoreException e) {
107: return false;
108: }
109: } else if (name.equals(PERSISTENT_PROPERTY)) {
110: return testProperty(res, true, false, value);
111: } else if (name.equals(PROJECT_PERSISTENT_PROPERTY)) {
112: return testProperty(res, true, true, value);
113: } else if (name.equals(SESSION_PROPERTY)) {
114: return testProperty(res, false, false, value);
115: } else if (name.equals(PROJECT_SESSION_PROPERTY)) {
116: return testProperty(res, false, true, value);
117: } else if (name.equals(CONTENT_TYPE_ID)) {
118: return testContentTypeProperty(res, value);
119: }
120: return false;
121: }
122:
123: /**
124: * Tests whether the content type for <code>resource</code> matches the
125: * <code>contentTypeId</code>. It is possible that this method call could
126: * cause the resource to be read. It is also possible (through poor plug-in
127: * design) for this method to load plug-ins.
128: *
129: * @param resource
130: * The resource for which the content type should be determined;
131: * must not be <code>null</code>.
132: * @param contentTypeId
133: * The expected content type; must not be <code>null</code>.
134: * @return <code>true</code> iff the best matching content type has an
135: * identifier that matches <code>contentTypeId</code>;
136: * <code>false</code> otherwise.
137: */
138: private final boolean testContentTypeProperty(
139: final IResource resource, final String contentTypeId) {
140: final String expectedValue = contentTypeId.trim();
141:
142: if (!(resource instanceof IFile)) {
143: return false;
144: }
145:
146: final IFile file = (IFile) resource;
147: String actualValue = null;
148:
149: try {
150: final IContentDescription contentDescription = file
151: .getContentDescription();
152:
153: if (contentDescription != null) {
154: final IContentType contentType = contentDescription
155: .getContentType();
156: actualValue = contentType.getId();
157: }
158: } catch (CoreException e) {
159: //ignore - this just means the file does not exist or is not accessible
160: }
161:
162: return expectedValue == null
163: || expectedValue.equals(actualValue);
164: }
165:
166: /**
167: * Tests whether a session or persistent property on the resource or its project
168: * matches the given value.
169: *
170: * @param resource
171: * the resource to check
172: * @param persistentFlag
173: * <code>true</code> for a persistent property, <code>false</code>
174: * for a session property
175: * @param projectFlag
176: * <code>true</code> to check the resource's project,
177: * <code>false</code> to check the resource itself
178: * @param value
179: * the attribute value, which has either the form "propertyName" or
180: * "propertyName=propertyValue"
181: * @return whether there is a match
182: */
183: private boolean testProperty(IResource resource,
184: boolean persistentFlag, boolean projectFlag, String value) {
185: String propertyName;
186: String expectedVal;
187: int i = value.indexOf('=');
188: if (i != -1) {
189: propertyName = value.substring(0, i).trim();
190: expectedVal = value.substring(i + 1).trim();
191: } else {
192: propertyName = value.trim();
193: expectedVal = null;
194: }
195: try {
196: QualifiedName key;
197: int dot = propertyName.lastIndexOf('.');
198: if (dot != -1) {
199: key = new QualifiedName(propertyName.substring(0, dot),
200: propertyName.substring(dot + 1));
201: } else {
202: key = new QualifiedName(null, propertyName);
203: }
204: IResource resToCheck = projectFlag ? resource.getProject()
205: : resource;
206: // getProject() on workspace root can be null
207: if (resToCheck == null) {
208: return false;
209: }
210: if (persistentFlag) {
211: String actualVal = resToCheck
212: .getPersistentProperty(key);
213: if (actualVal == null) {
214: return false;
215: }
216: return expectedVal == null
217: || expectedVal.equals(actualVal);
218: }
219:
220: Object actualVal = resToCheck.getSessionProperty(key);
221: if (actualVal == null) {
222: return false;
223: }
224:
225: return expectedVal == null
226: || expectedVal.equals(actualVal.toString());
227:
228: } catch (CoreException e) {
229: // ignore
230: }
231: return false;
232: }
233:
234: }
|