1 11 package org.eclipse.ui.internal; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 17 import org.eclipse.core.runtime.IAdaptable; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.ui.IElementFactory; 20 import org.eclipse.ui.IMemento; 21 import org.eclipse.ui.IPersistableElement; 22 import org.eclipse.ui.IWorkingSetManager; 23 import org.eclipse.ui.PlatformUI; 24 import org.eclipse.ui.internal.registry.WorkingSetDescriptor; 25 import org.eclipse.ui.internal.registry.WorkingSetRegistry; 26 import org.eclipse.ui.internal.util.Util; 27 28 36 public class WorkingSet extends AbstractWorkingSet { 37 private static final String DEFAULT_ID = "org.eclipse.ui.resourceWorkingSetPage"; 39 private String editPageId; 40 41 53 public WorkingSet(String name, String uniqueId, IAdaptable[] elements) { 54 super(name, uniqueId); 55 internalSetElements(elements); 56 } 57 58 68 WorkingSet(String name, String label, IMemento memento) { 69 super(name, label); 70 workingSetMemento = memento; 71 } 72 73 81 public boolean equals(Object object) { 82 if (this == object) { 83 return true; 84 } 85 if (object instanceof WorkingSet) { 86 WorkingSet workingSet = (WorkingSet) object; 87 return Util.equals(workingSet.getName(), getName()) 88 && Util.equals(workingSet.getElementsArray(), 89 getElementsArray()) 90 && Util.equals(workingSet.getId(), getId()); 91 } 92 return false; 93 } 94 95 98 public boolean isEditable() { 99 WorkingSetDescriptor descriptor = getDescriptor(null); 100 return descriptor != null && descriptor.isEditable(); 101 } 102 103 108 public String getId() { 109 return editPageId; 110 } 111 112 117 public ImageDescriptor getImageDescriptor() { 118 WorkingSetDescriptor descriptor = getDescriptor(DEFAULT_ID); 119 if (descriptor == null) { 120 return null; 121 } 122 return descriptor.getIcon(); 123 } 124 125 130 public int hashCode() { 131 int hashCode = getName().hashCode() & getElementsArray().hashCode(); 132 133 if (editPageId != null) { 134 hashCode &= editPageId.hashCode(); 135 } 136 return hashCode; 137 } 138 139 142 void restoreWorkingSet() { 143 IMemento[] itemMementos = workingSetMemento 144 .getChildren(IWorkbenchConstants.TAG_ITEM); 145 Set items = new HashSet (); 146 for (int i = 0; i < itemMementos.length; i++) { 147 IMemento itemMemento = itemMementos[i]; 148 String factoryID = itemMemento 149 .getString(IWorkbenchConstants.TAG_FACTORY_ID); 150 151 if (factoryID == null) { 152 WorkbenchPlugin 153 .log("Unable to restore working set item - no factory ID."); continue; 155 } 156 IElementFactory factory = PlatformUI.getWorkbench() 157 .getElementFactory(factoryID); 158 if (factory == null) { 159 WorkbenchPlugin 160 .log("Unable to restore working set item - cannot instantiate factory: " + factoryID); continue; 162 } 163 IAdaptable item = factory.createElement(itemMemento); 164 if (item == null) { 165 WorkbenchPlugin 166 .log("Unable to restore working set item - cannot instantiate item: " + factoryID); continue; 168 } 169 items.add(item); 170 } 171 internalSetElements((IAdaptable[]) items.toArray(new IAdaptable[items 172 .size()])); 173 } 174 175 182 public void saveState(IMemento memento) { 183 if (workingSetMemento != null) { 184 memento.putMemento(workingSetMemento); 187 } else { 188 memento.putString(IWorkbenchConstants.TAG_NAME, getName()); 189 memento.putString(IWorkbenchConstants.TAG_LABEL, getLabel()); 190 memento.putString(IWorkbenchConstants.TAG_EDIT_PAGE_ID, editPageId); 191 Iterator iterator = elements.iterator(); 192 while (iterator.hasNext()) { 193 IAdaptable adaptable = (IAdaptable) iterator.next(); 194 IPersistableElement persistable = (IPersistableElement) Util 195 .getAdapter(adaptable, IPersistableElement.class); 196 if (persistable != null) { 197 IMemento itemMemento = memento 198 .createChild(IWorkbenchConstants.TAG_ITEM); 199 200 itemMemento.putString(IWorkbenchConstants.TAG_FACTORY_ID, 201 persistable.getFactoryId()); 202 persistable.saveState(itemMemento); 203 } 204 } 205 } 206 } 207 208 213 public void setElements(IAdaptable[] newElements) { 214 internalSetElements(newElements); 215 fireWorkingSetChanged( 216 IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE, null); 217 } 218 219 224 public void setId(String pageId) { 225 editPageId = pageId; 226 } 227 228 public boolean isVisible() { 229 return true; 230 } 231 232 public boolean isSelfUpdating() { 233 WorkingSetDescriptor descriptor = getDescriptor(null); 234 return descriptor != null && descriptor.getUpdaterClassName() != null; 235 } 236 237 public boolean isAggregateWorkingSet() { 238 return false; 239 } 240 241 251 private WorkingSetDescriptor getDescriptor(String defaultId) { 252 WorkingSetRegistry registry = WorkbenchPlugin.getDefault() 253 .getWorkingSetRegistry(); 254 String id = getId(); 255 if (id == null) 256 id = defaultId; 257 if (id == null) 258 return null; 259 260 return registry.getWorkingSetDescriptor(id); 261 } 262 263 268 public IAdaptable[] adaptElements(IAdaptable[] objects) { 269 IWorkingSetManager manager = getManager(); 270 if (manager instanceof WorkingSetManager) { 271 WorkingSetDescriptor descriptor = getDescriptor(null); 272 if (descriptor == null || !descriptor.isElementAdapterClassLoaded()) 273 return objects; 274 return ((WorkingSetManager) manager).getElementAdapter( 275 descriptor).adaptElements(this, objects); 276 } 277 return objects; 278 } 279 } 280 | Popular Tags |