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