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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
package com.vaadin.tests.dd;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.ContainerHierarchicalWrapper;
import com.vaadin.event.Action;
import com.vaadin.event.Action.Handler;
import com.vaadin.event.DataBoundTransferable;
import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.event.dd.acceptcriteria.Not;
import com.vaadin.event.dd.acceptcriteria.SourceIsTarget;
import com.vaadin.server.Resource;
import com.vaadin.server.StreamResource;
import com.vaadin.server.StreamVariable;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.StreamResource.StreamSource;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.tests.components.TestBase;
import com.vaadin.tests.util.TestUtils;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.AbsoluteLayout.ComponentPosition;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.DragAndDropWrapper;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Html5File;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Tree.TreeDragMode;
import com.vaadin.ui.Tree.TreeTargetDetails;
import com.vaadin.ui.Window;
public class DDTest6 extends TestBase {
java.util.Random r = new java.util.Random(1);
File[] files = new File[] { new Folder("Docs"), new Folder("Music"),
new Folder("Images"), new File("document.doc"),
new File("song.mp3"), new File("photo.jpg") };
private static Tree tree1;
private HorizontalSplitPanel sp;
private BeanItemContainer<File> fs1;
private static DDTest6 instance;
@Override
protected void setup() {
instance = this; // Note, test only works with single app per server if
// get()
// not converted to thread local
sp = new HorizontalSplitPanel();
sp.setSplitPosition(20);
CssLayout l = new CssLayout();
sp.setFirstComponent(l);
tree1 = new Tree("Volume 1");
tree1.setImmediate(true);
fs1 = new BeanItemContainer<File>(File.class);
tree1.setContainerDataSource(fs1);
for (int i = 0; i < files.length; i++) {
fs1.addBean(files[i]);
if (files[i] instanceof Folder) {
tree1.setChildrenAllowed(files[i], true);
} else {
tree1.setChildrenAllowed(files[i], false);
}
if (i >= files.length / 2) {
tree1.setParent(files[i], files[i - files.length / 2]);
}
}
tree1.setItemCaptionPropertyId("name");
tree1.setItemIconPropertyId("icon");
tree1.setDragMode(TreeDragMode.NODE);
DropHandler dropHandler = new DropHandler() {
@Override
public AcceptCriterion getAcceptCriterion() {
return AcceptAll.get();
}
@Override
public void drop(DragAndDropEvent dropEvent) {
File file = null;
Folder folder = null;
TreeTargetDetails dropTargetData = (TreeTargetDetails) dropEvent
.getTargetDetails();
folder = (Folder) dropTargetData.getItemIdInto();
if (dropEvent.getTransferable() instanceof DataBoundTransferable) {
DataBoundTransferable transferable = (DataBoundTransferable) dropEvent
.getTransferable();
file = (File) transferable.getItemId();
} else if (dropEvent.getTransferable().getSourceComponent() instanceof FileIcon) {
FileIcon draggedIcon = (FileIcon) dropEvent
.getTransferable().getSourceComponent();
file = draggedIcon.file;
}
setParent(file, folder);
}
};
tree1.setDropHandler(dropHandler);
Handler actionHandler = new Handler() {
private Action[] actions = new Action[] { new Action("Remove") };
@Override
public void handleAction(Action action, Object sender, Object target) {
ContainerHierarchicalWrapper containerDataSource = (ContainerHierarchicalWrapper) tree1
.getContainerDataSource();
containerDataSource.removeItemRecursively(target);
}
@Override
public Action[] getActions(Object target, Object sender) {
return actions;
}
};
tree1.addActionHandler(actionHandler);
tree1.addListener(new Property.ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
Object value = event.getProperty().getValue();
if (value != null && !(value instanceof Folder)) {
value = tree1.getParent(value);
}
FolderView folderView = FolderView.get((Folder) value);
sp.setSecondComponent(folderView);
folderView.reload();
}
});
l.addComponent(tree1);
sp.setSecondComponent(FolderView.get(null));
getLayout().setSizeFull();
getLayout().addComponent(sp);
TestUtils
.injectCSS(
getLayout().getUI(),
""
+ ".v-tree .v-icon {height:16px;} "
+ ".v-tree-node-caption-drag-top {/*border-top: none;*/} "
+ ".v-tree-node-caption-drag-bottom {border-bottom: none ;} "
+ ".v-tree-node-caption-drag-center {background-color: transparent;}"
+ ".v-tree-node-caption-dragfolder { background-color: cyan;} ");
}
private final static ThemeResource FOLDER = new ThemeResource(
"../runo/icons/64/folder.png");
private final static ThemeResource DOC = new ThemeResource(
"../runo/icons/64/document.png");
public static class File {
private Resource icon = DOC;
private String name;
private ByteArrayOutputStream bas;
private String type;
public File(String fileName) {
name = fileName;
}
public File(String fileName, ByteArrayOutputStream bas) {
this(fileName);
this.bas = bas;
}
public void setIcon(Resource icon) {
this.icon = icon;
}
public Resource getIcon() {
return icon;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public Resource getResource() {
StreamSource streamSource = new StreamSource() {
@Override
public InputStream getStream() {
if (bas != null) {
byte[] byteArray = bas.toByteArray();
return new ByteArrayInputStream(byteArray);
}
return null;
}
};
return new StreamResource(streamSource, getName());
}
}
public static class Folder extends File {
public Folder(String fileName) {
super(fileName);
setIcon(FOLDER);
}
}
@Override
protected String getDescription() {
return "dd: tree and web desktop tests. FF36 supports draggin files from client side. (try dragging png image + double click) TODO more files, auto-opening folders";
}
@Override
protected Integer getTicketNumber() {
return 119;
}
private void openFile(File file) {
// ATM supports only images.
if (file.getType().equals("image/png")) {
Embedded embedded = new Embedded(file.getName(), file.getResource());
Window w = new Window(file.getName());
w.addComponent(embedded);
w.getContent().setSizeUndefined();
getMainWindow().addWindow(w);
} else if (file.getType().equals("text/csv")) {
showSpreadsheet(file);
}
}
private void showSpreadsheet(File file) {
// ApplicationResource resource = (ApplicationResource)
// file.getResource();
String string = new String(file.bas.toByteArray());
String[] rows = string.split("\n");
String[] cols = rows[0].split(",");
Table table = new Table();
for (String string2 : cols) {
// String col =
string2.replaceAll("\"", ""); // remove surrounding ""
table.addContainerProperty(string2, String.class, "");
}
for (int i = 1; i < rows.length; i++) {
String[] split = rows[i].split(",");
table.addItem(split, "" + i);
}
Window w = new Window(file.getName());
w.getContent().setSizeUndefined();
table.setEditable(true);
w.addComponent(table);
getMainWindow().addWindow(w);
}
static class FolderView extends DragAndDropWrapper implements DropHandler {
static final HashMap<Folder, FolderView> views = new HashMap<Folder, FolderView>();
public static FolderView get(Folder f) {
FolderView folder2 = views.get(f);
if (folder2 == null) {
folder2 = new FolderView(f);
views.put(f, folder2);
}
return folder2;
}
private Folder folder;
private AbsoluteLayout l;
private int x;
private int y;
private FolderView(Folder f) {
super(new AbsoluteLayout());
l = (AbsoluteLayout) getCompositionRoot();
setSizeFull();
l.setSizeFull();
folder = f;
setDropHandler(this);
}
@Override
public void attach() {
reload();
super.attach();
}
@SuppressWarnings("static-access")
void reload() {
Collection<?> children = folder == null ? DDTest6.get().tree1
.rootItemIds() : DDTest6.get().tree1.getChildren(folder);
if (children == null) {
l.removeAllComponents();
return;
} else {
// make modifiable
children = new HashSet<Object>(children);
}
Set<Component> removed = new HashSet<Component>();
for (Iterator<Component> componentIterator = l
.getComponentIterator(); componentIterator.hasNext();) {
FileIcon next = (FileIcon) componentIterator.next();
if (!children.contains(next.file)) {
removed.add(next);
} else {
children.remove(next.file);
}
}
for (Component component : removed) {
l.removeComponent(component);
}
for (Object object : children) {
FileIcon fileIcon = new FileIcon((File) object);
l.addComponent(fileIcon);
ComponentPosition position = l.getPosition(fileIcon);
position.setTop(Float.valueOf((y++ / 5) % 5 * 100),
UNITS_PIXELS);
position.setLeft(Float.valueOf(x++ % 5 * 100), UNITS_PIXELS);
}
}
@Override
@SuppressWarnings("static-access")
public void drop(DragAndDropEvent dropEvent) {
if (dropEvent.getTransferable().getSourceComponent() instanceof FileIcon) {
// update the position
DragAndDropWrapper.WrapperTransferable transferable = (WrapperTransferable) dropEvent
.getTransferable();
MouseEventDetails mouseDownEvent = transferable
.getMouseDownEvent();
WrapperTargetDetails dropTargetDetails = (WrapperTargetDetails) dropEvent
.getTargetDetails();
MouseEventDetails mouseEvent = dropTargetDetails
.getMouseEvent();
int deltaX = mouseEvent.getClientX()
- mouseDownEvent.getClientX();
int deltaY = mouseEvent.getClientY()
- mouseDownEvent.getClientY();
ComponentPosition position = l.getPosition(transferable
.getSourceComponent());
position.setTop(position.getTopValue() + deltaY, UNITS_PIXELS);
position.setLeft(position.getLeftValue() + deltaX, UNITS_PIXELS);
} else if (dropEvent.getTransferable().getSourceComponent() == tree1) {
// dragged something from tree to the folder shown
File draggedFile = (File) ((DataBoundTransferable) dropEvent
.getTransferable()).getItemId();
DDTest6.get().setParent(draggedFile, folder);
} else {
// expecting this to be an html5 drag
WrapperTransferable tr = (WrapperTransferable) dropEvent
.getTransferable();
Html5File[] files2 = tr.getFiles();
if (files2 != null) {
for (Html5File html5File : files2) {
String fileName = html5File.getFileName();
// int bytes = html5File.getFileSize();
final ByteArrayOutputStream bas = new ByteArrayOutputStream();
StreamVariable streamVariable = new StreamVariable() {
@Override
public OutputStream getOutputStream() {
return bas;
}
@Override
public boolean listenProgress() {
return false;
}
@Override
public void onProgress(StreamingProgressEvent event) {
}
@Override
public void streamingStarted(
StreamingStartEvent event) {
}
@Override
public void streamingFinished(
StreamingEndEvent event) {
}
@Override
public void streamingFailed(
StreamingErrorEvent event) {
}
@Override
public boolean isInterrupted() {
return false;
}
};
html5File.setStreamVariable(streamVariable);
File file = new File(fileName, bas);
file.setType(html5File.getType());
// FF don't know csv
if (fileName.endsWith(".csv")) {
file.setType("text/csv");
}
DDTest6.get().fs1.addBean(file);
DDTest6.get().tree1.setChildrenAllowed(file, false);
DDTest6.get().setParent(file, folder);
}
}
}
}
@Override
public AcceptCriterion getAcceptCriterion() {
return AcceptAll.get();
}
}
static class FileIcon extends DragAndDropWrapper {
private final File file;
private CssLayout l;
public FileIcon(final File file) {
super(new CssLayout());
l = (CssLayout) getCompositionRoot();
setWidth(null);
l.setWidth(null);
setDragStartMode(DragStartMode.WRAPPER); // drag all contained
// components, not just the
// one on it started
this.file = file;
Resource icon2 = file.getIcon();
String name = file.getName();
l.addComponent(new Embedded(null, icon2));
l.addComponent(new Label(name));
l.addListener(new LayoutClickListener() {
@Override
@SuppressWarnings("static-access")
public void layoutClick(LayoutClickEvent event) {
if (event.isDoubleClick()) {
if (file instanceof Folder) {
get().tree1.setValue(file);
} else {
String type = file.getType();
if (canDisplay(type)) {
DDTest6.get().openFile(file);
}
}
}
}
String[] knownTypes = new String[] { "image/png", "text/csv" };
private boolean canDisplay(String type) {
if (type != null) {
for (String t : knownTypes) {
if (t.equals(type)) {
return true;
}
}
}
return false;
}
});
if (file instanceof Folder) {
setDropHandler(new DropHandler() {
@Override
public AcceptCriterion getAcceptCriterion() {
return new Not(SourceIsTarget.get());
}
@Override
public void drop(DragAndDropEvent dropEvent) {
File f = null;
if (dropEvent.getTransferable().getSourceComponent() instanceof FileIcon) {
FileIcon new_name = (FileIcon) dropEvent
.getTransferable().getSourceComponent();
f = new_name.file;
} else if (dropEvent.getTransferable()
.getSourceComponent() == tree1) {
f = (File) ((DataBoundTransferable) dropEvent
.getTransferable()).getItemId();
}
if (f != null) {
get().setParent(f, (Folder) FileIcon.this.file);
}
}
});
}
}
}
static DDTest6 get() {
return instance;
}
public void setParent(File file, Folder newParent) {
tree1.setParent(file, newParent);
if (sp.getSecondComponent() instanceof FolderView) {
FolderView view = (FolderView) sp.getSecondComponent();
view.reload();
}
}
}
|