1
2
3
4
5
6
7
8
9
10
11
12
13
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
32
33
34
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
48
49 public DataTransferItem() {
50 this(null, null, null);
51 }
52
53
54
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
66
67
68
69
70
71 public static DataTransferItem buildStringItem(final CharSequence data, final String type) {
72 return new DataTransferItem(KIND_STRING, type, data);
73 }
74
75
76
77
78
79
80
81 public static DataTransferItem buildFileItem(final File file) {
82 return new DataTransferItem(KIND_FILE, file.getType(), file);
83 }
84
85
86
87
88 @JsxConstructor
89 public void jsConstructor() {
90
91 }
92
93
94
95
96
97
98 @JsxGetter
99 public String getKind() {
100 return kind_;
101 }
102
103
104
105
106
107
108 @JsxGetter
109 public String getType() {
110 return type_;
111 }
112
113
114
115
116
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
145
146
147
148 @JsxFunction
149 public File getAsFile() {
150 if (!isFile()) {
151 return null;
152 }
153
154 return (File) data_;
155 }
156
157
158
159
160
161
162 public boolean isFile() {
163 return kind_ == KIND_FILE;
164 }
165 }