1
2
3
4
5
6
7
8
9
10
11 package com.jsql.view.swing.list;
12
13 import com.jsql.util.LogLevelUtil;
14 import org.apache.commons.lang3.StringUtils;
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17
18 import javax.swing.*;
19 import java.awt.datatransfer.DataFlavor;
20 import java.awt.datatransfer.UnsupportedFlavorException;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25
26
27
28 public class ListTransfertHandler extends AbstractListTransfertHandler {
29
30 private static final Logger LOGGER = LogManager.getRootLogger();
31
32 @Override
33 protected String initTransferable() {
34 var stringTransferable = new StringBuilder();
35 for (ItemList itemPath: this.dragPaths) {
36 stringTransferable.append(itemPath).append("\n");
37 }
38 return stringTransferable.toString();
39 }
40
41 @Override
42 protected void parseStringDrop(TransferSupport support, DnDList list, DefaultListModel<ItemList> listModel) {
43 var dropLocation = (JList.DropLocation) support.getDropLocation();
44 int childIndex = dropLocation.getIndex();
45 List<Integer> listSelectedIndices = new ArrayList<>();
46
47
48 if (this.dragPaths != null && !this.dragPaths.isEmpty()) {
49 this.addFromList(listModel, childIndex, listSelectedIndices);
50 } else {
51 this.addFromOutside(support, listModel, childIndex, listSelectedIndices);
52 }
53
54 var selectedIndices = new int[listSelectedIndices.size()];
55 var i = 0;
56 for (Integer integer: listSelectedIndices) {
57 selectedIndices[i] = integer;
58 i++;
59 }
60 list.setSelectedIndices(selectedIndices);
61 }
62
63 private void addFromOutside(TransferSupport support, DefaultListModel<ItemList> listModel, int childIndexFrom, List<Integer> listSelectedIndices) {
64 try {
65 int childIndexTo = childIndexFrom;
66 var importString = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
67 for (String value: importString.split("\\n")) {
68 if (StringUtils.isNotEmpty(value)) {
69 listSelectedIndices.add(childIndexTo);
70 listModel.add(childIndexTo++, new ItemList(value.replace("\\", "/")));
71 }
72 }
73 } catch (UnsupportedFlavorException | IOException e) {
74 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
75 }
76 }
77
78 private void addFromList(DefaultListModel<ItemList> listModel, int childIndexFrom, List<Integer> listSelectedIndices) {
79 int childIndexTo = childIndexFrom;
80 for (ItemList value: this.dragPaths) {
81 if (StringUtils.isNotEmpty(value.toString())) {
82 var newValue = new ItemList(value.toString().replace("\\", "/"));
83 listSelectedIndices.add(childIndexTo);
84 listModel.add(childIndexTo++, newValue);
85 }
86 }
87 }
88
89 @Override
90 protected List<Integer> initStringPaste(String clipboardText, int selectedIndexFrom, DefaultListModel<ItemList> listModel) {
91 int selectedIndexTo = selectedIndexFrom;
92 List<Integer> selectedIndexes = new ArrayList<>();
93 for (String line: clipboardText.split("\\n")) {
94 if (StringUtils.isNotEmpty(line)) {
95 String newLine = line.replace("\\", "/");
96 var newItem = new ItemList(newLine);
97 selectedIndexes.add(selectedIndexTo);
98 listModel.add(selectedIndexTo++, newItem);
99 }
100 }
101 return selectedIndexes;
102 }
103 }