KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > WorkingSet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
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 /**
29  * A working set holds a number of IAdaptable elements. A working set is
30  * intended to group elements for presentation to the user or for operations on
31  * a set of elements.
32  *
33  * @see org.eclipse.ui.IWorkingSet
34  * @since 2.0
35  */

36 public class WorkingSet extends AbstractWorkingSet {
37     private static final String JavaDoc DEFAULT_ID = "org.eclipse.ui.resourceWorkingSetPage"; //$NON-NLS-1$
38

39     private String JavaDoc editPageId;
40
41     /**
42      * Creates a new working set.
43      *
44      * @param name
45      * the name of the new working set. Should not have leading or
46      * trailing whitespace.
47      * @param uniqueId
48      * the unique id
49      * @param element
50      * the content of the new working set. May be empty but not
51      * <code>null</code>.
52      */

53     public WorkingSet(String JavaDoc name, String JavaDoc uniqueId, IAdaptable[] elements) {
54         super(name, uniqueId);
55         internalSetElements(elements);
56     }
57
58     /**
59      * Creates a new working set from a memento.
60      *
61      * @param name
62      * the name of the new working set. Should not have leading or
63      * trailing whitespace.
64      * @param memento
65      * persistence memento containing the elements of the working
66      * set.
67      */

68     WorkingSet(String JavaDoc name, String JavaDoc label, IMemento memento) {
69         super(name, label);
70         workingSetMemento = memento;
71     }
72
73     /**
74      * Tests the receiver and the object for equality
75      *
76      * @param object
77      * object to compare the receiver to
78      * @return true=the object equals the receiver, the name is the same. false
79      * otherwise
80      */

81     public boolean equals(Object JavaDoc 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     /**
96      * {@inheritDoc}
97      */

98     public boolean isEditable() {
99         WorkingSetDescriptor descriptor = getDescriptor(null);
100         return descriptor != null && descriptor.isEditable();
101     }
102
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.eclipse.ui.IWorkingSet
107      */

108     public String JavaDoc getId() {
109         return editPageId;
110     }
111
112     /*
113      * (non-Javadoc)
114      *
115      * @see org.eclipse.ui.IWorkingSet#getImageDescriptor()
116      */

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     /**
126      * Returns the hash code.
127      *
128      * @return the hash code.
129      */

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     /**
140      * Recreates the working set elements from the persistence memento.
141      */

142     void restoreWorkingSet() {
143         IMemento[] itemMementos = workingSetMemento
144                 .getChildren(IWorkbenchConstants.TAG_ITEM);
145         Set JavaDoc items = new HashSet JavaDoc();
146         for (int i = 0; i < itemMementos.length; i++) {
147             IMemento itemMemento = itemMementos[i];
148             String JavaDoc 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."); //$NON-NLS-1$
154
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); //$NON-NLS-1$
161
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); //$NON-NLS-1$
167
continue;
168             }
169             items.add(item);
170         }
171         internalSetElements((IAdaptable[]) items.toArray(new IAdaptable[items
172                 .size()]));
173     }
174
175     /**
176      * Implements IPersistableElement. Persist the working set name and working
177      * set contents. The contents has to be either IPersistableElements or
178      * provide adapters for it to be persistent.
179      *
180      * @see org.eclipse.ui.IPersistableElement#saveState(IMemento)
181      */

182     public void saveState(IMemento memento) {
183         if (workingSetMemento != null) {
184             // just re-save the previous memento if the working set has
185
// not been restored
186
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 JavaDoc 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     /*
209      * (non-Javadoc)
210      *
211      * @see org.eclipse.ui.IWorkingSet
212      */

213     public void setElements(IAdaptable[] newElements) {
214         internalSetElements(newElements);
215         fireWorkingSetChanged(
216                 IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE, null);
217     }
218
219     /*
220      * (non-Javadoc)
221      *
222      * @see org.eclipse.ui.IWorkingSet
223      */

224     public void setId(String JavaDoc 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     /**
242      * Return the working set descriptor for this working set.
243      *
244      * @param defaultId
245      * the default working set type ID to use if this set has no
246      * defined type
247      * @return the descriptor for this working set or <code>null</code> if it
248      * cannot be determined
249      * @since 3.3
250      */

251     private WorkingSetDescriptor getDescriptor(String JavaDoc defaultId) {
252         WorkingSetRegistry registry = WorkbenchPlugin.getDefault()
253                 .getWorkingSetRegistry();
254         String JavaDoc 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     /*
264      * (non-Javadoc)
265      *
266      * @see org.eclipse.ui.IWorkingSet#adaptElements(org.eclipse.core.runtime.IAdaptable[])
267      */

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