-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathImageListDnDFrame.java
More file actions
156 lines (137 loc) · 4.29 KB
/
Copy pathImageListDnDFrame.java
File metadata and controls
156 lines (137 loc) · 4.29 KB
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
package dndImage;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.List;
import javax.imageio.*;
import javax.swing.*;
public class ImageListDnDFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 600;
private static final int DEFAULT_HEIGHT = 500;
private ImageList list1;
private ImageList list2;
public ImageListDnDFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
list1 = new ImageList(Paths.get(getClass().getPackage().getName(), "images1"));
list2 = new ImageList(Paths.get(getClass().getPackage().getName(), "images2"));
setLayout(new GridLayout(2, 1));
add(new JScrollPane(list1));
add(new JScrollPane(list2));
}
}
class ImageList extends JList<ImageIcon>
{
public ImageList(Path dir)
{
DefaultListModel<ImageIcon> model = new DefaultListModel<>();
try (DirectoryStream<Path> entries = Files.newDirectoryStream(dir))
{
for (Path entry : entries)
model.addElement(new ImageIcon(entry.toString()));
}
catch (IOException ex)
{
ex.printStackTrace();
}
setModel(model);
setVisibleRowCount(0);
setLayoutOrientation(JList.HORIZONTAL_WRAP);
setDragEnabled(true);
setDropMode(DropMode.ON_OR_INSERT);
setTransferHandler(new ImageListTransferHandler());
}
}
class ImageListTransferHandler extends TransferHandler
{
// support for drag
public int getSourceActions(JComponent source)
{
return COPY_OR_MOVE;
}
protected Transferable createTransferable(JComponent source)
{
ImageList list = (ImageList) source;
int index = list.getSelectedIndex();
if (index < 0) return null;
ImageIcon icon = list.getModel().getElementAt(index);
return new ImageTransferable(icon.getImage());
}
protected void exportDone(JComponent source, Transferable data, int action)
{
if (action == MOVE)
{
ImageList list = (ImageList) source;
int index = list.getSelectedIndex();
if (index < 0) return;
DefaultListModel<?> model = (DefaultListModel<?>) list.getModel();
model.remove(index);
}
}
// support for drop
public boolean canImport(TransferSupport support)
{
if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
{
if (support.getUserDropAction() == MOVE) support.setDropAction(COPY);
return true;
}
else return support.isDataFlavorSupported(DataFlavor.imageFlavor);
}
public boolean importData(TransferSupport support)
{
ImageList list = (ImageList) support.getComponent();
DefaultListModel<ImageIcon> model = (DefaultListModel<ImageIcon>) list.getModel();
Transferable transferable = support.getTransferable();
List<DataFlavor> flavors = Arrays.asList(transferable.getTransferDataFlavors());
List<Image> images = new ArrayList<>();
try
{
if (flavors.contains(DataFlavor.javaFileListFlavor))
{
@SuppressWarnings("unchecked") List<File> fileList
= (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
for (File f : fileList)
{
try
{
images.add(ImageIO.read(f));
}
catch (IOException ex)
{
// couldn't read image--skip
}
}
}
else if (flavors.contains(DataFlavor.imageFlavor))
{
images.add((Image) transferable.getTransferData(DataFlavor.imageFlavor));
}
int index;
if (support.isDrop())
{
JList.DropLocation location = (JList.DropLocation) support.getDropLocation();
index = location.getIndex();
if (!location.isInsert()) model.remove(index); // replace location
}
else index = model.size();
for (Image image : images)
{
model.add(index, new ImageIcon(image));
index++;
}
return true;
}
catch (IOException ex)
{
return false;
}
catch (UnsupportedFlavorException ex)
{
return false;
}
}
}