KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.ui.IMemento;
21 import org.eclipse.ui.IPersistableElement;
22 import org.eclipse.ui.IWorkingSet;
23 import org.eclipse.ui.IWorkingSetManager;
24 import org.eclipse.ui.internal.util.Util;
25
26 /**
27  * Abstract baseclass for IWorkingSet implementations.
28  *
29  * @since 3.2
30  */

31 public abstract class AbstractWorkingSet implements IAdaptable, IWorkingSet {
32
33     protected static final String JavaDoc FACTORY_ID = "org.eclipse.ui.internal.WorkingSetFactory"; //$NON-NLS-1$
34

35     static final String JavaDoc TAG_AGGREGATE = "aggregate"; //$NON-NLS-1$
36

37     private String JavaDoc name;
38
39     protected ArrayList JavaDoc elements;
40
41     private IWorkingSetManager manager;
42
43     protected IMemento workingSetMemento;
44
45     private String JavaDoc label;
46
47     /**
48      * Whether or not the label value should follow the name value. It should do
49      * this until a call to setLabel() differentiates it from the name.
50      */

51     private boolean labelBoundToName;
52
53     /**
54      * Create a new instance of this class
55      *
56      * @param name the unique name for this working set
57      * @param label the user-friendly name for this working set
58      */

59     public AbstractWorkingSet(String JavaDoc name, String JavaDoc label) {
60         Assert.isNotNull(name, "name must not be null"); //$NON-NLS-1$
61
this.name = name;
62         this.label = label;
63         labelBoundToName = Util.equals(name, label);
64     }
65     
66     /**
67      * Returns the receiver if the requested type is either IWorkingSet
68      * or IPersistableElement.
69      *
70      * @param adapter the requested type
71      * @return the receiver if the requested type is either IWorkingSet
72      * or IPersistableElement.
73      */

74     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
75         if (adapter == IWorkingSet.class
76                 || adapter == IPersistableElement.class) {
77             return this;
78         }
79         return Platform.getAdapterManager().getAdapter(this, adapter);
80     }
81
82     public String JavaDoc getName() {
83         return name;
84     }
85
86     public void setName(String JavaDoc newName) {
87         Assert.isNotNull(newName, "Working set name must not be null"); //$NON-NLS-1$
88

89         name = newName;
90         fireWorkingSetChanged(IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE, null);
91         
92         if (labelBoundToName) {
93                 label = newName;
94                 fireWorkingSetChanged(IWorkingSetManager.CHANGE_WORKING_SET_LABEL_CHANGE, null);
95         }
96     }
97
98     /**
99      * Connect this working set to a manger.
100      *
101      * @param manager the manager to connect to
102      */

103     public void connect(IWorkingSetManager manager) {
104         Assert.isTrue(this.manager == null, "A working set can only be connected to one manager"); //$NON-NLS-1$
105
this.manager= manager;
106     }
107
108     /**
109      * Disconnet this working set from its manager, if any.
110      */

111     public void disconnect() {
112         this.manager= null;
113     }
114
115     protected void fireWorkingSetChanged(String JavaDoc property, Object JavaDoc oldValue) {
116         AbstractWorkingSetManager receiver= manager != null
117             ? (AbstractWorkingSetManager)manager
118             : (AbstractWorkingSetManager)WorkbenchPlugin.getDefault().getWorkingSetManager();
119         receiver.workingSetChanged(this, property, oldValue);
120     }
121
122     /**
123      * Create a copy of the elements to store in the receiver.
124      *
125      * @param elements the elements to store a copy of in the
126      * receiver.
127      */

128     protected void internalSetElements(IAdaptable[] newElements) {
129         Assert.isNotNull(newElements,
130                 "Working set elements array must not be null"); //$NON-NLS-1$
131

132         elements = new ArrayList JavaDoc(newElements.length);
133         for (int i = 0; i < newElements.length; i++) {
134             elements.add(newElements[i]);
135         }
136     }
137
138     public IAdaptable[] getElements() {
139         ArrayList JavaDoc list = getElementsArray();
140         return (IAdaptable[]) list.toArray(new IAdaptable[list.size()]);
141     }
142
143     /**
144      * Returns the elements array list. Lazily restores the elements from
145      * persistence memento.
146      *
147      * @return the elements array list
148      */

149     protected ArrayList JavaDoc getElementsArray() {
150         if (elements == null) {
151             restoreWorkingSet();
152             workingSetMemento = null;
153         }
154         return elements;
155     }
156     
157     abstract void restoreWorkingSet();
158     
159     protected IWorkingSetManager getManager() {
160         return manager;
161     }
162
163     public String JavaDoc getFactoryId() {
164         return FACTORY_ID;
165     }
166
167     public String JavaDoc getLabel() {
168         return label;
169     }
170
171     public void setLabel(String JavaDoc label) {
172         this.label = label == null ? getName() : label;
173         labelBoundToName = Util.equals(label, name); // rebind the label to the name
174

175         fireWorkingSetChanged(
176                 IWorkingSetManager.CHANGE_WORKING_SET_LABEL_CHANGE, null);
177     }
178     
179     public boolean isEmpty() {
180         return getElementsArray().isEmpty();
181     }
182     
183     /*
184      * (non-Javadoc)
185      * @see org.eclipse.ui.IWorkingSet#getImage()
186      */

187     public final ImageDescriptor getImage() {
188         return getImageDescriptor();
189     }
190 }
191
Popular Tags