View Javadoc
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.draganddrop;
16  
17  import org.htmlunit.WebWindow;
18  import org.htmlunit.corejs.javascript.Callable;
19  import org.htmlunit.corejs.javascript.Context;
20  import org.htmlunit.javascript.AbstractJavaScriptEngine;
21  import org.htmlunit.javascript.HtmlUnitScriptable;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  import org.htmlunit.javascript.PostponedAction;
24  import org.htmlunit.javascript.configuration.JsxClass;
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.file.File;
29  
30  /**
31   * A JavaScript object for {@code DataTransferItem}.
32   *
33   * @author Ahmed Ashour
34   * @author Ronald Brill
35   */
36  @JsxClass
37  public class DataTransferItem extends HtmlUnitScriptable {
38  
39      private static final String KIND_STRING = "string";
40      private static final String KIND_FILE = "file";
41  
42      private final String kind_;
43      private final String type_;
44      private Object data_;
45  
46      /**
47       * Ctor.
48       */
49      public DataTransferItem() {
50          this(null, null, null);
51      }
52  
53      /**
54       * Creates an instance.
55       */
56      private DataTransferItem(final String kind, final String type, final Object data) {
57          super();
58  
59          kind_ = kind;
60          type_ = type;
61          data_ = data;
62      }
63  
64      /**
65       * @param data the data
66       * @param type the type
67       * @return a new {@link DataTransferItem}
68       */
69      public static DataTransferItem buildStringItem(final CharSequence data, final String type) {
70          return new DataTransferItem(KIND_STRING, type, data);
71      }
72  
73      /**
74       * @param file the file
75       * @return a new {@link DataTransferItem}
76       */
77      public static DataTransferItem buildFileItem(final File file) {
78          return new DataTransferItem(KIND_FILE, file.getType(), file);
79      }
80  
81      /**
82       * JavaScript constructor.
83       */
84      @JsxConstructor
85      public void jsConstructor() {
86          // nothing to do
87      }
88  
89      /**
90       * @return the {@code type} property
91       */
92      @JsxGetter
93      public String getKind() {
94          return kind_;
95      }
96  
97      /**
98       * @return the {@code type} property
99       */
100     @JsxGetter
101     public String getType() {
102         return type_;
103     }
104 
105     /**
106      * Invokes the given callback with the drag data item's string data as the argument
107      * if the item's kind is a Plain unicode string (i.e. kind is string).
108      * @param callback Function to execute
109      */
110     @JsxFunction
111     public void getAsString(final Object callback) {
112         if (!(callback instanceof Callable)) {
113             throw JavaScriptEngine.typeError(
114                     "getAsString callback '" + JavaScriptEngine.toString(callback) + "' is not a function");
115         }
116 
117         if (isFile()) {
118             return;
119         }
120 
121         final Callable fun = (Callable) callback;
122         final Object[] args = {data_};
123 
124         final WebWindow webWindow = getWindow().getWebWindow();
125         final PostponedAction action = new PostponedAction(webWindow.getEnclosedPage(), "getAsString callback") {
126             @Override
127             public void execute() {
128                 fun.call(Context.getCurrentContext(), getParentScope(), DataTransferItem.this, args);
129             }
130         };
131 
132         final AbstractJavaScriptEngine<?> engine = webWindow.getWebClient().getJavaScriptEngine();
133         engine.addPostponedAction(action);
134     }
135 
136     /**
137      * @return the drag data item's File object. If the item is not a file, this method returns null.
138      */
139     @JsxFunction
140     public File getAsFile() {
141         if (!isFile()) {
142             return null;
143         }
144 
145         return (File) data_;
146     }
147 
148     /**
149      * @return true if this is a file
150      */
151     public boolean isFile() {
152         return kind_ == KIND_FILE;
153     }
154 }