KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > syncinfo > DeferredResourceChangeHandler


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.team.internal.ccvs.core.syncinfo;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.team.core.TeamException;
18 import org.eclipse.team.internal.ccvs.core.*;
19 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
20 import org.eclipse.team.internal.ccvs.core.resources.EclipseSynchronizer;
21 import org.eclipse.team.internal.core.BackgroundEventHandler;
22
23 /**
24  * This class handles resources changes that are reported in deltas
25  * in a deferred manner (i.e. in a background job)
26  */

27 public class DeferredResourceChangeHandler extends BackgroundEventHandler {
28
29     public DeferredResourceChangeHandler() {
30         super(CVSMessages.DeferredResourceChangeHandler_0, CVSMessages.DeferredResourceChangeHandler_1);
31     }
32
33     private static final int IGNORE_FILE_CHANGED = 1;
34     private static final int RECREATED_CVS_RESOURCE = 2;
35     private static final int CONFLICTING_DELETION =3;
36     
37     private Set changedIgnoreFiles = new HashSet();
38     private Set recreatedResources = new HashSet();
39     private Set conflictingDeletion = new HashSet();
40
41     /* (non-Javadoc)
42      * @see org.eclipse.team.core.subscribers.BackgroundEventHandler#processEvent(org.eclipse.team.core.subscribers.BackgroundEventHandler.Event, org.eclipse.core.runtime.IProgressMonitor)
43      */

44     protected void processEvent(Event event, IProgressMonitor monitor) throws TeamException {
45         int type = event.getType();
46         switch (type) {
47             case IGNORE_FILE_CHANGED :
48                 changedIgnoreFiles.add(event.getResource());
49                 break;
50             case RECREATED_CVS_RESOURCE :
51                 recreatedResources.add(event.getResource());
52                 break;
53             case CONFLICTING_DELETION :
54                 conflictingDeletion.add(event.getResource());
55                 break;
56         }
57     }
58     
59     private IContainer[] getParents(Set files) {
60         Set parents = new HashSet();
61         for (Iterator iter = files.iterator(); iter.hasNext();) {
62             IFile file = (IFile) iter.next();
63             parents.add(file.getParent());
64         }
65         return (IContainer[]) parents.toArray(new IContainer[parents.size()]);
66     }
67
68     public void ignoreFileChanged(IFile file) {
69         if (isSharedWithCVS(file))
70             queueEvent(new ResourceEvent(file, IGNORE_FILE_CHANGED, IResource.DEPTH_ZERO), false);
71     }
72     
73     /**
74      * The resource has been added and has sync info that has not been written to disk.
75      * Queue an event to ensure that the CVS directory files
76      * are written to disk.
77      * @param resource the recently add resource
78      */

79     public void recreated(IResource resource) {
80         if (isSharedWithCVS(resource))
81             queueEvent(new ResourceEvent(resource, RECREATED_CVS_RESOURCE, IResource.DEPTH_ZERO), false);
82     }
83     
84     /* (non-Javadoc)
85      * @see org.eclipse.team.core.subscribers.BackgroundEventHandler#dispatchEvents()
86      */

87     protected boolean doDispatchEvents(IProgressMonitor monitor) {
88         // Handle ignore file changes
89
boolean workDone = !changedIgnoreFiles.isEmpty() || !recreatedResources.isEmpty();
90         try {
91             EclipseSynchronizer.getInstance().ignoreFilesChanged(getParents(changedIgnoreFiles));
92         } catch (CVSException e) {
93             // Log and continue
94
CVSProviderPlugin.log(e);
95         }
96         changedIgnoreFiles.clear();
97         // Handle recreations by project to reduce locking granularity
98
Map recreations = getResourcesByProject((IResource[]) recreatedResources.toArray(new IResource[recreatedResources.size()]));
99         recreatedResources.clear();
100         for (Iterator iter = recreations.values().iterator(); iter.hasNext();) {
101             List resources = (List) iter.next();
102             try {
103                 EclipseSynchronizer.getInstance().resourcesRecreated((IResource[]) resources.toArray(new IResource[resources.size()]), monitor);
104             } catch (CVSException e) {
105                 // Log and continue
106
CVSProviderPlugin.log(e);
107             }
108         }
109         IResource[] deletions = (IResource[]) conflictingDeletion.toArray(new IResource[conflictingDeletion.size()]);
110         conflictingDeletion.clear();
111         for (int i = 0; i < deletions.length; i++) {
112             IResource resource = deletions[i];
113             ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
114             try {
115                 if(!cvsResource.isFolder() && cvsResource.isManaged()) {
116                     cvsResource.unmanage(monitor);
117                 }
118             } catch (CVSException e) {
119                 // Log and continue
120
CVSProviderPlugin.log(e);
121             }
122         }
123         return workDone;
124     }
125     
126     private Map getResourcesByProject(IResource[] resources) {
127         Map result = new HashMap();
128         for (int i = 0; i < resources.length; i++) {
129             IResource resource = resources[i];
130             IProject project = resource.getProject();
131             List projectResources = (List)result.get(project);
132             if (projectResources == null) {
133                 projectResources = new ArrayList();
134                 result.put(project, projectResources);
135             }
136             projectResources.add(resource);
137         }
138         return result;
139     }
140
141     public void handleConflictingDeletion(IResource local) {
142         if (isSharedWithCVS(local))
143             queueEvent(new ResourceEvent(local, CONFLICTING_DELETION, IResource.DEPTH_ZERO), false);
144     }
145     
146     private boolean isSharedWithCVS(IResource resource) {
147         return CVSTeamProvider.isSharedWithCVS(resource.getProject());
148     }
149
150     protected Object JavaDoc getJobFamiliy() {
151         return this;
152     }
153
154 }
155
Popular Tags