DialogTranslate.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.dialog;
12
13
import com.jsql.util.GitUtil.ShowOnConsole;
14
import com.jsql.util.I18nUtil;
15
import com.jsql.util.LogLevelUtil;
16
import com.jsql.view.swing.dialog.translate.Language;
17
import com.jsql.view.swing.dialog.translate.WorkerTranslateInto;
18
import com.jsql.view.swing.popupmenu.JPopupMenuText;
19
import com.jsql.view.swing.text.JPopupTextArea;
20
import com.jsql.view.swing.text.JTextAreaPlaceholder;
21
import com.jsql.view.swing.util.I18nViewUtil;
22
import com.jsql.view.swing.util.MediatorHelper;
23
import com.jsql.view.swing.util.UiUtil;
24
import org.apache.commons.lang3.StringUtils;
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.awt.event.*;
31
import java.util.Locale;
32
import java.util.ResourceBundle;
33
34
/**
35
 * A dialog displaying current locale translation percentage.
36
 */
37
public class DialogTranslate extends JDialog {
38
    
39
    private static final Logger LOGGER = LogManager.getRootLogger();
40
41
    /**
42
     * Button receiving focus.
43
     */
44
    private final JButton buttonSend = new JButton();  // "Send" text displayed in target locale at runtime
45
46
    private Language languageInto;
47
    private final JLabel labelTranslation = new JLabel();
48
    private final JTextArea textToTranslate = new JPopupTextArea(new JTextAreaPlaceholder(I18nViewUtil.valueByKey("TRANSLATION_PLACEHOLDER"))).getProxy();
49
    private final JProgressBar progressBarTranslation = new JProgressBar();
50
    private String textBeforeChange = StringUtils.EMPTY;
51
    private JPanel lastLine;
52
53
    /**
54
     * Displays dialog into target locale.
55
     * Switching language does not affect this dialog
56
     */
57
    public DialogTranslate() {
58
        super(MediatorHelper.frame(), Dialog.ModalityType.MODELESS);
59 1 1. <init> : removed call to com/jsql/view/swing/dialog/DialogTranslate::setDefaultCloseOperation → NO_COVERAGE
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
60 1 1. <init> : removed call to com/jsql/view/swing/dialog/DialogTranslate::setIconImages → NO_COVERAGE
        this.setIconImages(UiUtil.getIcons());  // Define a small and large app icon
61
62 1 1. lambda$new$0 : removed call to com/jsql/view/swing/dialog/DialogTranslate::dispose → NO_COVERAGE
        ActionListener escapeListener = actionEvent -> this.dispose();  // Action for ESCAPE key
63 1 1. <init> : removed call to javax/swing/JRootPane::registerKeyboardAction → NO_COVERAGE
        this.getRootPane().registerKeyboardAction(
64
            escapeListener,
65
            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
66
            JComponent.WHEN_IN_FOCUSED_WINDOW
67
        );
68
69 1 1. <init> : removed call to com/jsql/view/swing/dialog/DialogTranslate::initLastLine → NO_COVERAGE
        this.initLastLine();
70
71 1 1. <init> : removed call to javax/swing/JLabel::setBorder → NO_COVERAGE
        this.labelTranslation.setBorder(UiUtil.BORDER_5PX);
72
        var contentPane = this.getContentPane();
73 1 1. <init> : removed call to java/awt/Container::add → NO_COVERAGE
        contentPane.add(this.labelTranslation, BorderLayout.NORTH);
74 1 1. <init> : removed call to java/awt/Container::add → NO_COVERAGE
        contentPane.add(this.lastLine, BorderLayout.SOUTH);
75
76 1 1. <init> : removed call to com/jsql/view/swing/dialog/DialogTranslate::initTextToTranslate → NO_COVERAGE
        this.initTextToTranslate();
77
78 1 1. <init> : removed call to java/awt/Container::add → NO_COVERAGE
        contentPane.add(new JScrollPane(this.textToTranslate), BorderLayout.CENTER);
79
    }
80
81
    /**
82
     * Set back default setting for About frame.
83
     */
84
    public final void initDialog(final Language language) {
85 1 1. initDialog : removed call to javax/swing/JProgressBar::setValue → NO_COVERAGE
        this.progressBarTranslation.setValue(0);
86 1 1. initDialog : removed call to javax/swing/JProgressBar::setString → NO_COVERAGE
        this.progressBarTranslation.setString("Loading...");
87
        this.languageInto = language;
88
89
        var bundleInto = ResourceBundle.getBundle(I18nUtil.BASE_NAME, Locale.forLanguageTag(language.getLanguageTag()));
90
        var localeInto = Locale.forLanguageTag(language.getLanguageTag());
91 1 1. initDialog : removed call to javax/swing/JLabel::setText → NO_COVERAGE
        this.labelTranslation.setText(  // set language into
92
            String.format(
93
                bundleInto.getString("TRANSLATION_TEXT"),
94
                localeInto.getDisplayLanguage(localeInto),
95
                localeInto.getDisplayLanguage(localeInto)
96
            )
97
        );
98
        ComponentOrientation orientation = ComponentOrientation.getOrientation(Locale.forLanguageTag(language.getLanguageTag()));
99 1 1. initDialog : removed call to javax/swing/JLabel::setComponentOrientation → NO_COVERAGE
        this.labelTranslation.setComponentOrientation(orientation);
100 1 1. initDialog : removed call to javax/swing/JProgressBar::setComponentOrientation → NO_COVERAGE
        this.progressBarTranslation.setComponentOrientation(orientation);
101 1 1. initDialog : removed call to javax/swing/JPanel::setComponentOrientation → NO_COVERAGE
        this.lastLine.setComponentOrientation(orientation);
102 1 1. initDialog : removed call to javax/swing/JButton::setText → NO_COVERAGE
        this.buttonSend.setText(  // display text at runtime
103
            ResourceBundle.getBundle(
104
                I18nUtil.BASE_NAME,
105
                Locale.forLanguageTag(language.getLanguageTag())
106
            ).getString("TRANSLATION_SEND")
107
        );
108
109 1 1. initDialog : removed call to javax/swing/JTextArea::setText → NO_COVERAGE
        this.textToTranslate.setText(null);
110 1 1. initDialog : removed call to javax/swing/JTextArea::setEditable → NO_COVERAGE
        this.textToTranslate.setEditable(false);
111 1 1. initDialog : removed call to javax/swing/JButton::setEnabled → NO_COVERAGE
        this.buttonSend.setEnabled(false);  // will be enabled when done with GitHub
112
        
113
        // Ubuntu Regular is compatible with all required languages, this includes Chinese and Arabic,
114
        // but it's not a technical Mono Font.
115
        // Only Monospaced works both for copy/paste utf8 foreign characters in JTextArea, and
116
        // it's a technical Mono Font.
117 1 1. initDialog : removed call to javax/swing/JTextArea::setFont → NO_COVERAGE
        this.textToTranslate.setFont(new Font(
118
            UiUtil.FONT_NAME_MONOSPACED,
119
            Font.PLAIN,
120
            UIManager.getDefaults().getFont("TextField.font").getSize()
121
        ));
122
        
123 1 1. initDialog : removed call to com/jsql/view/swing/dialog/translate/WorkerTranslateInto::execute → NO_COVERAGE
        new WorkerTranslateInto(this).execute();
124
125 1 1. initDialog : removed call to com/jsql/view/swing/dialog/DialogTranslate::setIconImage → NO_COVERAGE
        this.setIconImage(language.getFlag().getImage());
126 1 1. initDialog : removed call to com/jsql/view/swing/dialog/DialogTranslate::setTitle → NO_COVERAGE
        this.setTitle(bundleInto.getString("TRANSLATION_TITLE") +" "+ localeInto.getDisplayLanguage(localeInto));
127 1 1. initDialog : negated conditional → NO_COVERAGE
        if (!this.isVisible()) {  // Center the dialog
128 1 1. initDialog : removed call to com/jsql/view/swing/dialog/DialogTranslate::setSize → NO_COVERAGE
            this.setSize(640, 460);
129 1 1. initDialog : removed call to com/jsql/view/swing/dialog/DialogTranslate::setLocationRelativeTo → NO_COVERAGE
            this.setLocationRelativeTo(MediatorHelper.frame());
130 1 1. initDialog : removed call to javax/swing/JRootPane::setDefaultButton → NO_COVERAGE
            this.getRootPane().setDefaultButton(this.getButtonSend());
131
        }
132 1 1. initDialog : removed call to com/jsql/view/swing/dialog/DialogTranslate::setVisible → NO_COVERAGE
        this.setVisible(true);
133
    }
134
135
    private void initLastLine() {
136
        this.lastLine = new JPanel();
137 1 1. initLastLine : removed call to javax/swing/JPanel::setLayout → NO_COVERAGE
        this.lastLine.setLayout(new BoxLayout(this.lastLine, BoxLayout.LINE_AXIS));
138 1 1. initLastLine : removed call to javax/swing/JPanel::setBorder → NO_COVERAGE
        this.lastLine.setBorder(UiUtil.BORDER_5PX);
139
        
140 1 1. initLastLine : removed call to javax/swing/JButton::setToolTipText → NO_COVERAGE
        this.buttonSend.setToolTipText(
141
            String.join(
142
                StringUtils.EMPTY,
143
                "<html>",
144
                "<b>Send your translation to the developer</b><br>",
145
                "Your translation will be integrated in the next version of jSQL",
146
                "</html>"
147
            )
148
        );
149
        
150 1 1. initLastLine : removed call to javax/swing/JButton::addActionListener → NO_COVERAGE
        this.buttonSend.addActionListener(actionEvent -> {
151 1 1. lambda$initLastLine$1 : negated conditional → NO_COVERAGE
            if (this.textToTranslate.getText().equals(this.textBeforeChange)) {
152
                LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Nothing changed, translate a piece of text then click on Send");
153
                return;
154
            }
155
            
156
            // Escape Markdown character # for h1 in .properties
157
            String clientDescription = this.textToTranslate.getText()
158
                .replace("\\\\", "\\\\\\\\")
159
                .replaceAll("(?m)^#","\\\\#")
160
                .replace("<", "\\<");
161
              
162 1 1. lambda$initLastLine$1 : removed call to com/jsql/util/GitUtil::sendReport → NO_COVERAGE
            MediatorHelper.model().getMediatorUtils().getGitUtil().sendReport(
163
                clientDescription,
164
                ShowOnConsole.YES,
165
                this.languageInto +" translation"
166
            );
167 1 1. lambda$initLastLine$1 : removed call to com/jsql/view/swing/dialog/DialogTranslate::setVisible → NO_COVERAGE
            this.setVisible(false);
168
        });
169
170 1 1. initLastLine : removed call to com/jsql/view/swing/dialog/DialogTranslate::setLayout → NO_COVERAGE
        this.setLayout(new BorderLayout());
171
        
172 1 1. initLastLine : removed call to javax/swing/JProgressBar::setStringPainted → NO_COVERAGE
        this.progressBarTranslation.setStringPainted(true);
173 1 1. initLastLine : removed call to javax/swing/JProgressBar::setValue → NO_COVERAGE
        this.progressBarTranslation.setValue(0);
174
175
        this.lastLine.add(this.progressBarTranslation);
176
        this.lastLine.add(Box.createGlue());
177
        this.lastLine.add(this.buttonSend);
178
    }
179
180
    private void initTextToTranslate() {
181 1 1. initTextToTranslate : removed call to javax/swing/JTextArea::addMouseListener → NO_COVERAGE
        this.textToTranslate.addMouseListener(new MouseAdapter() {
182
            @Override
183
            public void mousePressed(MouseEvent e) {
184 1 1. mousePressed : removed call to java/awt/event/MouseAdapter::mousePressed → NO_COVERAGE
                super.mousePressed(e);
185
                DialogTranslate.this.textToTranslate.requestFocusInWindow();
186
            }
187
        });
188 1 1. initTextToTranslate : removed call to javax/swing/JTextArea::addFocusListener → NO_COVERAGE
        this.textToTranslate.addFocusListener(new FocusAdapter() {
189
            @Override
190
            public void focusGained(FocusEvent focusEvent) {
191 1 1. focusGained : removed call to javax/swing/text/Caret::setVisible → NO_COVERAGE
                DialogTranslate.this.textToTranslate.getCaret().setVisible(true);
192 1 1. focusGained : removed call to javax/swing/text/Caret::setSelectionVisible → NO_COVERAGE
                DialogTranslate.this.textToTranslate.getCaret().setSelectionVisible(true);
193
            }
194
        });
195 1 1. initTextToTranslate : removed call to javax/swing/JTextArea::setBorder → NO_COVERAGE
        this.textToTranslate.setBorder(UiUtil.BORDER_5PX);
196 1 1. initTextToTranslate : removed call to javax/swing/JTextArea::setDragEnabled → NO_COVERAGE
        this.textToTranslate.setDragEnabled(true);
197 1 1. initTextToTranslate : removed call to javax/swing/text/Caret::setBlinkRate → NO_COVERAGE
        this.textToTranslate.getCaret().setBlinkRate(500);
198 1 1. initTextToTranslate : removed call to javax/swing/JTextArea::setComponentPopupMenu → NO_COVERAGE
        this.textToTranslate.setComponentPopupMenu(new JPopupMenuText(this.textToTranslate));
199
    }
200
    
201
    
202
    // Getter / Setter
203
204
    public Language getLanguageInto() {
205 1 1. getLanguageInto : replaced return value with null for com/jsql/view/swing/dialog/DialogTranslate::getLanguageInto → NO_COVERAGE
        return this.languageInto;
206
    }
207
208
    public String getTextBeforeChange() {
209 1 1. getTextBeforeChange : replaced return value with "" for com/jsql/view/swing/dialog/DialogTranslate::getTextBeforeChange → NO_COVERAGE
        return this.textBeforeChange;
210
    }
211
212
    public void setTextBeforeChange(String textBeforeChange) {
213
        this.textBeforeChange = textBeforeChange;
214
    }
215
216
    public JButton getButtonSend() {
217 1 1. getButtonSend : replaced return value with null for com/jsql/view/swing/dialog/DialogTranslate::getButtonSend → NO_COVERAGE
        return this.buttonSend;
218
    }
219
220
    public JTextArea getTextToTranslate() {
221 1 1. getTextToTranslate : replaced return value with null for com/jsql/view/swing/dialog/DialogTranslate::getTextToTranslate → NO_COVERAGE
        return this.textToTranslate;
222
    }
223
224
    public JProgressBar getProgressBarTranslation() {
225 1 1. getProgressBarTranslation : replaced return value with null for com/jsql/view/swing/dialog/DialogTranslate::getProgressBarTranslation → NO_COVERAGE
        return this.progressBarTranslation;
226
    }
227
}

Mutations

59

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setDefaultCloseOperation → NO_COVERAGE

60

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setIconImages → NO_COVERAGE

62

1.1
Location : lambda$new$0
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::dispose → NO_COVERAGE

63

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

69

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::initLastLine → NO_COVERAGE

71

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

73

1.1
Location : <init>
Killed by : none
removed call to java/awt/Container::add → NO_COVERAGE

74

1.1
Location : <init>
Killed by : none
removed call to java/awt/Container::add → NO_COVERAGE

76

1.1
Location : <init>
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::initTextToTranslate → NO_COVERAGE

78

1.1
Location : <init>
Killed by : none
removed call to java/awt/Container::add → NO_COVERAGE

85

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JProgressBar::setValue → NO_COVERAGE

86

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JProgressBar::setString → NO_COVERAGE

91

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JLabel::setText → NO_COVERAGE

99

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JLabel::setComponentOrientation → NO_COVERAGE

100

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JProgressBar::setComponentOrientation → NO_COVERAGE

101

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JPanel::setComponentOrientation → NO_COVERAGE

102

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JButton::setText → NO_COVERAGE

109

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JTextArea::setText → NO_COVERAGE

110

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JTextArea::setEditable → NO_COVERAGE

111

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JButton::setEnabled → NO_COVERAGE

117

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JTextArea::setFont → NO_COVERAGE

123

1.1
Location : initDialog
Killed by : none
removed call to com/jsql/view/swing/dialog/translate/WorkerTranslateInto::execute → NO_COVERAGE

125

1.1
Location : initDialog
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setIconImage → NO_COVERAGE

126

1.1
Location : initDialog
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setTitle → NO_COVERAGE

127

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

128

1.1
Location : initDialog
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setSize → NO_COVERAGE

129

1.1
Location : initDialog
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setLocationRelativeTo → NO_COVERAGE

130

1.1
Location : initDialog
Killed by : none
removed call to javax/swing/JRootPane::setDefaultButton → NO_COVERAGE

132

1.1
Location : initDialog
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setVisible → NO_COVERAGE

137

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

138

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

140

1.1
Location : initLastLine
Killed by : none
removed call to javax/swing/JButton::setToolTipText → NO_COVERAGE

150

1.1
Location : initLastLine
Killed by : none
removed call to javax/swing/JButton::addActionListener → NO_COVERAGE

151

1.1
Location : lambda$initLastLine$1
Killed by : none
negated conditional → NO_COVERAGE

162

1.1
Location : lambda$initLastLine$1
Killed by : none
removed call to com/jsql/util/GitUtil::sendReport → NO_COVERAGE

167

1.1
Location : lambda$initLastLine$1
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setVisible → NO_COVERAGE

170

1.1
Location : initLastLine
Killed by : none
removed call to com/jsql/view/swing/dialog/DialogTranslate::setLayout → NO_COVERAGE

172

1.1
Location : initLastLine
Killed by : none
removed call to javax/swing/JProgressBar::setStringPainted → NO_COVERAGE

173

1.1
Location : initLastLine
Killed by : none
removed call to javax/swing/JProgressBar::setValue → NO_COVERAGE

181

1.1
Location : initTextToTranslate
Killed by : none
removed call to javax/swing/JTextArea::addMouseListener → NO_COVERAGE

184

1.1
Location : mousePressed
Killed by : none
removed call to java/awt/event/MouseAdapter::mousePressed → NO_COVERAGE

188

1.1
Location : initTextToTranslate
Killed by : none
removed call to javax/swing/JTextArea::addFocusListener → NO_COVERAGE

191

1.1
Location : focusGained
Killed by : none
removed call to javax/swing/text/Caret::setVisible → NO_COVERAGE

192

1.1
Location : focusGained
Killed by : none
removed call to javax/swing/text/Caret::setSelectionVisible → NO_COVERAGE

195

1.1
Location : initTextToTranslate
Killed by : none
removed call to javax/swing/JTextArea::setBorder → NO_COVERAGE

196

1.1
Location : initTextToTranslate
Killed by : none
removed call to javax/swing/JTextArea::setDragEnabled → NO_COVERAGE

197

1.1
Location : initTextToTranslate
Killed by : none
removed call to javax/swing/text/Caret::setBlinkRate → NO_COVERAGE

198

1.1
Location : initTextToTranslate
Killed by : none
removed call to javax/swing/JTextArea::setComponentPopupMenu → NO_COVERAGE

205

1.1
Location : getLanguageInto
Killed by : none
replaced return value with null for com/jsql/view/swing/dialog/DialogTranslate::getLanguageInto → NO_COVERAGE

209

1.1
Location : getTextBeforeChange
Killed by : none
replaced return value with "" for com/jsql/view/swing/dialog/DialogTranslate::getTextBeforeChange → NO_COVERAGE

217

1.1
Location : getButtonSend
Killed by : none
replaced return value with null for com/jsql/view/swing/dialog/DialogTranslate::getButtonSend → NO_COVERAGE

221

1.1
Location : getTextToTranslate
Killed by : none
replaced return value with null for com/jsql/view/swing/dialog/DialogTranslate::getTextToTranslate → NO_COVERAGE

225

1.1
Location : getProgressBarTranslation
Killed by : none
replaced return value with null for com/jsql/view/swing/dialog/DialogTranslate::getProgressBarTranslation → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.22.0