View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.event;
20  
21  import org.apache.myfaces.trinidad.context.Window;
22  import org.apache.myfaces.trinidad.context.Window.LifecycleState;
23  
24  /**
25   * Event delivered when the LifecycleState of a Window changes.  The <code>cause</code>
26   * indicates the cause ot the state change.  The state diagram for theWindow LifecycleStates
27   * is
28   <pre>
29                        +-----------load---------------+
30                        |                              |       ---expire---
31                        V      /---unload----\         |      /            \
32   <start> ---open--->OPEN-----               ----->UNLOADED--              -->CLOSED
33                        |      \--navigate---/         ^      \            /
34                        |                              |       ---close----
35                        +---------closing--------------+
36  
37   </pre>
38   * The new LifecycleStates can be retrieved by calling <code>getLifecycleState</code> on the
39   * source Window or by calling the <code>getNewLifecycleState</code> convenience function
40   * on the WindowLifecycleEvent
41   * @see org.apache.myfaces.trinidad.context.Window
42   * @see org.apache.myfaces.trinidad.context.Window.LifecycleState
43   */
44  public class WindowLifecycleEvent extends WindowEvent
45  {
46    /**
47     * What caused the delivery of the WindowLifecycleEvent.
48     */
49    public enum Cause
50    {
51      /**
52       * Delivered when a new Window is open
53       */
54      OPEN,
55      /**
56       * Delivered when the content of a Window have been unloaded but cause of the unloading
57       * isn't known.
58       */
59      UNLOAD,
60      /**
61       * Delivered when the content of a Window have been unloaded as a result of
62       * navigating within the application
63       */
64      NAVIGATE,
65      /**
66       * Delivered when the content of a Window have been unloaded in order to
67       * close the window
68       */
69      CLOSING,
70      
71      /**
72       * The contents of an existing Window are being reloaded
73       */
74      RELOAD,
75      
76      /**
77       * The Window is believed to have been closed by the user
78       */
79      EXPIRE,
80  
81      /**
82       * The Window is believed to have been closed by the user
83       */
84      CLOSE
85    }
86  
87    /**
88     * Creates a WindowOpenEvent event for the specified Window and cause.
89     */
90    public WindowLifecycleEvent(Window source, Cause cause)
91    {
92      super(source);
93      
94      if (cause == null)
95        throw new NullPointerException();
96      
97      _cause = cause;
98    }
99   
100   /**
101    * @return the cause of the WindowOpen event.
102    */
103   public Cause getCause()
104   {
105     return _cause;
106   }
107   
108   /**
109    * Returns the new LifecycleState that the Window has moved to.
110    */
111   public final LifecycleState getNewLifecycleState()
112   {
113     return getSource().getLifecycleState();
114   }
115 
116   @Override  
117   public int hashCode()
118   {
119     return getSource().hashCode() * 37 + _cause.hashCode();
120   }
121   
122   @Override
123   public boolean equals(Object o)
124   {
125     if (this == o)
126       return true;
127     else if ((o != null) && (o.getClass() == WindowLifecycleEvent.class))
128     {
129       return subclassEquals((WindowLifecycleEvent)o);
130     }
131     else
132     {
133       return false;
134     }
135   }
136   
137   @Override
138   public String toString()
139   {
140     return super.toString() + ",cause=" + _cause;
141   }
142   
143   /**
144    * Called by subclass <code>equals</code> implementation to check the WindowEvent
145    * portion of equivalence.
146    * @param e Non-null WindowEvent to compare for equality
147    * @return <code>true</code> if the the WindowEvent satisfies the WindowEvent portion
148    * of equivalence.
149    */
150   protected final boolean subclassEquals(WindowLifecycleEvent e)
151   {
152     return super.subclassEquals(e) && _cause.equals(e._cause);
153   }
154 
155   private final Cause _cause;
156   
157   private static final long serialVersionUID = 1L;
158 }