View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
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  import org.json.JSONArray;
18  import org.json.JSONException;
19  import org.json.JSONObject;
20  
21  import javax.swing.*;
22  import java.awt.datatransfer.DataFlavor;
23  import java.awt.datatransfer.UnsupportedFlavorException;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * Handler for processing cut/copy/paste/drag/drop action on a JList items.
30   */
31  public class ListTransfertHandlerScan extends AbstractListTransfertHandler {
32      
33      private static final Logger LOGGER = LogManager.getRootLogger();
34  
35      @Override
36      protected List<Integer> initStringPaste(String clipboardText, int selectedIndexFrom, DefaultListModel<ItemList> listModel) {
37          int selectedIndexTo = selectedIndexFrom;
38          List<Integer> selectedIndexes = new ArrayList<>();
39          for (ItemListScan itemListScan: ListTransfertHandlerScan.parse(clipboardText)) {
40              selectedIndexes.add(selectedIndexTo);
41              listModel.add(selectedIndexTo++, itemListScan);
42          }
43          return selectedIndexes;
44      }
45  
46      @Override
47      protected String initTransferable() {
48          List<JSONObject> jsons = new ArrayList<>();
49          var stringTransferable = new StringBuilder();
50          try {
51              for (ItemList itemPath: this.dragPaths) {
52                  ItemListScan itemScanPath = (ItemListScan) itemPath;
53                  jsons.add(new JSONObject(itemScanPath.getBeanInjectionToJSON()));
54              }
55              stringTransferable.append(new JSONArray(jsons).toString(4));
56          } catch (JSONException e) {
57              LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e.getMessage(), e);
58          }
59          return stringTransferable.toString();
60      }
61  
62      @Override
63      protected void parseStringDrop(TransferSupport support, DnDList list, DefaultListModel<ItemList> listModel) {
64          var dropLocation = (JList.DropLocation) support.getDropLocation();
65          int indexDropLocation = dropLocation.getIndex();
66  
67          List<Integer> listSelectedIndices = new ArrayList<>();
68  
69          if (this.dragPaths != null && !this.dragPaths.isEmpty()) {  // DnD from list
70              for (ItemList itemPath: this.dragPaths) {
71                  if (StringUtils.isNotEmpty(itemPath.toString())) {
72                      ItemListScan itemDrag = (ItemListScan) itemPath;  //! FUUuu
73                      var itemDrop = new ItemListScan(itemDrag.getBeanInjection());
74                      listSelectedIndices.add(indexDropLocation);
75                      listModel.add(indexDropLocation++, itemDrop);
76                  }
77              }
78          } else {  // DnD from outside
79              try {
80                  var importString = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
81                  for (ItemListScan itemListScan: ListTransfertHandlerScan.parse(importString)) {
82                      listSelectedIndices.add(indexDropLocation);
83                      listModel.add(indexDropLocation++, itemListScan);
84                  }
85              } catch (UnsupportedFlavorException | IOException e) {
86                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
87              }
88          }
89  
90          var selectedIndices = new int[listSelectedIndices.size()];
91          var i = 0;
92          for (Integer integer: listSelectedIndices) {
93              selectedIndices[i] = integer;
94              i++;
95          }
96          list.setSelectedIndices(selectedIndices);
97      }
98      
99      public static List<ItemListScan> parse(String clipboardText) {
100         List<ItemListScan> itemsParsed = new ArrayList<>();
101         try {
102             ListTransfertHandlerScan.parseJsonArray(clipboardText, itemsParsed);
103         } catch (JSONException eJsonArray) {
104             ListTransfertHandlerScan.parseJsonObject(clipboardText, itemsParsed);
105         }
106         return itemsParsed;
107     }
108 
109     private static void parseJsonArray(String clipboardText, List<ItemListScan> itemsParsed) {
110         var itemsJsonArray = new JSONArray(clipboardText);
111         for (var i = 0; i < itemsJsonArray.length(); i++) {
112             var newItem = new ItemListScan(itemsJsonArray.getJSONObject(i));
113             itemsParsed.add(newItem);
114         }
115     }
116 
117     private static void parseJsonObject(String clipboardText, List<ItemListScan> itemsParsed) {
118         try {
119             var newItem = new ItemListScan(new JSONObject(clipboardText));
120             itemsParsed.add(newItem);
121         } catch (JSONException e) {
122             for (String url: clipboardText.split("\\n")) {
123                 var beanInjection = new BeanInjection(url);
124                 var newItem = new ItemListScan(beanInjection);
125                 itemsParsed.add(newItem);
126             }
127         }
128     }
129 }