KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > WebWindowEvent


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit;
39
40 import java.util.EventObject JavaDoc;
41
42 /**
43  * An event that will be fired when a WebWindow changes.
44  *
45  * @version $Revision: 100 $
46  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
47  * @author David K. Taylor
48  */

49 public final class WebWindowEvent extends EventObject JavaDoc {
50     private static final long serialVersionUID = 6693838158619061745L;
51     private final Page oldPage_;
52     private final Page newPage_;
53     private final int type_;
54
55     /** A window has opened */
56     public static final int OPEN = 1;
57
58     /** A window has closed */
59     public static final int CLOSE = 2;
60
61     /** The content of the window has changed */
62     public static final int CHANGE = 3;
63
64     /**
65      * Create an instance
66      *
67      * @param webWindow The WebWindow that caused the event
68      * @param type The type - one of {@link #OPEN}, {@link #CLOSE} or {@link #CHANGE}
69      * @param oldPage The old contents of the web window
70      * @param newPage The new contents of the web window
71      */

72     public WebWindowEvent(
73             final WebWindow webWindow,
74             final int type,
75             final Page oldPage,
76             final Page newPage ) {
77         super(webWindow);
78         oldPage_ = oldPage;
79         newPage_ = newPage;
80
81         switch( type ) {
82             case OPEN:
83             case CLOSE:
84             case CHANGE:
85                 type_ = type;
86                 break;
87
88             default:
89                 throw new IllegalArgumentException JavaDoc(
90                     "type must be one of OPEN, CLOSE, CHANGE but got "+type);
91         }
92     }
93
94
95     /**
96      * Return true if the two objects are equal
97      *
98      * @param object The object to compare against.
99      * @return true if the two objects are equal.
100      */

101     public boolean equals( final Object JavaDoc object ) {
102         if( getClass() == object.getClass() ) {
103             final WebWindowEvent event = (WebWindowEvent)object;
104             return isEqual(getSource(), event.getSource())
105                 && getEventType() == event.getEventType()
106                 && isEqual(getOldPage(), event.getOldPage())
107                 && isEqual(getNewPage(), event.getNewPage());
108         }
109         return false;
110     }
111
112
113     /**
114      * Return the hash code for this object.
115      * @return the hash code for this object.
116      */

117     public int hashCode() {
118         return source.hashCode();
119     }
120
121     /**
122      * Return the oldPage
123      * @return the page or null if the window has no page
124      */

125     public Page getOldPage() {
126         return oldPage_;
127     }
128
129
130     /**
131      * Return the oldPage
132      * @return the page or null if the window has no page
133      */

134     public Page getNewPage() {
135         return newPage_;
136     }
137
138
139     /**
140      * Return the web window that fired the event.
141      * @return The web window that fired the event.
142      */

143     public WebWindow getWebWindow() {
144         return (WebWindow)getSource();
145     }
146
147
148     private boolean isEqual( final Object JavaDoc object1, final Object JavaDoc object2 ) {
149         final boolean result;
150
151         if( object1 == null && object2 == null ) {
152             result = true;
153         }
154         else if( object1 == null || object2 == null ) {
155             result = false;
156         }
157         else {
158             result = object1.equals(object2);
159         }
160
161         return result;
162     }
163
164
165     /**
166      * Return a string representation of this event
167      * @return A string representation of this event.
168      */

169     public String JavaDoc toString() {
170         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(80);
171         buffer.append("WebWindowEvent(source=[");
172         buffer.append(getSource());
173         buffer.append("] type=[");
174         switch( type_ ) {
175             case OPEN:
176                 buffer.append("OPEN");
177                 break;
178             case CLOSE:
179                 buffer.append("CLOSE");
180                 break;
181             case CHANGE:
182                 buffer.append("CHANGE");
183                 break;
184             default:
185                 buffer.append(type_);
186                 break;
187         }
188         buffer.append("] oldPage=[");
189         buffer.append(getOldPage());
190         buffer.append("] newPage=[");
191         buffer.append(getNewPage());
192         buffer.append("])");
193
194         return buffer.toString();
195     }
196
197     /** @return the event type */
198     public int getEventType() {
199         return type_;
200     }
201 }
202
Popular Tags