/
DNDDemo.java
82 lines (66 loc) · 2.49 KB
/
DNDDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.codename1.demos.dnd;
import com.codename1.components.SpanLabel;
import com.codename1.datatransfer.DropTarget;
import com.codename1.io.FileSystemStorage;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Image;
import com.codename1.ui.Toolbar;
import com.codename1.ui.layouts.BoxLayout;
import java.io.IOException;
/**
* This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose
* of building native mobile applications using Java.
*/
public class DNDDemo {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Drag and Drop Demo");
if (DropTarget.isSupported()) {
DropTarget dnd = DropTarget.create((evt)->{
String srcFile = (String)evt.getSource();
System.out.println("Src file is "+srcFile);
System.out.println("Location: "+evt.getX()+", "+evt.getY());
if (srcFile != null) {
try {
Image img = Image.createImage(FileSystemStorage.getInstance().openInputStream(srcFile));
hi.add(img);
hi.revalidate();
} catch (IOException ex) {
Log.e(ex);
}
}
}, Display.GALLERY_IMAGE);
}
hi.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
hi.setScrollableY(true);
hi.addComponent(new SpanLabel("Drag photos from your desktop into this app"));
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = Display.getInstance().getCurrent();
}
}
public void destroy() {
}
}