001    // Copyright May 20, 2006 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    package org.apache.tapestry.event;
015    
016    import java.util.HashMap;
017    import java.util.Map;
018    
019    import org.apache.commons.lang.builder.ToStringBuilder;
020    import org.apache.commons.lang.builder.ToStringStyle;
021    import org.apache.hivemind.util.Defense;
022    import org.apache.tapestry.IRequestCycle;
023    
024    
025    /**
026     * Represents a client side generated browser event.
027     * 
028     * @author jkuhnert
029     */
030    public class BrowserEvent
031    {
032        public static final String NAME="beventname";
033        public static final String TYPE="beventtype";
034        public static final String KEYS="beventkeys";
035        public static final String CHAR_CODE="beventcharCode";
036        public static final String PAGE_X="beventpageX";
037        public static final String PAGE_Y="beventpageY";
038        public static final String LAYER_X="beventlayerX";
039        public static final String LAYER_Y="beventlayerY";
040        
041        public static final String TARGET="beventtarget";
042        public static final String TARGET_ATTR_ID="id";
043        
044        private String _name;
045        private String _type;
046        private String[] _keys;
047        private String _charCode;
048        private String _pageX;
049        private String _pageY;
050        private String _layerX;
051        private String _layerY;
052        private EventTarget _target;
053        
054        /**
055         * Creates a new browser event that will extract its own
056         * parameters.
057         * 
058         * @param cycle
059         *          The request cycle to extract parameters from.
060         */
061        public BrowserEvent(IRequestCycle cycle)
062        {
063            Defense.notNull(cycle, "cycle");
064            
065            _name = cycle.getParameter(NAME);
066            _type = cycle.getParameter(TYPE);
067            _keys = cycle.getParameters(KEYS);
068            _charCode = cycle.getParameter(CHAR_CODE);
069            _pageX = cycle.getParameter(PAGE_X);
070            _pageY = cycle.getParameter(PAGE_Y);
071            _layerX = cycle.getParameter(LAYER_X);
072            _layerY = cycle.getParameter(LAYER_Y);
073            
074            Map props = new HashMap();
075            _target = new EventTarget(props);
076            
077            String targetId = cycle.getParameter(TARGET + "." + TARGET_ATTR_ID);
078            if (targetId != null) {
079                props.put(TARGET_ATTR_ID, targetId);
080            }
081        }
082        
083        /**
084         * Creates a new browser event with the specified
085         * name/target properties.
086         * 
087         * @param name The name of the event, ie "onClick", "onBlur", etc..
088         * @param target The target of the client side event.
089         */
090        public BrowserEvent(String name, EventTarget target)
091        {
092            _name = name;
093            _target = target;
094        }
095        
096        /**
097         * The name of the event that was generated. 
098         * 
099         * <p>
100         * Examples would be <code>onClick,onSelect,onLoad,etc...</code>.
101         * </p>
102         * @return The event name.
103         */
104        public String getName()
105        {
106            return _name;
107        }
108        
109        /**
110         * Returns the target of the client side event.
111         * 
112         * @return The target representation of the client side object event originally bound for.
113         */
114        public EventTarget getTarget()
115        {
116            return _target;
117        }
118        
119        /**
120         * @return the charCode
121         */
122        public String getCharCode()
123        {
124            return _charCode;
125        }
126        
127        /**
128         * @return the keys
129         */
130        public String[] getKeys()
131        {
132            return _keys;
133        }
134    
135        /**
136         * @return the layerX
137         */
138        public String getLayerX()
139        {
140            return _layerX;
141        }
142    
143        /**
144         * @return the layerY
145         */
146        public String getLayerY()
147        {
148            return _layerY;
149        }
150    
151        /**
152         * @return the pageX
153         */
154        public String getPageX()
155        {
156            return _pageX;
157        }
158    
159        /**
160         * @return the pageY
161         */
162        public String getPageY()
163        {
164            return _pageY;
165        }
166    
167        /**
168         * @return the type
169         */
170        public String getType()
171        {
172            return _type;
173        }
174    
175        /**
176         * Utility method to check if the current request contains
177         * a browser event.
178         * @param cycle
179         *          The associated request.
180         * @return True if the request contains browser event data.
181         */
182        public static boolean hasBrowserEvent(IRequestCycle cycle)
183        {
184            Defense.notNull(cycle, "cycle");
185            
186            return cycle.getParameter(NAME) != null;
187        }
188        
189        public String toString()
190        {
191            return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
192            .append("name", _name)
193            .append("type", _type)
194            .append("keys", _keys)
195            .append("charCode", _charCode)
196            .append("pageX", _pageX)
197            .append("pageY", _pageY)
198            .append("layerX", _layerX)
199            .append("layerY", _layerY)
200            .append("target", _target)
201            .toString();
202        }
203    }