KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > model > WorkbenchResource


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.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 /**
27  * An IWorkbenchAdapter that represents IResources.
28  */

29 public abstract class WorkbenchResource extends WorkbenchAdapter implements
30         IResourceActionFilter {
31
32     /**
33      * Answer the appropriate base image to use for the resource.
34      */

35     protected abstract ImageDescriptor getBaseImage(IResource resource);
36
37     /**
38      * Returns an image descriptor for this object.
39      */

40     public ImageDescriptor getImageDescriptor(Object JavaDoc o) {
41         IResource resource = getResource(o);
42         return resource == null ? null : getBaseImage(resource);
43     }
44
45     /**
46      * getLabel method comment.
47      */

48     public String JavaDoc getLabel(Object JavaDoc o) {
49         IResource resource = getResource(o);
50         return resource == null ? null : resource.getName();
51     }
52
53     /**
54      * Returns the parent of the given object. Returns null if the
55      * parent is not available.
56      */

57     public Object JavaDoc getParent(Object JavaDoc o) {
58         IResource resource = getResource(o);
59         return resource == null ? null : resource.getParent();
60     }
61
62     /**
63      * Returns the resource corresponding to this object,
64      * or null if there is none.
65      */

66     protected IResource getResource(Object JavaDoc 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     /**
77      * Returns whether the specific attribute matches the state of the target
78      * object.
79      *
80      * @param target the target object
81      * @param name the attribute name
82      * @param value the attribute value
83      * @return <code>true</code> if the attribute matches; <code>false</code> otherwise
84      */

85     public boolean testAttribute(Object JavaDoc target, String JavaDoc name, String JavaDoc 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"));//$NON-NLS-1$
101
} 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     /**
123      * Tests whether the content type for <code>resource</code> matches the
124      * <code>contentTypeId</code>. It is possible that this method call could
125      * cause the resource to be read. It is also possible (through poor plug-in
126      * design) for this method to load plug-ins.
127      *
128      * @param resource
129      * The resource for which the content type should be determined;
130      * must not be <code>null</code>.
131      * @param contentTypeId
132      * The expected content type; must not be <code>null</code>.
133      * @return <code>true</code> iff the best matching content type has an
134      * identifier that matches <code>contentTypeId</code>;
135      * <code>false</code> otherwise.
136      */

137     private final boolean testContentTypeProperty(final IResource resource,
138             final String JavaDoc contentTypeId) {
139         final String JavaDoc expectedValue = contentTypeId.trim();
140
141         if (!(resource instanceof IFile)) {
142             return false;
143         }
144
145         final IFile file = (IFile) resource;
146         String JavaDoc 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             //ignore - this just means the file does not exist or is not accessible
159
}
160
161         return expectedValue == null || expectedValue.equals(actualValue);
162     }
163
164     /**
165      * Tests whether a session or persistent property on the resource or its project
166      * matches the given value.
167      *
168      * @param resource
169      * the resource to check
170      * @param persistentFlag
171      * <code>true</code> for a persistent property, <code>false</code>
172      * for a session property
173      * @param projectFlag
174      * <code>true</code> to check the resource's project,
175      * <code>false</code> to check the resource itself
176      * @param value
177      * the attribute value, which has either the form "propertyName" or
178      * "propertyName=propertyValue"
179      * @return whether there is a match
180      */

181     private boolean testProperty(IResource resource, boolean persistentFlag,
182             boolean projectFlag, String JavaDoc value) {
183         String JavaDoc propertyName;
184         String JavaDoc 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             // getProject() on workspace root can be null
205
if (resToCheck == null) {
206                 return false;
207             }
208             if (persistentFlag) {
209                 String JavaDoc actualVal = resToCheck.getPersistentProperty(key);
210                 if (actualVal == null) {
211                     return false;
212                 }
213                 return expectedVal == null || expectedVal.equals(actualVal);
214             }
215
216             Object JavaDoc 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             // ignore
226
}
227         return false;
228     }
229
230 }
231
Popular Tags