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 static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
18  import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
19  
20  import org.htmlunit.corejs.javascript.ScriptableObject;
21  import org.htmlunit.html.DomNode;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  import org.htmlunit.javascript.configuration.JsxClass;
24  import org.htmlunit.javascript.configuration.JsxConstant;
25  import org.htmlunit.javascript.configuration.JsxConstructor;
26  import org.htmlunit.javascript.configuration.JsxFunction;
27  import org.htmlunit.javascript.configuration.JsxGetter;
28  import org.htmlunit.javascript.host.Window;
29  
30  /**
31   * JavaScript object representing a UI event. For general information on which properties and functions should be
32   * supported, see <a href="http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-UIEvent">DOM Level 3 Events</a>.
33   *
34   * @author Daniel Gredler
35   * @author Ahmed Ashour
36   * @author Frank Danek
37   * @author Ronald Brill
38   */
39  @JsxClass
40  public class UIEvent extends Event {
41  
42      /** Constant. */
43      @JsxConstant({FF, FF_ESR})
44      public static final int SCROLL_PAGE_DOWN = 0x8000;
45  
46      /** Constant. */
47      @JsxConstant({FF, FF_ESR})
48      public static final int SCROLL_PAGE_UP = 0xFFFF8000;
49  
50      /** Specifies some detail information about the event. */
51      private long detail_;
52  
53      /** Specifies some view information about the event. */
54      private Object view_;
55      private static final Object NO_VIEW = new Object();
56  
57      /**
58       * Creates a new UI event instance.
59       */
60      public UIEvent() {
61          super();
62      }
63  
64      /**
65       * JavaScript constructor.
66       *
67       * @param type the event type
68       * @param details the event details (optional)
69       */
70      @JsxConstructor
71      @Override
72      public void jsConstructor(final String type, final ScriptableObject details) {
73          super.jsConstructor(type, details);
74  
75          view_ = NO_VIEW;
76          if (details != null && !JavaScriptEngine.isUndefined(details)) {
77              final Object view = details.get("view", details);
78              if (view instanceof Window) {
79                  view_ = view;
80              }
81              else if (NOT_FOUND != view) {
82                  throw JavaScriptEngine.typeError("View must be a window.");
83              }
84          }
85      }
86  
87      /**
88       * Creates a new UI event instance.
89       *
90       * @param domNode the DOM node that triggered the event
91       * @param type the event type
92       */
93      public UIEvent(final DomNode domNode, final String type) {
94          super(domNode, type);
95      }
96  
97      /**
98       * Creates a new event instance.
99       * @param target the event target
100      * @param type the event type
101      */
102     public UIEvent(final EventTarget target, final String type) {
103         super(target, type);
104     }
105 
106     /**
107      * Returns some detail information about the event, depending on the event type. For mouse events,
108      * the detail property indicates how many times the mouse has been clicked in the same location for
109      * this event.
110      *
111      * @return some detail information about the event, depending on the event type
112      */
113     @JsxGetter
114     public long getDetail() {
115         return detail_;
116     }
117 
118     /**
119      * Sets the detail information for this event.
120      *
121      * @param detail the detail information for this event
122      */
123     protected void setDetail(final long detail) {
124         detail_ = detail;
125     }
126 
127     /**
128      * Returns the view from which the event was generated. In browsers, this is the originating window.
129      *
130      * @return the view from which the event was generated
131      */
132     @JsxGetter
133     public Window getView() {
134         if (view_ == NO_VIEW) {
135             return null;
136         }
137         if (view_ != null) {
138             return (Window) view_;
139         }
140         return getWindow();
141     }
142 
143     /**
144      * Implementation of the DOM Level 3 Event method for initializing the UI event.
145      *
146      * @param type the event type
147      * @param bubbles can the event bubble
148      * @param cancelable can the event be canceled
149      * @param view the view to use for this event
150      * @param detail the detail to set for the event
151      */
152     @JsxFunction
153     public void initUIEvent(
154             final String type,
155             final boolean bubbles,
156             final boolean cancelable,
157             final Object view,
158             final int detail) {
159         initEvent(type, bubbles, cancelable);
160         // Ignore the view parameter; we always use the window.
161         setDetail(detail);
162     }
163 
164     /**
165      * @return a number that indicates which button was pressed on the mouse,
166      *         or the numeric keyCode or the character code (charCode) of the key pressed on the keyboard
167      */
168     @JsxGetter
169     public int getWhich() {
170         return 0;
171     }
172 }