01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal.services;
11:
12: import java.util.HashMap;
13: import java.util.HashSet;
14: import java.util.Iterator;
15: import java.util.Map;
16: import java.util.Set;
17:
18: import org.eclipse.ui.ISourceProvider;
19: import org.eclipse.ui.services.IDisposable;
20:
21: /**
22: * <p>
23: * A service holding all of the registered source providers.
24: * </p>
25: * <p>
26: * This class is not intended for use outside of the
27: * <code>org.eclipse.ui.workbench</code> plug-in.
28: * </p>
29: *
30: * @since 3.2
31: */
32: public final class SourceProviderService implements
33: ISourceProviderService, IDisposable {
34:
35: /**
36: * The source providers registered with this service. This value is never
37: * <code>null</code>. This is a map of the source name ({@link String})
38: * to the source provider ({@link ISourceProvider}).
39: */
40: private final Map sourceProvidersByName = new HashMap();
41:
42: /**
43: * All of the source providers registered with this service. This value is
44: * never <code>null</code>.
45: */
46: private final Set sourceProviders = new HashSet();
47:
48: public final void dispose() {
49: final Iterator sourceProviderItr = sourceProviders.iterator();
50: while (sourceProviderItr.hasNext()) {
51: final ISourceProvider sourceProvider = (ISourceProvider) sourceProviderItr
52: .next();
53: sourceProvider.dispose();
54: }
55: sourceProviders.clear();
56: sourceProvidersByName.clear();
57: }
58:
59: public final ISourceProvider getSourceProvider(
60: final String sourceName) {
61: return (ISourceProvider) sourceProvidersByName.get(sourceName);
62: }
63:
64: public final ISourceProvider[] getSourceProviders() {
65: return (ISourceProvider[]) sourceProviders
66: .toArray(new ISourceProvider[sourceProviders.size()]);
67: }
68:
69: public final void registerProvider(
70: final ISourceProvider sourceProvider) {
71: if (sourceProvider == null) {
72: throw new NullPointerException(
73: "The source provider cannot be null"); //$NON-NLS-1$
74: }
75:
76: final String[] sourceNames = sourceProvider
77: .getProvidedSourceNames();
78: for (int i = 0; i < sourceNames.length; i++) {
79: final String sourceName = sourceNames[i];
80: sourceProvidersByName.put(sourceName, sourceProvider);
81: }
82: sourceProviders.add(sourceProvider);
83: }
84:
85: }
|