001: /*******************************************************************************
002: * Copyright (c) 2000, 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.registry;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import org.eclipse.core.runtime.Assert;
019: import org.eclipse.core.runtime.IConfigurationElement;
020: import org.eclipse.core.runtime.IExtension;
021: import org.eclipse.core.runtime.IExtensionPoint;
022: import org.eclipse.core.runtime.Platform;
023: import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
024: import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
025: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
026: import org.eclipse.ui.PlatformUI;
027: import org.eclipse.ui.dialogs.IWorkingSetPage;
028:
029: /**
030: * Stores working set descriptors for working set extensions.
031: */
032: public class WorkingSetRegistry implements IExtensionChangeHandler {
033: // used in Workbench plugin.xml for default workingSet extension
034: // @issue this is an IDE specific working set page!
035: private static final String DEFAULT_PAGE_ID = "org.eclipse.ui.resourceWorkingSetPage"; //$NON-NLS-1$
036:
037: private HashMap/*<String, WorkingSetDescriptor>*/workingSetDescriptors = new HashMap();
038:
039: /**
040: *
041: */
042: public WorkingSetRegistry() {
043: IExtensionTracker tracker = PlatformUI.getWorkbench()
044: .getExtensionTracker();
045: tracker.registerHandler(this , ExtensionTracker
046: .createExtensionPointFilter(getExtensionPointFilter()));
047:
048: }
049:
050: /**
051: *
052: * @return
053: * @since 3.3
054: */
055: private IExtensionPoint getExtensionPointFilter() {
056: return Platform.getExtensionRegistry().getExtensionPoint(
057: PlatformUI.PLUGIN_ID,
058: IWorkbenchRegistryConstants.PL_WORKINGSETS);
059: }
060:
061: /**
062: * Adds a working set descriptor.
063: *
064: * @param descriptor working set descriptor to add. Must not
065: * exist in the registry yet.
066: */
067: public void addWorkingSetDescriptor(WorkingSetDescriptor descriptor) {
068: Assert.isTrue(!workingSetDescriptors.containsValue(descriptor),
069: "working set descriptor already registered"); //$NON-NLS-1$
070: IExtensionTracker tracker = PlatformUI.getWorkbench()
071: .getExtensionTracker();
072: tracker.registerObject(descriptor.getConfigurationElement()
073: .getDeclaringExtension(), descriptor,
074: IExtensionTracker.REF_WEAK);
075: workingSetDescriptors.put(descriptor.getId(), descriptor);
076: }
077:
078: /**
079: * Returns the default, resource based, working set page
080: *
081: * @return the default working set page.
082: */
083: public IWorkingSetPage getDefaultWorkingSetPage() {
084: // @issue this will return the IDE resource working set page... not good for generic workbench
085: WorkingSetDescriptor descriptor = (WorkingSetDescriptor) workingSetDescriptors
086: .get(DEFAULT_PAGE_ID);
087:
088: if (descriptor != null) {
089: return descriptor.createWorkingSetPage();
090: }
091: return null;
092: }
093:
094: /**
095: * Returns the working set descriptor with the given id.
096: *
097: * @param pageId working set page id
098: * @return the working set descriptor with the given id.
099: */
100: public WorkingSetDescriptor getWorkingSetDescriptor(String pageId) {
101: return (WorkingSetDescriptor) workingSetDescriptors.get(pageId);
102: }
103:
104: /**
105: * Returns an array of all working set descriptors.
106: *
107: * @return an array of all working set descriptors.
108: */
109: public WorkingSetDescriptor[] getWorkingSetDescriptors() {
110: return (WorkingSetDescriptor[]) workingSetDescriptors.values()
111: .toArray(
112: new WorkingSetDescriptor[workingSetDescriptors
113: .size()]);
114: }
115:
116: /**
117: * Returns an array of all working set descriptors having
118: * a page class attribute
119: *
120: * @return an array of all working set descriptors having a
121: * page class attribute
122: */
123: public WorkingSetDescriptor[] getNewPageWorkingSetDescriptors() {
124: Collection descriptors = workingSetDescriptors.values();
125: List result = new ArrayList(descriptors.size());
126: for (Iterator iter = descriptors.iterator(); iter.hasNext();) {
127: WorkingSetDescriptor descriptor = (WorkingSetDescriptor) iter
128: .next();
129: if (descriptor.getPageClassName() != null) {
130: result.add(descriptor);
131: }
132: }
133: return (WorkingSetDescriptor[]) result
134: .toArray(new WorkingSetDescriptor[result.size()]);
135: }
136:
137: /**
138: * Returns <code>true</code> if there is a working set descriptor with
139: * a page class attribute. Otherwise <code>false</code> is returned.
140: *
141: * @return whether a descriptor with a page class attribute exists
142: */
143: public boolean hasNewPageWorkingSetDescriptor() {
144: Collection descriptors = workingSetDescriptors.values();
145: for (Iterator iter = descriptors.iterator(); iter.hasNext();) {
146: WorkingSetDescriptor descriptor = (WorkingSetDescriptor) iter
147: .next();
148: if (descriptor.getPageClassName() != null) {
149: return true;
150: }
151: }
152: return false;
153: }
154:
155: public WorkingSetDescriptor[] getUpdaterDescriptorsForNamespace(
156: String namespace) {
157: Collection descriptors = workingSetDescriptors.values();
158: List result = new ArrayList();
159: for (Iterator iter = descriptors.iterator(); iter.hasNext();) {
160: WorkingSetDescriptor descriptor = (WorkingSetDescriptor) iter
161: .next();
162: if (namespace.equals(descriptor.getUpdaterNamespace())) {
163: result.add(descriptor);
164: }
165: }
166: return (WorkingSetDescriptor[]) result
167: .toArray(new WorkingSetDescriptor[result.size()]);
168: }
169:
170: public WorkingSetDescriptor[] getElementAdapterDescriptorsForNamespace(
171: String namespace) {
172: Collection descriptors = workingSetDescriptors.values();
173: List result = new ArrayList();
174: for (Iterator iter = descriptors.iterator(); iter.hasNext();) {
175: WorkingSetDescriptor descriptor = (WorkingSetDescriptor) iter
176: .next();
177: if (namespace.equals(descriptor.getDeclaringNamespace())) {
178: result.add(descriptor);
179: }
180: }
181: return (WorkingSetDescriptor[]) result
182: .toArray(new WorkingSetDescriptor[result.size()]);
183: }
184:
185: /**
186: * Returns the working set page with the given id.
187: *
188: * @param pageId working set page id
189: * @return the working set page with the given id.
190: */
191: public IWorkingSetPage getWorkingSetPage(String pageId) {
192: WorkingSetDescriptor descriptor = (WorkingSetDescriptor) workingSetDescriptors
193: .get(pageId);
194:
195: if (descriptor == null) {
196: return null;
197: }
198: return descriptor.createWorkingSetPage();
199: }
200:
201: /**
202: * Loads the working set registry.
203: */
204: public void load() {
205: WorkingSetRegistryReader reader = new WorkingSetRegistryReader();
206: reader.readWorkingSets(Platform.getExtensionRegistry(), this );
207: }
208:
209: /* (non-Javadoc)
210: * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamichelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
211: */
212: public void addExtension(IExtensionTracker tracker,
213: IExtension extension) {
214: WorkingSetRegistryReader reader = new WorkingSetRegistryReader(
215: this );
216: IConfigurationElement[] elements = extension
217: .getConfigurationElements();
218: for (int i = 0; i < elements.length; i++) {
219: reader.readElement(elements[i]);
220: }
221: }
222:
223: /* (non-Javadoc)
224: * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
225: */
226: public void removeExtension(IExtension extension, Object[] objects) {
227: for (int i = 0; i < objects.length; i++) {
228: if (objects[i] instanceof WorkingSetDescriptor) {
229: WorkingSetDescriptor desc = (WorkingSetDescriptor) objects[i];
230: workingSetDescriptors.remove(desc.getId());
231: }
232: }
233: }
234: }
|