KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > SyncInfoWorkingSetFilter


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.core.subscribers;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.team.core.TeamException;
17 import org.eclipse.team.core.subscribers.*;
18 import org.eclipse.team.core.synchronize.*;
19 import org.eclipse.team.internal.core.TeamPlugin;
20
21 /**
22  * WorkingSet filter for a SyncSet.
23  */

24 public class SyncInfoWorkingSetFilter extends FastSyncInfoFilter {
25
26     private IResource[] resources;
27     
28     /* (non-Javadoc)
29      * @see org.eclipse.team.ui.sync.SyncInfoFilter#select(org.eclipse.team.core.subscribers.SyncInfo)
30      */

31     public boolean select(SyncInfo info) {
32         // if there's no set, the resource is included
33
if (isEmpty()) return true;
34         return isIncluded(info.getLocal());
35     }
36
37     /*
38      * Answer true if the given resource is included in the working set
39      */

40     private boolean isIncluded(IResource resource) {
41         // otherwise, if their is a parent of the resource in the set,
42
// it is included
43
for (int i = 0; i < resources.length; i++) {
44             IResource setResource = resources[i];
45             if (isParent(setResource, resource)) {
46                 return true;
47             }
48         }
49         return false;
50     }
51
52     private boolean isParent(IResource parent, IResource child) {
53         return (parent.getFullPath().isPrefixOf(child.getFullPath()));
54     }
55     
56     /* (non-Javadoc)
57      * @see org.eclipse.team.internal.ui.sync.views.SyncSetInputFromSubscriber#getRoots()
58      */

59     public IResource[] getRoots(Subscriber subscriber) {
60         IResource[] roots = subscriber.roots();
61         if (isEmpty()) return roots;
62         
63         // filter the roots by the selected working set
64
Set result = new HashSet();
65         for (int i = 0; i < roots.length; i++) {
66             IResource resource = roots[i];
67             result.addAll(Arrays.asList(getIntersectionWithSet(subscriber, resource)));
68         }
69         return (IResource[]) result.toArray(new IResource[result.size()]);
70     }
71
72     /*
73      * Answer the intersection between the given resource and it's children
74      * and the receiver's working set.
75      */

76     private IResource[] getIntersectionWithSet(Subscriber subscriber, IResource resource) {
77         List result = new ArrayList();
78         for (int i = 0; i < resources.length; i++) {
79             IResource setResource = resources[i];
80             if (setResource != null) {
81                 if (isParent(resource, setResource)) {
82                     try {
83                         if (subscriber.isSupervised(setResource)) {
84                             result.add(setResource);
85                         }
86                     } catch (TeamException e) {
87                         // Log the exception and add the resource to the list
88
TeamPlugin.log(e);
89                         result.add(setResource);
90                     }
91                 } else if (isParent(setResource, resource)) {
92                     result.add(resource);
93                 }
94             }
95         }
96         return (IResource[]) result.toArray(new IResource[result.size()]);
97     }
98
99     public void setWorkingSet(IResource[] resources) {
100         this.resources = resources;
101     }
102     
103     public IResource[] getWorkingSet() {
104         return this.resources;
105     }
106     
107     private boolean isEmpty() {
108         return resources == null || resources.length == 0;
109     }
110 }
111
Popular Tags