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 org.htmlunit.javascript.HtmlUnitScriptable;
18
19 /**
20 * A window opened in JavaScript via either <code>window.showModalDialog</code>
21 * or <code>window.showModelessDialog</code>.
22 * @author Daniel Gredler
23 * @author Ronald Brill
24 */
25 public class DialogWindow extends WebWindowImpl {
26
27 /** The arguments object exposed via the <code>dialogArguments</code> JavaScript property. */
28 private final Object arguments_;
29
30 /**
31 * Creates a new instance.
32 * @param webClient the web client that "owns" this window
33 * @param arguments the arguments object exposed via the <code>dialogArguments</code> JavaScript property
34 */
35 protected DialogWindow(final WebClient webClient, final Object arguments) {
36 super(webClient);
37 arguments_ = arguments;
38 performRegistration();
39 }
40
41 /**
42 * {@inheritDoc}
43 */
44 @Override
45 protected boolean isJavaScriptInitializationNeeded(final Page page) {
46 return getScriptableObject() == null;
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 @Override
53 public WebWindow getParentWindow() {
54 return this;
55 }
56
57 /**
58 * {@inheritDoc}
59 */
60 @Override
61 public WebWindow getTopWindow() {
62 return this;
63 }
64
65 /**
66 * {@inheritDoc}
67 */
68 @Override
69 public <T extends HtmlUnitScriptable> void setScriptableObject(final T scriptObject) {
70 if (scriptObject != null) {
71 scriptObject.put("dialogArguments", scriptObject, arguments_);
72 }
73 super.setScriptableObject(scriptObject);
74 }
75
76 /**
77 * Closes this window.
78 */
79 public void close() {
80 getJobManager().shutdown();
81 destroyChildren();
82 getWebClient().deregisterWebWindow(this);
83 }
84
85 /**
86 * Returns a string representation of this object.
87 * @return a string representation of this object
88 */
89 @Override
90 public String toString() {
91 return "DialogWindow[name=\"" + getName() + "\"]";
92 }
93 }