1 11 package org.eclipse.team.ui.synchronize; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 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 32 public class WorkingSetScope extends AbstractSynchronizeScope implements IPropertyChangeListener { 33 34 37 private final static String CTX_SETS = "workingset_scope_sets"; private final static String CTX_SET_NAME = "workingset_scope_name"; 40 43 private IWorkingSet[] sets; 44 45 50 public WorkingSetScope(IWorkingSet[] sets) { 51 setWorkingSets(sets); 52 } 53 54 60 protected WorkingSetScope(IMemento memento) { 61 super(memento); 62 } 63 64 69 protected void setWorkingSets(IWorkingSet[] sets) { 70 this.sets = sets; 71 PlatformUI.getWorkbench().getWorkingSetManager().addPropertyChangeListener(this); 72 } 73 74 77 public String getName() { 78 if (sets.length == 0) { 79 return TeamUIMessages.WorkingSetScope_0; 80 } 81 StringBuffer name = new StringBuffer (); 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(", "); } 88 } 89 return name.toString(); 90 } 91 92 95 public IResource[] getRoots() { 96 if (sets.length == 0) { 97 return null; 98 } 99 HashSet roots = new HashSet (); 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 roots, IResource[] resources) { 109 for (int i = 0; i < resources.length; i++) { 110 IResource newResource = resources[i]; 111 boolean add = true; 112 for (Iterator iter = roots.iterator(); iter.hasNext();) { 113 IResource existingResource = (IResource) iter.next(); 114 if (existingResource.equals(newResource)) { 115 add = false; 117 break; 118 } 119 if (existingResource.getFullPath().isPrefixOf(newResource.getFullPath())) { 120 add = false; 122 break; 123 } 124 if (newResource.getFullPath().isPrefixOf(existingResource.getFullPath())) { 125 iter.remove(); 127 } 128 } 129 if (add) { 130 roots.add(newResource); 131 } 132 } 133 } 134 135 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 156 public void dispose() { 157 super.dispose(); 158 if (PlatformUI.isWorkbenchRunning()) 159 PlatformUI.getWorkbench().getWorkingSetManager().removePropertyChangeListener(this); 160 } 161 162 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 178 protected void init(IMemento memento) { 179 super.init(memento); 180 IMemento[] rootNodes = memento.getChildren(CTX_SETS); 181 if (rootNodes != null) { 182 List sets = new ArrayList (); 183 for (int i = 0; i < rootNodes.length; i++) { 184 IMemento rootNode = rootNodes[i]; 185 String 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 |