KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > properties > ResourcePropertySource


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.views.properties;
12
13 import java.io.File JavaDoc;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.ResourceAttributes;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.jface.viewers.IBasicPropertyConstants;
19 import org.eclipse.osgi.util.TextProcessor;
20 import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils;
21 import org.eclipse.ui.internal.views.properties.IDEPropertiesMessages;
22
23 /**
24  * A Resource property source.
25  */

26 public class ResourcePropertySource implements IPropertySource {
27     protected static String JavaDoc NOT_LOCAL_TEXT = IDEPropertiesMessages.PropertySource_notLocal;
28
29     protected static String JavaDoc FILE_NOT_FOUND = IDEPropertiesMessages.PropertySource_notFound;
30
31     protected static String JavaDoc UNDEFINED_PATH_VARIABLE = IDEPropertiesMessages.PropertySource_undefinedPathVariable;
32
33     protected static String JavaDoc FILE_NOT_EXIST_TEXT = IDEPropertiesMessages.PropertySource_fileNotExist;
34
35     // The element for the property source
36
protected IResource element;
37
38     // Error message when setting a property incorrectly
39
protected String JavaDoc errorMessage = IDEPropertiesMessages.PropertySource_readOnly;
40
41     // Property Descriptors
42
static protected IPropertyDescriptor[] propertyDescriptors = new IPropertyDescriptor[7];
43
44     static protected IPropertyDescriptor[] propertyDescriptorsLinkVariable = new IPropertyDescriptor[8];
45     static {
46         PropertyDescriptor descriptor;
47
48         // resource name
49
descriptor = new PropertyDescriptor(IBasicPropertyConstants.P_TEXT,
50                 IResourcePropertyConstants.P_LABEL_RES);
51         descriptor.setAlwaysIncompatible(true);
52         descriptor
53                 .setCategory(IResourcePropertyConstants.P_FILE_SYSTEM_CATEGORY);
54         propertyDescriptors[0] = descriptor;
55         propertyDescriptorsLinkVariable[0] = descriptor;
56
57         // Relative path
58
descriptor = new PropertyDescriptor(
59                 IResourcePropertyConstants.P_PATH_RES,
60                 IResourcePropertyConstants.P_DISPLAYPATH_RES);
61         descriptor.setAlwaysIncompatible(true);
62         descriptor
63                 .setCategory(IResourcePropertyConstants.P_FILE_SYSTEM_CATEGORY);
64         propertyDescriptors[1] = descriptor;
65         propertyDescriptorsLinkVariable[1] = descriptor;
66
67         // readwrite state
68
descriptor = new PropertyDescriptor(
69                 IResourcePropertyConstants.P_EDITABLE_RES,
70                 IResourcePropertyConstants.P_DISPLAYEDITABLE_RES);
71         descriptor.setAlwaysIncompatible(true);
72         descriptor
73                 .setCategory(IResourcePropertyConstants.P_FILE_SYSTEM_CATEGORY);
74         propertyDescriptors[2] = descriptor;
75         propertyDescriptorsLinkVariable[2] = descriptor;
76
77         // derived state
78
descriptor = new PropertyDescriptor(
79                 IResourcePropertyConstants.P_DERIVED_RES,
80                 IResourcePropertyConstants.P_DISPLAYDERIVED_RES);
81         descriptor.setAlwaysIncompatible(true);
82         descriptor
83                 .setCategory(IResourcePropertyConstants.P_FILE_SYSTEM_CATEGORY);
84         propertyDescriptors[3] = descriptor;
85         propertyDescriptorsLinkVariable[3] = descriptor;
86
87         // last modified state
88
descriptor = new PropertyDescriptor(
89                 IResourcePropertyConstants.P_LAST_MODIFIED_RES,
90                 IResourcePropertyConstants.P_DISPLAY_LAST_MODIFIED);
91         descriptor.setAlwaysIncompatible(true);
92         descriptor
93                 .setCategory(IResourcePropertyConstants.P_FILE_SYSTEM_CATEGORY);
94         propertyDescriptors[4] = descriptor;
95         propertyDescriptorsLinkVariable[4] = descriptor;
96
97         // link state
98
descriptor = new PropertyDescriptor(
99                 IResourcePropertyConstants.P_LINKED_RES,
100                 IResourcePropertyConstants.P_DISPLAYLINKED_RES);
101         descriptor.setAlwaysIncompatible(true);
102         descriptor
103                 .setCategory(IResourcePropertyConstants.P_FILE_SYSTEM_CATEGORY);
104         propertyDescriptors[5] = descriptor;
105         propertyDescriptorsLinkVariable[5] = descriptor;
106
107         // location
108
descriptor = new PropertyDescriptor(
109                 IResourcePropertyConstants.P_LOCATION_RES,
110                 IResourcePropertyConstants.P_DISPLAYLOCATION_RES);
111         descriptor.setAlwaysIncompatible(true);
112         descriptor
113                 .setCategory(IResourcePropertyConstants.P_FILE_SYSTEM_CATEGORY);
114         propertyDescriptors[6] = descriptor;
115         propertyDescriptorsLinkVariable[6] = descriptor;
116
117         // resolved location
118
descriptor = new PropertyDescriptor(
119                 IResourcePropertyConstants.P_RESOLVED_LOCATION_RES,
120                 IResourcePropertyConstants.P_DISPLAYRESOLVED_LOCATION_RES);
121         descriptor.setAlwaysIncompatible(true);
122         descriptor
123                 .setCategory(IResourcePropertyConstants.P_FILE_SYSTEM_CATEGORY);
124         propertyDescriptorsLinkVariable[7] = descriptor;
125
126     }
127
128     /**
129      * Creates a PropertySource and stores its IResource
130      *
131      * @param res the resource for which this is a property source
132      */

133     public ResourcePropertySource(IResource res) {
134         Assert.isNotNull(res);
135         this.element = res;
136     }
137
138     /* (non-Javadoc)
139      * Method declared on IPropertySource.
140      */

141     public Object JavaDoc getEditableValue() {
142         return this;
143     }
144
145     /* (non-Javadoc)
146      * Method declared on IPropertySource.
147      */

148     public IPropertyDescriptor[] getPropertyDescriptors() {
149         if (isPathVariable(element)) {
150             return propertyDescriptorsLinkVariable;
151         }
152         return propertyDescriptors;
153     }
154
155     /* (non-Javadoc)
156      * Method declared on IPropertySource.
157      */

158     public Object JavaDoc getPropertyValue(Object JavaDoc name) {
159         if (name.equals(IBasicPropertyConstants.P_TEXT)) {
160             return element.getName();
161         }
162         if (name.equals(IResourcePropertyConstants.P_PATH_RES)) {
163             return TextProcessor.process(element.getFullPath().toString());
164         }
165         if (name.equals(IResourcePropertyConstants.P_LAST_MODIFIED_RES)) {
166             return IDEResourceInfoUtils.getDateStringValue(element);
167         }
168         if (name.equals(IResourcePropertyConstants.P_EDITABLE_RES)) {
169             final ResourceAttributes attributes = element.getResourceAttributes();
170             if (attributes == null || attributes.isReadOnly()) {
171                 return IDEPropertiesMessages.ResourceProperty_false;
172             }
173             return IDEPropertiesMessages.ResourceProperty_true;
174         }
175         if (name.equals(IResourcePropertyConstants.P_DERIVED_RES)) {
176             if (element.isDerived())
177                 return IDEPropertiesMessages.ResourceProperty_true;
178             return IDEPropertiesMessages.ResourceProperty_false;
179         }
180         if (name.equals(IResourcePropertyConstants.P_LINKED_RES)) {
181             if (element.isLinked())
182                 return IDEPropertiesMessages.ResourceProperty_true;
183             return IDEPropertiesMessages.ResourceProperty_false;
184         }
185         if (name.equals(IResourcePropertyConstants.P_LOCATION_RES)) {
186             return TextProcessor.process(IDEResourceInfoUtils.getLocationText(element));
187         }
188         if (name.equals(IResourcePropertyConstants.P_RESOLVED_LOCATION_RES)) {
189             return TextProcessor.process(IDEResourceInfoUtils.getResolvedLocationText(element));
190         }
191         return null;
192     }
193
194     /**
195      * Returns whether the given resource is a linked resource bound
196      * to a path variable.
197      *
198      * @param resource resource to test
199      * @return boolean <code>true</code> the given resource is a linked
200      * resource bound to a path variable. <code>false</code> the given
201      * resource is either not a linked resource or it is not using a
202      * path variable.
203      */

204     private boolean isPathVariable(IResource resource) {
205         if (!resource.isLinked()) {
206             return false;
207         }
208
209         IPath resolvedLocation = resource.getLocation();
210         if (resolvedLocation == null) {
211             // missing path variable
212
return true;
213         }
214         IPath rawLocation = resource.getRawLocation();
215         if (resolvedLocation.equals(rawLocation)) {
216             return false;
217         }
218
219         return true;
220     }
221
222     /* (non-Javadoc)
223      * Method declared on IPropertySource.
224      */

225     public boolean isPropertySet(Object JavaDoc property) {
226         return false;
227     }
228
229     /**
230      * The <code>ResourcePropertySource</code> implementation of this
231      * <code>IPropertySource</code> method does nothing since all
232      * properties are read-only.
233      */

234     public void resetPropertyValue(Object JavaDoc property) {
235     }
236
237     /**
238      * The <code>ResourcePropertySource</code> implementation of this
239      * <code>IPropertySource</code> method does nothing since all
240      * properties are read-only.
241      */

242     public void setPropertyValue(Object JavaDoc name, Object JavaDoc value) {
243     }
244
245     /**
246      * Get the java.io.File equivalent of the passed
247      * IFile. If the location does not exist then return
248      * <code>null</code>
249      * @param resource the resource to lookup
250      * @return java.io.File or <code>null</code>.
251      */

252     protected File JavaDoc getFile(IResource resource) {
253         IPath location = resource.getLocation();
254         if (location == null) {
255             return null;
256         }
257         return location.toFile();
258     }
259
260 }
261
Popular Tags