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.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 final 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       * Creates a string data transfer item.
66       *
67       * @param data the data
68       * @param type the type
69       * @return a new {@link DataTransferItem}
70       */
71      public static DataTransferItem buildStringItem(final CharSequence data, final String type) {
72          return new DataTransferItem(KIND_STRING, type, data);
73      }
74  
75      /**
76       * Creates a file data transfer item.
77       *
78       * @param file the file
79       * @return a new {@link DataTransferItem}
80       */
81      public static DataTransferItem buildFileItem(final File file) {
82          return new DataTransferItem(KIND_FILE, file.getType(), file);
83      }
84  
85      /**
86       * JavaScript constructor.
87       */
88      @JsxConstructor
89      public void jsConstructor() {
90          // nothing to do
91      }
92  
93      /**
94       * Returns the {@code type} property.
95       *
96       * @return the {@code type} property
97       */
98      @JsxGetter
99      public String getKind() {
100         return kind_;
101     }
102 
103     /**
104      * Returns the {@code type} property.
105      *
106      * @return the {@code type} property
107      */
108     @JsxGetter
109     public String getType() {
110         return type_;
111     }
112 
113     /**
114      * Invokes the given callback with the drag data item's string data if the item contains a plain Unicode string.
115      *
116      * @param callback Function to execute
117      */
118     @JsxFunction
119     public void getAsString(final Object callback) {
120         if (!(callback instanceof Callable fun)) {
121             throw JavaScriptEngine.typeError(
122                     "getAsString callback '" + JavaScriptEngine.toString(callback) + "' is not a function");
123         }
124 
125         if (isFile()) {
126             return;
127         }
128 
129         final Object[] args = {data_};
130 
131         final WebWindow webWindow = getWindow().getWebWindow();
132         final PostponedAction action = new PostponedAction(webWindow.getEnclosedPage(), "getAsString callback") {
133             @Override
134             public void execute() {
135                 fun.call(Context.getCurrentContext(), getParentScope(), DataTransferItem.this, args);
136             }
137         };
138 
139         final AbstractJavaScriptEngine<?> engine = webWindow.getWebClient().getJavaScriptEngine();
140         engine.addPostponedAction(action);
141     }
142 
143     /**
144      * Returns the file represented by this drag data item.
145      *
146      * @return the drag data item's {@link File}, or {@code null} if this item is not a file
147      */
148     @JsxFunction
149     public File getAsFile() {
150         if (!isFile()) {
151             return null;
152         }
153 
154         return (File) data_;
155     }
156 
157     /**
158      * Returns whether this item represents a file.
159      *
160      * @return {@code true} if this item represents a file
161      */
162     public boolean isFile() {
163         return kind_ == KIND_FILE;
164     }
165 }