001: /*
002: * Copyright (c) 2004-2008 QOS.ch
003: * All rights reserved.
004: *
005: * Permission is hereby granted, free of charge, to any person obtaining
006: * a copy of this software and associated documentation files (the
007: * "Software"), to deal in the Software without restriction, including
008: * without limitation the rights to use, copy, modify, merge, publish,
009: * distribute, sublicense, and/or sell copies of the Software, and to
010: * permit persons to whom the Software is furnished to do so, subject to
011: * the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be
014: * included in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
017: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
019: * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020: * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021: * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
022: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024:
025: package org.slf4j.migrator.internal;
026:
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.io.File;
030: import java.util.ArrayList;
031: import java.util.List;
032:
033: import javax.swing.ButtonGroup;
034: import javax.swing.JButton;
035: import javax.swing.JCheckBox;
036: import javax.swing.JFileChooser;
037: import javax.swing.JFrame;
038: import javax.swing.JLabel;
039: import javax.swing.JOptionPane;
040: import javax.swing.JProgressBar;
041: import javax.swing.JRadioButton;
042: import javax.swing.JTextField;
043: import javax.swing.SpringLayout;
044: import javax.swing.WindowConstants;
045:
046: import org.slf4j.migrator.Constant;
047: import org.slf4j.migrator.helper.SpringLayoutHelper;
048:
049: /**
050: * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
051: * Builder, which is free for non-commercial use. If Jigloo is being used
052: * commercially (ie, by a corporation, company or business for any purpose
053: * whatever) then you should purchase a license for each developer using Jigloo.
054: * Please visit www.cloudgarden.com for details. Use of Jigloo implies
055: * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
056: * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
057: * ANY CORPORATE OR COMMERCIAL PURPOSE.
058: */
059: public class MigratorFrame extends JFrame implements ActionListener {
060: private static final long serialVersionUID = 1L;
061:
062: private static final int BASIC_PADDING = 10;
063: private static final int FOLDER_COLUMNS = 40;
064: private static final String MIGRATE_COMMAND = "MIGRATE_COMMAND";
065: private static final String BROWSE_COMMAND = "BROWSE_COMMAND";
066: static final String EXIT_COMMAND = "EXIT_COMMAND";
067:
068: static final int X_SIZE = 700;
069: static final int Y_SIZE = 400;
070:
071: private SpringLayout layoutManager = new SpringLayout();
072: private SpringLayoutHelper slh = new SpringLayoutHelper(
073: layoutManager, BASIC_PADDING);
074:
075: private JLabel migrationLabel;
076:
077: private JRadioButton radioLog4j;
078: private JRadioButton radioJCL;
079: private ButtonGroup buttonGroup;
080:
081: private JTextField folderTextField;
082: private JLabel warningLabel;
083: JButton migrateButton;
084: private JButton browseButton;
085: private JLabel folderLabel;
086:
087: private JCheckBox awareCheckBox;
088: private JLabel awareLabel;
089:
090: JLabel otherLabel;
091: JProgressBar progressBar;
092: private JFileChooser fileChooser;
093:
094: public MigratorFrame() {
095: super ();
096: initGUI();
097: }
098:
099: 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: createButtonGroup();
120: createFolderLabel();
121: createFolderTextField();
122: createBrowseButton();
123: createMigrateButton();
124: createAwareCheckbox();
125: createAwareLabel();
126: createWarningLabel();
127: createFileChooser();
128:
129: otherLabel = new JLabel();
130: otherLabel.setText("");
131: createProgressBar();
132:
133: }
134:
135: /**
136: *
137: */
138: private void constrainAll() {
139:
140: // contrain migration label
141: layoutManager.putConstraint(SpringLayout.WEST, migrationLabel,
142: BASIC_PADDING, SpringLayout.EAST, this );
143:
144: layoutManager.putConstraint(SpringLayout.NORTH, migrationLabel,
145: BASIC_PADDING, SpringLayout.NORTH, this );
146:
147: slh.placeToTheRight(migrationLabel, radioJCL, BASIC_PADDING,
148: -BASIC_PADDING / 2);
149: slh.placeBelow(radioJCL, radioLog4j, 0, 0);
150:
151: slh.placeBelow(migrationLabel, folderLabel, 0,
152: BASIC_PADDING * 5);
153: slh.placeToTheRight(folderLabel, folderTextField);
154: slh.placeToTheRight(folderTextField, browseButton,
155: BASIC_PADDING, -BASIC_PADDING / 2);
156:
157: slh.placeBelow(folderLabel, warningLabel, 0, BASIC_PADDING * 3);
158:
159: slh.placeBelow(warningLabel, awareCheckBox, 0,
160: (int) (BASIC_PADDING * 1.5));
161: slh.placeToTheRight(awareCheckBox, awareLabel);
162:
163: slh.placeBelow(awareCheckBox, migrateButton, 0,
164: BASIC_PADDING * 3);
165:
166: slh.placeBelow(migrateButton, otherLabel, 0, BASIC_PADDING * 2);
167:
168: slh.placeBelow(otherLabel, progressBar, 0, BASIC_PADDING);
169: }
170:
171: private void addAllComponentsToContextPane() {
172: getContentPane().add(migrationLabel);
173: getContentPane().add(radioJCL);
174: getContentPane().add(radioLog4j);
175:
176: getContentPane().add(folderLabel);
177: getContentPane().add(folderTextField);
178: getContentPane().add(browseButton);
179: getContentPane().add(migrateButton);
180:
181: getContentPane().add(awareCheckBox);
182: getContentPane().add(awareLabel);
183:
184: getContentPane().add(warningLabel);
185:
186: getContentPane().add(otherLabel);
187: getContentPane().add(progressBar);
188: }
189:
190: private void createButtonGroup() {
191: buttonGroup = new ButtonGroup();
192: buttonGroup.add(radioJCL);
193: buttonGroup.add(radioLog4j);
194: }
195:
196: private void createMigrationLabel() {
197: migrationLabel = new JLabel();
198: migrationLabel.setText("Migration Type");
199: }
200:
201: private void createRadioJCL() {
202: radioJCL = new JRadioButton();
203: radioJCL.setText("from Jakarta Commons Logging to SLF4J");
204: radioJCL
205: .setToolTipText("Select this button if you wish to migrate a Java project using Jakarta Commons Logging to use SLF4J.");
206: }
207:
208: private void createRadioLog4j() {
209: radioLog4j = new JRadioButton();
210: radioLog4j.setText("from log4j to SLF4J ");
211: radioLog4j
212: .setToolTipText("Select this button if you wish to migrate a Java project using log4j to use SLF4J.");
213: }
214:
215: private void createFolderLabel() {
216: folderLabel = new JLabel();
217: folderLabel.setText("Project Directory");
218: }
219:
220: private void createFolderTextField() {
221: folderTextField = new JTextField();
222: folderTextField.setColumns(FOLDER_COLUMNS);
223: }
224:
225: private void createBrowseButton() {
226: browseButton = new JButton();
227: browseButton.setText("Browse");
228: browseButton.addActionListener(this );
229: browseButton.setActionCommand(BROWSE_COMMAND);
230: browseButton
231: .setToolTipText("Click this button to browse the file systems on your computer.");
232: }
233:
234: private void createAwareCheckbox() {
235: awareCheckBox = new JCheckBox();
236: awareCheckBox
237: .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
243: .setText("<html>"
244: + "<p>I am aware that this tool will directly modify all Java source files</p>"
245: + "<p>in the selected folder without creating backup files.</p>"
246: + "</html>");
247: }
248:
249: private void createWarningLabel() {
250: warningLabel = new JLabel();
251: warningLabel
252: .setText("<html>"
253: + "<p><span color=\"red\">WARNING:</span> This SLF4J migration tool will directly modify all Java source files</p>"
254: + "<p>in the selected project folder without creating a backup of the original files.</p>"
255: + "</html>");
256: }
257:
258: private void createMigrateButton() {
259: migrateButton = new JButton();
260: migrateButton.setText("Migrate Project to SLF4J");
261: migrateButton
262: .setToolTipText("Click this button to initiate migration of your project.");
263: migrateButton.addActionListener(this );
264: migrateButton.setActionCommand(MIGRATE_COMMAND);
265: }
266:
267: private void createFileChooser() {
268: fileChooser = new JFileChooser();
269: fileChooser.setDialogTitle("Source folder selector");
270: fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
271: }
272:
273: private void createProgressBar() {
274: progressBar = new JProgressBar(0, 1);
275: progressBar.setPreferredSize(new java.awt.Dimension(
276: (int) (X_SIZE * 0.8), 5));
277: progressBar.setVisible(false);
278: }
279:
280: public void disableInput() {
281: radioJCL.setEnabled(false);
282: radioLog4j.setEnabled(false);
283:
284: browseButton.setEnabled(false);
285:
286: folderTextField.setEnabled(false);
287: awareCheckBox.setEnabled(false);
288: migrateButton.setText("Migration in progress");
289: migrateButton.setEnabled(false);
290:
291: }
292:
293: public void actionPerformed(ActionEvent e) {
294:
295: if (MIGRATE_COMMAND.equals(e.getActionCommand())) {
296:
297: List<String> errorList = doSanityAnalysis();
298: if (errorList.size() > 0) {
299: showDialogBox(errorList);
300: } else {
301:
302: File projectFolder = new File(folderTextField.getText());
303: int conversionType;
304: if (radioJCL.isSelected()) {
305: conversionType = Constant.JCL_TO_SLF4J;
306: } else if (radioLog4j.isSelected()) {
307: conversionType = Constant.LOG4J_TO_SLF4J;
308: } else {
309: // we cannot possibly reach here
310: throw new IllegalStateException(
311: "One of JCL or log4j project must have been previously chosen.");
312: }
313: ConversionTask task = new ConversionTask(projectFolder,
314: this , conversionType);
315: task.launch();
316: }
317: } else if (BROWSE_COMMAND.equals(e.getActionCommand())) {
318: showFileChooser();
319: } else if (EXIT_COMMAND.equals(e.getActionCommand())) {
320: this .dispose();
321: }
322: }
323:
324: void showFileChooser() {
325: int returnVal = fileChooser.showOpenDialog(this );
326: if (returnVal == JFileChooser.APPROVE_OPTION) {
327: File selectedFile = fileChooser.getSelectedFile();
328: folderTextField.setText(selectedFile.getAbsolutePath());
329: }
330: }
331:
332: List<String> doSanityAnalysis() {
333:
334: List<String> errorList = new ArrayList<String>();
335: if (!radioJCL.isSelected() && !radioLog4j.isSelected()) {
336: errorList
337: .add("Please select the migration type (JCL to SLF4J or log4j to SLF4J)");
338: }
339:
340: String folder = folderTextField.getText();
341:
342: if (folder == null || folder.length() == 0) {
343: errorList
344: .add("Please select the folder of the project to migrate");
345: } else if (!isDirectory(folder)) {
346: errorList.add("[" + folder
347: + "] does not look like a valid folder");
348: }
349:
350: if (!awareCheckBox.isSelected()) {
351: errorList
352: .add("Cannot initiate migration unless you acknowledge<p>that files will be modified without creating backup files");
353: }
354: return errorList;
355: }
356:
357: void showDialogBox(List<String> errorList) {
358: StringBuffer buf = new StringBuffer();
359: buf.append("<html>");
360: int i = 1;
361: for (String msg : errorList) {
362: buf.append("<p>");
363: buf.append(i);
364: buf.append(". ");
365: buf.append(msg);
366: buf.append("</p>");
367: i++;
368: }
369: buf.append("</html>");
370:
371: JOptionPane.showMessageDialog(this , buf.toString(), "",
372: JOptionPane.ERROR_MESSAGE);
373: }
374:
375: boolean isDirectory(String filename) {
376: if (filename == null) {
377: return false;
378: }
379: File file = new File(filename);
380: if (file.exists() && file.isDirectory()) {
381: return true;
382: } else {
383: return false;
384: }
385: }
386: }
|