001: /*******************************************************************************
002: * Copyright (c) 2005, 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.ArrayList;
013: import java.util.Arrays;
014: import java.util.HashSet;
015: import java.util.Set;
016:
017: import org.eclipse.core.runtime.IAdaptable;
018: import org.eclipse.jface.resource.ImageDescriptor;
019: import org.eclipse.jface.util.IPropertyChangeListener;
020: import org.eclipse.jface.util.PropertyChangeEvent;
021: import org.eclipse.ui.IMemento;
022: import org.eclipse.ui.IWorkingSet;
023: import org.eclipse.ui.IWorkingSetManager;
024: import org.eclipse.ui.internal.util.Util;
025:
026: /**
027: *
028: * @since 3.2
029: */
030: public class AggregateWorkingSet extends AbstractWorkingSet implements
031: IPropertyChangeListener {
032:
033: private IWorkingSet[] components;
034:
035: /**
036: *
037: * @param name
038: * @param label
039: * @param components
040: */
041: public AggregateWorkingSet(String name, String label,
042: IWorkingSet[] components) {
043: super (name, label);
044:
045: IWorkingSet[] componentCopy = new IWorkingSet[components.length];
046: System.arraycopy(components, 0, componentCopy, 0,
047: components.length);
048: internalSetComponents(componentCopy);
049: constructElements(false);
050: }
051:
052: /**
053: *
054: * @param name
055: * @param label
056: * @param memento
057: */
058: public AggregateWorkingSet(String name, String label,
059: IMemento memento) {
060: super (name, label);
061: workingSetMemento = memento;
062: }
063:
064: void setComponents(IWorkingSet[] components) {
065: internalSetComponents(components);
066: constructElements(true);
067: }
068:
069: private void internalSetComponents(IWorkingSet[] components) {
070: this .components = components;
071: }
072:
073: /**
074: * Takes the elements from all component working sets and sets them to be
075: * the elements of this working set. Any duplicates are trimmed.
076: *
077: * @param fireEvent whether a working set change event should be fired
078: */
079: private void constructElements(boolean fireEvent) {
080: Set elements = new HashSet();
081: for (int i = 0; i < components.length; i++) {
082: IWorkingSet workingSet = components[i];
083: elements.addAll(Arrays.asList(workingSet.getElements()));
084: }
085: internalSetElements((IAdaptable[]) elements
086: .toArray(new IAdaptable[elements.size()]));
087: if (fireEvent) {
088: fireWorkingSetChanged(
089: IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE,
090: null);
091: }
092: }
093:
094: public String getId() {
095: return null;
096: }
097:
098: public ImageDescriptor getImageDescriptor() {
099: return WorkbenchImages
100: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_WORKING_SETS);
101: }
102:
103: /**
104: * A no-op for aggregates - their contents should be derived.
105: */
106: public void setElements(IAdaptable[] elements) {
107: }
108:
109: public void setId(String id) {
110:
111: }
112:
113: /**
114: * Aggregates are not editable.
115: */
116: public boolean isEditable() {
117: return false;
118: }
119:
120: /**
121: * Aggregates should not generally be visible in the UI.
122: */
123: public boolean isVisible() {
124: return false;
125: }
126:
127: public void saveState(IMemento memento) {
128: if (workingSetMemento != null) {
129: // just re-save the previous memento if the working set has
130: // not been restored
131: memento.putMemento(workingSetMemento);
132: } else {
133: memento.putString(IWorkbenchConstants.TAG_NAME, getName());
134: memento
135: .putString(IWorkbenchConstants.TAG_LABEL,
136: getLabel());
137: memento.putString(AbstractWorkingSet.TAG_AGGREGATE,
138: Boolean.TRUE.toString());
139:
140: for (int i = 0; i < components.length; i++) {
141: IWorkingSet componentSet = components[i];
142: memento.createChild(
143: IWorkbenchConstants.TAG_WORKING_SET,
144: componentSet.getName());
145: }
146: }
147: }
148:
149: public void connect(IWorkingSetManager manager) {
150: manager.addPropertyChangeListener(this );
151: super .connect(manager);
152: }
153:
154: public void disconnect() {
155: getManager().removePropertyChangeListener(this );
156: super .disconnect();
157: }
158:
159: /**
160: * Return the component working sets.
161: *
162: * @return the component working sets
163: */
164: public IWorkingSet[] getComponents() {
165: if (components == null) {
166: restoreWorkingSet();
167: workingSetMemento = null;
168: }
169: return components;
170: }
171:
172: public void propertyChange(PropertyChangeEvent event) {
173: String property = event.getProperty();
174: if (property
175: .equals(IWorkingSetManager.CHANGE_WORKING_SET_REMOVE)) {
176: for (int i = 0; i < getComponents().length; i++) {
177: IWorkingSet set = getComponents()[i];
178: if (set.equals(event.getOldValue())) {
179: IWorkingSet[] newComponents = new IWorkingSet[components.length - 1];
180: Util.arrayCopyWithRemoval(getComponents(),
181: newComponents, i);
182: setComponents(newComponents);
183: }
184: }
185: } else if (property
186: .equals(IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE)) {
187: for (int i = 0; i < getComponents().length; i++) {
188: IWorkingSet set = getComponents()[i];
189: if (set.equals(event.getNewValue())) {
190: constructElements(true);
191: break;
192: }
193: }
194: }
195: }
196:
197: void restoreWorkingSet() {
198: IWorkingSetManager manager = getManager();
199: if (manager == null) {
200: throw new IllegalStateException();
201: }
202: IMemento[] workingSetReferences = workingSetMemento
203: .getChildren(IWorkbenchConstants.TAG_WORKING_SET);
204: ArrayList list = new ArrayList(workingSetReferences.length);
205:
206: for (int i = 0; i < workingSetReferences.length; i++) {
207: IMemento setReference = workingSetReferences[i];
208: String setId = setReference.getID();
209: IWorkingSet set = manager.getWorkingSet(setId);
210: if (set != null) {
211: list.add(set);
212: }
213: }
214: internalSetComponents((IWorkingSet[]) list
215: .toArray(new IWorkingSet[list.size()]));
216: constructElements(false);
217: }
218:
219: public boolean equals(Object object) {
220: if (this == object) {
221: return true;
222: }
223: if (object instanceof AggregateWorkingSet) {
224: AggregateWorkingSet workingSet = (AggregateWorkingSet) object;
225:
226: return Util.equals(workingSet.getName(), getName())
227: && Util.equals(workingSet.getComponents(),
228: getComponents());
229: }
230: return false;
231: }
232:
233: public int hashCode() {
234: int hashCode = getName().hashCode()
235: & getComponents().hashCode();
236: return hashCode;
237: }
238:
239: public boolean isSelfUpdating() {
240: if (components == null || components.length == 0) {
241: return false;
242: }
243: for (int i = 0; i < components.length; i++) {
244: if (!components[i].isSelfUpdating()) {
245: return false;
246: }
247: }
248: return true;
249: }
250:
251: public boolean isAggregateWorkingSet() {
252: return true;
253: }
254:
255: /* (non-Javadoc)
256: * @see org.eclipse.ui.IWorkingSet#adaptElements(org.eclipse.core.runtime.IAdaptable[])
257: */
258: public IAdaptable[] adaptElements(IAdaptable[] objects) {
259: return new IAdaptable[0];
260: }
261: }
|