001    // Copyright Jun 2, 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.internal.event;
015    
016    
017    /**
018     * Provides a mapping for listener methods that are bound to events, used
019     * internally by {@link ComponentEventProperty}.
020     */
021    public class EventBoundListener implements Cloneable
022    {
023        // the method name to invoke
024        private String _methodName;
025        // if not null the form to submit before invoking listener
026        private String _formId;
027        // if _formId set whether or not to validate form when submitted
028        private boolean _validateForm;
029        // The targeted component to listen to events on
030        private String _componentId;
031        // The id path of the component that the listener method is connected to
032        private String _componentIdPath;
033        
034        // If targeting a form, whether or not to submit it asynchronously
035        private boolean _async;
036        // Whether or not to focus the form
037        private boolean _focus;
038        // If this is an autoSubmit form bound event, ie we need to discover the formId dynamically
039        private boolean _autoSubmit;
040        
041        /**
042         * Creates a new listener binding. 
043         * @param methodName
044         *          The method to invoke.
045         */
046        public EventBoundListener(String methodName, String componentId)
047        {
048            this(methodName, componentId, true);
049        }
050    
051        /**
052         * Creates a new listener binding.
053         * @param methodName
054         *          The method to invoke.
055         */
056        public EventBoundListener(String methodName, String componentId, boolean autoSubmit)
057        {
058            this(methodName, null, false, componentId, true, false, autoSubmit);
059        }
060    
061        /**
062         * Creates a new listener binding. 
063         * @param methodName
064         *          The method to invoke.
065         * @param formId
066         *          If not null the form to submit before invoking listener
067         * @param validateForm
068         *          If formId is set, whether or not to validate form when submitting.
069         */
070        public EventBoundListener(String methodName, String formId, 
071                boolean validateForm, String componentId, boolean async, boolean focus, boolean autoSubmit)
072        {
073            _methodName = methodName;
074            _formId = formId;
075            _validateForm = validateForm;
076            _componentId = componentId;
077            _async = async;
078            _focus = focus;
079            _autoSubmit = autoSubmit;
080        }
081    
082        /**
083         * Creates a new listener binding.
084         * @param methodName
085         *          The method to invoke.
086         * @param formId
087         *          If not null the form to submit before invoking listener
088         * @param validateForm
089         *          If formId is set, whether or not to validate form when submitting.
090         */
091        public EventBoundListener(String methodName, String formId,
092                boolean validateForm, String componentId, boolean async, boolean focus)
093        {
094            this(methodName, formId, validateForm, componentId, async, focus, true);
095        }
096        
097        /**
098         * @return the formId
099         */
100        public String getFormId()
101        {
102            return _formId;
103        }
104    
105        public void setFormId(String id)
106        {
107            _formId = id;
108        }
109    
110        /**
111         * @return the methodName
112         */
113        public String getMethodName()
114        {
115            return _methodName;
116        }
117        
118        /**
119         * @return the componentId
120         */
121        public String getComponentId()
122        {
123            return _componentId;
124        }
125    
126        public void setComponentId(String id)
127        {
128            _componentId = id;
129        }
130    
131        public void setComponentIdPath(String idPath)
132        {
133            _componentIdPath = idPath;
134        }
135    
136        /**
137         * @return the validateForm
138         */
139        public boolean isValidateForm()
140        {
141            return _validateForm;
142        }
143        
144        /**
145         * Whether or not listener should submit form
146         * asynchronously.
147         * 
148         * @return True if listener is asynchronous.
149         */
150        public boolean isAsync()
151        {
152            return _async;
153        }
154        
155        public boolean shouldFocusForm()
156        {
157            return _focus;
158        }
159    
160        public boolean isAutoSubmit()
161        {
162            return _autoSubmit;
163        }
164    
165        public String getComponentIdPath()
166        {
167            return _componentIdPath;
168        }
169    
170        public Object clone()
171        throws CloneNotSupportedException
172        {
173            return super.clone();
174        }
175    
176        /**
177         * {@inheritDoc}
178         */
179        public int hashCode()
180        {
181            final int prime = 31;
182            int result = 1;
183            result = prime * result + ((_componentId == null) ? 0 : _componentId.hashCode());
184            result = prime * result + ((_methodName == null) ? 0 : _methodName.hashCode());
185            result = prime * result + ((_formId == null) ? 0 : _formId.hashCode());
186            return result;
187        }
188        
189        /** 
190         * {@inheritDoc}
191         */
192        public boolean equals(Object obj)
193        {
194            if (this == obj) return true;
195            if (obj == null) return false;
196            if (getClass() != obj.getClass()) return false;
197            final EventBoundListener other = (EventBoundListener) obj;
198            if (_componentId == null) {
199                if (other._componentId != null) return false;
200            } else if (!_componentId.equals(other._componentId)) return false;
201            if (_methodName == null) {
202                if (other._methodName != null) return false;
203            } else if (!_methodName.equals(other._methodName)) return false;
204            return true;
205        }
206    }