001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.tools.builder;
010:
011: import java.io.File;
012: import javax.swing.JFileChooser;
013: import javax.swing.filechooser.FileFilter;
014: import org.acm.seguin.awt.ExceptionPrinter;
015: import org.acm.seguin.awt.GUIExceptionPrinter;
016: import org.acm.seguin.awt.TextExceptionPrinter;
017: import org.acm.seguin.io.AllFileFilter;
018: import org.acm.seguin.io.DirectoryTreeTraversal;
019: import org.acm.seguin.io.ExtensionFileFilter;
020: import org.acm.seguin.io.FileCopy;
021: import org.acm.seguin.pretty.PrettyPrintFile;
022: import org.acm.seguin.tools.RefactoryInstaller;
023: import org.acm.seguin.util.FileSettings;
024: import org.acm.seguin.util.MissingSettingsException;
025:
026: /**
027: * Traverses a directory structure and performs all refactorings on the files.
028: *
029: * @author Chris Seguin
030: * @created May 12, 1999
031: */
032: public class PrettyPrinter extends DirectoryTreeTraversal {
033: // Instance Variables
034: private PrettyPrintFile ppf;
035:
036: /**
037: * Creates a refactory
038: *
039: * @param init the initial directory or file
040: * @param quiet Description of Parameter
041: */
042: public PrettyPrinter(String init, boolean quiet) {
043: super (init);
044:
045: if (init == null) {
046: return;
047: }
048:
049: ppf = new PrettyPrintFile();
050: ppf.setAsk(!quiet && (new File(init)).isDirectory());
051: }
052:
053: /**
054: * Determines if this file should be handled by this traversal
055: *
056: * @param currentFile the current file
057: * @return true if the file should be handled
058: */
059: protected boolean isTarget(File currentFile) {
060: return (currentFile.getName().endsWith(".java"));
061: }
062:
063: /**
064: * Visits the current file
065: *
066: * @param currentFile the current file
067: */
068: protected void visit(File currentFile) {
069: if (ppf.isApplicable(currentFile)) {
070: System.out.println("Applying the Pretty Printer: "
071: + currentFile.getPath());
072: makeBackup(currentFile);
073: ppf.apply(currentFile);
074: }
075: }
076:
077: /**
078: * Make a backup of the file before applying the pretty printer
079: *
080: * @param currentFile Description of Parameter
081: */
082: private void makeBackup(File currentFile) {
083: String backupExt;
084: try {
085: backupExt = FileSettings.getRefactoryPrettySettings()
086: .getString("pretty.printer.backup.ext");
087: backupExt = (backupExt == null) ? "" : backupExt.trim();
088: } catch (MissingSettingsException mse) {
089: backupExt = "";
090: }
091:
092: if ((backupExt != null) && (backupExt.length() > 0)) {
093: File parentDir = currentFile.getParentFile();
094: String name = currentFile.getName();
095: File dest = new File(parentDir, name + backupExt);
096: (new FileCopy(currentFile, dest, false)).run();
097: }
098: }
099:
100: /**
101: * The main program
102: *
103: * @param args Description of Parameter
104: */
105: public static void main(String[] args) {
106: try {
107: int lastOption = -1;
108: boolean quiet = false;
109:
110: for (int ndx = 0; ndx < args.length; ndx++) {
111: if (args[ndx].equals("-quiet")
112: || args[ndx].equals("-u")) {
113: quiet = true;
114: lastOption = ndx;
115: ExceptionPrinter
116: .register(new TextExceptionPrinter());
117: } else if (args[ndx].equals("-?")
118: || args[ndx].equalsIgnoreCase("-h")
119: || args[ndx].equalsIgnoreCase("-help")) {
120: printHelpMessage();
121: return;
122: } else if (args[ndx].equals("-config")) {
123: String dir = args[ndx + 1];
124: ndx++;
125: FileSettings.setSettingsRoot(dir);
126: }
127: }
128:
129: // Make sure everything is installed properly
130: (new RefactoryInstaller(false)).run();
131:
132: if (lastOption + 1 >= args.length) {
133: // no more arguments left
134: if (quiet) {
135: prettyPrinter(quiet);
136: } else {
137: prettyPrinter(System.getProperty("user.dir"), quiet);
138: }
139: } else {
140: // process remaining arguments as file / dir names
141: for (int ndx = lastOption + 1; ndx < args.length; ++ndx) {
142: prettyPrinter(args[ndx], quiet);
143: }
144: }
145: } catch (Throwable thrown) {
146: thrown.printStackTrace(System.out);
147: System.exit(1);
148: }
149:
150: // Exit
151: System.exit(ExceptionPrinter.getExceptionsPrinted());
152: }
153:
154: /**
155: * Refactor the current file
156: *
157: * @param filename Description of Parameter
158: * @param quiet Description of Parameter
159: */
160: public static void prettyPrinter(String filename, boolean quiet) {
161: (new PrettyPrinter(filename, quiet)).run();
162: }
163:
164: /**
165: * Refactor the current file
166: *
167: * @param quiet Description of Parameter
168: */
169: public static void prettyPrinter(boolean quiet) {
170: JFileChooser chooser = new JFileChooser();
171:
172: // Create the java file filter
173: ExtensionFileFilter filter = new ExtensionFileFilter();
174: filter.addExtension(".java");
175: filter.setDescription("Java Source Files (.java)");
176: chooser.setFileFilter(filter);
177:
178: // Add other file filters - All
179: FileFilter allFilter = new AllFileFilter();
180: chooser.addChoosableFileFilter(allFilter);
181:
182: // Set it so that files and directories can be selected
183: chooser
184: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
185:
186: // Set the directory to the current directory
187: chooser.setCurrentDirectory(new File(System
188: .getProperty("user.dir")));
189:
190: // Get the user's selection
191: int returnVal = chooser.showOpenDialog(null);
192: if (returnVal == JFileChooser.APPROVE_OPTION) {
193: (new PrettyPrinter(chooser.getSelectedFile()
194: .getAbsolutePath(), quiet)).run();
195: }
196: }
197:
198: /** Print a help message */
199: private static void printHelpMessage() {
200: System.out
201: .println("Syntax: java PrettyPrinter file // means refactor this file");
202: System.out
203: .println(" OR java PrettyPrinter [-quiet|-u] dir // means refactor this directory");
204: System.out
205: .println(" OR java PrettyPrinter [-quiet|-u] // means refactor the current directory");
206: System.out
207: .println(" the -quiet or the -u flag tells the pretty printer not to prompt the user");
208: }
209: }
|