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 ProgressEvent}.
25   *
26   * @author Ahmed Ashour
27   * @author Ronald Brill
28   * @author Thorsten Wendelmuth
29   * @author Lai Quang Duong
30   */
31  @JsxClass
32  public class ProgressEvent extends Event {
33  
34      private boolean lengthComputable_;
35      private double loaded_;
36      private double total_;
37  
38      /**
39       * Default constructor.
40       */
41      public ProgressEvent() {
42          super();
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      @JsxConstructor
50      public void jsConstructor(final String type, final ScriptableObject details) {
51          super.jsConstructor(type, details);
52  
53          if (details != null && !JavaScriptEngine.isUndefined(details)) {
54              final Object lengthComputable = details.get("lengthComputable");
55              lengthComputable_ = JavaScriptEngine.toBoolean(lengthComputable);
56  
57              final Object loaded = details.get("loaded");
58              loaded_ = JavaScriptEngine.toNumber(loaded);
59  
60              final Object total = details.get("total");
61              total_ = JavaScriptEngine.toNumber(total);
62          }
63      }
64  
65      /**
66       * Creates a new event instance.
67       * @param target the event target
68       * @param type the event type
69       */
70      public ProgressEvent(final EventTarget target, final String type) {
71          this(target, type, false, 0d, 0d);
72      }
73  
74      /**
75       * Creates a new event instance.
76       * @param target the event target
77       * @param type the event type
78       * @param lengthComputable whether the total size is known
79       * @param loaded the number of bytes loaded
80       * @param total the total number of bytes
81       */
82      public ProgressEvent(final EventTarget target, final String type,
83              final boolean lengthComputable, final double loaded, final double total) {
84          super(target, type);
85          lengthComputable_ = lengthComputable;
86          loaded_ = loaded;
87          total_ = total;
88      }
89  
90      /**
91       * Returns the lengthComputable property from the event.
92       * @return the lengthComputable property from the event.
93       */
94      @JsxGetter
95      public boolean isLengthComputable() {
96          return lengthComputable_;
97      }
98  
99      /**
100      * Sets the lengthComputable information for this event.
101      *
102      * @param lengthComputable the lengthComputable information for this event
103      */
104     public void setLengthComputable(final boolean lengthComputable) {
105         lengthComputable_ = lengthComputable;
106     }
107 
108     /**
109      * Returns the loaded property from the event.
110      * @return the loaded property from the event.
111      */
112     @JsxGetter
113     public double getLoaded() {
114         return loaded_;
115     }
116 
117     /**
118      * Sets the loaded information for this event.
119      *
120      * @param loaded the loaded information for this event
121      */
122     public void setLoaded(final double loaded) {
123         loaded_ = loaded;
124     }
125 
126     /**
127      * Returns the total property from the event.
128      * @return the total property from the event.
129      */
130     @JsxGetter
131     public double getTotal() {
132         return total_;
133     }
134 
135     /**
136      * Sets the total information for this event.
137      *
138      * @param total the total information for this event
139      */
140     public void setTotal(final double total) {
141         total_ = total;
142     }
143 }