ListTransfertHandler.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
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
 * Handler for processing cut/copy/paste/drag/drop action on a JList items.
27
 */
28
public class ListTransfertHandler extends AbstractListTransfertHandler {
29
    
30
    /**
31
     * Log4j logger sent to view.
32
     */
33
    private static final Logger LOGGER = LogManager.getRootLogger();
34
35
    @Override
36
    protected String initializeTransferable() {
37
        
38
        var stringTransferable = new StringBuilder();
39
40
        for (ItemList itemPath: this.dragPaths) {
41
            stringTransferable.append(itemPath).append("\n");
42
        }
43
        
44 1 1. initializeTransferable : replaced return value with "" for com/jsql/view/swing/list/ListTransfertHandler::initializeTransferable → NO_COVERAGE
        return stringTransferable.toString();
45
    }
46
47
    @Override
48
    protected void parseStringDrop(TransferSupport support, DnDList list, DefaultListModel<ItemList> listModel) {
49
        
50
        var dropLocation = (JList.DropLocation) support.getDropLocation();
51
        int childIndex = dropLocation.getIndex();
52
53
        List<Integer> listSelectedIndices = new ArrayList<>();
54
55
        // DnD from list
56 2 1. parseStringDrop : negated conditional → NO_COVERAGE
2. parseStringDrop : negated conditional → NO_COVERAGE
        if (this.dragPaths != null && !this.dragPaths.isEmpty()) {
57 1 1. parseStringDrop : removed call to com/jsql/view/swing/list/ListTransfertHandler::addFromList → NO_COVERAGE
            this.addFromList(listModel, childIndex, listSelectedIndices);
58
        } else {
59 1 1. parseStringDrop : removed call to com/jsql/view/swing/list/ListTransfertHandler::addFromOutside → NO_COVERAGE
            this.addFromOutside(support, listModel, childIndex, listSelectedIndices);
60
        }
61
62
        var selectedIndices = new int[listSelectedIndices.size()];
63
        var i = 0;
64
        
65
        for (Integer integer: listSelectedIndices) {
66
            
67
            selectedIndices[i] = integer;
68 1 1. parseStringDrop : Changed increment from 1 to -1 → NO_COVERAGE
            i++;
69
        }
70
        
71 1 1. parseStringDrop : removed call to com/jsql/view/swing/list/DnDList::setSelectedIndices → NO_COVERAGE
        list.setSelectedIndices(selectedIndices);
72
    }
73
74
    private void addFromOutside(TransferSupport support, DefaultListModel<ItemList> listModel, int childIndexFrom, List<Integer> listSelectedIndices) {
75
        try {
76
            int childIndexTo = childIndexFrom;
77
            
78
            var importString = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
79
            
80
            for (String value: importString.split("\\n")) {
81 1 1. addFromOutside : negated conditional → NO_COVERAGE
                if (StringUtils.isNotEmpty(value)) {
82
                    
83
                    listSelectedIndices.add(childIndexTo);
84 2 1. addFromOutside : removed call to javax/swing/DefaultListModel::add → NO_COVERAGE
2. addFromOutside : Changed increment from 1 to -1 → NO_COVERAGE
                    listModel.add(childIndexTo++, new ItemList(value.replace("\\", "/")));
85
                }
86
            }
87
        } catch (UnsupportedFlavorException | IOException e) {
88
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
89
        }
90
    }
91
92
    private void addFromList(DefaultListModel<ItemList> listModel, int childIndexFrom, List<Integer> listSelectedIndices) {
93
        
94
        int childIndexTo = childIndexFrom;
95
        
96
        for (ItemList value: this.dragPaths) {
97 1 1. addFromList : negated conditional → NO_COVERAGE
            if (StringUtils.isNotEmpty(value.toString())) {
98
                
99
                //! FUUuu
100
                var newValue = new ItemList(value.toString().replace("\\", "/"));
101
                listSelectedIndices.add(childIndexTo);
102 2 1. addFromList : removed call to javax/swing/DefaultListModel::add → NO_COVERAGE
2. addFromList : Changed increment from 1 to -1 → NO_COVERAGE
                listModel.add(childIndexTo++, newValue);
103
            }
104
        }
105
    }
106
107
    @Override
108
    protected List<Integer> initializeStringPaste(String clipboardText, int selectedIndexFrom, DefaultListModel<ItemList> listModel) {
109
        
110
        int selectedIndexTo = selectedIndexFrom;
111
        List<Integer> selectedIndexes = new ArrayList<>();
112
113
        for (String line: clipboardText.split("\\n")) {
114 1 1. initializeStringPaste : negated conditional → NO_COVERAGE
            if (StringUtils.isNotEmpty(line)) {
115
                
116
                String newLine = line.replace("\\", "/");
117
                var newItem = new ItemList(newLine);
118
                selectedIndexes.add(selectedIndexTo);
119 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++, newItem);
120
            }
121
        }
122
        
123 1 1. initializeStringPaste : replaced return value with Collections.emptyList for com/jsql/view/swing/list/ListTransfertHandler::initializeStringPaste → NO_COVERAGE
        return selectedIndexes;
124
    }
125
}

Mutations

44

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

56

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

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

57

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

59

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

68

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

71

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

81

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

84

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

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

97

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

102

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

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

114

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

119

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

123

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

Active mutators

Tests examined


Report generated by PIT 1.16.1