PHP Console View
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / views / PHPConsole.java
1 package net.sourceforge.phpeclipse.views;
2
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
9
10 Contributors:
11     IBM Corporation - Initial implementation
12     Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.text.MessageFormat;
18
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.actions.PHPActionMessages;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.text.BadLocationException;
27 import org.eclipse.jface.text.Document;
28 import org.eclipse.jface.text.TextViewer;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.part.ViewPart;
39 import sun.security.krb5.internal.crypto.e;
40
41 /**
42  * The PHPConsole is used to display the output if you start MySQL/Apache
43  * @see ViewPart
44  */
45 public class PHPConsole extends ViewPart {
46
47         public static final String CONSOLE_ID = "net.sourceforge.phpeclipse.views.phpconsoleview";
48         
49         private TextViewer viewer = null;
50         private Document document = null;
51
52         /**
53          * The constructor.
54          */
55         public PHPConsole() {
56         }
57
58         /**
59          * Insert the method's description here.
60          * @see ViewPart#createPartControl
61          */
62         public void createPartControl(Composite parent)  {
63                 viewer = new TextViewer(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
64                 GridData viewerData = new GridData(GridData.FILL_BOTH);
65                 viewer.getControl().setLayoutData(viewerData);
66                 viewer.setEditable(false);
67         }
68
69         /**
70          * Insert the method's description here.
71          * @see ViewPart#setFocus
72          */
73         public void setFocus()  {
74         }
75         
76         /**
77          * Set the text for the viewer
78          */
79         public void setOutputText(String text) {
80                 document = new Document(text);
81                 viewer.setDocument(document);
82         }
83
84   public void appendOutputText(String text) {
85     try {
86       document.replace(document.getLength(), 0, text);
87     } catch (BadLocationException e) {
88     }
89   //  viewer.setDocument(document);
90   }
91   
92   /**
93    * Prints out the string represented by the string buffer
94    */
95   static public void write(String output) {
96     try {
97       IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
98       PHPConsole console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
99
100       if (console != null) {
101         console.appendOutputText(output);
102       } else if (
103         PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
104         page.showView(PHPConsole.CONSOLE_ID);
105         console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
106         console.setOutputText(output);
107       }
108     } catch (PartInitException e) {
109       PHPeclipsePlugin.getDefault().getLog().log(
110         new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(), 0, PHPActionMessages.getString("PHPStartApacheAction.consoleViewOpeningProblem"), e));
111     }
112
113   }
114
115   /**
116    * Creates a string buffer from the given input stream
117    */
118   static public String getStringFromStream(InputStream stream) throws IOException {
119     StringBuffer buffer = new StringBuffer();
120     byte[] b = new byte[100];
121     int finished = 0;
122     while (finished != -1) {
123       finished = stream.read(b);
124       if (finished != -1) {
125         String current = new String(b, 0, finished);
126         buffer.append(current);
127       }
128     }
129     return buffer.toString();
130   }
131
132
133
134
135 }