001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.uilib.event;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.araneaframework.InputData;
023: import org.araneaframework.core.EventListener;
024:
025: /**
026: * This class manages onchange and onclick events of {@link org.araneaframework.uilib.form.Control}s that support those types of events.
027: *
028: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
029: */
030: public class StandardControlEventListenerAdapter implements
031: EventListener {
032: private static final Log log = LogFactory
033: .getLog(StandardControlEventListenerAdapter.class);
034:
035: private Collection onClickEventListeners;
036: private Collection onChangeEventListeners;
037:
038: /**
039: * Adds a {@link OnChangeEventListener}which is called when the control value is changing.
040: *
041: * @param onChangeEventListener {@link OnChangeEventListener}which is called when the control value is changing.
042: */
043: public void addOnChangeEventListener(
044: OnChangeEventListener onChangeEventListener) {
045: if (onChangeEventListeners == null)
046: onChangeEventListeners = new ArrayList(1);
047: onChangeEventListeners.add(onChangeEventListener);
048: }
049:
050: public void clearOnChangeEventListeners() {
051: if (onChangeEventListeners != null)
052: onChangeEventListeners.clear();
053: }
054:
055: /** @since 1.1 */
056: public void removeOnChangeEventListener(
057: OnChangeEventListener listener) {
058: onChangeEventListeners.remove(listener);
059: }
060:
061: /**
062: * Returns whether there are any {@link OnChangeEventListener}s registered.
063: *
064: * @return whether there are any {@link OnChangeEventListener}s registered.
065: */
066: public boolean hasOnChangeEventListeners() {
067: return onChangeEventListeners != null
068: && onChangeEventListeners.size() > 0;
069: }
070:
071: /**
072: * Adds a {@link OnClickEventListener}which is called when the control is clicked.
073: *
074: * @param onClickEventListener {@link OnClickEventListener} which is called when the control is clicked.
075: */
076: public void addOnClickEventListener(
077: OnClickEventListener onClickEventListener) {
078: if (onClickEventListeners == null)
079: onClickEventListeners = new ArrayList(1);
080: onClickEventListeners.add(onClickEventListener);
081: }
082:
083: public void clearOnClickEventListeners() {
084: if (onClickEventListeners != null)
085: onClickEventListeners.clear();
086: }
087:
088: /** @since 1.1 */
089: public void removeOnClickEventListener(OnClickEventListener listener) {
090: onClickEventListeners.remove(listener);
091: }
092:
093: /**
094: * Returns whether there are any {@link OnClickEventListener}s registered.
095: *
096: * @return whether there are any {@link OnClickEventListener}s registered.
097: */
098: public boolean hasOnClickEventListeners() {
099: return onClickEventListeners != null
100: && onClickEventListeners.size() > 0;
101: }
102:
103: public void processEvent(Object eventId, InputData input)
104: throws Exception {
105: if (OnChangeEventListener.ON_CHANGE_EVENT.equals(eventId)) {
106: if (onChangeEventListeners != null)
107: for (Iterator i = onChangeEventListeners.iterator(); i
108: .hasNext();) {
109: ((OnChangeEventListener) i.next()).onChange();
110: }
111: } else if (OnClickEventListener.ON_CLICK_EVENT.equals(eventId)) {
112: if (onClickEventListeners != null)
113: for (Iterator i = onClickEventListeners.iterator(); i
114: .hasNext();) {
115: ((OnClickEventListener) i.next()).onClick();
116: }
117: } else {
118: log
119: .warn("Cannot deliver event as no event listeners were registered for the event id '"
120: + eventId + "'!");
121: }
122: }
123: }
|