ListTransfertHandlerScan.java

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 2 1. initStringPaste : removed call to javax/swing/DefaultListModel::add → NO_COVERAGE
2. initStringPaste : Changed increment from 1 to -1 → NO_COVERAGE
            listModel.add(selectedIndexTo++, itemListScan);
42
        }
43 1 1. initStringPaste : replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandlerScan::initStringPaste → NO_COVERAGE
        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 1 1. initTransferable : replaced return value with "" for com/jsql/view/swing/list/ListTransfertHandlerScan::initTransferable → NO_COVERAGE
        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 2 1. parseStringDrop : negated conditional → NO_COVERAGE
2. parseStringDrop : negated conditional → NO_COVERAGE
        if (this.dragPaths != null && !this.dragPaths.isEmpty()) {  // DnD from list
70
            for (ItemList itemPath: this.dragPaths) {
71 1 1. parseStringDrop : negated conditional → NO_COVERAGE
                if (StringUtils.isNotEmpty(itemPath.toString())) {
72
                    ItemListScan itemDrag = (ItemListScan) itemPath;  //! FUUuu
73
                    var itemDrop = new ItemListScan(itemDrag.getBeanInjection());
74
                    listSelectedIndices.add(indexDropLocation);
75 2 1. parseStringDrop : removed call to javax/swing/DefaultListModel::add → NO_COVERAGE
2. parseStringDrop : Changed increment from 1 to -1 → NO_COVERAGE
                    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 2 1. parseStringDrop : Changed increment from 1 to -1 → NO_COVERAGE
2. parseStringDrop : removed call to javax/swing/DefaultListModel::add → NO_COVERAGE
                    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 1 1. parseStringDrop : Changed increment from 1 to -1 → NO_COVERAGE
            i++;
95
        }
96 1 1. parseStringDrop : removed call to com/jsql/view/swing/list/DnDList::setSelectedIndices → NO_COVERAGE
        list.setSelectedIndices(selectedIndices);
97
    }
98
    
99
    public static List<ItemListScan> parse(String clipboardText) {
100
        List<ItemListScan> itemsParsed = new ArrayList<>();
101
        try {
102 1 1. parse : removed call to com/jsql/view/swing/list/ListTransfertHandlerScan::parseJsonArray → NO_COVERAGE
            ListTransfertHandlerScan.parseJsonArray(clipboardText, itemsParsed);
103
        } catch (JSONException eJsonArray) {
104 1 1. parse : removed call to com/jsql/view/swing/list/ListTransfertHandlerScan::parseJsonObject → NO_COVERAGE
            ListTransfertHandlerScan.parseJsonObject(clipboardText, itemsParsed);
105
        }
106 1 1. parse : replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandlerScan::parse → NO_COVERAGE
        return itemsParsed;
107
    }
108
109
    private static void parseJsonArray(String clipboardText, List<ItemListScan> itemsParsed) {
110
        var itemsJsonArray = new JSONArray(clipboardText);
111 2 1. parseJsonArray : changed conditional boundary → NO_COVERAGE
2. parseJsonArray : negated conditional → NO_COVERAGE
        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
}

Mutations

41

1.1
Location : initStringPaste
Killed by : none
removed call to javax/swing/DefaultListModel::add → NO_COVERAGE

2.2
Location : initStringPaste
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

43

1.1
Location : initStringPaste
Killed by : none
replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandlerScan::initStringPaste → NO_COVERAGE

59

1.1
Location : initTransferable
Killed by : none
replaced return value with "" for com/jsql/view/swing/list/ListTransfertHandlerScan::initTransferable → NO_COVERAGE

69

1.1
Location : parseStringDrop
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : parseStringDrop
Killed by : none
negated conditional → NO_COVERAGE

71

1.1
Location : parseStringDrop
Killed by : none
negated conditional → NO_COVERAGE

75

1.1
Location : parseStringDrop
Killed by : none
removed call to javax/swing/DefaultListModel::add → NO_COVERAGE

2.2
Location : parseStringDrop
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

83

1.1
Location : parseStringDrop
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

2.2
Location : parseStringDrop
Killed by : none
removed call to javax/swing/DefaultListModel::add → NO_COVERAGE

94

1.1
Location : parseStringDrop
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

96

1.1
Location : parseStringDrop
Killed by : none
removed call to com/jsql/view/swing/list/DnDList::setSelectedIndices → NO_COVERAGE

102

1.1
Location : parse
Killed by : none
removed call to com/jsql/view/swing/list/ListTransfertHandlerScan::parseJsonArray → NO_COVERAGE

104

1.1
Location : parse
Killed by : none
removed call to com/jsql/view/swing/list/ListTransfertHandlerScan::parseJsonObject → NO_COVERAGE

106

1.1
Location : parse
Killed by : none
replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandlerScan::parse → NO_COVERAGE

111

1.1
Location : parseJsonArray
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : parseJsonArray
Killed by : none
negated conditional → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.1