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.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 */
30 @JsxClass
31 public class ProgressEvent extends Event {
32
33 private boolean lengthComputable_;
34 private Object loaded_ = Long.valueOf(0L);
35 private long total_;
36
37 /**
38 * Default constructor.
39 */
40 public ProgressEvent() {
41 super();
42 }
43
44 /**
45 * {@inheritDoc}
46 */
47 @Override
48 @JsxConstructor
49 public void jsConstructor(final String type, final ScriptableObject details) {
50 super.jsConstructor(type, details);
51
52 if (details != null && !JavaScriptEngine.isUndefined(details)) {
53 final Object lengthComputable = details.get("lengthComputable");
54 if (lengthComputable instanceof Boolean) {
55 lengthComputable_ = (Boolean) lengthComputable;
56 }
57 else {
58 lengthComputable_ = Boolean.parseBoolean(lengthComputable.toString());
59 }
60
61 final Object loaded = details.get("loaded");
62 if (loaded instanceof Long) {
63 loaded_ = loaded;
64 }
65 else if (loaded instanceof Double) {
66 loaded_ = ((Double) loaded).longValue();
67 }
68 else {
69 try {
70 loaded_ = Long.parseLong(loaded.toString());
71 }
72 catch (final NumberFormatException ignored) {
73 // ignore
74 }
75 }
76
77 final Object total = details.get("total");
78 if (total instanceof Long) {
79 total_ = (Long) total;
80 }
81 else if (total instanceof Double) {
82 total_ = ((Double) total).longValue();
83 }
84 else {
85 try {
86 total_ = Long.parseLong(details.get("total").toString());
87 }
88 catch (final NumberFormatException ignored) {
89 // ignore
90 }
91 }
92 }
93 }
94
95 /**
96 * Creates a new event instance.
97 * @param target the event target
98 * @param type the event type
99 */
100 public ProgressEvent(final EventTarget target, final String type) {
101 super(target, type);
102 }
103
104 /**
105 * Returns the lengthComputable property from the event.
106 * @return the lengthComputable property from the event.
107 */
108 @JsxGetter
109 public boolean isLengthComputable() {
110 return lengthComputable_;
111 }
112
113 /**
114 * Sets the lengthComputable information for this event.
115 *
116 * @param lengthComputable the lengthComputable information for this event
117 */
118 public void setLengthComputable(final boolean lengthComputable) {
119 lengthComputable_ = lengthComputable;
120 }
121
122 /**
123 * Returns the loaded property from the event.
124 * @return the loaded property from the event.
125 */
126 @JsxGetter
127 public Object getLoaded() {
128 return loaded_;
129 }
130
131 /**
132 * Sets the loaded information for this event.
133 *
134 * @param loaded the loaded information for this event
135 */
136 public void setLoaded(final Object loaded) {
137 loaded_ = loaded;
138 }
139
140 /**
141 * Returns the total property from the event.
142 * @return the total property from the event.
143 */
144 @JsxGetter
145 public long getTotal() {
146 return total_;
147 }
148
149 /**
150 * Sets the total information for this event.
151 *
152 * @param total the total information for this event
153 */
154 public void setTotal(final long total) {
155 total_ = total;
156 }
157 }