added a first php $f command to the PHP console
[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       
104       Runtime runtime = Runtime.getRuntime();
105
106       // runs the command
107       Process p = runtime.exec(command);
108
109       // gets the input stream to have the post-compile-time information
110       InputStream stream = p.getInputStream();
111
112       // get the string from Stream
113       String consoleOutput = PHPConsole.getStringFromStream(stream);
114
115       // prints out the information
116           if (console!=null) {
117         console.write(consoleOutput);
118           }
119       return consoleOutput;
120
121     } catch (IOException e) {
122
123       System.err.println("Problem");
124       e.printStackTrace();
125
126     }
127     return "";
128   }
129
130   public void selectionChanged(IAction action, ISelection selection) {
131
132   }
133
134   public void init(IWorkbenchWindow window) {
135     this.activeWindow = window;
136   }
137
138   public void dispose() {
139
140   }
141
142 //  static class OutputThread extends Thread {
143 //    InputStream fInputStream;
144 //    PHPConsole console;
145 //
146 //    OutputThread(InputStream inputStream, PHPConsole console) {
147 //      this.fInputStream = inputStream;
148 //      this.console = console;
149 //    }
150 //
151 //    public void run() {
152 //      try {
153 //        BufferedReader bin = new BufferedReader(new InputStreamReader(fInputStream));
154 //
155 //        String bufferRow;
156 //        while ((bufferRow = bin.readLine()) != null) {
157 //
158 //          // prints out the information
159 //          console.write( bufferRow );
160 //
161 //        }
162 //        bin.close();
163 //
164 //      } catch (IOException e) {
165 //        MessageDialog.openError(null, "Error in output", e.toString());
166 //      } finally {
167 //
168 //      }
169 //    }
170 //  }
171
172 }