1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
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 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit;
16
17 import java.util.EventObject;
18
19 /**
20 * An event that will be fired when a WebWindow changes.
21 *
22 * @author Mike Bowler
23 * @author David K. Taylor
24 * @author Ronald Brill
25 */
26 public final class WebWindowEvent extends EventObject {
27 private final Page oldPage_;
28 private final Page newPage_;
29 private final int type_;
30
31 /** A window has opened. */
32 public static final int OPEN = 1;
33
34 /** A window has closed. */
35 public static final int CLOSE = 2;
36
37 /** The content of the window has changed. */
38 public static final int CHANGE = 3;
39
40 /**
41 * Creates an instance.
42 *
43 * @param webWindow the WebWindow that caused the event
44 * @param type the type - one of {@link #OPEN}, {@link #CLOSE} or {@link #CHANGE}
45 * @param oldPage the old contents of the web window
46 * @param newPage the new contents of the web window
47 */
48 public WebWindowEvent(
49 final WebWindow webWindow,
50 final int type,
51 final Page oldPage,
52 final Page newPage) {
53 super(webWindow);
54 oldPage_ = oldPage;
55 newPage_ = newPage;
56
57 switch (type) {
58 case OPEN:
59 case CLOSE:
60 case CHANGE:
61 type_ = type;
62 break;
63
64 default:
65 throw new IllegalArgumentException(
66 "type must be one of OPEN, CLOSE, CHANGE but got " + type);
67 }
68 }
69
70 /**
71 * Returns true if the two objects are equal.
72 *
73 * @param object the object to compare against
74 * @return true if the two objects are equal
75 */
76 @Override
77 public boolean equals(final Object object) {
78 if (null == object) {
79 return false;
80 }
81 if (getClass() == object.getClass()) {
82 final WebWindowEvent event = (WebWindowEvent) object;
83 return isEqual(getSource(), event.getSource())
84 && getEventType() == event.getEventType()
85 && isEqual(getOldPage(), event.getOldPage())
86 && isEqual(getNewPage(), event.getNewPage());
87 }
88 return false;
89 }
90
91 /**
92 * Returns the hash code for this object.
93 * @return the hash code for this object
94 */
95 @Override
96 public int hashCode() {
97 return source.hashCode();
98 }
99
100 /**
101 * Returns the oldPage.
102 * @return the page or null if the window has no page
103 */
104 public Page getOldPage() {
105 return oldPage_;
106 }
107
108 /**
109 * Returns the oldPage.
110 * @return the page or null if the window has no page
111 */
112 public Page getNewPage() {
113 return newPage_;
114 }
115
116 /**
117 * Returns the web window that fired the event.
118 * @return the web window that fired the event
119 */
120 public WebWindow getWebWindow() {
121 return (WebWindow) getSource();
122 }
123
124 private static boolean isEqual(final Object object1, final Object object2) {
125 final boolean result;
126
127 if (object1 == null && object2 == null) {
128 result = true;
129 }
130 else if (object1 == null || object2 == null) {
131 result = false;
132 }
133 else {
134 result = object1.equals(object2);
135 }
136
137 return result;
138 }
139
140 /**
141 * Returns a string representation of this event.
142 * @return a string representation of this event
143 */
144 @Override
145 public String toString() {
146 final StringBuilder builder = new StringBuilder(80)
147 .append("WebWindowEvent(source=[")
148 .append(getSource())
149 .append("] type=[");
150 switch (type_) {
151 case OPEN:
152 builder.append("OPEN");
153 break;
154 case CLOSE:
155 builder.append("CLOSE");
156 break;
157 case CHANGE:
158 builder.append("CHANGE");
159 break;
160 default:
161 builder.append(type_);
162 break;
163 }
164 builder.append("] oldPage=[")
165 .append(getOldPage())
166 .append("] newPage=[")
167 .append(getNewPage())
168 .append("])");
169
170 return builder.toString();
171 }
172
173 /**
174 * @return the event type
175 */
176 public int getEventType() {
177 return type_;
178 }
179 }