aboutsummaryrefslogtreecommitdiff
path: root/slf4j-migrator/src/main/java/org/slf4j/migrator/internal/MigratorFrame.java
blob: 3ab15c603383c8531f4c38668ff89a6fae035f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
 * Copyright (c) 2004-2011 QOS.ch
 * All rights reserved.
 *
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.slf4j.migrator.internal;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.WindowConstants;

import org.slf4j.migrator.Constant;
import org.slf4j.migrator.helper.SpringLayoutHelper;

public class MigratorFrame extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;

    private static final int BASIC_PADDING = 10;
    private static final int FOLDER_COLUMNS = 40;
    private static final String MIGRATE_COMMAND = "MIGRATE_COMMAND";
    private static final String BROWSE_COMMAND = "BROWSE_COMMAND";
    static final String EXIT_COMMAND = "EXIT_COMMAND";

    static final int X_SIZE = 700;
    static final int Y_SIZE = 400;

    private final SpringLayout layoutManager = new SpringLayout();
    private final SpringLayoutHelper slh = new SpringLayoutHelper(layoutManager, BASIC_PADDING);

    private JLabel migrationLabel;

    private JRadioButton radioLog4j;
    private JRadioButton radioJCL;
    private JRadioButton radioJUL;
    private ButtonGroup buttonGroup;

    private JTextField folderTextField;
    private JLabel warningLabel;
    JButton migrateButton;
    private JButton browseButton;
    private JLabel folderLabel;

    private JCheckBox awareCheckBox;
    private JLabel awareLabel;

    JLabel otherLabel;
    JProgressBar progressBar;
    private JFileChooser fileChooser;

    public MigratorFrame() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(layoutManager);
            this.setTitle("SLF4J migrator");

            createComponents();
            constrainAll();
            addAllComponentsToContextPane();
            pack();
            this.setSize(700, 400);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createComponents() {
        createMigrationLabel();
        createRadioJCL();
        createRadioLog4j();
        createRadioJUL();
        createButtonGroup();
        createFolderLabel();
        createFolderTextField();
        createBrowseButton();
        createMigrateButton();
        createAwareCheckbox();
        createAwareLabel();
        createWarningLabel();
        createFileChooser();

        otherLabel = new JLabel();
        otherLabel.setText("");
        createProgressBar();

    }

    /**
     * 
     */
    private void constrainAll() {

        // constraints migration label
        layoutManager.putConstraint(SpringLayout.WEST, migrationLabel, BASIC_PADDING, SpringLayout.EAST, this);

        layoutManager.putConstraint(SpringLayout.NORTH, migrationLabel, BASIC_PADDING, SpringLayout.NORTH, this);

        slh.placeToTheRight(migrationLabel, radioJCL, BASIC_PADDING, -BASIC_PADDING / 2);
        slh.placeBelow(radioJCL, radioLog4j, 0, 0);

        slh.placeBelow(radioLog4j, radioJUL, 0, 0);

        slh.placeBelow(migrationLabel, folderLabel, 0, BASIC_PADDING * 6);
        slh.placeToTheRight(folderLabel, folderTextField);
        slh.placeToTheRight(folderTextField, browseButton, BASIC_PADDING, -BASIC_PADDING / 2);

        slh.placeBelow(folderLabel, warningLabel, 0, BASIC_PADDING * 3);

        slh.placeBelow(warningLabel, awareCheckBox, 0, (int) (BASIC_PADDING * 1.5));
        slh.placeToTheRight(awareCheckBox, awareLabel);

        slh.placeBelow(awareCheckBox, migrateButton, 0, BASIC_PADDING * 3);

        slh.placeBelow(migrateButton, otherLabel, 0, BASIC_PADDING * 2);

        slh.placeBelow(otherLabel, progressBar, 0, BASIC_PADDING);
    }

    private void addAllComponentsToContextPane() {
        getContentPane().add(migrationLabel);
        getContentPane().add(radioJCL);
        getContentPane().add(radioLog4j);
        getContentPane().add(radioJUL);

        getContentPane().add(folderLabel);
        getContentPane().add(folderTextField);
        getContentPane().add(browseButton);
        getContentPane().add(migrateButton);

        getContentPane().add(awareCheckBox);
        getContentPane().add(awareLabel);

        getContentPane().add(warningLabel);

        getContentPane().add(otherLabel);
        getContentPane().add(progressBar);
    }

    private void createButtonGroup() {
        buttonGroup = new ButtonGroup();
        buttonGroup.add(radioJCL);
        buttonGroup.add(radioLog4j);
        buttonGroup.add(radioJUL);
    }

    private void createMigrationLabel() {
        migrationLabel = new JLabel();
        migrationLabel.setText("Migration Type");
    }

    private void createRadioJCL() {
        radioJCL = new JRadioButton();
        radioJCL.setText("from Jakarta Commons Logging to SLF4J");
        radioJCL.setToolTipText("Select this button if you wish to migrate a Java project using Jakarta Commons Logging to use SLF4J.");
    }

    private void createRadioLog4j() {
        radioLog4j = new JRadioButton();
        radioLog4j.setText("from log4j to SLF4J ");
        radioLog4j.setToolTipText("Select this button if you wish to migrate a Java project using log4j to use SLF4J.");
    }

    private void createRadioJUL() {
        radioJUL = new JRadioButton();
        radioJUL.setText("from JUL to SLF4J ");
        radioJUL.setToolTipText("Select this button if you wish to migrate a Java project using java.utl.logging (JUL) to use SLF4J.");
    }

    private void createFolderLabel() {
        folderLabel = new JLabel();
        folderLabel.setText("Project Directory");
    }

    private void createFolderTextField() {
        folderTextField = new JTextField();
        folderTextField.setColumns(FOLDER_COLUMNS);
    }

    private void createBrowseButton() {
        browseButton = new JButton();
        browseButton.setText("Browse");
        browseButton.addActionListener(this);
        browseButton.setActionCommand(BROWSE_COMMAND);
        browseButton.setToolTipText("Click this button to browse the file systems on your computer.");
    }

    private void createAwareCheckbox() {
        awareCheckBox = new JCheckBox();
        awareCheckBox.setToolTipText("<html><p>Check this box of you understand that the migration tool<p>will <b>not</b> backup your Java source files.</html>");
    }

    private void createAwareLabel() {
        awareLabel = new JLabel();
        awareLabel.setText("<html>" + "<p>I am aware that this tool will directly modify all Java source files"
                        + "<p>in the selected folder without creating backup files." + "</html>");
    }

    private void createWarningLabel() {
        warningLabel = new JLabel();
        warningLabel.setText("<html>" + "<p><span color=\"red\">WARNING:</span> This SLF4J migration tool will directly modify all Java source files"
                        + "<p>in the selected project folder without creating a backup of the original files." + "</html>");
    }

    private void createMigrateButton() {
        migrateButton = new JButton();
        migrateButton.setText("Migrate Project to SLF4J");
        migrateButton.setToolTipText("Click this button to initiate migration of your project.");
        migrateButton.addActionListener(this);
        migrateButton.setActionCommand(MIGRATE_COMMAND);
    }

    private void createFileChooser() {
        fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Source folder selector");
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }

    private void createProgressBar() {
        progressBar = new JProgressBar(0, 1);
        progressBar.setPreferredSize(new java.awt.Dimension((int) (X_SIZE * 0.8), 5));
        progressBar.setVisible(false);
    }

    public void disableInput() {
        radioJCL.setEnabled(false);
        radioLog4j.setEnabled(false);

        browseButton.setEnabled(false);

        folderTextField.setEnabled(false);
        awareCheckBox.setEnabled(false);
        migrateButton.setText("Migration in progress");
        migrateButton.setEnabled(false);

    }

    public void actionPerformed(ActionEvent e) {

        if (MIGRATE_COMMAND.equals(e.getActionCommand())) {

            List<String> errorList = doSanityAnalysis();
            if (errorList.size() > 0) {
                showDialogBox(errorList);
            } else {

                File projectFolder = new File(folderTextField.getText());
                int conversionType;
                if (radioJCL.isSelected()) {
                    conversionType = Constant.JCL_TO_SLF4J;
                } else if (radioLog4j.isSelected()) {
                    conversionType = Constant.LOG4J_TO_SLF4J;
                } else if (radioJUL.isSelected()) {
                    conversionType = Constant.JUL_TO_SLF4J;
                } else {
                    // we cannot possibly reach here
                    throw new IllegalStateException("One of JCL or log4j project must have been previously chosen.");
                }
                ConversionTask task = new ConversionTask(projectFolder, this, conversionType);
                task.launch();
            }
        } else if (BROWSE_COMMAND.equals(e.getActionCommand())) {
            showFileChooser();
        } else if (EXIT_COMMAND.equals(e.getActionCommand())) {
            this.dispose();
        }
    }

    void showFileChooser() {
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            folderTextField.setText(selectedFile.getAbsolutePath());
        }
    }

    List<String> doSanityAnalysis() {

        List<String> errorList = new ArrayList<>();
        if (!radioJCL.isSelected() && !radioLog4j.isSelected() && !radioJUL.isSelected()) {
            errorList.add("Please select the migration type: JCL, log4j, or JUL to SLF4J.");
        }

        String folder = folderTextField.getText();

        if (folder == null || folder.length() == 0) {
            errorList.add("Please select the folder of the project to migrate");
        } else if (!isDirectory(folder)) {
            errorList.add("[" + folder + "] does not look like a valid folder");
        }

        if (!awareCheckBox.isSelected()) {
            errorList.add("Cannot initiate migration unless you acknowledge<p>that files will be modified without creating backup files");
        }
        return errorList;
    }

    void showDialogBox(List<String> errorList) {
        StringBuilder buf = new StringBuilder();
        buf.append("<html>");
        int i = 1;
        for (String msg : errorList) {
            buf.append("<p>");
            buf.append(i);
            buf.append(". ");
            buf.append(msg);
            buf.append("");
            i++;
        }
        buf.append("</html>");

        JOptionPane.showMessageDialog(this, buf.toString(), "", JOptionPane.ERROR_MESSAGE);
    }

    boolean isDirectory(String filename) {
        if (filename == null) {
            return false;
        }
        File file = new File(filename);
        if (file.exists() && file.isDirectory()) {
            return true;
        } else {
            return false;
        }
    }
}