KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > event > EventCollection


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/event/EventCollection.java,v 1.7 2004/07/28 09:36:27 ib Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/07/28 09:36:27 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.event;
25
26 import java.util.*;
27
28 /**
29  * Event collection class
30  *
31  * @version $Revision: 1.7 $
32  */

33 public class EventCollection extends EventObject {
34     List collection = new ArrayList();
35
36     public final static Collected COLLECTED = new Collected();
37     public final static VetoableCollected VETOABLE_COLLECTED = new VetoableCollected();
38
39     public final static String JavaDoc GROUP = "event-collection";
40     public final static AbstractEventMethod[] methods = new AbstractEventMethod[] { COLLECTED, VETOABLE_COLLECTED };
41
42     public EventCollection(Object JavaDoc source) {
43         super(source);
44     }
45
46     public void addEvent(AbstractEventMethod method, EventObject event) {
47         synchronized ( collection ) {
48             collection.add(new Event(method, event));
49         }
50     }
51
52     public List getCollection() {
53         return collection;
54     }
55
56     public String JavaDoc toString() {
57         String JavaDoc NEWLINE = System.getProperty("line.separator");
58         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(256);
59         buffer.append(getClass().getName()).append("[collected events=");
60         synchronized ( collection ) {
61             for ( Iterator i = collection.iterator(); i.hasNext(); ) {
62                 buffer.append(NEWLINE);
63                 Event event = (Event)i.next();
64                 if ( event.getEvent() instanceof RemoteInformation ) {
65                     StringBuffer JavaDoc infoBuffer = new StringBuffer JavaDoc();
66                     String JavaDoc[][] information = ((RemoteInformation)event.getEvent()).getInformation();
67                     boolean first = true;
68                     for ( int j = 0; j < information.length; j++ ) {
69                         if ( !first ) infoBuffer.append(", ");
70                         first = false;
71                         infoBuffer.append(information[j][0]).append("=").append(information[j][1]);
72                     }
73                     buffer.append("["+event.getClass().getName()+" [name="+event.getMethod().getId()+", information: "+infoBuffer.toString()+"]]");
74                 } else {
75                     buffer.append("["+event.getClass().getName()+" [name="+event.getMethod().getId()+"]]");
76                 }
77             }
78         }
79         buffer.append("]");
80         return buffer.toString();
81     }
82
83     public static class Collected extends EventMethod {
84         public Collected() {
85             super(GROUP, "collected");
86         }
87
88         public void fireEvent(EventListener listener, EventObject event) {
89             if (listener instanceof EventCollectionListener ) {
90                 ((EventCollectionListener)listener).collected((EventCollection)event);
91             }
92         }
93     }
94
95     public static class VetoableCollected extends VetoableEventMethod {
96         public VetoableCollected() {
97             super(GROUP, "vetoable-collected");
98         }
99
100         public void fireVetaoableEvent(EventListener listener, EventObject event) throws VetoException {
101             if (listener instanceof EventCollectionListener ) {
102                 ((EventCollectionListener)listener).vetoableCollected((EventCollection)event);
103             }
104         }
105     }
106
107     public class Event {
108         private AbstractEventMethod method;
109         private EventObject event;
110
111         public Event(AbstractEventMethod method, EventObject event) {
112             this.method = method;
113             this.event = event;
114         }
115
116         public AbstractEventMethod getMethod() {
117             return method;
118         }
119
120         public EventObject getEvent() {
121             return event;
122         }
123     }
124 }
Popular Tags