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;
011:
012: import java.util.Map;
013:
014: import org.eclipse.core.commands.util.Tracing;
015: import org.eclipse.ui.internal.misc.Policy;
016:
017: /**
018: * <p>
019: * An implementation of <code>ISourceProvider</code> that provides listener
020: * support. Subclasses need only call <code>fireSourceChanged</code> whenever
021: * appropriate.
022: * </p>
023: *
024: * @since 3.1
025: */
026: public abstract class AbstractSourceProvider implements ISourceProvider {
027:
028: /**
029: * Whether source providers should print out debugging information to the
030: * console when events arrive.
031: *
032: * @since 3.2
033: */
034: protected static boolean DEBUG = Policy.DEBUG_SOURCES;
035:
036: /**
037: * The listeners to this source provider. This value is never
038: * <code>null</code>. {@link #listenerCount} should be consulted to get
039: * the real length.
040: */
041: private ISourceProviderListener[] listeners = new ISourceProviderListener[7];
042:
043: /**
044: * The number of listeners in the array.
045: */
046: private int listenerCount = 0;
047:
048: public final void addSourceProviderListener(
049: final ISourceProviderListener listener) {
050: if (listener == null) {
051: throw new NullPointerException(
052: "The listener cannot be null"); //$NON-NLS-1$
053: }
054:
055: if (listenerCount == listeners.length) {
056: final ISourceProviderListener[] growArray = new ISourceProviderListener[listeners.length + 4];
057: System.arraycopy(listeners, 0, growArray, 0,
058: listeners.length);
059: listeners = growArray;
060: }
061: listeners[listenerCount++] = listener;
062: }
063:
064: /**
065: * Notifies all listeners that a single source has changed.
066: *
067: * @param sourcePriority
068: * The source priority that has changed.
069: * @param sourceName
070: * The name of the source that has changed; must not be
071: * <code>null</code>.
072: * @param sourceValue
073: * The new value for the source; may be <code>null</code>.
074: */
075: protected final void fireSourceChanged(final int sourcePriority,
076: final String sourceName, final Object sourceValue) {
077: for (int i = 0; i < listenerCount; i++) {
078: final ISourceProviderListener listener = listeners[i];
079: listener.sourceChanged(sourcePriority, sourceName,
080: sourceValue);
081: }
082: }
083:
084: /**
085: * Notifies all listeners that multiple sources have changed.
086: *
087: * @param sourcePriority
088: * The source priority that has changed.
089: * @param sourceValuesByName
090: * The map of source names (<code>String</code>) to source
091: * values (<code>Object</code>) that have changed; must not
092: * be <code>null</code>. The names must not be
093: * <code>null</code>, but the values may be <code>null</code>.
094: */
095: protected final void fireSourceChanged(final int sourcePriority,
096: final Map sourceValuesByName) {
097: for (int i = 0; i < listenerCount; i++) {
098: final ISourceProviderListener listener = listeners[i];
099: listener.sourceChanged(sourcePriority, sourceValuesByName);
100: }
101: }
102:
103: /**
104: * Logs a debugging message in an appropriate manner. If the message is
105: * <code>null</code> or the <code>DEBUG</code> is <code>false</code>,
106: * then this method does nothing.
107: *
108: * @param message
109: * The debugging message to log; if <code>null</code>, then
110: * nothing is logged.
111: * @since 3.2
112: */
113: protected final void logDebuggingInfo(final String message) {
114: if (DEBUG && (message != null)) {
115: Tracing.printTrace("SOURCES", message); //$NON-NLS-1$
116: }
117: }
118:
119: public final void removeSourceProviderListener(
120: final ISourceProviderListener listener) {
121: if (listener == null) {
122: throw new NullPointerException(
123: "The listener cannot be null"); //$NON-NLS-1$
124: }
125:
126: int emptyIndex = -1;
127: for (int i = 0; i < listenerCount; i++) {
128: if (listeners[i] == listener) {
129: listeners[i] = null;
130: emptyIndex = i;
131: }
132: }
133:
134: if (emptyIndex != -1) {
135: // Compact the array.
136: for (int i = emptyIndex + 1; i < listenerCount; i++) {
137: listeners[i - 1] = listeners[i];
138: }
139: listenerCount--;
140: }
141: }
142:
143: }
|