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:
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.core.runtime.Platform;
017: import org.eclipse.jface.resource.ImageDescriptor;
018: import org.eclipse.ui.IMemento;
019: import org.eclipse.ui.IPersistableElement;
020: import org.eclipse.ui.IWorkingSet;
021: import org.eclipse.ui.IWorkingSetManager;
022: import org.eclipse.ui.internal.util.Util;
023:
024: /**
025: * Abstract baseclass for IWorkingSet implementations.
026: *
027: * @since 3.2
028: */
029: public abstract class AbstractWorkingSet implements IAdaptable,
030: IWorkingSet {
031:
032: protected static final String FACTORY_ID = "org.eclipse.ui.internal.WorkingSetFactory"; //$NON-NLS-1$
033:
034: static final String TAG_AGGREGATE = "aggregate"; //$NON-NLS-1$
035:
036: private String name;
037:
038: protected ArrayList elements;
039:
040: private IWorkingSetManager manager;
041:
042: protected IMemento workingSetMemento;
043:
044: private String label;
045:
046: /**
047: * Whether or not the label value should follow the name value. It should do
048: * this until a call to setLabel() differentiates it from the name.
049: */
050: private boolean labelBoundToName;
051:
052: /**
053: * Create a new instance of this class
054: *
055: * @param name the unique name for this working set
056: * @param label the user-friendly name for this working set
057: */
058: public AbstractWorkingSet(String name, String label) {
059: Assert.isNotNull(name, "name must not be null"); //$NON-NLS-1$
060: this .name = name;
061: this .label = label;
062: labelBoundToName = Util.equals(name, label);
063: }
064:
065: /**
066: * Returns the receiver if the requested type is either IWorkingSet
067: * or IPersistableElement.
068: *
069: * @param adapter the requested type
070: * @return the receiver if the requested type is either IWorkingSet
071: * or IPersistableElement.
072: */
073: public Object getAdapter(Class adapter) {
074: if (adapter == IWorkingSet.class
075: || adapter == IPersistableElement.class) {
076: return this ;
077: }
078: return Platform.getAdapterManager().getAdapter(this , adapter);
079: }
080:
081: public String getName() {
082: return name;
083: }
084:
085: public void setName(String newName) {
086: Assert.isNotNull(newName, "Working set name must not be null"); //$NON-NLS-1$
087:
088: name = newName;
089: fireWorkingSetChanged(
090: IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE, null);
091:
092: if (labelBoundToName) {
093: label = newName;
094: fireWorkingSetChanged(
095: IWorkingSetManager.CHANGE_WORKING_SET_LABEL_CHANGE,
096: null);
097: }
098: }
099:
100: /**
101: * Connect this working set to a manger.
102: *
103: * @param manager the manager to connect to
104: */
105: public void connect(IWorkingSetManager manager) {
106: Assert.isTrue(this .manager == null,
107: "A working set can only be connected to one manager"); //$NON-NLS-1$
108: this .manager = manager;
109: }
110:
111: /**
112: * Disconnet this working set from its manager, if any.
113: */
114: public void disconnect() {
115: this .manager = null;
116: }
117:
118: protected void fireWorkingSetChanged(String property,
119: Object oldValue) {
120: AbstractWorkingSetManager receiver = manager != null ? (AbstractWorkingSetManager) manager
121: : (AbstractWorkingSetManager) WorkbenchPlugin
122: .getDefault().getWorkingSetManager();
123: receiver.workingSetChanged(this , property, oldValue);
124: }
125:
126: /**
127: * Create a copy of the elements to store in the receiver.
128: *
129: * @param elements the elements to store a copy of in the
130: * receiver.
131: */
132: protected void internalSetElements(IAdaptable[] newElements) {
133: Assert.isNotNull(newElements,
134: "Working set elements array must not be null"); //$NON-NLS-1$
135:
136: elements = new ArrayList(newElements.length);
137: for (int i = 0; i < newElements.length; i++) {
138: elements.add(newElements[i]);
139: }
140: }
141:
142: public IAdaptable[] getElements() {
143: ArrayList list = getElementsArray();
144: return (IAdaptable[]) list.toArray(new IAdaptable[list.size()]);
145: }
146:
147: /**
148: * Returns the elements array list. Lazily restores the elements from
149: * persistence memento.
150: *
151: * @return the elements array list
152: */
153: protected ArrayList getElementsArray() {
154: if (elements == null) {
155: restoreWorkingSet();
156: workingSetMemento = null;
157: }
158: return elements;
159: }
160:
161: abstract void restoreWorkingSet();
162:
163: protected IWorkingSetManager getManager() {
164: return manager;
165: }
166:
167: public String getFactoryId() {
168: return FACTORY_ID;
169: }
170:
171: public String getLabel() {
172: return label;
173: }
174:
175: public void setLabel(String label) {
176: this .label = label == null ? getName() : label;
177: labelBoundToName = Util.equals(label, name); // rebind the label to the name
178:
179: fireWorkingSetChanged(
180: IWorkingSetManager.CHANGE_WORKING_SET_LABEL_CHANGE,
181: null);
182: }
183:
184: public boolean isEmpty() {
185: return getElementsArray().isEmpty();
186: }
187:
188: /*
189: * (non-Javadoc)
190: * @see org.eclipse.ui.IWorkingSet#getImage()
191: */
192: public final ImageDescriptor getImage() {
193: return getImageDescriptor();
194: }
195: }
|