KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Set JavaDoc;
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 /**
29  *
30  * @since 3.2
31  */

32 public class AggregateWorkingSet extends AbstractWorkingSet implements
33         IPropertyChangeListener {
34
35     private IWorkingSet[] components;
36
37     /**
38      *
39      * @param name
40      * @param label
41      * @param components
42      */

43     public AggregateWorkingSet(String JavaDoc name, String JavaDoc 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     /**
54      *
55      * @param name
56      * @param label
57      * @param memento
58      */

59     public AggregateWorkingSet(String JavaDoc name, String JavaDoc 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     /**
74      * Takes the elements from all component working sets and sets them to be
75      * the elements of this working set. Any duplicates are trimmed.
76      *
77      * @param fireEvent whether a working set change event should be fired
78      */

79     private void constructElements(boolean fireEvent) {
80         Set JavaDoc elements = new HashSet JavaDoc();
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 JavaDoc getId() {
94         return null;
95     }
96
97     public ImageDescriptor getImageDescriptor() {
98         return WorkbenchImages
99                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_WORKING_SETS);
100     }
101
102     /**
103      * A no-op for aggregates - their contents should be derived.
104      */

105     public void setElements(IAdaptable[] elements) {
106     }
107
108     public void setId(String JavaDoc id) {
109
110     }
111
112     /**
113      * Aggregates are not editable.
114      */

115     public boolean isEditable() {
116         return false;
117     }
118
119     /**
120      * Aggregates should not generally be visible in the UI.
121      */

122     public boolean isVisible() {
123         return false;
124     }
125
126     public void saveState(IMemento memento) {
127         if (workingSetMemento != null) {
128             // just re-save the previous memento if the working set has
129
// not been restored
130
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     /**
156      * Return the component working sets.
157      *
158      * @return the component working sets
159      */

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 JavaDoc 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 JavaDoc();
197         }
198         IMemento[] workingSetReferences = workingSetMemento
199                 .getChildren(IWorkbenchConstants.TAG_WORKING_SET);
200         ArrayList JavaDoc list = new ArrayList JavaDoc(workingSetReferences.length);
201
202         for (int i = 0; i < workingSetReferences.length; i++) {
203             IMemento setReference = workingSetReferences[i];
204             String JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
250      * @see org.eclipse.ui.IWorkingSet#adaptElements(org.eclipse.core.runtime.IAdaptable[])
251      */

252     public IAdaptable[] adaptElements(IAdaptable[] objects) {
253         return new IAdaptable[0];
254     }
255 }
256
Popular Tags