View Javadoc
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.javascript.host.event;
16  
17  import org.htmlunit.corejs.javascript.ScriptableObject;
18  import org.htmlunit.javascript.JavaScriptEngine;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxConstructor;
21  import org.htmlunit.javascript.configuration.JsxGetter;
22  
23  /**
24   * A JavaScript object for {@code CloseEvent}.
25   *
26   * @author Ahmed Ashour
27   * @author Ronald Brill
28   */
29  @JsxClass
30  public class CloseEvent extends Event {
31  
32      private String reason_;
33      private int code_;
34      private boolean wasClean_;
35  
36      /**
37       * Creates a new event instance.
38       */
39      public CloseEvent() {
40          super();
41          setType(TYPE_CLOSE);
42          reason_ = "";
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public void eventCreated() {
50          super.eventCreated();
51          setType("");
52      }
53  
54      /**
55       * JavaScript constructor.
56       *
57       * @param type the event type
58       * @param details the event details (optional)
59       */
60      @Override
61      @JsxConstructor
62      public void jsConstructor(final String type, final ScriptableObject details) {
63          super.jsConstructor(type, details);
64  
65          if (details != null && !JavaScriptEngine.isUndefined(details)) {
66              code_ = JavaScriptEngine.toInt32(details.get("code"));
67              wasClean_ = JavaScriptEngine.toBoolean(details.get("wasClean"));
68  
69              final Object reason = details.get("reason");
70              if (!isNullMissingOrUndefined(reason)) {
71                  reason_ = JavaScriptEngine.toString(reason);
72              }
73  
74          }
75      }
76  
77      /**
78       * @return the code
79       */
80      @JsxGetter
81      public int getCode() {
82          return code_;
83      }
84  
85      /**
86       * @param code the code
87       */
88      public void setCode(final int code) {
89          code_ = code;
90      }
91  
92      /**
93       * @return the reason
94       */
95      @JsxGetter
96      public String getReason() {
97          return reason_;
98      }
99  
100     /**
101      * @param reason the reason
102      */
103     public void setReason(final String reason) {
104         reason_ = reason;
105     }
106 
107     /**
108      * @return the wasClean
109      */
110     @JsxGetter
111     public boolean isWasClean() {
112         return wasClean_;
113     }
114 
115     /**
116      * @param wasClean the wasClean
117      */
118     public void setWasClean(final boolean wasClean) {
119         wasClean_ = wasClean;
120     }
121 }