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

Mutations

38

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

48

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

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

49

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

51

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

58

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

60

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

68

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

70

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

81

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

84

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

94

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

98

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

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

101

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

Active mutators

Tests examined


Report generated by PIT 1.19.1