1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashSet ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.jface.resource.ImageDescriptor; 21 import org.eclipse.jface.util.IPropertyChangeListener; 22 import org.eclipse.jface.util.PropertyChangeEvent; 23 import org.eclipse.ui.IMemento; 24 import org.eclipse.ui.IWorkingSet; 25 import org.eclipse.ui.IWorkingSetManager; 26 import org.eclipse.ui.internal.util.Util; 27 28 32 public class AggregateWorkingSet extends AbstractWorkingSet implements 33 IPropertyChangeListener { 34 35 private IWorkingSet[] components; 36 37 43 public AggregateWorkingSet(String name, String label, 44 IWorkingSet[] components) { 45 super(name, label); 46 47 IWorkingSet[] componentCopy = new IWorkingSet[components.length]; 48 System.arraycopy(components, 0, componentCopy, 0, components.length); 49 internalSetComponents(componentCopy); 50 constructElements(false); 51 } 52 53 59 public AggregateWorkingSet(String name, String label, IMemento memento) { 60 super(name, label); 61 workingSetMemento = memento; 62 } 63 64 void setComponents(IWorkingSet[] components) { 65 internalSetComponents(components); 66 constructElements(true); 67 } 68 69 private void internalSetComponents(IWorkingSet[] components) { 70 this.components = components; 71 } 72 73 79 private void constructElements(boolean fireEvent) { 80 Set elements = new HashSet (); 81 for (int i = 0; i < components.length; i++) { 82 IWorkingSet workingSet = components[i]; 83 elements.addAll(Arrays.asList(workingSet.getElements())); 84 } 85 internalSetElements((IAdaptable[]) elements 86 .toArray(new IAdaptable[elements.size()])); 87 if (fireEvent) { 88 fireWorkingSetChanged( 89 IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE, null); 90 } 91 } 92 93 public String getId() { 94 return null; 95 } 96 97 public ImageDescriptor getImageDescriptor() { 98 return WorkbenchImages 99 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_WORKING_SETS); 100 } 101 102 105 public void setElements(IAdaptable[] elements) { 106 } 107 108 public void setId(String id) { 109 110 } 111 112 115 public boolean isEditable() { 116 return false; 117 } 118 119 122 public boolean isVisible() { 123 return false; 124 } 125 126 public void saveState(IMemento memento) { 127 if (workingSetMemento != null) { 128 memento.putMemento(workingSetMemento); 131 } else { 132 memento.putString(IWorkbenchConstants.TAG_NAME, getName()); 133 memento.putString(IWorkbenchConstants.TAG_LABEL, getLabel()); 134 memento.putString(AbstractWorkingSet.TAG_AGGREGATE, Boolean.TRUE 135 .toString()); 136 137 for (int i = 0; i < components.length; i++) { 138 IWorkingSet componentSet = components[i]; 139 memento.createChild(IWorkbenchConstants.TAG_WORKING_SET, 140 componentSet.getName()); 141 } 142 } 143 } 144 145 public void connect(IWorkingSetManager manager) { 146 manager.addPropertyChangeListener(this); 147 super.connect(manager); 148 } 149 150 public void disconnect() { 151 getManager().removePropertyChangeListener(this); 152 super.disconnect(); 153 } 154 155 160 public IWorkingSet[] getComponents() { 161 if (components == null) { 162 restoreWorkingSet(); 163 workingSetMemento = null; 164 } 165 return components; 166 } 167 168 public void propertyChange(PropertyChangeEvent event) { 169 String property = event.getProperty(); 170 if (property.equals(IWorkingSetManager.CHANGE_WORKING_SET_REMOVE)) { 171 for (int i = 0; i < getComponents().length; i++) { 172 IWorkingSet set = getComponents()[i]; 173 if (set.equals(event.getOldValue())) { 174 IWorkingSet[] newComponents = new IWorkingSet[components.length - 1]; 175 Util 176 .arrayCopyWithRemoval(getComponents(), 177 newComponents, i); 178 setComponents(newComponents); 179 } 180 } 181 } else if (property 182 .equals(IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE)) { 183 for (int i = 0; i < getComponents().length; i++) { 184 IWorkingSet set = getComponents()[i]; 185 if (set.equals(event.getNewValue())) { 186 constructElements(true); 187 break; 188 } 189 } 190 } 191 } 192 193 void restoreWorkingSet() { 194 IWorkingSetManager manager = getManager(); 195 if (manager == null) { 196 throw new IllegalStateException (); 197 } 198 IMemento[] workingSetReferences = workingSetMemento 199 .getChildren(IWorkbenchConstants.TAG_WORKING_SET); 200 ArrayList list = new ArrayList (workingSetReferences.length); 201 202 for (int i = 0; i < workingSetReferences.length; i++) { 203 IMemento setReference = workingSetReferences[i]; 204 String setId = setReference.getID(); 205 IWorkingSet set = manager.getWorkingSet(setId); 206 if (set != null) { 207 list.add(set); 208 } 209 } 210 internalSetComponents((IWorkingSet[]) list 211 .toArray(new IWorkingSet[list.size()])); 212 constructElements(false); 213 } 214 215 public boolean equals(Object object) { 216 if (this == object) { 217 return true; 218 } 219 if (object instanceof AggregateWorkingSet) { 220 AggregateWorkingSet workingSet = (AggregateWorkingSet) object; 221 222 return Util.equals(workingSet.getName(), getName()) 223 && Util.equals(workingSet.getComponents(), getComponents()); 224 } 225 return false; 226 } 227 228 public int hashCode() { 229 int hashCode = getName().hashCode() & getComponents().hashCode(); 230 return hashCode; 231 } 232 233 public boolean isSelfUpdating() { 234 if (components == null || components.length == 0) { 235 return false; 236 } 237 for (int i= 0; i < components.length; i++) { 238 if (!components[i].isSelfUpdating()) { 239 return false; 240 } 241 } 242 return true; 243 } 244 245 public boolean isAggregateWorkingSet() { 246 return true; 247 } 248 249 252 public IAdaptable[] adaptElements(IAdaptable[] objects) { 253 return new IAdaptable[0]; 254 } 255 } 256 | Popular Tags |