01: package tide.exttools.AStyle;
02:
03: // Idea and first version provided by Mike K.
04: //
05: import tide.editor.*;
06: import tide.project.*;
07: import tide.utils.*;
08: import tide.sources.*;
09: import snow.utils.gui.*;
10: import snow.utils.*;
11: import javax.swing.*;
12: import javax.swing.event.*;
13: import java.io.*;
14: import java.util.*;
15:
16: /** Astyle is nice and helps in an general formatting.
17: */
18: public final class AStyleLauncher {
19: private AStyleLauncher() {
20: }
21:
22: /** As argument: either a source or a directory.
23: * Must be called in a separate thread.
24: * TODO: SPLIT max 100 sources at a time because of Runtime.exec arg limitation...
25: */
26: public static void _formatx(final List<SourceFile> sources,
27: ProgressModalDialog pmd) throws Exception {
28: ProjectSettings actualProject = MainEditorFrame.instance
29: .getActualProject();
30:
31: File AStylebin = new File(actualProject.getProperty(
32: "AStyle_path",
33: AStyleSettingsDialog.defaultAStyleLocation));
34: if (!AStylebin.exists())
35: throw new Exception("AStyle bin not found at "
36: + AStylebin.getAbsolutePath());
37:
38: File javaExePath = actualProject.getJava_TOOL();
39: String options = actualProject.getProperty("AStyle_Options",
40: AStyleSettingsDialog.defaultOptions);
41:
42: List<String> execCommand = new ArrayList<String>();
43: execCommand.add(AStylebin.getAbsolutePath());
44:
45: execCommand.add(options);
46:
47: MainEditorFrame.instance.outputPanels.selectToolsTab(false);
48: MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc
49: .clearDocument();
50: MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc
51: .appendDatedLine("AStyle format");
52: for (SourceFile sf : sources) {
53: if (sf.javaFile.canWrite()) {
54: execCommand.add(sf.javaFile.getAbsolutePath());
55: } else {
56: MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc
57: .appendErrorLine("Can't style readonly file: "
58: + sf.getJavaName());
59: }
60: }
61:
62: //System.out.println("Running AStyle: "+execCommand);
63: Process proc = new ProcessBuilder(execCommand).start();
64:
65: pmd.setProcessToOptionalKill(proc);
66: pmd.start();
67:
68: MainEditorFrame.instance.outputPanels.processesManager
69: .addProcess("AStyle format ", proc, true);
70:
71: StreamGobbler sg = new StreamGobbler(
72: proc.getInputStream(),
73: MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc
74: .createWriterForThisDocument(false), "AStyle");
75: sg.start();
76:
77: // errors
78: StreamGobbler sge = new StreamGobbler(
79: proc.getErrorStream(),
80: MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc
81: .createWriterForThisDocument(true), "AStyle");
82: sge.start();
83:
84: // wait until proc completed... ignoring return... appears as gobbled out...
85: proc.waitFor();
86:
87: }
88: }
|