001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.core.event;
019:
020: import java.util.Arrays;
021:
022: import javax.swing.event.EventListenerList;
023:
024: import org.apache.ivy.plugins.repository.TransferEvent;
025: import org.apache.ivy.plugins.repository.TransferListener;
026: import org.apache.ivy.util.filter.Filter;
027:
028: public class EventManager implements TransferListener {
029:
030: private EventListenerList listeners = new EventListenerList();
031:
032: public void addIvyListener(IvyListener listener) {
033: listeners.add(IvyListener.class, listener);
034: }
035:
036: public void addIvyListener(IvyListener listener, String eventName) {
037: addIvyListener(listener, new IvyEventFilter(eventName, null,
038: null));
039: }
040:
041: public void addIvyListener(IvyListener listener, Filter filter) {
042: listeners.add(IvyListener.class, new FilteredIvyListener(
043: listener, filter));
044: }
045:
046: public void removeIvyListener(IvyListener listener) {
047: listeners.remove(IvyListener.class, listener);
048: IvyListener[] listeners = (IvyListener[]) this .listeners
049: .getListeners(IvyListener.class);
050: for (int i = 0; i < listeners.length; i++) {
051: if (listeners[i] instanceof FilteredIvyListener) {
052: if (listener
053: .equals(((FilteredIvyListener) listeners[i])
054: .getIvyListener())) {
055: this .listeners.remove(IvyListener.class,
056: listeners[i]);
057: }
058: }
059: }
060: }
061:
062: public boolean hasIvyListener(IvyListener listener) {
063: return Arrays.asList(listeners.getListeners(IvyListener.class))
064: .contains(listener);
065: }
066:
067: public void fireIvyEvent(IvyEvent evt) {
068: Object[] listeners = this .listeners.getListenerList();
069: for (int i = listeners.length - 2; i >= 0; i -= 2) {
070: if (listeners[i] == IvyListener.class) {
071: ((IvyListener) listeners[i + 1]).progress(evt);
072: }
073: }
074: }
075:
076: public void addTransferListener(TransferListener listener) {
077: listeners.add(TransferListener.class, listener);
078: }
079:
080: public void removeTransferListener(TransferListener listener) {
081: listeners.remove(TransferListener.class, listener);
082: }
083:
084: public boolean hasTransferListener(TransferListener listener) {
085: return Arrays.asList(
086: listeners.getListeners(TransferListener.class))
087: .contains(listener);
088: }
089:
090: protected void fireTransferEvent(TransferEvent evt) {
091: Object[] listeners = this .listeners.getListenerList();
092: for (int i = listeners.length - 2; i >= 0; i -= 2) {
093: if (listeners[i] == TransferListener.class) {
094: ((TransferListener) listeners[i + 1])
095: .transferProgress(evt);
096: }
097: }
098: }
099:
100: public void transferProgress(TransferEvent evt) {
101: fireTransferEvent(evt);
102: fireIvyEvent(evt);
103: }
104:
105: }
|