KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > AbstractSourceProvider


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;
13
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.commands.util.Tracing;
17 import org.eclipse.ui.internal.misc.Policy;
18
19 /**
20  * <p>
21  * An implementation of <code>ISourceProvider</code> that provides listener
22  * support. Subclasses need only call <code>fireSourceChanged</code> whenever
23  * appropriate.
24  * </p>
25  *
26  * @since 3.1
27  */

28 public abstract class AbstractSourceProvider implements ISourceProvider {
29
30     /**
31      * Whether source providers should print out debugging information to the
32      * console when events arrive.
33      *
34      * @since 3.2
35      */

36     protected static boolean DEBUG = Policy.DEBUG_SOURCES;
37
38     /**
39      * The listeners to this source provider. This value is never
40      * <code>null</code>. {@link #listenerCount} should be consulted to get
41      * the real length.
42      */

43     private ISourceProviderListener[] listeners = new ISourceProviderListener[7];
44
45     /**
46      * The number of listeners in the array.
47      */

48     private int listenerCount = 0;
49
50     public final void addSourceProviderListener(
51             final ISourceProviderListener listener) {
52         if (listener == null) {
53             throw new NullPointerException JavaDoc("The listener cannot be null"); //$NON-NLS-1$
54
}
55
56         if (listenerCount == listeners.length) {
57             final ISourceProviderListener[] growArray = new ISourceProviderListener[listeners.length + 4];
58             System.arraycopy(listeners, 0, growArray, 0, listeners.length);
59             listeners = growArray;
60         }
61         listeners[listenerCount++] = listener;
62     }
63
64     /**
65      * Notifies all listeners that a single source has changed.
66      *
67      * @param sourcePriority
68      * The source priority that has changed.
69      * @param sourceName
70      * The name of the source that has changed; must not be
71      * <code>null</code>.
72      * @param sourceValue
73      * The new value for the source; may be <code>null</code>.
74      */

75     protected final void fireSourceChanged(final int sourcePriority,
76             final String JavaDoc sourceName, final Object JavaDoc sourceValue) {
77         for (int i = 0; i < listenerCount; i++) {
78             final ISourceProviderListener listener = listeners[i];
79             listener.sourceChanged(sourcePriority, sourceName, sourceValue);
80         }
81     }
82
83     /**
84      * Notifies all listeners that multiple sources have changed.
85      *
86      * @param sourcePriority
87      * The source priority that has changed.
88      * @param sourceValuesByName
89      * The map of source names (<code>String</code>) to source
90      * values (<code>Object</code>) that have changed; must not
91      * be <code>null</code>. The names must not be
92      * <code>null</code>, but the values may be <code>null</code>.
93      */

94     protected final void fireSourceChanged(final int sourcePriority,
95             final Map JavaDoc sourceValuesByName) {
96         for (int i = 0; i < listenerCount; i++) {
97             final ISourceProviderListener listener = listeners[i];
98             listener.sourceChanged(sourcePriority, sourceValuesByName);
99         }
100     }
101
102     /**
103      * Logs a debugging message in an appropriate manner. If the message is
104      * <code>null</code> or the <code>DEBUG</code> is <code>false</code>,
105      * then this method does nothing.
106      *
107      * @param message
108      * The debugging message to log; if <code>null</code>, then
109      * nothing is logged.
110      * @since 3.2
111      */

112     protected final void logDebuggingInfo(final String JavaDoc message) {
113         if (DEBUG && (message != null)) {
114             Tracing.printTrace("SOURCES", message); //$NON-NLS-1$
115
}
116     }
117
118     public final void removeSourceProviderListener(
119             final ISourceProviderListener listener) {
120         if (listener == null) {
121             throw new NullPointerException JavaDoc("The listener cannot be null"); //$NON-NLS-1$
122
}
123
124         int emptyIndex = -1;
125         for (int i = 0; i < listenerCount; i++) {
126             if (listeners[i] == listener) {
127                 listeners[i] = null;
128                 emptyIndex = i;
129             }
130         }
131         
132         if (emptyIndex != -1) {
133             // Compact the array.
134
for (int i = emptyIndex + 1; i < listenerCount; i++) {
135                 listeners[i - 1] = listeners[i];
136             }
137             listenerCount--;
138         }
139     }
140
141 }
142
Popular Tags