View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.gui.builder.event;
17  
18  import net.sf.jguiraffe.gui.forms.ComponentHandler;
19  
20  import org.apache.commons.lang.ObjectUtils;
21  
22  /**
23   * <p>
24   * An event class used in the form and form builder framework to deliver event
25   * information related to form elements (controls or input components).
26   * </p>
27   * <p>
28   * All input components in a {@link net.sf.jguiraffe.gui.forms.Form Form} object
29   * (i.e. form elements with an associated
30   * {@link net.sf.jguiraffe.gui.forms.ComponentHandler ComponentHandler} can act
31   * as event sources. Therefore all event objects contain a reference to the
32   * component handler of the event source and the name under which the
33   * corresponding component was registered at the form. These properties can be
34   * used by custom event listener implementations to find out, which component
35   * has caused the event and to access the component's current input data.
36   * </p>
37   *
38   * @author Oliver Heger
39   * @version $Id: FormEvent.java 205 2012-01-29 18:29:57Z oheger $
40   */
41  public class FormEvent extends BuilderEvent
42  {
43      /**
44       * The serial version UID.
45       */
46      private static final long serialVersionUID = 9144620760327400752L;
47  
48      /**
49       * Stores a reference to the component handler of the component that caused
50       * this event.
51       */
52      private final transient ComponentHandler<?> handler;
53  
54      /** Stores the name of the component that caused this event. */
55      private final String name;
56  
57      /**
58       * Creates a new instance of <code>FormEvent</code> and initializes it.
59       *
60       * @param source the event source; this should be the platform specific
61       * event object that is wrapped by this generic object instance
62       * @param handler the component handler
63       * @param name the component's name
64       */
65      public FormEvent(Object source, ComponentHandler<?> handler, String name)
66      {
67          super(source);
68          this.handler = handler;
69          this.name = name;
70      }
71  
72      /**
73       * Returns the handler object of the component, which caused this event.
74       *
75       * @return the component handler
76       */
77      public ComponentHandler<?> getHandler()
78      {
79          return handler;
80      }
81  
82      /**
83       * Returns the name of the component, which caused this event.
84       *
85       * @return the component's name
86       */
87      public String getName()
88      {
89          return name;
90      }
91  
92      /**
93       * Returns a hash code for this object.
94       *
95       * @return a hash code
96       * @since 1.3
97       */
98      @Override
99      public int hashCode()
100     {
101         final int factor = 37;
102         int result = ObjectUtils.hashCode(getName());
103         result = factor * result + ObjectUtils.hashCode(getHandler());
104         result = factor * result + ObjectUtils.hashCode(getSource());
105         return result;
106     }
107 
108     /**
109      * {@inheritDoc} This base implementation tests the handler and name
110      * properties. It is implemented in a way that subclasses can override it to
111      * add checks for additional properties.
112      *
113      * @since 1.3
114      */
115     @Override
116     public boolean equals(Object obj)
117     {
118         if (this == obj)
119         {
120             return true;
121         }
122         if (obj == null || !getClass().equals(obj.getClass()))
123         {
124             return false;
125         }
126 
127         FormEvent c = (FormEvent) obj;
128         return ObjectUtils.equals(getName(), c.getName())
129                 && ObjectUtils.equals(getHandler(), c.getHandler())
130                 && ObjectUtils.equals(getSource(), c.getSource());
131     }
132 }