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 package org.slf4j.migrator.internal;
26
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.io.File;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import javax.swing.ButtonGroup;
34 import javax.swing.JButton;
35 import javax.swing.JCheckBox;
36 import javax.swing.JFileChooser;
37 import javax.swing.JFrame;
38 import javax.swing.JLabel;
39 import javax.swing.JOptionPane;
40 import javax.swing.JProgressBar;
41 import javax.swing.JRadioButton;
42 import javax.swing.JTextField;
43 import javax.swing.SpringLayout;
44 import javax.swing.WindowConstants;
45
46 import org.slf4j.migrator.Constant;
47 import org.slf4j.migrator.helper.SpringLayoutHelper;
48
49
50
51
52
53
54
55
56
57
58
59 public class MigratorFrame extends JFrame implements ActionListener {
60 private static final long serialVersionUID = 1L;
61
62 private static final int BASIC_PADDING = 10;
63 private static final int FOLDER_COLUMNS = 40;
64 private static final String MIGRATE_COMMAND = "MIGRATE_COMMAND";
65 private static final String BROWSE_COMMAND = "BROWSE_COMMAND";
66 static final String EXIT_COMMAND = "EXIT_COMMAND";
67
68 static final int X_SIZE = 700;
69 static final int Y_SIZE = 400;
70
71 private SpringLayout layoutManager = new SpringLayout();
72 private SpringLayoutHelper slh = new SpringLayoutHelper(layoutManager,
73 BASIC_PADDING);
74
75 private JLabel migrationLabel;
76
77 private JRadioButton radioLog4j;
78 private JRadioButton radioJCL;
79 private JRadioButton radioJUL;
80 private ButtonGroup buttonGroup;
81
82 private JTextField folderTextField;
83 private JLabel warningLabel;
84 JButton migrateButton;
85 private JButton browseButton;
86 private JLabel folderLabel;
87
88 private JCheckBox awareCheckBox;
89 private JLabel awareLabel;
90
91 JLabel otherLabel;
92 JProgressBar progressBar;
93 private JFileChooser fileChooser;
94
95 public MigratorFrame() {
96 super();
97 initGUI();
98 }
99
100 private void initGUI() {
101 try {
102 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
103 getContentPane().setLayout(layoutManager);
104 this.setTitle("SLF4J migrator");
105
106 createComponents();
107 constrainAll();
108 addAllComponentsToContextPane();
109 pack();
110 this.setSize(700, 400);
111 } catch (Exception e) {
112 e.printStackTrace();
113 }
114 }
115
116 private void createComponents() {
117 createMigrationLabel();
118 createRadioJCL();
119 createRadioLog4j();
120 createRadioJUL();
121 createButtonGroup();
122 createFolderLabel();
123 createFolderTextField();
124 createBrowseButton();
125 createMigrateButton();
126 createAwareCheckbox();
127 createAwareLabel();
128 createWarningLabel();
129 createFileChooser();
130
131 otherLabel = new JLabel();
132 otherLabel.setText("");
133 createProgressBar();
134
135 }
136
137
138
139
140 private void constrainAll() {
141
142
143 layoutManager.putConstraint(SpringLayout.WEST, migrationLabel,
144 BASIC_PADDING, SpringLayout.EAST, this);
145
146 layoutManager.putConstraint(SpringLayout.NORTH, migrationLabel,
147 BASIC_PADDING, SpringLayout.NORTH, this);
148
149 slh.placeToTheRight(migrationLabel, radioJCL, BASIC_PADDING,
150 -BASIC_PADDING / 2);
151 slh.placeBelow(radioJCL, radioLog4j, 0, 0);
152
153 slh.placeBelow(radioLog4j, radioJUL, 0, 0);
154
155
156 slh.placeBelow(migrationLabel, folderLabel, 0, BASIC_PADDING * 6);
157 slh.placeToTheRight(folderLabel, folderTextField);
158 slh.placeToTheRight(folderTextField, browseButton, BASIC_PADDING,
159 -BASIC_PADDING / 2);
160
161 slh.placeBelow(folderLabel, warningLabel, 0, BASIC_PADDING * 3);
162
163 slh.placeBelow(warningLabel, awareCheckBox, 0, (int) (BASIC_PADDING * 1.5));
164 slh.placeToTheRight(awareCheckBox, awareLabel);
165
166 slh.placeBelow(awareCheckBox, migrateButton, 0, BASIC_PADDING * 3);
167
168 slh.placeBelow(migrateButton, otherLabel, 0, BASIC_PADDING * 2);
169
170 slh.placeBelow(otherLabel, progressBar, 0, BASIC_PADDING);
171 }
172
173 private void addAllComponentsToContextPane() {
174 getContentPane().add(migrationLabel);
175 getContentPane().add(radioJCL);
176 getContentPane().add(radioLog4j);
177 getContentPane().add(radioJUL);
178
179 getContentPane().add(folderLabel);
180 getContentPane().add(folderTextField);
181 getContentPane().add(browseButton);
182 getContentPane().add(migrateButton);
183
184 getContentPane().add(awareCheckBox);
185 getContentPane().add(awareLabel);
186
187 getContentPane().add(warningLabel);
188
189 getContentPane().add(otherLabel);
190 getContentPane().add(progressBar);
191 }
192
193 private void createButtonGroup() {
194 buttonGroup = new ButtonGroup();
195 buttonGroup.add(radioJCL);
196 buttonGroup.add(radioLog4j);
197 buttonGroup.add(radioJUL);
198 }
199
200 private void createMigrationLabel() {
201 migrationLabel = new JLabel();
202 migrationLabel.setText("Migration Type");
203 }
204
205 private void createRadioJCL() {
206 radioJCL = new JRadioButton();
207 radioJCL.setText("from Jakarta Commons Logging to SLF4J");
208 radioJCL
209 .setToolTipText("Select this button if you wish to migrate a Java project using Jakarta Commons Logging to use SLF4J.");
210 }
211
212 private void createRadioLog4j() {
213 radioLog4j = new JRadioButton();
214 radioLog4j.setText("from log4j to SLF4J ");
215 radioLog4j
216 .setToolTipText("Select this button if you wish to migrate a Java project using log4j to use SLF4J.");
217 }
218
219 private void createRadioJUL() {
220 radioJUL = new JRadioButton();
221 radioJUL.setText("from JUL to SLF4J ");
222 radioJUL
223 .setToolTipText("Select this button if you wish to migrate a Java project using java.utl.logging (JUL) to use SLF4J.");
224 }
225 private void createFolderLabel() {
226 folderLabel = new JLabel();
227 folderLabel.setText("Project Directory");
228 }
229
230 private void createFolderTextField() {
231 folderTextField = new JTextField();
232 folderTextField.setColumns(FOLDER_COLUMNS);
233 }
234
235 private void createBrowseButton() {
236 browseButton = new JButton();
237 browseButton.setText("Browse");
238 browseButton.addActionListener(this);
239 browseButton.setActionCommand(BROWSE_COMMAND);
240 browseButton
241 .setToolTipText("Click this button to browse the file systems on your computer.");
242 }
243
244 private void createAwareCheckbox() {
245 awareCheckBox = new JCheckBox();
246 awareCheckBox
247 .setToolTipText("<html><p>Check this box of you understand that the migration tool<p>will <b>not</b> backup your Java source files.</html>");
248 }
249
250 private void createAwareLabel() {
251 awareLabel = new JLabel();
252 awareLabel
253 .setText("<html>"
254 + "<p>I am aware that this tool will directly modify all Java source files</p>"
255 + "<p>in the selected folder without creating backup files.</p>"
256 + "</html>");
257 }
258
259 private void createWarningLabel() {
260 warningLabel = new JLabel();
261 warningLabel
262 .setText("<html>"
263 + "<p><span color=\"red\">WARNING:</span> This SLF4J migration tool will directly modify all Java source files</p>"
264 + "<p>in the selected project folder without creating a backup of the original files.</p>"
265 + "</html>");
266 }
267
268 private void createMigrateButton() {
269 migrateButton = new JButton();
270 migrateButton.setText("Migrate Project to SLF4J");
271 migrateButton
272 .setToolTipText("Click this button to initiate migration of your project.");
273 migrateButton.addActionListener(this);
274 migrateButton.setActionCommand(MIGRATE_COMMAND);
275 }
276
277 private void createFileChooser() {
278 fileChooser = new JFileChooser();
279 fileChooser.setDialogTitle("Source folder selector");
280 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
281 }
282
283 private void createProgressBar() {
284 progressBar = new JProgressBar(0, 1);
285 progressBar
286 .setPreferredSize(new java.awt.Dimension((int) (X_SIZE * 0.8), 5));
287 progressBar.setVisible(false);
288 }
289
290 public void disableInput() {
291 radioJCL.setEnabled(false);
292 radioLog4j.setEnabled(false);
293
294 browseButton.setEnabled(false);
295
296 folderTextField.setEnabled(false);
297 awareCheckBox.setEnabled(false);
298 migrateButton.setText("Migration in progress");
299 migrateButton.setEnabled(false);
300
301 }
302
303 public void actionPerformed(ActionEvent e) {
304
305 if (MIGRATE_COMMAND.equals(e.getActionCommand())) {
306
307 List<String> errorList = doSanityAnalysis();
308 if (errorList.size() > 0) {
309 showDialogBox(errorList);
310 } else {
311
312 File projectFolder = new File(folderTextField.getText());
313 int conversionType;
314 if(radioJCL.isSelected()) {
315 conversionType = Constant.JCL_TO_SLF4J;
316 } else if (radioLog4j.isSelected()) {
317 conversionType = Constant.LOG4J_TO_SLF4J;
318 } else if (radioJUL.isSelected()) {
319 conversionType = Constant.JUL_TO_SLF4J;
320 } else {
321
322 throw new IllegalStateException("One of JCL or log4j project must have been previously chosen.");
323 }
324 ConversionTask task = new ConversionTask(projectFolder, this,
325 conversionType);
326 task.launch();
327 }
328 } else if (BROWSE_COMMAND.equals(e.getActionCommand())) {
329 showFileChooser();
330 } else if (EXIT_COMMAND.equals(e.getActionCommand())) {
331 this.dispose();
332 }
333 }
334
335 void showFileChooser() {
336 int returnVal = fileChooser.showOpenDialog(this);
337 if (returnVal == JFileChooser.APPROVE_OPTION) {
338 File selectedFile = fileChooser.getSelectedFile();
339 folderTextField.setText(selectedFile.getAbsolutePath());
340 }
341 }
342
343 List<String> doSanityAnalysis() {
344
345 List<String> errorList = new ArrayList<String>();
346 if (!radioJCL.isSelected() && !radioLog4j.isSelected() && !radioJUL.isSelected()) {
347 errorList
348 .add("Please select the migration type: JCL, log4j, or JUL to SLF4J.");
349 }
350
351 String folder = folderTextField.getText();
352
353 if (folder == null || folder.length() == 0) {
354 errorList.add("Please select the folder of the project to migrate");
355 } else if (!isDirectory(folder)) {
356 errorList.add("[" + folder + "] does not look like a valid folder");
357 }
358
359 if (!awareCheckBox.isSelected()) {
360 errorList
361 .add("Cannot initiate migration unless you acknowledge<p>that files will be modified without creating backup files");
362 }
363 return errorList;
364 }
365
366 void showDialogBox(List<String> errorList) {
367 StringBuffer buf = new StringBuffer();
368 buf.append("<html>");
369 int i = 1;
370 for (String msg : errorList) {
371 buf.append("<p>");
372 buf.append(i);
373 buf.append(". ");
374 buf.append(msg);
375 buf.append("</p>");
376 i++;
377 }
378 buf.append("</html>");
379
380 JOptionPane.showMessageDialog(this, buf.toString(), "",
381 JOptionPane.ERROR_MESSAGE);
382 }
383
384 boolean isDirectory(String filename) {
385 if (filename == null) {
386 return false;
387 }
388 File file = new File(filename);
389 if (file.exists() && file.isDirectory()) {
390 return true;
391 } else {
392 return false;
393 }
394 }
395 }