1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.IAdaptable; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.jface.resource.ImageDescriptor; 20 import org.eclipse.ui.IMemento; 21 import org.eclipse.ui.IPersistableElement; 22 import org.eclipse.ui.IWorkingSet; 23 import org.eclipse.ui.IWorkingSetManager; 24 import org.eclipse.ui.internal.util.Util; 25 26 31 public abstract class AbstractWorkingSet implements IAdaptable, IWorkingSet { 32 33 protected static final String FACTORY_ID = "org.eclipse.ui.internal.WorkingSetFactory"; 35 static final String TAG_AGGREGATE = "aggregate"; 37 private String name; 38 39 protected ArrayList elements; 40 41 private IWorkingSetManager manager; 42 43 protected IMemento workingSetMemento; 44 45 private String label; 46 47 51 private boolean labelBoundToName; 52 53 59 public AbstractWorkingSet(String name, String label) { 60 Assert.isNotNull(name, "name must not be null"); this.name = name; 62 this.label = label; 63 labelBoundToName = Util.equals(name, label); 64 } 65 66 74 public Object getAdapter(Class adapter) { 75 if (adapter == IWorkingSet.class 76 || adapter == IPersistableElement.class) { 77 return this; 78 } 79 return Platform.getAdapterManager().getAdapter(this, adapter); 80 } 81 82 public String getName() { 83 return name; 84 } 85 86 public void setName(String newName) { 87 Assert.isNotNull(newName, "Working set name must not be null"); 89 name = newName; 90 fireWorkingSetChanged(IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE, null); 91 92 if (labelBoundToName) { 93 label = newName; 94 fireWorkingSetChanged(IWorkingSetManager.CHANGE_WORKING_SET_LABEL_CHANGE, null); 95 } 96 } 97 98 103 public void connect(IWorkingSetManager manager) { 104 Assert.isTrue(this.manager == null, "A working set can only be connected to one manager"); this.manager= manager; 106 } 107 108 111 public void disconnect() { 112 this.manager= null; 113 } 114 115 protected void fireWorkingSetChanged(String property, Object oldValue) { 116 AbstractWorkingSetManager receiver= manager != null 117 ? (AbstractWorkingSetManager)manager 118 : (AbstractWorkingSetManager)WorkbenchPlugin.getDefault().getWorkingSetManager(); 119 receiver.workingSetChanged(this, property, oldValue); 120 } 121 122 128 protected void internalSetElements(IAdaptable[] newElements) { 129 Assert.isNotNull(newElements, 130 "Working set elements array must not be null"); 132 elements = new ArrayList (newElements.length); 133 for (int i = 0; i < newElements.length; i++) { 134 elements.add(newElements[i]); 135 } 136 } 137 138 public IAdaptable[] getElements() { 139 ArrayList list = getElementsArray(); 140 return (IAdaptable[]) list.toArray(new IAdaptable[list.size()]); 141 } 142 143 149 protected ArrayList getElementsArray() { 150 if (elements == null) { 151 restoreWorkingSet(); 152 workingSetMemento = null; 153 } 154 return elements; 155 } 156 157 abstract void restoreWorkingSet(); 158 159 protected IWorkingSetManager getManager() { 160 return manager; 161 } 162 163 public String getFactoryId() { 164 return FACTORY_ID; 165 } 166 167 public String getLabel() { 168 return label; 169 } 170 171 public void setLabel(String label) { 172 this.label = label == null ? getName() : label; 173 labelBoundToName = Util.equals(label, name); 175 fireWorkingSetChanged( 176 IWorkingSetManager.CHANGE_WORKING_SET_LABEL_CHANGE, null); 177 } 178 179 public boolean isEmpty() { 180 return getElementsArray().isEmpty(); 181 } 182 183 187 public final ImageDescriptor getImage() { 188 return getImageDescriptor(); 189 } 190 } 191 | Popular Tags |