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
    /**
34
     * Log4j logger sent to view.
35
     */
36
    private static final Logger LOGGER = LogManager.getRootLogger();
37
38
    @Override
39
    protected List<Integer> initStringPaste(String clipboardText, int selectedIndexFrom, DefaultListModel<ItemList> listModel) {
40
        int selectedIndexTo = selectedIndexFrom;
41
        List<Integer> selectedIndexes = new ArrayList<>();
42
        for (ItemListScan itemListScan: ListTransfertHandlerScan.parse(clipboardText)) {
43
            selectedIndexes.add(selectedIndexTo);
44 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);
45
        }
46 1 1. initStringPaste : replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandlerScan::initStringPaste → NO_COVERAGE
        return selectedIndexes;
47
    }
48
49
    @Override
50
    protected String initTransferable() {
51
        List<JSONObject> jsons = new ArrayList<>();
52
        var stringTransferable = new StringBuilder();
53
        try {
54
            for (ItemList itemPath: this.dragPaths) {
55
                ItemListScan itemScanPath = (ItemListScan) itemPath;
56
                jsons.add(new JSONObject(itemScanPath.getBeanInjectionToJSON()));
57
            }
58
            stringTransferable.append(new JSONArray(jsons).toString(4));
59
        } catch (JSONException e) {
60
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e.getMessage(), e);
61
        }
62 1 1. initTransferable : replaced return value with "" for com/jsql/view/swing/list/ListTransfertHandlerScan::initTransferable → NO_COVERAGE
        return stringTransferable.toString();
63
    }
64
65
    @Override
66
    protected void parseStringDrop(TransferSupport support, DnDList list, DefaultListModel<ItemList> listModel) {
67
        var dropLocation = (JList.DropLocation) support.getDropLocation();
68
        int indexDropLocation = dropLocation.getIndex();
69
70
        List<Integer> listSelectedIndices = new ArrayList<>();
71
72 2 1. parseStringDrop : negated conditional → NO_COVERAGE
2. parseStringDrop : negated conditional → NO_COVERAGE
        if (this.dragPaths != null && !this.dragPaths.isEmpty()) {  // DnD from list
73
            for (ItemList itemPath: this.dragPaths) {
74 1 1. parseStringDrop : negated conditional → NO_COVERAGE
                if (StringUtils.isNotEmpty(itemPath.toString())) {
75
                    ItemListScan itemDrag = (ItemListScan) itemPath;  //! FUUuu
76
                    var itemDrop = new ItemListScan(itemDrag.getBeanInjection());
77
                    listSelectedIndices.add(indexDropLocation);
78 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);
79
                }
80
            }
81
        } else {  // DnD from outside
82
            try {
83
                var importString = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
84
                for (ItemListScan itemListScan: ListTransfertHandlerScan.parse(importString)) {
85
                    listSelectedIndices.add(indexDropLocation);
86 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);
87
                }
88
            } catch (UnsupportedFlavorException | IOException e) {
89
                LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
90
            }
91
        }
92
93
        var selectedIndices = new int[listSelectedIndices.size()];
94
        var i = 0;
95
        for (Integer integer: listSelectedIndices) {
96
            selectedIndices[i] = integer;
97 1 1. parseStringDrop : Changed increment from 1 to -1 → NO_COVERAGE
            i++;
98
        }
99 1 1. parseStringDrop : removed call to com/jsql/view/swing/list/DnDList::setSelectedIndices → NO_COVERAGE
        list.setSelectedIndices(selectedIndices);
100
    }
101
    
102
    public static List<ItemListScan> parse(String clipboardText) {
103
        List<ItemListScan> itemsParsed = new ArrayList<>();
104
        try {
105 1 1. parse : removed call to com/jsql/view/swing/list/ListTransfertHandlerScan::parseJsonArray → NO_COVERAGE
            ListTransfertHandlerScan.parseJsonArray(clipboardText, itemsParsed);
106
        } catch (JSONException eJsonArray) {
107 1 1. parse : removed call to com/jsql/view/swing/list/ListTransfertHandlerScan::parseJsonObject → NO_COVERAGE
            ListTransfertHandlerScan.parseJsonObject(clipboardText, itemsParsed);
108
        }
109 1 1. parse : replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandlerScan::parse → NO_COVERAGE
        return itemsParsed;
110
    }
111
112
    private static void parseJsonArray(String clipboardText, List<ItemListScan> itemsParsed) {
113
        var itemsJsonArray = new JSONArray(clipboardText);
114 2 1. parseJsonArray : changed conditional boundary → NO_COVERAGE
2. parseJsonArray : negated conditional → NO_COVERAGE
        for (var i = 0; i < itemsJsonArray.length(); i++) {
115
            var newItem = new ItemListScan(itemsJsonArray.getJSONObject(i));
116
            itemsParsed.add(newItem);
117
        }
118
    }
119
120
    private static void parseJsonObject(String clipboardText, List<ItemListScan> itemsParsed) {
121
        try {
122
            var newItem = new ItemListScan(new JSONObject(clipboardText));
123
            itemsParsed.add(newItem);
124
        } catch (JSONException e) {
125
            for (String url: clipboardText.split("\\n")) {
126
                var beanInjection = new BeanInjection(url);
127
                var newItem = new ItemListScan(beanInjection);
128
                itemsParsed.add(newItem);
129
            }
130
        }
131
    }
132
}

Mutations

44

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

46

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

62

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

72

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

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

74

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

78

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

86

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

97

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

99

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

105

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

107

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

109

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

114

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