KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > WorkingSetScope


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.ui.synchronize;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.team.internal.ui.TeamUIMessages;
22 import org.eclipse.team.internal.ui.Utils;
23 import org.eclipse.ui.*;
24
25 /**
26  * A synchronize scope whose roots are defined by a working set.
27  * <p>
28  * Clients are not expected to subclass this class.
29  * </p>
30  * @since 3.0
31  */

32 public class WorkingSetScope extends AbstractSynchronizeScope implements IPropertyChangeListener {
33     
34     /*
35      * Constants used to save and restore this scope
36      */

37     private final static String JavaDoc CTX_SETS = "workingset_scope_sets"; //$NON-NLS-1$
38
private final static String JavaDoc CTX_SET_NAME = "workingset_scope_name"; //$NON-NLS-1$
39

40     /*
41      * The working sets associated with this scope
42      */

43     private IWorkingSet[] sets;
44     
45     /**
46      * Create the scope for the working sets
47      *
48      * @param sets the working sets that defines this scope
49      */

50     public WorkingSetScope(IWorkingSet[] sets) {
51         setWorkingSets(sets);
52     }
53     
54     /**
55      * Create this scope from it's previously saved state
56      *
57      * @param memento the memento containing a previous scope information
58      * that is used to initialize this scope.
59      */

60     protected WorkingSetScope(IMemento memento) {
61         super(memento);
62     }
63     
64     /**
65      * Initialize this working set scope with the provided working sets.
66      *
67      * @since 3.1
68      */

69     protected void setWorkingSets(IWorkingSet[] sets) {
70         this.sets = sets;
71         PlatformUI.getWorkbench().getWorkingSetManager().addPropertyChangeListener(this);
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.team.ui.synchronize.ISynchronizeScope#getName()
76      */

77     public String JavaDoc getName() {
78         if (sets.length == 0) {
79             return TeamUIMessages.WorkingSetScope_0;
80         }
81         StringBuffer JavaDoc name = new StringBuffer JavaDoc();
82         for (int i = 0; i < sets.length; i++) {
83             IWorkingSet set = sets[i];
84             name.append(set.getName());
85             if (i < sets.length - 1) {
86                 name.append(", "); //$NON-NLS-1$
87
}
88         }
89         return name.toString();
90     }
91     
92     /* (non-Javadoc)
93      * @see org.eclipse.team.ui.synchronize.ISynchronizeScope#getRoots()
94      */

95     public IResource[] getRoots() {
96         if (sets.length == 0) {
97             return null;
98         }
99         HashSet JavaDoc roots = new HashSet JavaDoc();
100         for (int i = 0; i < sets.length; i++) {
101             IWorkingSet set = sets[i];
102             IResource[] resources = Utils.getResources(set.getElements());
103             addNonOverlapping(roots, resources);
104     }
105         return (IResource[]) roots.toArray(new IResource[roots.size()]);
106     }
107     
108     private void addNonOverlapping(HashSet JavaDoc roots, IResource[] resources) {
109         for (int i = 0; i < resources.length; i++) {
110             IResource newResource = resources[i];
111             boolean add = true;
112             for (Iterator JavaDoc iter = roots.iterator(); iter.hasNext();) {
113                 IResource existingResource = (IResource) iter.next();
114                 if (existingResource.equals(newResource)) {
115                     // No need to add it since it is already there
116
add = false;
117                     break;
118                 }
119                 if (existingResource.getFullPath().isPrefixOf(newResource.getFullPath())) {
120                     // No need to add it since a parent is already there
121
add = false;
122                     break;
123                 }
124                 if (newResource.getFullPath().isPrefixOf(existingResource.getFullPath())) {
125                     // Remove existing and continue
126
iter.remove();
127                 }
128             }
129             if (add) {
130                 roots.add(newResource);
131             }
132         }
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
137      */

138     public void propertyChange(PropertyChangeEvent event) {
139         if (event.getProperty() == IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE) {
140             IWorkingSet newSet = (IWorkingSet) event.getNewValue();
141             for (int i = 0; i < sets.length; i++) {
142                 IWorkingSet set = sets[i];
143                 if (newSet == set) {
144                     fireRootsChanges();
145                     return;
146                 }
147             }
148         } else if(event.getProperty() == IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE) {
149             firePropertyChangedEvent(new PropertyChangeEvent(this, NAME, null, event.getNewValue()));
150         }
151     }
152     
153     /* (non-Javadoc)
154      * @see org.eclipse.team.ui.synchronize.ISynchronizeScope#dispose()
155      */

156     public void dispose() {
157         super.dispose();
158         if (PlatformUI.isWorkbenchRunning())
159             PlatformUI.getWorkbench().getWorkingSetManager().removePropertyChangeListener(this);
160     }
161     
162     /* (non-Javadoc)
163      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeScope#saveState(org.eclipse.ui.IMemento)
164      */

165     public void saveState(IMemento memento) {
166         super.saveState(memento);
167         for (int i = 0; i < sets.length; i++) {
168             IWorkingSet set = sets[i];
169             IMemento rootNode = memento.createChild(CTX_SETS);
170             rootNode.putString(CTX_SET_NAME, set.getName());
171         }
172     }
173     
174     /*
175      * (non-Javadoc)
176      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeScope#init(org.eclipse.ui.IMemento)
177      */

178     protected void init(IMemento memento) {
179         super.init(memento);
180         IMemento[] rootNodes = memento.getChildren(CTX_SETS);
181         if (rootNodes != null) {
182             List JavaDoc sets = new ArrayList JavaDoc();
183             for (int i = 0; i < rootNodes.length; i++) {
184                 IMemento rootNode = rootNodes[i];
185                 String JavaDoc setName = rootNode.getString(CTX_SET_NAME);
186                 IWorkingSet set = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(setName);
187                 if (set != null) {
188                     sets.add(set);
189                 }
190             }
191             setWorkingSets((IWorkingSet[]) sets.toArray(new IWorkingSet[sets.size()]));
192         }
193     }
194 }
195
Popular Tags