*** empty log message ***
[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
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.actions.PHPActionMessages;
20
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.resource.JFaceResources;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.Document;
27 import org.eclipse.jface.text.TextViewer;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.StyledText;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.ui.IActionBars;
33 import org.eclipse.ui.IWorkbenchActionConstants;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.part.ViewPart;
38
39 /**
40  * The PHPConsole is used to display the output if you start MySQL/Apache
41  * @see ViewPart
42  */
43 public class PHPConsole extends ViewPart {
44
45   public static final String CONSOLE_ID = "net.sourceforge.phpeclipse.views.phpconsoleview";
46
47   private TextViewer viewer = null;
48   private Document document = null;
49
50   /**
51    * The constructor.
52    */
53   public PHPConsole() {
54   }
55
56   /**
57    * Insert the method's description here.
58    * @see ViewPart#createPartControl
59    */
60   public void createPartControl(Composite parent) {
61     viewer = new TextViewer(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
62     GridData viewerData = new GridData(GridData.FILL_BOTH);
63     viewer.getControl().setLayoutData(viewerData);
64     viewer.setEditable(false);
65     
66     StyledText widget = viewer.getTextWidget();
67     widget.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
68     Action cutAction = new Action() {
69       public void run() {
70         viewer.getTextWidget().cut();
71       }
72     };
73     Action copyAction = new Action() {
74       public void run() {
75         viewer.getTextWidget().copy();
76       }
77     };
78     Action pasteAction = new Action() {
79       public void run() {
80         viewer.getTextWidget().paste();
81       }
82     };
83
84     IActionBars bars = this.getViewSite().getActionBars();
85     bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
86     bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
87     bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
88   }
89
90   /**
91    * Insert the method's description here.
92    * @see ViewPart#setFocus
93    */
94   public void setFocus() {
95   }
96
97   /**
98    * Set the text for the viewer
99    */
100   public void setOutputText(String text) {
101     document = new Document(text);
102     viewer.setDocument(document);
103   }
104
105   public void appendOutputText(String text) {
106     try {
107       if (document == null) {
108         document = new Document(text);
109         viewer.setDocument(document);
110       }
111       document.replace(document.getLength(), 0, text);
112     } catch (BadLocationException e) {
113     }
114     //  viewer.setDocument(document);
115   }
116
117   /**
118    * Prints out the string represented by the string buffer
119    */
120   public static void write(String output) {
121     try {
122       IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
123       PHPConsole console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
124
125       if (console != null) {
126         console.appendOutputText(output);
127       } else if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
128         page.showView(PHPConsole.CONSOLE_ID);
129         console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
130         console.setOutputText(output);
131       }
132     } catch (PartInitException e) {
133       PHPeclipsePlugin.getDefault().getLog().log(
134         new Status(
135           IStatus.ERROR,
136           PHPeclipsePlugin.getPluginId(),
137           0,
138           PHPActionMessages.getString("PHPStartApacheAction.consoleViewOpeningProblem"),
139           e));
140     }
141
142   }
143
144   /**
145    * Creates a string buffer from the given input stream
146    */
147   public static String getStringFromStream(InputStream stream) throws IOException {
148     StringBuffer buffer = new StringBuffer();
149     byte[] b = new byte[100];
150     int finished = 0;
151     while (finished != -1) {
152       finished = stream.read(b);
153       if (finished != -1) {
154         String current = new String(b, 0, finished);
155         buffer.append(current);
156       }
157     }
158     return buffer.toString();
159   }
160
161 }