KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > resources > team > ResourceRuleFactory


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.resources.team;
12
13 import java.util.HashSet JavaDoc;
14 import org.eclipse.core.resources.*;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.jobs.ISchedulingRule;
17 import org.eclipse.core.runtime.jobs.MultiRule;
18
19 /**
20  * Default implementation of IResourceRuleFactory. The teamHook extension
21  * may subclass to provide more specialized scheduling rules for workspace operations that
22  * they participate in.
23  *
24  * @see IResourceRuleFactory
25  * @since 3.0
26  */

27 public class ResourceRuleFactory implements IResourceRuleFactory {
28     private final IWorkspace workspace = ResourcesPlugin.getWorkspace();
29
30     /**
31      * Creates a new default resource rule factory. This constructor must only
32      * be called by subclasses.
33      */

34     protected ResourceRuleFactory() {
35         super();
36     }
37
38     /**
39      * Default implementation of <code>IResourceRuleFactory#buildRule</code>.
40      * This default implementation always returns the workspace root.
41      * <p>
42      * Subclasses may not currently override this method.
43      *
44      * @see org.eclipse.core.resources.IResourceRuleFactory#buildRule()
45      */

46     public final ISchedulingRule buildRule() {
47         return workspace.getRoot();
48     }
49
50     /**
51      * Default implementation of <code>IResourceRuleFactory#charsetRule</code>.
52      * This default implementation always returns the project of the resource
53      * whose charset setting is being changed, or <code>null</code> if the
54      * resource is the workspace root.
55      * <p>
56      * Subclasses may override this method. The rule provided by an overriding
57      * method must at least contain the rule from this default implementation.
58      * </p>
59      *
60      * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
61      * @see org.eclipse.core.resources.IResourceRuleFactory#charsetRule(IResource)
62      * @since 3.1
63      */

64     public ISchedulingRule charsetRule(IResource resource) {
65         if (resource.getType() == IResource.ROOT)
66             return null;
67         return resource.getProject();
68     }
69
70     /**
71      * Default implementation of <code>IResourceRuleFactory#copyRule</code>.
72      * This default implementation always returns the parent of the destination
73      * resource.
74      * <p>
75      * Subclasses may override this method. The rule provided by an overriding
76      * method must at least contain the rule from this default implementation.
77      *
78      * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
79      * @see org.eclipse.core.resources.IResourceRuleFactory#copyRule(IResource, IResource)
80      */

81     public ISchedulingRule copyRule(IResource source, IResource destination) {
82         //source is not modified, destination is created
83
return parent(destination);
84     }
85
86     /**
87      * Default implementation of <code>IResourceRuleFactory#createRule</code>.
88      * This default implementation always returns the parent of the resource
89      * being created.
90      * <p>
91      * Subclasses may override this method. The rule provided by an overriding
92      * method must at least contain the rule from this default implementation.
93      *
94      * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
95      * @see org.eclipse.core.resources.IResourceRuleFactory#createRule(IResource)
96      */

97     public ISchedulingRule createRule(IResource resource) {
98         return parent(resource);
99     }
100
101     /**
102      * Default implementation of <code>IResourceRuleFactory#deleteRule</code>.
103      * This default implementation always returns the parent of the resource
104      * being deleted.
105      * <p>
106      * Subclasses may override this method. The rule provided by an overriding
107      * method must at least contain the rule from this default implementation.
108      *
109      * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
110      * @see org.eclipse.core.resources.IResourceRuleFactory#deleteRule(IResource)
111      */

112     public ISchedulingRule deleteRule(IResource resource) {
113         return parent(resource);
114     }
115
116     private boolean isReadOnly(IResource resource) {
117         ResourceAttributes attributes = resource.getResourceAttributes();
118         return attributes == null ? false : attributes.isReadOnly();
119     }
120
121     /**
122      * Default implementation of <code>IResourceRuleFactory#markerRule</code>.
123      * This default implementation always returns <code>null</code>.
124      * <p>
125      * Subclasses may not currently override this method.
126      *
127      * @see org.eclipse.core.resources.IResourceRuleFactory#markerRule(IResource)
128      */

129     public final ISchedulingRule markerRule(IResource resource) {
130         return null;
131     }
132
133     /**
134      * Default implementation of <code>IResourceRuleFactory#modifyRule</code>.
135      * This default implementation returns the resource being modified, or the
136      * parent resource if modifying a project description file.
137      * Note that this must encompass any rule required by the <code>validateSave</code> hook.
138      * <p>
139      * Subclasses may override this method. The rule provided by an overriding
140      * method must at least contain the rule from this default implementation.
141      *
142      * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
143      * @see org.eclipse.core.resources.IResourceRuleFactory#modifyRule(IResource)
144      * @see FileModificationValidator#validateSave(IFile)
145      * @see IProjectDescription#DESCRIPTION_FILE_NAME
146      */

147     public ISchedulingRule modifyRule(IResource resource) {
148         //modifying the project description requires the root
149
if (resource.getType() == IResource.PROJECT)
150             return workspace.getRoot();
151         IPath path = resource.getFullPath();
152         //modifying the project description may cause linked resources to be created or deleted
153
if (path.segmentCount() == 2 && path.segment(1).equals(IProjectDescription.DESCRIPTION_FILE_NAME))
154             return parent(resource);
155         return resource;
156     }
157
158     /**
159      * Default implementation of <code>IResourceRuleFactory#moveRule</code>.
160      * This default implementation returns a rule that combines the parent
161      * of the source resource and the parent of the destination resource.
162      * <p>
163      * Subclasses may override this method. The rule provided by an overriding
164      * method must at least contain the rule from this default implementation.
165      *
166      * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
167      * @see org.eclipse.core.resources.IResourceRuleFactory#moveRule(IResource, IResource)
168      */

169     public ISchedulingRule moveRule(IResource source, IResource destination) {
170         //move needs the parent of both source and destination
171
return MultiRule.combine(parent(source), parent(destination));
172     }
173
174     /**
175      * Convenience method to return the parent of the given resource,
176      * or the resource itself for projects and the workspace root.
177      * @param resource the resource to compute the parent of
178      * @return the parent resource for folders and files, and the
179      * resource itself for projects and the workspace root.
180      */

181     protected final ISchedulingRule parent(IResource resource) {
182         switch (resource.getType()) {
183             case IResource.ROOT :
184             case IResource.PROJECT :
185                 return resource;
186             default :
187                 return resource.getParent();
188         }
189     }
190
191     /**
192      * Default implementation of <code>IResourceRuleFactory#refreshRule</code>.
193      * This default implementation always returns the parent of the resource
194      * being refreshed.
195      * <p>
196      * Subclasses may override this method. The rule provided by an overriding
197      * method must at least contain the rule from this default implementation.
198      *
199      * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
200      * @see org.eclipse.core.resources.IResourceRuleFactory#refreshRule(IResource)
201      */

202     public ISchedulingRule refreshRule(IResource resource) {
203         return parent(resource);
204     }
205
206     /**
207      * Default implementation of <code>IResourceRuleFactory#validateEditRule</code>.
208      * This default implementation returns a rule that combines the parents of
209      * all read-only resources, or <code>null</code> if there are no read-only
210      * resources.
211      * <p>
212      * Subclasses may override this method. The rule provided by an overriding
213      * method must at least contain the rule from this default implementation.
214      *
215      * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
216      * @see org.eclipse.core.resources.IResourceRuleFactory#validateEditRule(IResource[])
217      */

218     public ISchedulingRule validateEditRule(IResource[] resources) {
219         if (resources.length == 0)
220             return null;
221         //optimize rule for single file
222
if (resources.length == 1)
223             return isReadOnly(resources[0]) ? parent(resources[0]) : null;
224         //need a lock on the parents of all read-only files
225
HashSet JavaDoc rules = new HashSet JavaDoc();
226         for (int i = 0; i < resources.length; i++)
227             if (isReadOnly(resources[i]))
228                 rules.add(parent(resources[i]));
229         if (rules.isEmpty())
230             return null;
231         if (rules.size() == 1)
232             return (ISchedulingRule) rules.iterator().next();
233         ISchedulingRule[] ruleArray = (ISchedulingRule[]) rules.toArray(new ISchedulingRule[rules.size()]);
234         return new MultiRule(ruleArray);
235     }
236 }
237
Popular Tags