KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > events > DocumentEventSupport


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom.events;
19
20 import org.apache.batik.dom.util.HashTable;
21 import org.w3c.dom.DOMException JavaDoc;
22 import org.w3c.dom.events.Event JavaDoc;
23
24 /**
25  * This class implements the behavior of DocumentEvent.
26  *
27  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
28  * @version $Id: DocumentEventSupport.java,v 1.7 2005/02/22 09:12:59 cam Exp $
29  */

30 public class DocumentEventSupport {
31     
32     /**
33      * The Event type.
34      */

35     public static final String JavaDoc EVENT_TYPE = "Events";
36     
37     /**
38      * The MutationEvent type.
39      */

40     public static final String JavaDoc MUTATION_EVENT_TYPE = "MutationEvents";
41     
42     /**
43      * The MouseEvent type.
44      */

45     public static final String JavaDoc MOUSE_EVENT_TYPE = "MouseEvents";
46
47     /**
48      * The UIEvent type.
49      */

50     public static final String JavaDoc UI_EVENT_TYPE = "UIEvents";
51
52     /**
53      * The KeyEvent type.
54      */

55     public static final String JavaDoc KEY_EVENT_TYPE = "KeyEvents";
56
57     /**
58      * The event factories table.
59      */

60     protected HashTable eventFactories = new HashTable();
61     {
62         eventFactories.put(EVENT_TYPE.toLowerCase(),
63                            new SimpleEventFactory());
64         eventFactories.put(MUTATION_EVENT_TYPE.toLowerCase(),
65                            new MutationEventFactory());
66         eventFactories.put(MOUSE_EVENT_TYPE.toLowerCase(),
67                            new MouseEventFactory());
68         eventFactories.put(KEY_EVENT_TYPE.toLowerCase(),
69                            new KeyEventFactory());
70         eventFactories.put(UI_EVENT_TYPE.toLowerCase(),
71                            new UIEventFactory());
72     }
73
74     /**
75      * Creates a new Event depending on the specified parameter.
76      *
77      * @param eventType The <code>eventType</code> parameter specifies the
78      * type of <code>Event</code> interface to be created. If the
79      * <code>Event</code> interface specified is supported by the
80      * implementation this method will return a new <code>Event</code> of
81      * the interface type requested. If the <code>Event</code> is to be
82      * dispatched via the <code>dispatchEvent</code> method the
83      * appropriate event init method must be called after creation in order
84      * to initialize the <code>Event</code>'s values. As an example, a
85      * user wishing to synthesize some kind of <code>UIEvent</code> would
86      * call <code>createEvent</code> with the parameter "UIEvent". The
87      * <code>initUIEvent</code> method could then be called on the newly
88      * created <code>UIEvent</code> to set the specific type of UIEvent to
89      * be dispatched and set its context information.The
90      * <code>createEvent</code> method is used in creating
91      * <code>Event</code>s when it is either inconvenient or unnecessary
92      * for the user to create an <code>Event</code> themselves. In cases
93      * where the implementation provided <code>Event</code> is
94      * insufficient, users may supply their own <code>Event</code>
95      * implementations for use with the <code>dispatchEvent</code> method.
96      *
97      * @return The newly created <code>Event</code>
98      *
99      * @exception DOMException
100      * NOT_SUPPORTED_ERR: Raised if the implementation does not support the
101      * type of <code>Event</code> interface requested
102      */

103     public Event JavaDoc createEvent(String JavaDoc eventType)
104         throws DOMException JavaDoc {
105         EventFactory ef = (EventFactory)eventFactories.get(eventType.toLowerCase());
106         if (ef == null) {
107             throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR,
108                                    "Bad event type: " + eventType);
109         }
110         return ef.createEvent();
111     }
112
113     /**
114      * Registers a new EventFactory object.
115      */

116     public void registerEventFactory(String JavaDoc eventType,
117                                             EventFactory factory) {
118         eventFactories.put(eventType.toLowerCase(), factory);
119     }
120
121
122     /**
123      * This interface represents an event factory.
124      */

125     public interface EventFactory {
126         /**
127          * Creates a new Event object.
128          */

129         Event JavaDoc createEvent();
130     }
131
132     /**
133      * To create a simple event.
134      */

135     protected static class SimpleEventFactory implements EventFactory {
136         /**
137          * Creates a new Event object.
138          */

139         public Event JavaDoc createEvent() {
140             return new DOMEvent();
141         }
142     }
143
144     /**
145      * To create a mutation event.
146      */

147     protected static class MutationEventFactory implements EventFactory {
148         /**
149          * Creates a new Event object.
150          */

151         public Event JavaDoc createEvent() {
152             return new DOMMutationEvent();
153         }
154     }
155
156     /**
157      * To create a mouse event.
158      */

159     protected static class MouseEventFactory implements EventFactory {
160         /**
161          * Creates a new Event object.
162          */

163         public Event JavaDoc createEvent() {
164             return new DOMMouseEvent();
165         }
166     }
167
168     /**
169      * To create a key event.
170      */

171     protected static class KeyEventFactory implements EventFactory {
172         /**
173          * Creates a new Event object.
174          */

175         public Event JavaDoc createEvent() {
176             return new DOMKeyEvent();
177         }
178     }
179
180     /**
181      * To create a UI event.
182      */

183     protected static class UIEventFactory implements EventFactory {
184         /**
185          * Creates a new Event object.
186          */

187         public Event JavaDoc createEvent() {
188             return new DOMUIEvent();
189         }
190     }
191 }
192
Popular Tags