KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > resources > mapping > ModelProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.core.resources.mapping;
12
13 import java.util.*;
14 import org.eclipse.core.internal.resources.mapping.ModelProviderManager;
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.*;
17
18 /**
19  * Represents the provider of a logical model. The main purpose of this
20  * API is to support batch operations on sets of <code>ResourceMapping</code>
21  * objects that are part of the same model.
22  *
23  * TODO: include xml snippet
24  *
25  * <p>
26  * This class may be subclassed by clients.
27  * </p>
28  * @see org.eclipse.core.resources.mapping.ResourceMapping
29  * @since 3.2
30  */

31 public abstract class ModelProvider extends PlatformObject {
32
33     /**
34      * The model provider id of the Resources model.
35      */

36     public static final String JavaDoc RESOURCE_MODEL_PROVIDER_ID = "org.eclipse.core.resources.modelProvider"; //$NON-NLS-1$
37

38     private IModelProviderDescriptor descriptor;
39
40     /**
41      * Return the descriptor for the model provider of the given id
42      * or <code>null</code> if the provider has not been registered.
43      * @param id a model provider id.
44      * @return the descriptor for the model provider of the given id
45      * or <code>null</code> if the provider has not been registered
46      */

47     public static IModelProviderDescriptor getModelProviderDescriptor(String JavaDoc id) {
48         IModelProviderDescriptor[] descs = ModelProviderManager.getDefault().getDescriptors();
49         for (int i = 0; i < descs.length; i++) {
50             IModelProviderDescriptor descriptor = descs[i];
51             if (descriptor.getId().equals(id)) {
52                 return descriptor;
53             }
54         }
55         return null;
56     }
57
58     /**
59      * Return the descriptors for all model providers that are registered.
60      *
61      * @return the descriptors for all model providers that are registered.
62      */

63     public static IModelProviderDescriptor[] getModelProviderDescriptors() {
64         return ModelProviderManager.getDefault().getDescriptors();
65     }
66
67     /* (non-Javadoc)
68      * @see java.lang.Object#equals(java.lang.Object)
69      */

70     public boolean equals(Object JavaDoc obj) {
71         if (obj instanceof ModelProvider) {
72             ModelProvider other = (ModelProvider) obj;
73             return other.getDescriptor().getId().equals(getDescriptor().getId());
74         }
75         return super.equals(obj);
76     }
77
78     /**
79      * Return the descriptor of this model provider. The descriptor
80      * is set during initialization so implements cannot call this method
81      * until after the <code>initialize</code> method is invoked.
82      * @return the descriptor of this model provider
83      */

84     public final IModelProviderDescriptor getDescriptor() {
85         return descriptor;
86     }
87     
88     /**
89      * Returns the unique identifier of this model provider.
90      * <p>
91      * The model provider identifier is composed of the model provider's
92      * plug-in id and the simple id of the provider extension. For example, if
93      * plug-in <code>"com.xyz"</code> defines a provider extension with id
94      * <code>"myModelProvider"</code>, the unique model provider identifier will be
95      * <code>"com.xyz.myModelProvider"</code>.
96      * </p>
97      *
98      * @return the unique model provider identifier
99      */

100     public final String JavaDoc getId() {
101         return descriptor.getId();
102     }
103
104     /**
105      * Return the resource mappings that cover the given resource.
106      * By default, an empty array is returned. Subclass may override
107      * this method but should consider overriding either
108      * {@link #getMappings(IResource[], ResourceMappingContext, IProgressMonitor)}
109      * or ({@link #getMappings(ResourceTraversal[], ResourceMappingContext, IProgressMonitor)}
110      * if more context is needed to determine the proper mappings.
111      *
112      * @param resource the resource
113      * @param context a resource mapping context
114      * @param monitor a progress monitor, or <code>null</code> if progress
115      * reporting is not desired
116      * @return the resource mappings that cover the given resource.
117      * @exception CoreException
118      */

119     public ResourceMapping[] getMappings(IResource resource, ResourceMappingContext context, IProgressMonitor monitor) throws CoreException {
120         return new ResourceMapping[0];
121     }
122
123     /**
124      * Return the set of mappings that cover the given resources.
125      * This method is used to map operations on resources to
126      * operations on resource mappings. By default, this method
127      * calls <code>getMapping(IResource)</code> for each resource.
128      * <p>
129      * Subclasses may override this method.
130      * </p>
131      *
132      * @param resources the resources
133      * @param context a resource mapping context
134      * @param monitor a progress monitor, or <code>null</code> if progress
135      * reporting is not desired
136      * @return the set of mappings that cover the given resources
137      * @exception CoreException
138      */

139     public ResourceMapping[] getMappings(IResource[] resources, ResourceMappingContext context, IProgressMonitor monitor) throws CoreException {
140         Set mappings = new HashSet();
141         for (int i = 0; i < resources.length; i++) {
142             IResource resource = resources[i];
143             ResourceMapping[] resourceMappings = getMappings(resource, context, monitor);
144             if (resourceMappings.length > 0)
145                 mappings.addAll(Arrays.asList(resourceMappings));
146         }
147         return (ResourceMapping[]) mappings.toArray(new ResourceMapping[mappings.size()]);
148     }
149
150     /**
151      * Return the set of mappings that overlap with the given resource traversals.
152      * This method is used to map operations on resources to
153      * operations on resource mappings. By default, this method
154      * calls {@link #getMappings(IResource[], ResourceMappingContext, IProgressMonitor)}
155      * with the resources extract from each traversal.
156      * <p>
157      * Subclasses may override this method.
158      * </p>
159      *
160      * @param traversals the traversals
161      * @param context a resource mapping context
162      * @param monitor a progress monitor, or <code>null</code> if progress
163      * reporting is not desired
164      * @return the set of mappings that overlap with the given resource traversals
165      */

166     public ResourceMapping[] getMappings(ResourceTraversal[] traversals, ResourceMappingContext context, IProgressMonitor monitor) throws CoreException {
167         Set result = new HashSet();
168         for (int i = 0; i < traversals.length; i++) {
169             ResourceTraversal traversal = traversals[i];
170             ResourceMapping[] mappings = getMappings(traversal.getResources(), context, monitor);
171             for (int j = 0; j < mappings.length; j++)
172                 result.add(mappings[j]);
173         }
174         return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
175     }
176
177     /**
178      * Return a set of traversals that cover the given resource mappings. The
179      * provided mappings must be from this provider or one of the providers this
180      * provider extends.
181      * <p>
182      * The default implementation accumulates the traversals from the given
183      * mappings. Subclasses can override to provide a more optimal
184      * transformation.
185      * </p>
186      *
187      * @param mappings the mappings being mapped to resources
188      * @param context the context used to determine the set of traversals that
189      * cover the mappings
190      * @param monitor a progress monitor, or <code>null</code> if progress
191      * reporting is not desired
192      * @return a set of traversals that cover the given mappings
193      * @exception CoreException
194      */

195     public ResourceTraversal[] getTraversals(ResourceMapping[] mappings, ResourceMappingContext context, IProgressMonitor monitor) throws CoreException {
196         try {
197             monitor.beginTask("", 100 * mappings.length); //$NON-NLS-1$
198
List traversals = new ArrayList();
199             for (int i = 0; i < mappings.length; i++) {
200                 ResourceMapping mapping = mappings[i];
201                 traversals.addAll(Arrays.asList(mapping.getTraversals(context, new SubProgressMonitor(monitor, 100))));
202             }
203             return (ResourceTraversal[]) traversals.toArray(new ResourceTraversal[traversals.size()]);
204         } finally {
205             monitor.done();
206         }
207     }
208
209     /* (non-Javadoc)
210      * @see java.lang.Object#hashCode()
211      */

212     public int hashCode() {
213         return getDescriptor().getId().hashCode();
214     }
215
216     /**
217      * This method is called by the model provider framework when the model
218      * provider is instantiated. This method should not be called by clients and
219      * cannot be overridden by subclasses. However, it invokes the
220      * <code>initialize</code> method once the descriptor is set so subclasses
221      * can override that method if they need to do additional initialization.
222      *
223      * @param desc the description of the provider as it appears in the plugin manifest
224      */

225     public final void init(IModelProviderDescriptor desc) {
226         if (descriptor != null)
227             // prevent subsequent calls from damaging this instance
228
return;
229         descriptor = desc;
230         initialize();
231     }
232
233     /**
234      * Initialization method that is called after the descriptor
235      * of this provider is set. Subclasses may override.
236      */

237     protected void initialize() {
238         // Do nothing
239
}
240
241     /**
242      * Validate the proposed changes contained in the given delta.
243      * <p>
244      * This method must return either a {@link ModelStatus}, or a {@link MultiStatus}
245      * whose children are {@link ModelStatus}. The severity of the returned status
246      * indicates the severity of the possible side-effects of the operation. Any
247      * severity other than <code>OK</code> will be shown to the user. The
248      * message should be a human readable message that will allow the user to
249      * make a decision on whether to continue with the operation. The model
250      * provider id should indicate which model is flagging the possible side effects.
251      * <p>
252      * This default implementation accepts all changes and returns a status with
253      * severity <code>OK</code>. Subclasses should override to perform
254      * validation specific to their model.
255      * </p>
256      *
257      * @param delta a delta tree containing the proposed changes
258      * @param monitor a progress monitor, or <code>null</code> if progress
259      * reporting is not desired
260      * @return a status indicating any potential side effects
261      * on the model that provided this validator.
262      */

263     public IStatus validateChange(IResourceDelta delta, IProgressMonitor monitor) {
264         return new ModelStatus(IStatus.OK, ResourcesPlugin.PI_RESOURCES, descriptor.getId(), Status.OK_STATUS.getMessage());
265     }
266 }
267
Popular Tags