Parser detects wrong include files now
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / views / PHPConsole.java
index 8406b8e..2e98d5d 100644 (file)
@@ -9,11 +9,13 @@ http://www.eclipse.org/legal/cpl-v10.html
 
 Contributors:
     IBM Corporation - Initial implementation
-    Klaus Hartlage - www.eclipseproject.de
+    www.phpeclipse.de
 **********************************************************************/
 
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 
 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
@@ -33,21 +35,13 @@ import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.TextViewer;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.StyledText;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IWorkbenchActionConstants;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.part.ViewPart;
 
 /**
@@ -56,13 +50,17 @@ import org.eclipse.ui.part.ViewPart;
  */
 public class PHPConsole extends ViewPart {
 
-  public static final String CONSOLE_ID = "net.sourceforge.phpeclipse.views.phpconsoleview";
+  public static final String CONSOLE_ID =
+    "net.sourceforge.phpeclipse.views.phpconsoleview";
   private int COMMAND_COMBO_SIZE = 10;
 
   private TextViewer fViewer = null;
   private Document fDocument = null;
   private StyledText fStyledText;
-  private Combo fCommandCombo;
+  // private Combo fCommandCombo;
+  //  private ProcessOutputWriter consoleOut;
+  //  private ProcessOutputWriter consoleErr;
+
   // private Action goAction;
 
   private Action cutAction = new Action() {
@@ -75,11 +73,11 @@ public class PHPConsole extends ViewPart {
       fStyledText.copy();
     }
   };
-  private Action pasteAction = new Action() {
-    public void run() {
-      fViewer.getTextWidget().paste();
-    }
-  };
+  //  private Action pasteAction = new Action() {
+  //    public void run() {
+  //      fViewer.getTextWidget().paste();
+  //    }
+  //  };
   private Action selectAllAction = new Action() {
     public void run() {
       fStyledText.selectAll();
@@ -101,32 +99,33 @@ public class PHPConsole extends ViewPart {
    * @see ViewPart#createPartControl
    */
   public void createPartControl(Composite parent) {
-    Composite container = new Composite(parent, SWT.NULL);
-    //   control = container;
-    GridLayout layout = new GridLayout();
-    layout.marginWidth = 0;
-    layout.marginHeight = 0;
-    layout.verticalSpacing = 0;
-    container.setLayout(layout);
-    Composite navContainer = new Composite(container, SWT.NONE);
-    layout = new GridLayout();
-    layout.numColumns = 2;
-    layout.marginHeight = 1;
-    navContainer.setLayout(layout);
-    createCommandBar(navContainer);
-    navContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-
-    fViewer = new TextViewer(container, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
+    //    Composite container = new Composite(parent, SWT.NULL);
+    //    //   control = container;
+    //    GridLayout layout = new GridLayout();
+    //    layout.marginWidth = 0;
+    //    layout.marginHeight = 0;
+    //    layout.verticalSpacing = 0;
+    //    container.setLayout(layout);
+    //    Composite navContainer = new Composite(container, SWT.NONE);
+    //    layout = new GridLayout();
+    //    layout.numColumns = 2;
+    //    layout.marginHeight = 1;
+    //    navContainer.setLayout(layout);
+    //    createCommandBar(navContainer);
+    //    navContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+
+    fViewer = new TextViewer(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
     GridData viewerData = new GridData(GridData.FILL_BOTH);
     fViewer.getControl().setLayoutData(viewerData);
-    fViewer.setEditable(true);
+    fViewer.setEditable(false);
 
     fStyledText = fViewer.getTextWidget();
-    fStyledText.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
+    fStyledText.setFont(
+      JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
 
     cutAction.setText("Cut");
     copyAction.setText("Copy");
-    pasteAction.setText("Paste");
+    //   pasteAction.setText("Paste");
     selectAllAction.setText("Select All");
     clearAction.setText("Clear PHP Console");
     clearAction.setImageDescriptor(PHPUiImages.DESC_CLEAR);
@@ -135,71 +134,170 @@ public class PHPConsole extends ViewPart {
     IActionBars bars = this.getViewSite().getActionBars();
     bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
     bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
-    bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
+    //  bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
 
     hookContextMenu();
     // hookDoubleClickAction();
     contributeToActionBars();
 
+    //    appendOutputText("This is the PHP console.\n");
+    //    appendOutputText("Type: \"php $f\" to run the current editor file.\n");
+
   }
 
   private void createCommandBar(Composite parent) {
-    Label addressLabel = new Label(parent, SWT.NONE);
-    addressLabel.setText("Command:");
-
-    fCommandCombo = new Combo(parent, SWT.DROP_DOWN | SWT.BORDER);
-    fCommandCombo.addModifyListener(new ModifyListener() {
-      public void modifyText(ModifyEvent e) {
-        String text = fCommandCombo.getText();
-        //       goAction.setEnabled(text.length() > 0);
-      }
-    });
-    fCommandCombo.addSelectionListener(new SelectionListener() {
-      public void widgetSelected(SelectionEvent e) {
-        String text = fCommandCombo.getItem(fCommandCombo.getSelectionIndex());
-        if (text.length() > 0) {
-          fCommandCombo.setText(text);
-      //    executeCommand(text);
-        }
-      }
-      public void widgetDefaultSelected(SelectionEvent e) {
-        executeCommand(fCommandCombo.getText());
-      }
-    });
+    //    Label addressLabel = new Label(parent, SWT.NONE);
+    //    addressLabel.setText("Command:");
+
+    //    fCommandCombo = new Combo(parent, SWT.DROP_DOWN | SWT.BORDER);
+    //    fCommandCombo.addModifyListener(new ModifyListener() {
+    //      public void modifyText(ModifyEvent e) {
+    //        String text = fCommandCombo.getText();
+    //        //       goAction.setEnabled(text.length() > 0);
+    //      }
+    //    });
+    //    fCommandCombo.addSelectionListener(new SelectionListener() {
+    //      public void widgetSelected(SelectionEvent e) {
+    //        String text = fCommandCombo.getItem(fCommandCombo.getSelectionIndex());
+    //        if (text.length() > 0) {
+    //          fCommandCombo.setText(text);
+    //          //    executeCommand(text);
+    //        }
+    //      }
+    //      public void widgetDefaultSelected(SelectionEvent e) {
+    //        executeCommand(fCommandCombo.getText());
+    //      }
+    //    });
     GridData gd = new GridData(GridData.FILL_HORIZONTAL);
-    fCommandCombo.setLayoutData(gd);
-    //    ToolBar toolbar = new ToolBar(parent, SWT.FLAT | SWT.HORIZONTAL);
-    //    toolBarManager = new ToolBarManager(toolbar);
-    //    makeActions();
-    //    IToolBarManager localBar =
-    //      getViewSite().getActionBars().getToolBarManager();
-    //    localBar.add(backwardAction);
-    //    localBar.add(forwardAction);
+    //  fCommandCombo.setLayoutData(gd);
   }
 
-  private void executeCommand(String command) {
-    command.trim();
-    write( "Test: "+command );
-    String[] items = fCommandCombo.getItems();
-    int loc = -1;
-    String normURL = command;
-    for (int i = 0; i < items.length; i++) {
-      String normItem = items[i];
-      if (normURL.equals(normItem)) {
-        // match 
-        loc = i;
-        break;
-      }
-    }
-    if (loc != -1) {
-      fCommandCombo.remove(loc);
-    }
-    fCommandCombo.add(command, 0);
-    if (fCommandCombo.getItemCount() > COMMAND_COMBO_SIZE) {
-      fCommandCombo.remove(fCommandCombo.getItemCount() - 1);
-    }
-    fCommandCombo.getParent().layout(true);
-  }
+  //  private void executeCommand(String command) {
+  //    command.trim();
+  //    if (command.equals("")) {
+  //      fCommandCombo.forceFocus();
+  //      return;
+  //    }
+  //    execute(command);
+  //
+  //    fCommandCombo.forceFocus();
+  //    // add to Combo history
+  //    String[] items = fCommandCombo.getItems();
+  //    int loc = -1;
+  //    String normURL = command;
+  //    for (int i = 0; i < items.length; i++) {
+  //      String normItem = items[i];
+  //      if (normURL.equals(normItem)) {
+  //        // match 
+  //        loc = i;
+  //        break;
+  //      }
+  //    }
+  //    if (loc != -1) {
+  //      fCommandCombo.remove(loc);
+  //    }
+  //    fCommandCombo.add(command, 0);
+  //    if (fCommandCombo.getItemCount() > COMMAND_COMBO_SIZE) {
+  //      fCommandCombo.remove(fCommandCombo.getItemCount() - 1);
+  //    }
+  //    fCommandCombo.getParent().layout(true);
+  //  }
+
+  //  private void execute(String command) {  
+  //    
+  //    ArrayList args = new ArrayList();
+  //
+  //    command = command.replace('\\', '§');
+  //
+  //    StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(command));
+  //    tokenizer.resetSyntax();
+  //
+  //    tokenizer.whitespaceChars(0, ' ');
+  //    tokenizer.wordChars('!', 255);
+  //
+  //    tokenizer.quoteChar('"');
+  //    tokenizer.quoteChar('\'');
+  //
+  //    int token;
+  //    try {
+  //      while ((token = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) {
+  //        if (token == StreamTokenizer.TT_WORD) {
+  //          args.add(tokenizer.sval);
+  //        }
+  //      }
+  //    } catch (IOException e) {
+  //      // 
+  //    }
+  //    String arg = "";
+  //    // replace variables in arguments
+  //
+  ////    IFile file = getFile();
+  //    IFile file = PHPeclipsePlugin.getDefault().getLastEditorFile();
+  //    if (file != null) {
+  //      String fileLocation = file.getLocation().toString();
+  //      for (int i = 0; i < args.size(); i++) {
+  //        arg = args.get(i).toString();
+  //        if (arg.equals("$f")) {
+  //          //current php editor file
+  //          if (File.separatorChar == '\\') {
+  //            fileLocation = fileLocation.replace('/', '\\');
+  //          }
+  //          args.set(i, fileLocation);
+  //        }
+  //      }
+  //    }
+  //
+  //    final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
+  //
+  //    String arg0 = "";
+  //    String temp;
+  //    StringBuffer commandBuffer = new StringBuffer(1024);
+  //    //    Program.launch(command);
+  //    if (args.size() > 0) {
+  //      arg0 = (String) args.get(0);
+  //      arg0 = arg0.replace('§', '\\');
+  //      args.remove(0);
+  //      if (arg0.equals("php")) {
+  //        temp = store.getString(PHPeclipsePlugin.PHP_RUN_PREF);
+  //        if (temp != null) {
+  //          arg0 = temp;
+  //        }
+  //      }
+  //      commandBuffer.append(arg0 + " ");
+  //    }
+  //    String[] stringArgs = new String[args.size()];
+  //    for (int i = 0; i < args.size(); i++) {
+  //      arg = (String) args.get(i);
+  //      arg = arg.replace('§', '\\');
+  //      stringArgs[i] = arg;
+  //      commandBuffer.append(arg + " ");
+  //    }
+  //    commandBuffer.append("\n");
+  //
+  //    try {
+  //      command = commandBuffer.toString();
+  //      write(command+"\n");
+  //      Runtime runtime = Runtime.getRuntime();
+  //
+  //      // runs the command
+  //      Process process = runtime.exec(command);
+  //      
+  //      consoleOut = new ProcessOutputWriter(process.getInputStream());
+  //      consoleOut.start();
+  //      consoleErr = new ProcessOutputWriter(process.getErrorStream());
+  //      consoleErr.start();
+  //            
+  //      //process.waitFor();
+  ////      InputStream in = process.getInputStream();
+  ////      String output = getStringFromStream(in);
+  ////      write(output);
+  ////      in.close();
+  ////    } catch (InterruptedException e) {
+  ////      write(e.toString());
+  //    } catch (IOException e) {
+  //      write(e.toString());
+  //    }
+  //  }
 
   private void hookContextMenu() {
     MenuManager menuMgr = new MenuManager("#PopupMenu");
@@ -223,14 +321,14 @@ public class PHPConsole extends ViewPart {
   private void fillLocalPullDown(IMenuManager manager) {
     manager.add(cutAction);
     manager.add(copyAction);
-    manager.add(pasteAction);
+    //   manager.add(pasteAction);
     manager.add(selectAllAction);
   }
 
   private void fillContextMenu(IMenuManager manager) {
     manager.add(cutAction);
     manager.add(copyAction);
-    manager.add(pasteAction);
+    //  manager.add(pasteAction);
     manager.add(selectAllAction);
     // Other plug-ins can contribute there actions here
     manager.add(new Separator("Additions"));
@@ -244,6 +342,7 @@ public class PHPConsole extends ViewPart {
    * @see ViewPart#setFocus
    */
   public void setFocus() {
+    //   fCommandCombo.forceFocus();
   }
 
   /**
@@ -268,11 +367,20 @@ public class PHPConsole extends ViewPart {
   }
 
   public static PHPConsole getInstance() {
-    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+    // IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+    IWorkbenchPage page =
+      PHPeclipsePlugin
+        .getDefault()
+        .getWorkbench()
+        .getActiveWorkbenchWindow()
+        .getActivePage();
     PHPConsole console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
 
-    if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
-
+    if (PHPeclipsePlugin
+      .getDefault()
+      .getPreferenceStore()
+      .getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) 
+      == true) {
       try {
         page.showView(PHPConsole.CONSOLE_ID);
         if (console == null) {
@@ -284,16 +392,16 @@ public class PHPConsole extends ViewPart {
             IStatus.ERROR,
             PHPeclipsePlugin.getPluginId(),
             0,
-            PHPActionMessages.getString("PHPStartApacheAction.consoleViewOpeningProblem"),
+            PHPActionMessages.getString(
+              "PHPStartApacheAction.consoleViewOpeningProblem"),
             e));
       }
-
     }
     return console;
   }
 
   /**
-   * Prints out the string represented by the string buffer
+   * Prints out the string represented by the string 
    */
   public synchronized void write(String output) {
     appendOutputText(output);
@@ -302,7 +410,8 @@ public class PHPConsole extends ViewPart {
   /**
    * Creates a string buffer from the given input stream
    */
-  public static String getStringFromStream(InputStream stream) throws IOException {
+  public static String getStringFromStream(InputStream stream)
+    throws IOException {
     StringBuffer buffer = new StringBuffer();
     byte[] b = new byte[100];
     int finished = 0;
@@ -316,4 +425,59 @@ public class PHPConsole extends ViewPart {
     return buffer.toString();
   }
 
+  /**
+   * Finds the file that's currently opened in the PHP Text Editor
+   */
+  //  protected IFile getFile() {
+  //    ITextEditor editor = PHPeclipsePlugin.getDefault().getLastEditorFile();
+  //
+  //    IEditorInput editorInput = null;
+  //    if (editor != null) {
+  //      editorInput = editor.getEditorInput();
+  //    }
+  //
+  //    if (editorInput instanceof IFileEditorInput)
+  //      return ((IFileEditorInput) editorInput).getFile();
+  //
+  //    // if nothing was found, which should never happen
+  //    return null;
+  //  }
+
+  class ProcessOutputWriter extends Thread {
+    boolean fStreamClosed;
+    InputStream fInputStream;
+
+    ProcessOutputWriter(InputStream inputStream) {
+      fInputStream = inputStream;
+      fStreamClosed = false;
+    }
+
+    public void closeStream() {
+      fStreamClosed = true;
+      try {
+        fInputStream.close();
+      } catch (IOException io) {
+      }
+    }
+
+    public void run() {
+      try {
+        BufferedReader in =
+          new BufferedReader(new InputStreamReader(fInputStream));
+
+        String line;
+        while ((line = in.readLine()) != null) {
+          write(line);
+        }
+        in.close();
+      } catch (Exception e) {
+        e.printStackTrace(System.out);
+        if (!fStreamClosed) {
+          //  write("\nPHP Console Exception: "+ e.toString() );
+        }
+      } finally {
+      }
+    }
+  }
+
 }