avoid "console==null" problems
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPStartApacheAction.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.actions;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.text.MessageFormat;
17
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.views.PHPConsole;
20
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
26
27 public class PHPStartApacheAction implements IWorkbenchWindowActionDelegate {
28   protected IWorkbenchWindow activeWindow = null;
29
30   public void run(IAction action) {
31     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
32     String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
33     // replace backslash with slash in the DocumentRoot under Windows
34     documentRoot = documentRoot.replace('\\', '/');
35     String[] arguments = { documentRoot };
36     MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.APACHE_START_PREF));
37     execute(form.format(arguments), "Start Apache: ");
38   }
39
40 //  public static void execute(String command, String consoleMessage) {
41 //    //                MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
42 //    try {
43 //      PHPConsole console = PHPConsole.getInstance();
44 //      console.write(consoleMessage + command + "\n");
45 //      Runtime runtime = Runtime.getRuntime(); 
46 //
47 //      // runs the command
48 //      Process p = runtime.exec(command);
49 //
50 //      if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
51 //
52 //          OutputThread out = new OutputThread(p.getInputStream(), console);
53 //          OutputThread err = new OutputThread(p.getErrorStream(), console);
54 //          out.start();
55 //          err.start();
56 //        
57 //      }
58 //
59 //    } catch (IOException e) {
60 //
61 //      System.err.println("Problem");
62 //      e.printStackTrace();
63 //
64 //    }
65 //
66 //  }
67
68   public static String execute(String command, String consoleMessage) {
69     //    MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
70     try {
71       PHPConsole console = PHPConsole.getInstance();
72       console.write(consoleMessage + command + "\n");
73       Runtime runtime = Runtime.getRuntime();
74
75       // runs the command
76       Process p = runtime.exec(command);
77
78       // gets the input stream to have the post-compile-time information
79       InputStream stream = p.getInputStream();
80
81       // get the string from Stream
82       String consoleOutput = PHPConsole.getStringFromStream(stream);
83
84       // prints out the information
85       console.write(consoleOutput);
86       return consoleOutput;
87
88     } catch (IOException e) {
89
90       System.err.println("Problem");
91       e.printStackTrace();
92
93     }
94     return "";
95   }
96   public static String getParserOutput(String command, String consoleMessage) {
97     //    MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
98     try {
99       PHPConsole console = PHPConsole.getInstance();
100       if (console!=null) {
101         console.write(consoleMessage + command + "\n");
102       }
103       Runtime runtime = Runtime.getRuntime();
104
105       // runs the command
106       Process p = runtime.exec(command);
107
108       // gets the input stream to have the post-compile-time information
109       InputStream stream = p.getInputStream();
110
111       // get the string from Stream
112       String consoleOutput = PHPConsole.getStringFromStream(stream);
113
114       // prints out the information
115           if (console!=null) {
116         console.write(consoleOutput);
117           }
118       return consoleOutput;
119
120     } catch (IOException e) {
121
122       System.err.println("Problem");
123       e.printStackTrace();
124
125     }
126     return "";
127   }
128
129   public void selectionChanged(IAction action, ISelection selection) {
130
131   }
132
133   public void init(IWorkbenchWindow window) {
134     this.activeWindow = window;
135   }
136
137   public void dispose() {
138
139   }
140
141 //  static class OutputThread extends Thread {
142 //    InputStream fInputStream;
143 //    PHPConsole console;
144 //
145 //    OutputThread(InputStream inputStream, PHPConsole console) {
146 //      this.fInputStream = inputStream;
147 //      this.console = console;
148 //    }
149 //
150 //    public void run() {
151 //      try {
152 //        BufferedReader bin = new BufferedReader(new InputStreamReader(fInputStream));
153 //
154 //        String bufferRow;
155 //        while ((bufferRow = bin.readLine()) != null) {
156 //
157 //          // prints out the information
158 //          console.write( bufferRow );
159 //
160 //        }
161 //        bin.close();
162 //
163 //      } catch (IOException e) {
164 //        MessageDialog.openError(null, "Error in output", e.toString());
165 //      } finally {
166 //
167 //      }
168 //    }
169 //  }
170
171 }