001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.runtime.generator;
020:
021: import java.util.HashMap;
022:
023: import com.sun.mirror.type.TypeMirror;
024: import com.sun.mirror.declaration.TypeParameterDeclaration;
025:
026: /**
027: * The EventAdaptor class represents the generated class that is necessary to route
028: * events for a EventSet onto implemented EventHandlers on an implementation class.
029: */
030: public class EventAdaptor {
031: /**
032: * Constructs a new EventAdaptor for events declared on an EventSet
033: */
034: public EventAdaptor(AptEventField eventField, AptEventSet eventSet) {
035: _eventField = eventField;
036: _eventSet = eventSet;
037: _className = initClassName();
038: }
039:
040: /**
041: * Computes a unique adaptor class name
042: */
043: private String initClassName() {
044: StringBuffer sb = new StringBuffer();
045: String fieldName = _eventField.getName();
046: String setName = _eventSet.getClassName();
047: sb.append(Character.toUpperCase(fieldName.charAt(0)));
048: if (fieldName.length() > 1)
049: sb.append(fieldName.substring(1));
050: sb.append(setName.substring(setName.lastIndexOf('.') + 1));
051: sb.append("EventAdaptor");
052: return sb.toString();
053: }
054:
055: /**
056: * Returns the name of the generated class for this adaptor.
057: */
058: public String getClassName() {
059: return _className;
060: }
061:
062: /**
063: * Returns the name of the generated class for this adaptor, including any formal type
064: * declarations from the associate event set.
065: */
066: public String getFormalClassName() {
067: StringBuffer sb = new StringBuffer(_className);
068: sb.append(_eventSet.getFormalTypeParameters());
069: return sb.toString();
070: }
071:
072: /**
073: * Returns the event field associated with this event adaptor
074: */
075: public AptEventField getEventField() {
076: return _eventField;
077: }
078:
079: /**
080: * Returns the EventSet associated with this Adaptor
081: */
082: public AptEventSet getEventSet() {
083: return _eventSet;
084: }
085:
086: /**
087: * Adds a new EventHandler for a Event to the EventAdaptor
088: */
089: public void addHandler(AptEvent event, AptMethod eventHandler) {
090: assert event.getEventSet() == _eventSet;
091: _handlerMap.put(event, eventHandler);
092: }
093:
094: /**
095: * Returns true if there is an EventHandler for ControlEvent on this EventAdaptor
096: */
097: public boolean hasHandler(AptEvent event) {
098: return _handlerMap.containsKey(event);
099: }
100:
101: /**
102: * Returns the EventHandler for a ControlEvent on this EventAdaptor
103: */
104: public AptMethod getHandler(AptEvent event) {
105: return _handlerMap.get(event);
106: }
107:
108: /**
109: * Returns any formal type parameter declaration for EventSet interface associated with
110: * the adaptor class. This will bind the formal types of the interface based on any type
111: * binding from the event field declaration
112: */
113: public String getEventSetBinding() {
114: // Get the type bindings for the associated event field.
115: HashMap<String, TypeMirror> typeBinding = _eventField
116: .getTypeBindingMap();
117:
118: StringBuffer sb = new StringBuffer();
119: boolean isFirst = true;
120: for (TypeParameterDeclaration tpd : _eventSet.getDeclaration()
121: .getFormalTypeParameters()) {
122: if (isFirst) {
123: sb.append("<");
124: isFirst = false;
125: } else
126: sb.append(", ");
127:
128: // Map from the declared formal type name to the bound type name
129: // If no map entry exists (i.e. not bindings were specified on the field
130: // declaration, then the implied binding is to Object.class
131: String typeName = tpd.getSimpleName();
132: if (typeBinding.containsKey(typeName))
133: sb.append(typeBinding.get(tpd.getSimpleName()));
134: else
135: sb.append("java.lang.Object");
136: }
137: if (!isFirst)
138: sb.append(">");
139:
140: return sb.toString();
141: }
142:
143: private String _className;
144: private AptEventField _eventField;
145: private AptEventSet _eventSet;
146: private HashMap<AptEvent, AptMethod> _handlerMap = new HashMap<AptEvent, AptMethod>();
147: }
|