1 /*
2 * Copyright (c) 2002-2025 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 */
25 public final class WebWindowEvent extends EventObject {
26 private final Page oldPage_;
27 private final Page newPage_;
28 private final int type_;
29
30 /** A window has opened. */
31 public static final int OPEN = 1;
32
33 /** A window has closed. */
34 public static final int CLOSE = 2;
35
36 /** The content of the window has changed. */
37 public static final int CHANGE = 3;
38
39 /**
40 * Creates an instance.
41 *
42 * @param webWindow the WebWindow that caused the event
43 * @param type the type - one of {@link #OPEN}, {@link #CLOSE} or {@link #CHANGE}
44 * @param oldPage the old contents of the web window
45 * @param newPage the new contents of the web window
46 */
47 public WebWindowEvent(
48 final WebWindow webWindow,
49 final int type,
50 final Page oldPage,
51 final Page newPage) {
52 super(webWindow);
53 oldPage_ = oldPage;
54 newPage_ = newPage;
55
56 switch (type) {
57 case OPEN:
58 case CLOSE:
59 case CHANGE:
60 type_ = type;
61 break;
62
63 default:
64 throw new IllegalArgumentException(
65 "type must be one of OPEN, CLOSE, CHANGE but got " + type);
66 }
67 }
68
69 /**
70 * Returns true if the two objects are equal.
71 *
72 * @param object the object to compare against
73 * @return true if the two objects are equal
74 */
75 @Override
76 public boolean equals(final Object object) {
77 if (null == object) {
78 return false;
79 }
80 if (getClass() == object.getClass()) {
81 final WebWindowEvent event = (WebWindowEvent) object;
82 return isEqual(getSource(), event.getSource())
83 && getEventType() == event.getEventType()
84 && isEqual(getOldPage(), event.getOldPage())
85 && isEqual(getNewPage(), event.getNewPage());
86 }
87 return false;
88 }
89
90 /**
91 * Returns the hash code for this object.
92 * @return the hash code for this object
93 */
94 @Override
95 public int hashCode() {
96 return source.hashCode();
97 }
98
99 /**
100 * Returns the oldPage.
101 * @return the page or null if the window has no page
102 */
103 public Page getOldPage() {
104 return oldPage_;
105 }
106
107 /**
108 * Returns the oldPage.
109 * @return the page or null if the window has no page
110 */
111 public Page getNewPage() {
112 return newPage_;
113 }
114
115 /**
116 * Returns the web window that fired the event.
117 * @return the web window that fired the event
118 */
119 public WebWindow getWebWindow() {
120 return (WebWindow) getSource();
121 }
122
123 private static boolean isEqual(final Object object1, final Object object2) {
124 final boolean result;
125
126 if (object1 == null && object2 == null) {
127 result = true;
128 }
129 else if (object1 == null || object2 == null) {
130 result = false;
131 }
132 else {
133 result = object1.equals(object2);
134 }
135
136 return result;
137 }
138
139 /**
140 * Returns a string representation of this event.
141 * @return a string representation of this event
142 */
143 @Override
144 public String toString() {
145 final StringBuilder builder = new StringBuilder(80)
146 .append("WebWindowEvent(source=[")
147 .append(getSource())
148 .append("] type=[");
149 switch (type_) {
150 case OPEN:
151 builder.append("OPEN");
152 break;
153 case CLOSE:
154 builder.append("CLOSE");
155 break;
156 case CHANGE:
157 builder.append("CHANGE");
158 break;
159 default:
160 builder.append(type_);
161 break;
162 }
163 builder.append("] oldPage=[")
164 .append(getOldPage())
165 .append("] newPage=[")
166 .append(getNewPage())
167 .append("])");
168
169 return builder.toString();
170 }
171
172 /**
173 * @return the event type
174 */
175 public int getEventType() {
176 return type_;
177 }
178 }