001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.HashSet;
013: import java.util.Iterator;
014: import java.util.Set;
015:
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.ui.IElementFactory;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.IPersistableElement;
021: import org.eclipse.ui.IWorkingSetManager;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.internal.misc.Policy;
024: import org.eclipse.ui.internal.registry.WorkingSetDescriptor;
025: import org.eclipse.ui.internal.registry.WorkingSetRegistry;
026: import org.eclipse.ui.internal.util.Util;
027:
028: /**
029: * A working set holds a number of IAdaptable elements. A working set is
030: * intended to group elements for presentation to the user or for operations on
031: * a set of elements.
032: *
033: * @see org.eclipse.ui.IWorkingSet
034: * @since 2.0
035: */
036: public class WorkingSet extends AbstractWorkingSet {
037: private static final String DEFAULT_ID = "org.eclipse.ui.resourceWorkingSetPage"; //$NON-NLS-1$
038:
039: private String editPageId;
040:
041: /**
042: * Creates a new working set.
043: *
044: * @param name
045: * the name of the new working set. Should not have leading or
046: * trailing whitespace.
047: * @param uniqueId
048: * the unique id
049: * @param element
050: * the content of the new working set. May be empty but not
051: * <code>null</code>.
052: */
053: public WorkingSet(String name, String uniqueId,
054: IAdaptable[] elements) {
055: super (name, uniqueId);
056: internalSetElements(elements);
057: }
058:
059: /**
060: * Creates a new working set from a memento.
061: *
062: * @param name
063: * the name of the new working set. Should not have leading or
064: * trailing whitespace.
065: * @param memento
066: * persistence memento containing the elements of the working
067: * set.
068: */
069: WorkingSet(String name, String label, IMemento memento) {
070: super (name, label);
071: workingSetMemento = memento;
072: }
073:
074: /**
075: * Tests the receiver and the object for equality
076: *
077: * @param object
078: * object to compare the receiver to
079: * @return true=the object equals the receiver, the name is the same. false
080: * otherwise
081: */
082: public boolean equals(Object object) {
083: if (this == object) {
084: return true;
085: }
086: if (object instanceof WorkingSet) {
087: WorkingSet workingSet = (WorkingSet) object;
088: return Util.equals(workingSet.getName(), getName())
089: && Util.equals(workingSet.getElementsArray(),
090: getElementsArray())
091: && Util.equals(workingSet.getId(), getId());
092: }
093: return false;
094: }
095:
096: /**
097: * {@inheritDoc}
098: */
099: public boolean isEditable() {
100: WorkingSetDescriptor descriptor = getDescriptor(null);
101: return descriptor != null && descriptor.isEditable();
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see org.eclipse.ui.IWorkingSet
108: */
109: public String getId() {
110: return editPageId;
111: }
112:
113: /*
114: * (non-Javadoc)
115: *
116: * @see org.eclipse.ui.IWorkingSet#getImageDescriptor()
117: */
118: public ImageDescriptor getImageDescriptor() {
119: WorkingSetDescriptor descriptor = getDescriptor(DEFAULT_ID);
120: if (descriptor == null) {
121: return null;
122: }
123: return descriptor.getIcon();
124: }
125:
126: /**
127: * Returns the hash code.
128: *
129: * @return the hash code.
130: */
131: public int hashCode() {
132: int hashCode = getName().hashCode();
133:
134: if (editPageId != null) {
135: hashCode &= editPageId.hashCode();
136: }
137: return hashCode;
138: }
139:
140: /**
141: * Recreates the working set elements from the persistence memento.
142: */
143: void restoreWorkingSet() {
144: IMemento[] itemMementos = workingSetMemento
145: .getChildren(IWorkbenchConstants.TAG_ITEM);
146: Set items = new HashSet();
147: for (int i = 0; i < itemMementos.length; i++) {
148: IMemento itemMemento = itemMementos[i];
149: String factoryID = itemMemento
150: .getString(IWorkbenchConstants.TAG_FACTORY_ID);
151:
152: if (factoryID == null) {
153: WorkbenchPlugin
154: .log("Unable to restore working set item - no factory ID."); //$NON-NLS-1$
155: continue;
156: }
157: IElementFactory factory = PlatformUI.getWorkbench()
158: .getElementFactory(factoryID);
159: if (factory == null) {
160: WorkbenchPlugin
161: .log("Unable to restore working set item - cannot instantiate factory: " + factoryID); //$NON-NLS-1$
162: continue;
163: }
164: IAdaptable item = factory.createElement(itemMemento);
165: if (item == null) {
166: if (Policy.DEBUG_WORKING_SETS)
167: WorkbenchPlugin
168: .log("Unable to restore working set item - cannot instantiate item: " + factoryID); //$NON-NLS-1$
169: continue;
170: }
171: items.add(item);
172: }
173: internalSetElements((IAdaptable[]) items
174: .toArray(new IAdaptable[items.size()]));
175: }
176:
177: /**
178: * Implements IPersistableElement. Persist the working set name and working
179: * set contents. The contents has to be either IPersistableElements or
180: * provide adapters for it to be persistent.
181: *
182: * @see org.eclipse.ui.IPersistableElement#saveState(IMemento)
183: */
184: public void saveState(IMemento memento) {
185: if (workingSetMemento != null) {
186: // just re-save the previous memento if the working set has
187: // not been restored
188: memento.putMemento(workingSetMemento);
189: } else {
190: memento.putString(IWorkbenchConstants.TAG_NAME, getName());
191: memento
192: .putString(IWorkbenchConstants.TAG_LABEL,
193: getLabel());
194: memento.putString(IWorkbenchConstants.TAG_EDIT_PAGE_ID,
195: editPageId);
196: Iterator iterator = elements.iterator();
197: while (iterator.hasNext()) {
198: IAdaptable adaptable = (IAdaptable) iterator.next();
199: IPersistableElement persistable = (IPersistableElement) Util
200: .getAdapter(adaptable,
201: IPersistableElement.class);
202: if (persistable != null) {
203: IMemento itemMemento = memento
204: .createChild(IWorkbenchConstants.TAG_ITEM);
205:
206: itemMemento.putString(
207: IWorkbenchConstants.TAG_FACTORY_ID,
208: persistable.getFactoryId());
209: persistable.saveState(itemMemento);
210: }
211: }
212: }
213: }
214:
215: /*
216: * (non-Javadoc)
217: *
218: * @see org.eclipse.ui.IWorkingSet
219: */
220: public void setElements(IAdaptable[] newElements) {
221: internalSetElements(newElements);
222: fireWorkingSetChanged(
223: IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE,
224: null);
225: }
226:
227: /*
228: * (non-Javadoc)
229: *
230: * @see org.eclipse.ui.IWorkingSet
231: */
232: public void setId(String pageId) {
233: editPageId = pageId;
234: }
235:
236: public boolean isVisible() {
237: return true;
238: }
239:
240: public boolean isSelfUpdating() {
241: WorkingSetDescriptor descriptor = getDescriptor(null);
242: return descriptor != null
243: && descriptor.getUpdaterClassName() != null;
244: }
245:
246: public boolean isAggregateWorkingSet() {
247: return false;
248: }
249:
250: /**
251: * Return the working set descriptor for this working set.
252: *
253: * @param defaultId
254: * the default working set type ID to use if this set has no
255: * defined type
256: * @return the descriptor for this working set or <code>null</code> if it
257: * cannot be determined
258: * @since 3.3
259: */
260: private WorkingSetDescriptor getDescriptor(String defaultId) {
261: WorkingSetRegistry registry = WorkbenchPlugin.getDefault()
262: .getWorkingSetRegistry();
263: String id = getId();
264: if (id == null)
265: id = defaultId;
266: if (id == null)
267: return null;
268:
269: return registry.getWorkingSetDescriptor(id);
270: }
271:
272: /*
273: * (non-Javadoc)
274: *
275: * @see org.eclipse.ui.IWorkingSet#adaptElements(org.eclipse.core.runtime.IAdaptable[])
276: */
277: public IAdaptable[] adaptElements(IAdaptable[] objects) {
278: IWorkingSetManager manager = getManager();
279: if (manager instanceof WorkingSetManager) {
280: WorkingSetDescriptor descriptor = getDescriptor(null);
281: if (descriptor == null
282: || !descriptor.isElementAdapterClassLoaded())
283: return objects;
284: return ((WorkingSetManager) manager).getElementAdapter(
285: descriptor).adaptElements(this, objects);
286: }
287: return objects;
288: }
289: }
|