AStyleLauncher.java :  » IDE » tIDE » tide » exttools » AStyle » Java Open Source

Java Open Source » IDE » tIDE 
tIDE » tide » exttools » AStyle » AStyleLauncher.java
package tide.exttools.AStyle;

// Idea and first version provided by Mike K.
//
import tide.editor.*;
import tide.project.*;
import tide.utils.*;
import tide.sources.*;
import snow.utils.gui.*;
import snow.utils.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

/** Astyle is nice and helps in an general formatting.
*/
public final class AStyleLauncher
{
  private AStyleLauncher()
  {
  }

  /** As argument: either a source or a directory.
  *   Must be called in a separate thread.
  *  TODO: SPLIT max 100 sources at a time because of Runtime.exec arg limitation...
  */
  public static void _formatx(final List<SourceFile> sources, ProgressModalDialog pmd) throws Exception
  {
    ProjectSettings actualProject = MainEditorFrame.instance.getActualProject();

    File AStylebin = new File(actualProject.getProperty("AStyle_path", AStyleSettingsDialog.defaultAStyleLocation));
    if(!AStylebin.exists()) throw new Exception ("AStyle bin not found at "+AStylebin.getAbsolutePath());

    File javaExePath = actualProject.getJava_TOOL();
    String options = actualProject.getProperty("AStyle_Options", AStyleSettingsDialog.defaultOptions);

    List<String> execCommand = new ArrayList<String>();
    execCommand.add( AStylebin.getAbsolutePath() );

    execCommand.add( options);

    MainEditorFrame.instance.outputPanels.selectToolsTab(false);
    MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc.clearDocument();
    MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc.appendDatedLine("AStyle format");
    for(SourceFile sf: sources)
    {
       if(sf.javaFile.canWrite())
       {
         execCommand.add( sf.javaFile.getAbsolutePath() );
       }
       else
       {
          MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc.appendErrorLine("Can't style readonly file: "+sf.getJavaName());
       }
    }



    //System.out.println("Running AStyle: "+execCommand);
    Process proc = new ProcessBuilder(execCommand).start();

    pmd.setProcessToOptionalKill(proc);
    pmd.start();

    MainEditorFrame.instance.outputPanels.processesManager.addProcess("AStyle format ", proc, true);

    StreamGobbler sg = new StreamGobbler(proc.getInputStream(),
      MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc.createWriterForThisDocument(false), "AStyle");
    sg.start();

    // errors
    StreamGobbler sge = new StreamGobbler(proc.getErrorStream(),
    MainEditorFrame.instance.outputPanels.toolsOutputPanel.doc.createWriterForThisDocument(true), "AStyle");
    sge.start();

    // wait until proc completed... ignoring return... appears as gobbled out...
    proc.waitFor();

  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.