KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > Rules


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.internal.resources;
12
13 import java.util.*;
14 import org.eclipse.core.internal.events.ILifecycleListener;
15 import org.eclipse.core.internal.events.LifecycleEvent;
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.resources.team.ResourceRuleFactory;
18 import org.eclipse.core.resources.team.TeamHook;
19 import org.eclipse.core.runtime.jobs.ISchedulingRule;
20 import org.eclipse.core.runtime.jobs.MultiRule;
21
22 /**
23  * Class for calculating scheduling rules for resource changing operations.
24  * This factory delegates to the TeamHook to obtain an appropriate factory
25  * for the resource that the operation is proposing to modify.
26  */

27 class Rules implements IResourceRuleFactory, ILifecycleListener {
28     private final ResourceRuleFactory defaultFactory = new ResourceRuleFactory() {};
29     /**
30      * Map of project names to the factory for that project.
31      */

32     private final Map projectsToRules = Collections.synchronizedMap(new HashMap());
33     private final TeamHook teamHook;
34     private final IWorkspaceRoot root;
35
36     /**
37      * Creates a new scheduling rule factory for the given workspace
38      * @param workspace
39      */

40     Rules(Workspace workspace) {
41         this.root = workspace.getRoot();
42         this.teamHook = workspace.getTeamHook();
43         workspace.addLifecycleListener(this);
44     }
45
46     /**
47      * Obtains the scheduling rule from the appropriate factory for a build operation.
48      */

49     public ISchedulingRule buildRule() {
50         //team hook currently cannot change this rule
51
return root;
52     }
53
54     /**
55      * Obtains the scheduling rule from the appropriate factories for a copy operation.
56      */

57     public ISchedulingRule copyRule(IResource source, IResource destination) {
58         if (source.getType() == IResource.ROOT || destination.getType() == IResource.ROOT)
59             return root;
60         //source is not modified, destination is created
61
return factoryFor(destination).copyRule(source, destination);
62     }
63
64     /**
65      * Obtains the scheduling rule from the appropriate factory for a create operation.
66      */

67     public ISchedulingRule createRule(IResource resource) {
68         if (resource.getType() == IResource.ROOT)
69             return root;
70         return factoryFor(resource).createRule(resource);
71     }
72
73     /**
74      * Obtains the scheduling rule from the appropriate factory for a delete operation.
75      */

76     public ISchedulingRule deleteRule(IResource resource) {
77         if (resource.getType() == IResource.ROOT)
78             return root;
79         return factoryFor(resource).deleteRule(resource);
80     }
81
82     /**
83      * Returns the scheduling rule factory for the given resource
84      */

85     private IResourceRuleFactory factoryFor(IResource destination) {
86         IResourceRuleFactory fac = (IResourceRuleFactory) projectsToRules.get(destination.getFullPath().segment(0));
87         if (fac == null) {
88             //use the default factory if the project is not yet accessible
89
if (!destination.getProject().isAccessible())
90                 return defaultFactory;
91             //ask the team hook to supply one
92
fac = teamHook.getRuleFactory(destination.getProject());
93             projectsToRules.put(destination.getFullPath().segment(0), fac);
94         }
95         return fac;
96     }
97
98     /* (non-Javadoc)
99      * @see org.eclipse.core.internal.events.ILifecycleListener#handleEvent(org.eclipse.core.internal.events.LifecycleEvent)
100      */

101     public void handleEvent(LifecycleEvent event) {
102         //clear resource rule factory for projects that are about to be closed
103
//or deleted. It is ok to do this during a PRE event because the rule
104
//has already been obtained at this point.
105
switch (event.kind) {
106             case LifecycleEvent.PRE_PROJECT_CLOSE :
107             case LifecycleEvent.PRE_PROJECT_DELETE :
108             case LifecycleEvent.PRE_PROJECT_MOVE :
109                 setRuleFactory((IProject) event.resource, null);
110         }
111     }
112
113     /**
114      * Obtains the scheduling rule from the appropriate factory for a charset change operation.
115      */

116     public ISchedulingRule charsetRule(IResource resource) {
117         if (resource.getType() == IResource.ROOT)
118             return null;
119         return factoryFor(resource).charsetRule(resource);
120     }
121
122     /**
123      * Obtains the scheduling rule from the appropriate factory for a marker change operation.
124      */

125     public ISchedulingRule markerRule(IResource resource) {
126         //team hook currently cannot change this rule
127
return null;
128     }
129
130     /**
131      * Obtains the scheduling rule from the appropriate factory for a modify operation.
132      */

133     public ISchedulingRule modifyRule(IResource resource) {
134         if (resource.getType() == IResource.ROOT)
135             return root;
136         return factoryFor(resource).modifyRule(resource);
137     }
138
139     /**
140      * Obtains the scheduling rule from the appropriate factories for a move operation.
141      */

142     public ISchedulingRule moveRule(IResource source, IResource destination) {
143         if (source.getType() == IResource.ROOT || destination.getType() == IResource.ROOT)
144             return root;
145         //treat a move across projects as a create on the destination and a delete on the source
146
if (!source.getFullPath().segment(0).equals(destination.getFullPath().segment(0)))
147             return MultiRule.combine(deleteRule(source), createRule(destination));
148         return factoryFor(source).moveRule(source, destination);
149     }
150
151     /**
152      * Obtains the scheduling rule from the appropriate factory for a refresh operation.
153      */

154     public ISchedulingRule refreshRule(IResource resource) {
155         if (resource.getType() == IResource.ROOT)
156             return root;
157         return factoryFor(resource).refreshRule(resource);
158     }
159
160     /* (non-javadoc)
161      * Implements TeamHook#setRuleFactory
162      */

163     void setRuleFactory(IProject project, IResourceRuleFactory factory) {
164         if (factory == null)
165             projectsToRules.remove(project.getName());
166         else
167             projectsToRules.put(project.getName(), factory);
168     }
169
170     /**
171      * Combines rules for each parameter to validateEdit from the corresponding
172      * rule factories.
173      */

174     public ISchedulingRule validateEditRule(IResource[] resources) {
175         if (resources.length == 0)
176             return null;
177         //optimize rule for single file
178
if (resources.length == 1) {
179             if (resources[0].getType() == IResource.ROOT)
180                 return root;
181             return factoryFor(resources[0]).validateEditRule(resources);
182         }
183         //gather rules for each resource from appropriate factory
184
HashSet rules = new HashSet();
185         IResource[] oneResource = new IResource[1];
186         for (int i = 0; i < resources.length; i++) {
187             if (resources[i].getType() == IResource.ROOT)
188                 return root;
189             oneResource[0] = resources[i];
190             ISchedulingRule rule = factoryFor(resources[i]).validateEditRule(oneResource);
191             if (rule != null)
192                 rules.add(rule);
193         }
194         if (rules.isEmpty())
195             return null;
196         if (rules.size() == 1)
197             return (ISchedulingRule) rules.iterator().next();
198         ISchedulingRule[] ruleArray = (ISchedulingRule[]) rules.toArray(new ISchedulingRule[rules.size()]);
199         return new MultiRule(ruleArray);
200     }
201 }
202
Popular Tags