ManagerUpload.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.manager;
12
13
import com.jsql.model.exception.JSqlException;
14
import com.jsql.util.I18nUtil;
15
import com.jsql.util.LogLevelUtil;
16
import com.jsql.view.swing.list.DnDList;
17
import com.jsql.view.swing.list.ItemList;
18
import com.jsql.view.swing.manager.util.JButtonStateful;
19
import com.jsql.view.swing.scrollpane.LightScrollPane;
20
import com.jsql.view.swing.text.JPopupTextField;
21
import com.jsql.view.swing.ui.FlatButtonMouseAdapter;
22
import com.jsql.view.swing.util.I18nViewUtil;
23
import com.jsql.view.swing.util.MediatorHelper;
24
import com.jsql.view.swing.util.UiUtil;
25
import org.apache.logging.log4j.LogManager;
26
import org.apache.logging.log4j.Logger;
27
28
import javax.swing.*;
29
import java.awt.*;
30
import java.io.BufferedReader;
31
import java.io.IOException;
32
import java.io.InputStreamReader;
33
import java.nio.charset.StandardCharsets;
34
import java.util.ArrayList;
35
import java.util.List;
36
import java.util.Objects;
37
38
/**
39
 * Manager to upload files to the host.
40
 */
41
public class ManagerUpload extends AbstractManagerList {
42
    
43
    /**
44
     * Log4j logger sent to view.
45
     */
46
    private static final Logger LOGGER = LogManager.getRootLogger();
47
48
    /**
49
     * Build the manager panel.
50
     */
51
    public ManagerUpload() {
52
        
53 1 1. <init> : removed call to com/jsql/view/swing/manager/ManagerUpload::setLayout → NO_COVERAGE
        this.setLayout(new BorderLayout());
54
55
        this.defaultText = "UPLOAD_RUN_BUTTON_LABEL";
56
57
        List<ItemList> pathsList = new ArrayList<>();
58
        
59
        try (
60
            var inputStream = UiUtil.class.getClassLoader().getResourceAsStream(UiUtil.PATH_WEB_FOLDERS);
61
            var inputStreamReader = new InputStreamReader(Objects.requireNonNull(inputStream), StandardCharsets.UTF_8);
62
            var reader = new BufferedReader(inputStreamReader)
63
        ) {
64
            String line;
65
            
66 1 1. <init> : negated conditional → NO_COVERAGE
            while ((line = reader.readLine()) != null) {
67
                pathsList.add(new ItemList(line));
68
            }
69
        } catch (IOException e) {
70
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
71
        }
72
73
        this.listPaths = new DnDList(pathsList);
74
        
75 1 1. <init> : removed call to com/jsql/view/swing/list/DnDList::setBorder → NO_COVERAGE
        this.getListPaths().setBorder(BorderFactory.createEmptyBorder(0, 0, LightScrollPane.THUMB_SIZE, 0));
76 1 1. <init> : removed call to com/jsql/view/swing/manager/ManagerUpload::add → NO_COVERAGE
        this.add(new LightScrollPane(0, 0, 0, 0, this.getListPaths()), BorderLayout.CENTER);
77
        
78
        var southPanel = new JPanel();
79 1 1. <init> : removed call to javax/swing/JPanel::setLayout → NO_COVERAGE
        southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
80
81
        final JTextField shellURL = new JPopupTextField(I18nUtil.valueByKey("UPLOAD_URL_LABEL")).getProxy();
82
        String urlTooltip = I18nUtil.valueByKey("UPLOAD_URL_TOOLTIP");
83
        
84 1 1. <init> : removed call to javax/swing/JTextField::setToolTipText → NO_COVERAGE
        shellURL.setToolTipText(urlTooltip);
85 1 1. <init> : removed call to javax/swing/JTextField::setBorder → NO_COVERAGE
        shellURL.setBorder(
86
            BorderFactory.createCompoundBorder(
87
                BorderFactory.createCompoundBorder(
88
                    BorderFactory.createMatteBorder(0, 0, 0, 0, UiUtil.COLOR_COMPONENT_BORDER),
89
                    BorderFactory.createMatteBorder(1, 1, 0, 1, UiUtil.COLOR_DEFAULT_BACKGROUND)
90
                ),
91
                UiUtil.BORDER_BLU
92
            )
93
        );
94
95
        var lastLine = new JPanel();
96 1 1. <init> : removed call to javax/swing/JPanel::setLayout → NO_COVERAGE
        lastLine.setLayout(new BoxLayout(lastLine, BoxLayout.X_AXIS));
97 1 1. <init> : removed call to javax/swing/JPanel::setBorder → NO_COVERAGE
        lastLine.setBorder(
98
            BorderFactory.createCompoundBorder(
99
                BorderFactory.createMatteBorder(0, 0, 0, 0, UiUtil.COLOR_COMPONENT_BORDER),
100
                BorderFactory.createEmptyBorder(1, 0, 1, 1)
101
            )
102
        );
103
104 1 1. <init> : removed call to com/jsql/view/swing/manager/ManagerUpload::initializeRunButton → NO_COVERAGE
        this.initializeRunButton(shellURL);
105
106
        this.privilege = new JLabel(I18nUtil.valueByKey("PRIVILEGE_LABEL"), UiUtil.ICON_SQUARE_GREY, SwingConstants.LEFT);
107 1 1. <init> : removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE
        I18nViewUtil.addComponentForKey("PRIVILEGE_LABEL", this.privilege);
108 1 1. <init> : removed call to javax/swing/JLabel::setBorder → NO_COVERAGE
        this.privilege.setBorder(BorderFactory.createMatteBorder(2, 0, 0, 0, UiUtil.COLOR_DEFAULT_BACKGROUND));
109 1 1. <init> : removed call to javax/swing/JLabel::setToolTipText → NO_COVERAGE
        this.privilege.setToolTipText(I18nUtil.valueByKey("PRIVILEGE_TOOLTIP"));
110
111 1 1. <init> : removed call to javax/swing/JLabel::setVisible → NO_COVERAGE
        this.loader.setVisible(false);
112
113
        lastLine.add(this.privilege);
114
        lastLine.add(Box.createHorizontalGlue());
115
        lastLine.add(this.run);
116
117
        southPanel.add(shellURL);
118
        southPanel.add(lastLine);
119 1 1. <init> : removed call to com/jsql/view/swing/manager/ManagerUpload::add → NO_COVERAGE
        this.add(southPanel, BorderLayout.SOUTH);
120
    }
121
122
    private void initializeRunButton(final JTextField shellURL) {
123
        
124
        this.run = new JButtonStateful(this.defaultText);
125 1 1. initializeRunButton : removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE
        I18nViewUtil.addComponentForKey(this.defaultText, this.run);
126 1 1. initializeRunButton : removed call to com/jsql/view/swing/manager/util/JButtonStateful::setToolTipText → NO_COVERAGE
        this.run.setToolTipText(I18nUtil.valueByKey("UPLOAD_RUN_BUTTON_TOOLTIP"));
127 1 1. initializeRunButton : removed call to com/jsql/view/swing/manager/util/JButtonStateful::setEnabled → NO_COVERAGE
        this.run.setEnabled(false);
128
        
129 1 1. initializeRunButton : removed call to com/jsql/view/swing/manager/util/JButtonStateful::setContentAreaFilled → NO_COVERAGE
        this.run.setContentAreaFilled(false);
130 1 1. initializeRunButton : removed call to com/jsql/view/swing/manager/util/JButtonStateful::setBorder → NO_COVERAGE
        this.run.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
131 1 1. initializeRunButton : removed call to com/jsql/view/swing/manager/util/JButtonStateful::setBackground → NO_COVERAGE
        this.run.setBackground(UiUtil.COLOR_FOCUS_GAINED);
132
        
133 1 1. initializeRunButton : removed call to com/jsql/view/swing/manager/util/JButtonStateful::addMouseListener → NO_COVERAGE
        this.run.addMouseListener(new FlatButtonMouseAdapter(this.run));
134
        
135 2 1. lambda$initializeRunButton$0 : removed call to com/jsql/view/swing/manager/ManagerUpload::initializeRunAction → NO_COVERAGE
2. initializeRunButton : removed call to com/jsql/view/swing/manager/util/JButtonStateful::addActionListener → NO_COVERAGE
        this.run.addActionListener(actionEvent -> this.initializeRunAction(shellURL));
136
    }
137
138
    private void initializeRunAction(final JTextField shellURL) {
139
        
140 1 1. initializeRunAction : negated conditional → NO_COVERAGE
        if (ManagerUpload.this.getListPaths().getSelectedValuesList().isEmpty()) {
141
            
142
            LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Select directory(ies) to upload a file into");
143
            return;
144
        }
145
146
        final var filechooser = new JFileChooser(MediatorHelper.model().getMediatorUtils().getPreferencesUtil().getPathFile());
147 1 1. initializeRunAction : removed call to javax/swing/JFileChooser::setDialogTitle → NO_COVERAGE
        filechooser.setDialogTitle(I18nUtil.valueByKey("UPLOAD_DIALOG_TEXT"));
148
        
149
        // Fix #2402: NullPointerException on showOpenDialog()
150
        // Fix #40547: ClassCastException on showOpenDialog()
151
        try {
152
            int returnVal = filechooser.showOpenDialog(MediatorHelper.frame());
153
            
154 1 1. initializeRunAction : negated conditional → NO_COVERAGE
            if (returnVal != JFileChooser.APPROVE_OPTION) {
155
                return;
156
            }
157
                
158 1 1. initializeRunAction : removed call to com/jsql/view/swing/manager/ManagerUpload::uploadFiles → NO_COVERAGE
            this.uploadFiles(shellURL, filechooser);
159
            
160
        } catch (NullPointerException | ClassCastException e) {
161
            LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
162
        }
163
    }
164
165
    private void uploadFiles(final JTextField shellURL, final JFileChooser filechooser) {
166
        
167
        for (final Object path: ManagerUpload.this.getListPaths().getSelectedValuesList()) {
168
            
169
            new Thread(
170
                () -> {
171
                
172
                    var file = filechooser.getSelectedFile();
173
                    
174
                    try {
175 1 1. lambda$uploadFiles$1 : removed call to javax/swing/JLabel::setVisible → NO_COVERAGE
                        ManagerUpload.this.loader.setVisible(true);
176 1 1. lambda$uploadFiles$1 : removed call to com/jsql/model/accessible/ResourceAccess::uploadFile → NO_COVERAGE
                        MediatorHelper.model().getResourceAccess().uploadFile(path.toString(), shellURL.getText(), file);
177
                        
178
                    } catch (JSqlException e) {
179
                        LOGGER.log(
180
                            LogLevelUtil.CONSOLE_ERROR,
181
                            String.format("Payload creation error: %s", e.getMessage())
182
                        );
183
                    } catch (IOException e) {
184
                        LOGGER.log(
185
                            LogLevelUtil.CONSOLE_ERROR,
186
                            String.format("Posting file failed: %s", e.getMessage()),
187
                            e
188
                        );
189
                    } catch (InterruptedException e) {
190
191
                        LOGGER.log(LogLevelUtil.IGNORE, e, e);
192 1 1. lambda$uploadFiles$1 : removed call to java/lang/Thread::interrupt → NO_COVERAGE
                        Thread.currentThread().interrupt();
193
                    }
194
                },
195
                "ThreadUpload"
196 1 1. uploadFiles : removed call to java/lang/Thread::start → NO_COVERAGE
            ).start();
197
        }
198
    }
199
}

Mutations

53

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/manager/ManagerUpload::setLayout → NO_COVERAGE

66

1.1
Location : <init>
Killed by : none
negated conditional → NO_COVERAGE

75

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/list/DnDList::setBorder → NO_COVERAGE

76

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/manager/ManagerUpload::add → NO_COVERAGE

79

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JPanel::setLayout → NO_COVERAGE

84

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JTextField::setToolTipText → NO_COVERAGE

85

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JTextField::setBorder → NO_COVERAGE

96

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JPanel::setLayout → NO_COVERAGE

97

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JPanel::setBorder → NO_COVERAGE

104

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/manager/ManagerUpload::initializeRunButton → NO_COVERAGE

107

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE

108

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JLabel::setBorder → NO_COVERAGE

109

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JLabel::setToolTipText → NO_COVERAGE

111

1.1
Location : <init>
Killed by : none
removed call to javax/swing/JLabel::setVisible → NO_COVERAGE

119

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/manager/ManagerUpload::add → NO_COVERAGE

125

1.1
Location : initializeRunButton
Killed by : none
removed call to com/jsql/view/swing/util/I18nViewUtil::addComponentForKey → NO_COVERAGE

126

1.1
Location : initializeRunButton
Killed by : none
removed call to com/jsql/view/swing/manager/util/JButtonStateful::setToolTipText → NO_COVERAGE

127

1.1
Location : initializeRunButton
Killed by : none
removed call to com/jsql/view/swing/manager/util/JButtonStateful::setEnabled → NO_COVERAGE

129

1.1
Location : initializeRunButton
Killed by : none
removed call to com/jsql/view/swing/manager/util/JButtonStateful::setContentAreaFilled → NO_COVERAGE

130

1.1
Location : initializeRunButton
Killed by : none
removed call to com/jsql/view/swing/manager/util/JButtonStateful::setBorder → NO_COVERAGE

131

1.1
Location : initializeRunButton
Killed by : none
removed call to com/jsql/view/swing/manager/util/JButtonStateful::setBackground → NO_COVERAGE

133

1.1
Location : initializeRunButton
Killed by : none
removed call to com/jsql/view/swing/manager/util/JButtonStateful::addMouseListener → NO_COVERAGE

135

1.1
Location : lambda$initializeRunButton$0
Killed by : none
removed call to com/jsql/view/swing/manager/ManagerUpload::initializeRunAction → NO_COVERAGE

2.2
Location : initializeRunButton
Killed by : none
removed call to com/jsql/view/swing/manager/util/JButtonStateful::addActionListener → NO_COVERAGE

140

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

147

1.1
Location : initializeRunAction
Killed by : none
removed call to javax/swing/JFileChooser::setDialogTitle → NO_COVERAGE

154

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

158

1.1
Location : initializeRunAction
Killed by : none
removed call to com/jsql/view/swing/manager/ManagerUpload::uploadFiles → NO_COVERAGE

175

1.1
Location : lambda$uploadFiles$1
Killed by : none
removed call to javax/swing/JLabel::setVisible → NO_COVERAGE

176

1.1
Location : lambda$uploadFiles$1
Killed by : none
removed call to com/jsql/model/accessible/ResourceAccess::uploadFile → NO_COVERAGE

192

1.1
Location : lambda$uploadFiles$1
Killed by : none
removed call to java/lang/Thread::interrupt → NO_COVERAGE

196

1.1
Location : uploadFiles
Killed by : none
removed call to java/lang/Thread::start → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.16.1