KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import com.sun.mirror.declaration.FieldDeclaration;
25 import com.sun.mirror.declaration.TypeDeclaration;
26 import com.sun.mirror.declaration.TypeParameterDeclaration;
27 import com.sun.mirror.type.DeclaredType;
28 import com.sun.mirror.type.ReferenceType;
29 import com.sun.mirror.type.TypeMirror;
30
31 /**
32  * The AptEventField class represents a field type that is also an event source
33  */

34 abstract public class AptEventField extends AptField
35 {
36     public AptEventField(FieldDeclaration fieldDecl)
37     {
38         super(fieldDecl);
39     }
40
41     /**
42      * Inits the ControlInterface associated with this event field. The public interface
43      * for controls and contextual services, and their associated events can be modeled in the
44      * same way. Subclasses will override this to assign an appropriate interface.
45      */

46     abstract protected AptControlInterface initControlInterface();
47
48     /**
49      * Computes the binding from any formal type parameters declared on the control interface
50      * to bound types on the field declaration.
51      */

52     private void initTypeParameterBindings()
53     {
54         //
55
// Get an iterator to both the declared type arguments and the original type
56
// declaration on the associated control interface
57
//
58
DeclaredType fieldType = (DeclaredType)_fieldDecl.getType();
59         Iterator JavaDoc<TypeMirror> paramBoundIter = fieldType.getActualTypeArguments().iterator();
60
61         TypeDeclaration intfDecl = (TypeDeclaration)_controlIntf.getTypeDeclaration();
62         Iterator JavaDoc<TypeParameterDeclaration> paramDeclIter =
63                                             intfDecl.getFormalTypeParameters().iterator();
64
65         //
66
// Iterate through them in parallel, creating a mapping from the original formal
67
// type parameter name to the actual bound type. In parallel, also build up a
68
// representation of the bound type declaration.
69
//
70
// NOTE: If no type binding is done on the field declaration, then loop below
71
// will not execute and no mappings/empty bound decl will be the result.
72
//
73
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
74         boolean isFirst = true;
75         while (paramBoundIter.hasNext())
76         {
77             TypeMirror paramBound = paramBoundIter.next();
78             TypeParameterDeclaration paramDecl = paramDeclIter.next();
79
80             //
81
// Save a mapping from the formal type name to the bound mirror type
82
//
83
_typeBindingMap.put(paramDecl.getSimpleName(), paramBound);
84
85             if (isFirst)
86             {
87                 sb.append("<");
88                 isFirst = false;
89             }
90             else
91                 sb.append(", ");
92             sb.append(paramBound);
93         }
94         if (!isFirst)
95             sb.append(">");
96         
97         _boundParameterDecl = sb.toString();
98     }
99
100     /**
101      * Returns the ControlInterface associated with this event field
102      */

103     public AptControlInterface getControlInterface()
104     {
105         if (_controlIntf == null)
106         {
107             _controlIntf = initControlInterface();
108             if (_controlIntf != null)
109                 initTypeParameterBindings();
110         }
111         return _controlIntf;
112     }
113
114     /**
115      * Gets the EventAdaptor for a particular EventSet
116      */

117     public EventAdaptor getEventAdaptor(AptEventSet eventSet)
118     {
119         return _eventAdaptors.get(eventSet);
120     }
121
122     /**
123      * Adds a EventAdaptor for a particular EventSet
124      */

125     public void addEventAdaptor(AptEventSet eventSet, EventAdaptor eventAdaptor)
126     {
127         assert !_eventAdaptors.containsKey(eventSet);
128         _eventAdaptors.put(eventSet, eventAdaptor);
129     }
130
131     /**
132      * Returns all EventAdaptors for this EventField
133      */

134     public Collection JavaDoc<EventAdaptor> getEventAdaptors()
135     {
136         return _eventAdaptors.values();
137     }
138
139     /**
140      * Returns the bound parameter declaration for this event field
141      */

142     public String JavaDoc getBoundParameters()
143     {
144         return _boundParameterDecl;
145     }
146
147     /**
148      * Returns the formal type binding map (from name to bound type) for the event field
149      */

150     public HashMap JavaDoc<String JavaDoc, TypeMirror> getTypeBindingMap()
151     {
152         return _typeBindingMap;
153     }
154
155     HashMap JavaDoc<AptEventSet, EventAdaptor> _eventAdaptors =
156         new HashMap JavaDoc<AptEventSet, EventAdaptor>();
157
158     String JavaDoc _boundParameterDecl;
159     HashMap JavaDoc<String JavaDoc,TypeMirror> _typeBindingMap = new HashMap JavaDoc<String JavaDoc,TypeMirror>();
160     private AptControlInterface _controlIntf;
161 }
162
Popular Tags