KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > EventAdaptor


1 package org.apache.beehive.controls.runtime.generator;
2 /*
3  * Copyright 2004 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  * $Header:$
18  */

19
20 import java.util.HashMap JavaDoc;
21
22 import com.sun.mirror.type.TypeMirror;
23 import com.sun.mirror.declaration.TypeParameterDeclaration;
24
25 /**
26  * The EventAdaptor class represents the generated class that is necessary to route
27  * events for a EventSet onto implemented EventHandlers on an implementation class.
28  */

29 public class EventAdaptor
30 {
31     /**
32      * Constructs a new EventAdaptor for events declared on an EventSet
33      */

34     public EventAdaptor(AptEventField eventField, AptEventSet eventSet)
35     {
36         _eventField = eventField;
37         _eventSet = eventSet;
38         _className = initClassName();
39     }
40
41     /**
42      * Computes a unique adaptor class name
43      */

44     private String JavaDoc initClassName()
45     {
46         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
47         String JavaDoc fieldName = _eventField.getName();
48         String JavaDoc setName = _eventSet.getClassName();
49         sb.append(Character.toUpperCase(fieldName.charAt(0)));
50         if (fieldName.length() > 1)
51             sb.append(fieldName.substring(1));
52         sb.append(setName.substring(setName.lastIndexOf('.') + 1));
53         sb.append("EventAdaptor");
54         return sb.toString();
55     }
56
57     /**
58      * Returns the name of the generated class for this adaptor.
59      */

60     public String JavaDoc getClassName()
61     {
62         return _className;
63     }
64
65     /**
66      * Returns the name of the generated class for this adaptor, including any formal type
67      * declarations from the associate event set.
68      */

69     public String JavaDoc getFormalClassName()
70     {
71         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(_className);
72         sb.append(_eventSet.getFormalTypeParameters());
73         return sb.toString();
74     }
75
76     /**
77      * Returns the event field associated with this event adaptor
78      */

79     public AptEventField getEventField() { return _eventField; }
80
81     /**
82      * Returns the EventSet associated with this Adaptor
83      */

84     public AptEventSet getEventSet() { return _eventSet; }
85
86     /**
87      * Adds a new EventHandler for a Event to the EventAdaptor
88      */

89     public void addHandler(AptEvent event, AptMethod eventHandler)
90     {
91         assert event.getEventSet() == _eventSet;
92         _handlerMap.put(event, eventHandler);
93     }
94
95     /**
96      * Returns true if there is an EventHandler for ControlEvent on this EventAdaptor
97      */

98     public boolean hasHandler(AptEvent event)
99     {
100         return _handlerMap.containsKey(event);
101     }
102
103     /**
104      * Returns the EventHandler for a ControlEvent on this EventAdaptor
105      */

106     public AptMethod getHandler(AptEvent event)
107     {
108         return _handlerMap.get(event);
109     }
110
111     /**
112      * Returns any formal type parameter declaration for EventSet interface associated with
113      * the adaptor class. This will bind the formal types of the interface based on any type
114      * binding from the event field declaration
115      */

116     public String JavaDoc getEventSetBinding()
117     {
118         // Get the type bindings for the associated event field.
119
HashMap JavaDoc<String JavaDoc,TypeMirror> typeBinding = _eventField.getTypeBindingMap();
120
121         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
122         boolean isFirst = true;
123         for (TypeParameterDeclaration tpd :
124              _eventSet.getDeclaration().getFormalTypeParameters())
125         {
126             if (isFirst)
127             {
128                 sb.append("<");
129                 isFirst = false;
130             }
131             else
132                 sb.append(", ");
133
134             // Map from the declared formal type name to the bound type name
135
// If no map entry exists (i.e. not bindings were specified on the field
136
// declaration, then the implied binding is to Object.class
137
String JavaDoc typeName = tpd.getSimpleName();
138             if (typeBinding.containsKey(typeName))
139                 sb.append(typeBinding.get(tpd.getSimpleName()));
140             else
141                 sb.append("java.lang.Object");
142         }
143         if (!isFirst)
144             sb.append(">");
145
146         return sb.toString();
147     }
148
149     private String JavaDoc _className;
150     private AptEventField _eventField;
151     private AptEventSet _eventSet;
152     private HashMap JavaDoc<AptEvent, AptMethod> _handlerMap = new HashMap JavaDoc<AptEvent,AptMethod>();
153 }
154
Popular Tags