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 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 public static DataTransferItem buildStringItem(final CharSequence data, final String type) {
70 return new DataTransferItem(KIND_STRING, type, data);
71 }
72
73
74
75
76
77 public static DataTransferItem buildFileItem(final File file) {
78 return new DataTransferItem(KIND_FILE, file.getType(), file);
79 }
80
81
82
83
84 @JsxConstructor
85 public void jsConstructor() {
86
87 }
88
89
90
91
92 @JsxGetter
93 public String getKind() {
94 return kind_;
95 }
96
97
98
99
100 @JsxGetter
101 public String getType() {
102 return type_;
103 }
104
105
106
107
108
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
138
139 @JsxFunction
140 public File getAsFile() {
141 if (!isFile()) {
142 return null;
143 }
144
145 return (File) data_;
146 }
147
148
149
150
151 public boolean isFile() {
152 return kind_ == KIND_FILE;
153 }
154 }