View Javadoc

1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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   * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
51   * Builder, which is free for non-commercial use. If Jigloo is being used
52   * commercially (ie, by a corporation, company or business for any purpose
53   * whatever) then you should purchase a license for each developer using Jigloo.
54   * Please visit www.cloudgarden.com for details. Use of Jigloo implies
55   * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
56   * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
57   * ANY CORPORATE OR COMMERCIAL PURPOSE.
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, BASIC_PADDING);
73  
74      private JLabel migrationLabel;
75  
76      private JRadioButton radioLog4j;
77      private JRadioButton radioJCL;
78      private JRadioButton radioJUL;
79      private ButtonGroup buttonGroup;
80  
81      private JTextField folderTextField;
82      private JLabel warningLabel;
83      JButton migrateButton;
84      private JButton browseButton;
85      private JLabel folderLabel;
86  
87      private JCheckBox awareCheckBox;
88      private JLabel awareLabel;
89  
90      JLabel otherLabel;
91      JProgressBar progressBar;
92      private JFileChooser fileChooser;
93  
94      public MigratorFrame() {
95          super();
96          initGUI();
97      }
98  
99      private void initGUI() {
100         try {
101             setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
102             getContentPane().setLayout(layoutManager);
103             this.setTitle("SLF4J migrator");
104 
105             createComponents();
106             constrainAll();
107             addAllComponentsToContextPane();
108             pack();
109             this.setSize(700, 400);
110         } catch (Exception e) {
111             e.printStackTrace();
112         }
113     }
114 
115     private void createComponents() {
116         createMigrationLabel();
117         createRadioJCL();
118         createRadioLog4j();
119         createRadioJUL();
120         createButtonGroup();
121         createFolderLabel();
122         createFolderTextField();
123         createBrowseButton();
124         createMigrateButton();
125         createAwareCheckbox();
126         createAwareLabel();
127         createWarningLabel();
128         createFileChooser();
129 
130         otherLabel = new JLabel();
131         otherLabel.setText("");
132         createProgressBar();
133 
134     }
135 
136     /**
137      * 
138      */
139     private void constrainAll() {
140 
141         // contrain migration label
142         layoutManager.putConstraint(SpringLayout.WEST, migrationLabel, BASIC_PADDING, SpringLayout.EAST, this);
143 
144         layoutManager.putConstraint(SpringLayout.NORTH, migrationLabel, BASIC_PADDING, SpringLayout.NORTH, this);
145 
146         slh.placeToTheRight(migrationLabel, radioJCL, BASIC_PADDING, -BASIC_PADDING / 2);
147         slh.placeBelow(radioJCL, radioLog4j, 0, 0);
148 
149         slh.placeBelow(radioLog4j, radioJUL, 0, 0);
150 
151         slh.placeBelow(migrationLabel, folderLabel, 0, BASIC_PADDING * 6);
152         slh.placeToTheRight(folderLabel, folderTextField);
153         slh.placeToTheRight(folderTextField, browseButton, BASIC_PADDING, -BASIC_PADDING / 2);
154 
155         slh.placeBelow(folderLabel, warningLabel, 0, BASIC_PADDING * 3);
156 
157         slh.placeBelow(warningLabel, awareCheckBox, 0, (int) (BASIC_PADDING * 1.5));
158         slh.placeToTheRight(awareCheckBox, awareLabel);
159 
160         slh.placeBelow(awareCheckBox, migrateButton, 0, BASIC_PADDING * 3);
161 
162         slh.placeBelow(migrateButton, otherLabel, 0, BASIC_PADDING * 2);
163 
164         slh.placeBelow(otherLabel, progressBar, 0, BASIC_PADDING);
165     }
166 
167     private void addAllComponentsToContextPane() {
168         getContentPane().add(migrationLabel);
169         getContentPane().add(radioJCL);
170         getContentPane().add(radioLog4j);
171         getContentPane().add(radioJUL);
172 
173         getContentPane().add(folderLabel);
174         getContentPane().add(folderTextField);
175         getContentPane().add(browseButton);
176         getContentPane().add(migrateButton);
177 
178         getContentPane().add(awareCheckBox);
179         getContentPane().add(awareLabel);
180 
181         getContentPane().add(warningLabel);
182 
183         getContentPane().add(otherLabel);
184         getContentPane().add(progressBar);
185     }
186 
187     private void createButtonGroup() {
188         buttonGroup = new ButtonGroup();
189         buttonGroup.add(radioJCL);
190         buttonGroup.add(radioLog4j);
191         buttonGroup.add(radioJUL);
192     }
193 
194     private void createMigrationLabel() {
195         migrationLabel = new JLabel();
196         migrationLabel.setText("Migration Type");
197     }
198 
199     private void createRadioJCL() {
200         radioJCL = new JRadioButton();
201         radioJCL.setText("from Jakarta Commons Logging to SLF4J");
202         radioJCL.setToolTipText("Select this button if you wish to migrate a Java project using Jakarta Commons Logging to use SLF4J.");
203     }
204 
205     private void createRadioLog4j() {
206         radioLog4j = new JRadioButton();
207         radioLog4j.setText("from log4j to SLF4J ");
208         radioLog4j.setToolTipText("Select this button if you wish to migrate a Java project using log4j to use SLF4J.");
209     }
210 
211     private void createRadioJUL() {
212         radioJUL = new JRadioButton();
213         radioJUL.setText("from JUL to SLF4J ");
214         radioJUL.setToolTipText("Select this button if you wish to migrate a Java project using java.utl.logging (JUL) to use SLF4J.");
215     }
216 
217     private void createFolderLabel() {
218         folderLabel = new JLabel();
219         folderLabel.setText("Project Directory");
220     }
221 
222     private void createFolderTextField() {
223         folderTextField = new JTextField();
224         folderTextField.setColumns(FOLDER_COLUMNS);
225     }
226 
227     private void createBrowseButton() {
228         browseButton = new JButton();
229         browseButton.setText("Browse");
230         browseButton.addActionListener(this);
231         browseButton.setActionCommand(BROWSE_COMMAND);
232         browseButton.setToolTipText("Click this button to browse the file systems on your computer.");
233     }
234 
235     private void createAwareCheckbox() {
236         awareCheckBox = new JCheckBox();
237         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>");
238     }
239 
240     private void createAwareLabel() {
241         awareLabel = new JLabel();
242         awareLabel.setText("<html>" + "<p>I am aware that this tool will directly modify all Java source files</p>"
243                         + "<p>in the selected folder without creating backup files.</p>" + "</html>");
244     }
245 
246     private void createWarningLabel() {
247         warningLabel = new JLabel();
248         warningLabel.setText("<html>" + "<p><span color=\"red\">WARNING:</span> This SLF4J migration tool will directly modify all Java source files</p>"
249                         + "<p>in the selected project folder without creating a backup of the original files.</p>" + "</html>");
250     }
251 
252     private void createMigrateButton() {
253         migrateButton = new JButton();
254         migrateButton.setText("Migrate Project to SLF4J");
255         migrateButton.setToolTipText("Click this button to initiate migration of your project.");
256         migrateButton.addActionListener(this);
257         migrateButton.setActionCommand(MIGRATE_COMMAND);
258     }
259 
260     private void createFileChooser() {
261         fileChooser = new JFileChooser();
262         fileChooser.setDialogTitle("Source folder selector");
263         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
264     }
265 
266     private void createProgressBar() {
267         progressBar = new JProgressBar(0, 1);
268         progressBar.setPreferredSize(new java.awt.Dimension((int) (X_SIZE * 0.8), 5));
269         progressBar.setVisible(false);
270     }
271 
272     public void disableInput() {
273         radioJCL.setEnabled(false);
274         radioLog4j.setEnabled(false);
275 
276         browseButton.setEnabled(false);
277 
278         folderTextField.setEnabled(false);
279         awareCheckBox.setEnabled(false);
280         migrateButton.setText("Migration in progress");
281         migrateButton.setEnabled(false);
282 
283     }
284 
285     public void actionPerformed(ActionEvent e) {
286 
287         if (MIGRATE_COMMAND.equals(e.getActionCommand())) {
288 
289             List<String> errorList = doSanityAnalysis();
290             if (errorList.size() > 0) {
291                 showDialogBox(errorList);
292             } else {
293 
294                 File projectFolder = new File(folderTextField.getText());
295                 int conversionType;
296                 if (radioJCL.isSelected()) {
297                     conversionType = Constant.JCL_TO_SLF4J;
298                 } else if (radioLog4j.isSelected()) {
299                     conversionType = Constant.LOG4J_TO_SLF4J;
300                 } else if (radioJUL.isSelected()) {
301                     conversionType = Constant.JUL_TO_SLF4J;
302                 } else {
303                     // we cannot possibly reach here
304                     throw new IllegalStateException("One of JCL or log4j project must have been previously chosen.");
305                 }
306                 ConversionTask task = new ConversionTask(projectFolder, this, conversionType);
307                 task.launch();
308             }
309         } else if (BROWSE_COMMAND.equals(e.getActionCommand())) {
310             showFileChooser();
311         } else if (EXIT_COMMAND.equals(e.getActionCommand())) {
312             this.dispose();
313         }
314     }
315 
316     void showFileChooser() {
317         int returnVal = fileChooser.showOpenDialog(this);
318         if (returnVal == JFileChooser.APPROVE_OPTION) {
319             File selectedFile = fileChooser.getSelectedFile();
320             folderTextField.setText(selectedFile.getAbsolutePath());
321         }
322     }
323 
324     List<String> doSanityAnalysis() {
325 
326         List<String> errorList = new ArrayList<String>();
327         if (!radioJCL.isSelected() && !radioLog4j.isSelected() && !radioJUL.isSelected()) {
328             errorList.add("Please select the migration type: JCL, log4j, or JUL to SLF4J.");
329         }
330 
331         String folder = folderTextField.getText();
332 
333         if (folder == null || folder.length() == 0) {
334             errorList.add("Please select the folder of the project to migrate");
335         } else if (!isDirectory(folder)) {
336             errorList.add("[" + folder + "] does not look like a valid folder");
337         }
338 
339         if (!awareCheckBox.isSelected()) {
340             errorList.add("Cannot initiate migration unless you acknowledge<p>that files will be modified without creating backup files");
341         }
342         return errorList;
343     }
344 
345     void showDialogBox(List<String> errorList) {
346         StringBuilder buf = new StringBuilder();
347         buf.append("<html>");
348         int i = 1;
349         for (String msg : errorList) {
350             buf.append("<p>");
351             buf.append(i);
352             buf.append(". ");
353             buf.append(msg);
354             buf.append("</p>");
355             i++;
356         }
357         buf.append("</html>");
358 
359         JOptionPane.showMessageDialog(this, buf.toString(), "", JOptionPane.ERROR_MESSAGE);
360     }
361 
362     boolean isDirectory(String filename) {
363         if (filename == null) {
364             return false;
365         }
366         File file = new File(filename);
367         if (file.exists() && file.isDirectory()) {
368             return true;
369         } else {
370             return false;
371         }
372     }
373 }