View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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    * http://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 com.gargoylesoftware.htmlunit.javascript.host.event;
16  
17  import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.EVENT_ONCLOSE_INIT_CLOSE_EVENT_THROWS;
18  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
19  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
20  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF;
21  import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE;
22  
23  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
24  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
25  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction;
26  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter;
27  
28  import net.sourceforge.htmlunit.corejs.javascript.Context;
29  import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
30  import net.sourceforge.htmlunit.corejs.javascript.Undefined;
31  
32  /**
33   * A JavaScript object for {@code CloseEvent}.
34   *
35   * @author Ahmed Ashour
36   * @author Ronald Brill
37   */
38  @JsxClass
39  public class CloseEvent extends Event {
40  
41      private String reason_;
42      private int code_;
43      private boolean wasClean_;
44  
45      /**
46       * Creates a new event instance.
47       */
48      public CloseEvent() {
49          setType(TYPE_CLOSE);
50          reason_ = "";
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public void eventCreated() {
58          super.eventCreated();
59          setType("");
60      }
61  
62      /**
63       * JavaScript constructor.
64       *
65       * @param type the event type
66       * @param details the event details (optional)
67       */
68      @Override
69      @JsxConstructor({CHROME, FF, EDGE})
70      public void jsConstructor(final String type, final ScriptableObject details) {
71          super.jsConstructor(type, details);
72  
73          int code = 0;
74          String reason = "";
75          boolean wasClean = false;
76  
77          if (details != null && !Undefined.instance.equals(details)) {
78              final Double detailCode = (Double) details.get("code");
79              if (detailCode != null) {
80                  code = detailCode.intValue();
81              }
82  
83              final String detailReason = (String) details.get("reason");
84              if (detailReason != null) {
85                  reason = detailReason;
86              }
87  
88              final Boolean detailWasClean = (Boolean) details.get("wasClean");
89              if (detailWasClean != null) {
90                  wasClean = detailWasClean.booleanValue();
91              }
92          }
93          code_ = code;
94          reason_ = reason;
95          wasClean_ = wasClean;
96      }
97  
98      /**
99       * Initializes this close event.
100      * @param type the event type
101      * @param bubbles whether or not the event should bubble
102      * @param cancelable whether or not the event the event should be cancelable
103      * @param wasClean the wasClean flag
104      * @param reasonCode the reason code
105      * @param reason the reason
106      */
107     @JsxFunction({FF, IE})
108     public void initCloseEvent(final String type, final boolean bubbles, final boolean cancelable,
109             final boolean wasClean, final int reasonCode, final String reason) {
110         if (getBrowserVersion().hasFeature(EVENT_ONCLOSE_INIT_CLOSE_EVENT_THROWS)) {
111             Context.throwAsScriptRuntimeEx(new IllegalArgumentException("Illegal call to initCloseEvent()"));
112         }
113         super.initEvent(type, bubbles, cancelable);
114         wasClean_ = wasClean;
115         code_ = reasonCode;
116         reason_ = reason;
117     }
118 
119     /**
120      * @return the code
121      */
122     @JsxGetter
123     public int getCode() {
124         return code_;
125     }
126 
127     /**
128      * @param code the code
129      */
130     public void setCode(final int code) {
131         code_ = code;
132     }
133 
134     /**
135      * @return the reason
136      */
137     @JsxGetter
138     public String getReason() {
139         return reason_;
140     }
141 
142     /**
143      * @param reason the reason
144      */
145     public void setReason(final String reason) {
146         reason_ = reason;
147     }
148 
149     /**
150      * @return the wasClean
151      */
152     @JsxGetter
153     public boolean isWasClean() {
154         return wasClean_;
155     }
156 
157     /**
158      * @param wasClean the wasClean
159      */
160     public void setWasClean(final boolean wasClean) {
161         wasClean_ = wasClean;
162     }
163 }