ListTransfertHandlerScan.java

1
/*******************************************************************************
2
 * Copyhacked (H) 2012-2020.
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 about 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> initializeStringPaste(String clipboardText, int selectedIndexFrom, DefaultListModel<ItemList> listModel) {
40
        
41
        int selectedIndexTo = selectedIndexFrom;
42
        List<Integer> selectedIndexes = new ArrayList<>();
43
44
        for (ItemListScan itemListScan: ListTransfertHandlerScan.parse(clipboardText)) {
45
46
            selectedIndexes.add(selectedIndexTo);
47 2 1. initializeStringPaste : Changed increment from 1 to -1 → NO_COVERAGE
2. initializeStringPaste : removed call to javax/swing/DefaultListModel::add → NO_COVERAGE
            listModel.add(selectedIndexTo++, itemListScan);
48
        }
49
        
50 1 1. initializeStringPaste : replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandlerScan::initializeStringPaste → NO_COVERAGE
        return selectedIndexes;
51
    }
52
53
    @Override
54
    protected String initializeTransferable() {
55
        
56
        List<JSONObject> jsons = new ArrayList<>();
57
        var stringTransferable = new StringBuilder();
58
        
59
        try {
60
            for (ItemList itemPath: this.dragPaths) {
61
                
62
                ItemListScan itemScanPath = (ItemListScan) itemPath;
63
                jsons.add(new JSONObject(itemScanPath.getBeanInjectionToJSON()));
64
            }
65
            
66
            stringTransferable.append(new JSONArray(jsons).toString(4));
67
            
68
        } catch (JSONException e) {
69
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e.getMessage(), e);
70
        }
71
        
72 1 1. initializeTransferable : replaced return value with "" for com/jsql/view/swing/list/ListTransfertHandlerScan::initializeTransferable → NO_COVERAGE
        return stringTransferable.toString();
73
    }
74
75
    @Override
76
    protected void parseStringDrop(TransferSupport support, DnDList list, DefaultListModel<ItemList> listModel) {
77
        
78
        var dropLocation = (JList.DropLocation) support.getDropLocation();
79
        int indexDropLocation = dropLocation.getIndex();
80
81
        List<Integer> listSelectedIndices = new ArrayList<>();
82
83
        // DnD from list
84 2 1. parseStringDrop : negated conditional → NO_COVERAGE
2. parseStringDrop : negated conditional → NO_COVERAGE
        if (this.dragPaths != null && !this.dragPaths.isEmpty()) {
85
            for (ItemList itemPath: this.dragPaths) {
86 1 1. parseStringDrop : negated conditional → NO_COVERAGE
                if (StringUtils.isNotEmpty(itemPath.toString())) {
87
                    
88
                    //! FUUuu
89
                    ItemListScan itemDrag = (ItemListScan) itemPath;
90
                    var itemDrop = new ItemListScan(itemDrag.getBeanInjection());
91
                    listSelectedIndices.add(indexDropLocation);
92 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);
93
                }
94
            }
95
        } else {  // DnD from outside
96
            try {
97
                var importString = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
98
                
99
                for (ItemListScan itemListScan: ListTransfertHandlerScan.parse(importString)) {
100
                    
101
                    listSelectedIndices.add(indexDropLocation);
102 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);
103
                }
104
            } catch (UnsupportedFlavorException | IOException e) {
105
                LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
106
            }
107
        }
108
109
        var selectedIndices = new int[listSelectedIndices.size()];
110
        var i = 0;
111
        
112
        for (Integer integer: listSelectedIndices) {
113
            
114
            selectedIndices[i] = integer;
115 1 1. parseStringDrop : Changed increment from 1 to -1 → NO_COVERAGE
            i++;
116
        }
117
        
118 1 1. parseStringDrop : removed call to com/jsql/view/swing/list/DnDList::setSelectedIndices → NO_COVERAGE
        list.setSelectedIndices(selectedIndices);
119
    }
120
    
121
    public static List<ItemListScan> parse(String clipboardText) {
122
        
123
        List<ItemListScan> itemsParsed = new ArrayList<>();
124
        
125
        try {
126 1 1. parse : removed call to com/jsql/view/swing/list/ListTransfertHandlerScan::parseJsonArray → NO_COVERAGE
            parseJsonArray(clipboardText, itemsParsed);
127
        } catch (JSONException eJsonArray) {
128 1 1. parse : removed call to com/jsql/view/swing/list/ListTransfertHandlerScan::parseJsonObject → NO_COVERAGE
            parseJsonObject(clipboardText, itemsParsed);
129
        }
130
        
131 1 1. parse : replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandlerScan::parse → NO_COVERAGE
        return itemsParsed;
132
    }
133
134
    private static void parseJsonArray(String clipboardText, List<ItemListScan> itemsParsed) {
135
        
136
        var itemsJsonArray = new JSONArray(clipboardText);
137
        
138 2 1. parseJsonArray : changed conditional boundary → NO_COVERAGE
2. parseJsonArray : negated conditional → NO_COVERAGE
        for (var i = 0; i < itemsJsonArray.length(); i++) {
139
            
140
            var itemJsonObject = itemsJsonArray.getJSONObject(i);
141
            
142
            var beanInjection = new BeanInjection(
143
                itemJsonObject.optString("url", StringUtils.EMPTY),
144
                itemJsonObject.optString("request", StringUtils.EMPTY),
145
                itemJsonObject.optString("header", StringUtils.EMPTY),
146
                itemJsonObject.optString("method", StringUtils.EMPTY),
147
                itemJsonObject.optString("vendor", StringUtils.EMPTY),
148
                itemJsonObject.optString("requestType", StringUtils.EMPTY)
149
            );
150
            
151
            var newItem = new ItemListScan(beanInjection);
152
            itemsParsed.add(newItem);
153
        }
154
    }
155
156
    private static void parseJsonObject(String clipboardText, List<ItemListScan> itemsParsed) {
157
        try {
158
            var itemsJsonObject = new JSONObject(clipboardText);
159
            
160
            var beanInjection = new BeanInjection(
161
                itemsJsonObject.optString("url", StringUtils.EMPTY),
162
                itemsJsonObject.optString("request", StringUtils.EMPTY),
163
                itemsJsonObject.optString("header", StringUtils.EMPTY),
164
                itemsJsonObject.optString("method", StringUtils.EMPTY),
165
                itemsJsonObject.optString("vendor", StringUtils.EMPTY),
166
                itemsJsonObject.optString("requestType", StringUtils.EMPTY)
167
            );
168
            
169
            var newItem = new ItemListScan(beanInjection);
170
            itemsParsed.add(newItem);
171
            
172
        } catch (JSONException e) {
173
            for (String url: clipboardText.split("\\n")) {
174
                
175
                var beanInjection = new BeanInjection(url);
176
                
177
                var newItem = new ItemListScan(beanInjection);
178
                itemsParsed.add(newItem);
179
            }
180
        }
181
    }
182
}

Mutations

47

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

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

50

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

72

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

84

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

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

86

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

92

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

102

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

115

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

118

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

126

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

128

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

131

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

138

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.16.1