1 package net.sourceforge.phpeclipse.views;
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
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
15 import java.io.IOException;
16 import java.io.InputStream;
18 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.actions.PHPActionMessages;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IMenuListener;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.action.MenuManager;
29 import org.eclipse.jface.action.Separator;
30 import org.eclipse.jface.resource.JFaceResources;
31 import org.eclipse.jface.text.BadLocationException;
32 import org.eclipse.jface.text.Document;
33 import org.eclipse.jface.text.TextViewer;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.custom.StyledText;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Menu;
39 import org.eclipse.ui.IActionBars;
40 import org.eclipse.ui.IWorkbenchActionConstants;
41 import org.eclipse.ui.IWorkbenchPage;
42 import org.eclipse.ui.PartInitException;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.part.ViewPart;
47 * The PHPConsole is used to display the output if you start MySQL/Apache
50 public class PHPConsole extends ViewPart {
52 public static final String CONSOLE_ID =
53 "net.sourceforge.phpeclipse.views.phpconsoleview";
55 private TextViewer viewer = null;
56 private Document document = null;
57 private StyledText widget;
58 private Action cutAction = new Action() {
60 viewer.getTextWidget().cut();
63 private Action copyAction = new Action() {
68 private Action pasteAction = new Action() {
70 viewer.getTextWidget().paste();
73 private Action selectAllAction = new Action() {
78 private Action clearAction = new Action() {
90 * Insert the method's description here.
91 * @see ViewPart#createPartControl
93 public void createPartControl(Composite parent) {
94 viewer = new TextViewer(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
95 GridData viewerData = new GridData(GridData.FILL_BOTH);
96 viewer.getControl().setLayoutData(viewerData);
97 viewer.setEditable(true);
99 widget = viewer.getTextWidget();
101 JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
103 cutAction.setText("Cut");
104 copyAction.setText("Copy");
105 pasteAction.setText("Paste");
106 selectAllAction.setText("Select All");
107 clearAction.setText("Clear PHP Console");
108 clearAction.setImageDescriptor(PHPUiImages.DESC_CLEAR);
109 clearAction.setToolTipText("Clear PHP Console");
111 IActionBars bars = this.getViewSite().getActionBars();
112 bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
113 bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
114 bars.setGlobalActionHandler(
115 IWorkbenchActionConstants.PASTE,
119 // hookDoubleClickAction();
120 contributeToActionBars();
125 private void hookContextMenu() {
126 MenuManager menuMgr = new MenuManager("#PopupMenu");
127 menuMgr.setRemoveAllWhenShown(true);
128 menuMgr.addMenuListener(new IMenuListener() {
129 public void menuAboutToShow(IMenuManager manager) {
130 PHPConsole.this.fillContextMenu(manager);
133 Menu menu = menuMgr.createContextMenu(viewer.getControl());
134 viewer.getControl().setMenu(menu);
135 getSite().registerContextMenu(menuMgr, viewer);
138 private void contributeToActionBars() {
139 IActionBars bars = getViewSite().getActionBars();
140 fillLocalPullDown(bars.getMenuManager());
141 fillLocalToolBar(bars.getToolBarManager());
144 private void fillLocalPullDown(IMenuManager manager) {
145 manager.add(cutAction);
146 manager.add(copyAction);
147 manager.add(pasteAction);
148 manager.add(selectAllAction);
151 private void fillContextMenu(IMenuManager manager) {
152 manager.add(cutAction);
153 manager.add(copyAction);
154 manager.add(pasteAction);
155 manager.add(selectAllAction);
156 // Other plug-ins can contribute there actions here
157 manager.add(new Separator("Additions"));
160 private void fillLocalToolBar(IToolBarManager manager) {
161 manager.add(clearAction);
164 * Insert the method's description here.
165 * @see ViewPart#setFocus
167 public void setFocus() {
171 * Set the text for the viewer
173 public void setOutputText(String text) {
174 document = new Document(text);
175 viewer.setDocument(document);
178 public void appendOutputText(String text) {
180 if (document == null) {
181 document = new Document(text);
182 viewer.setDocument(document);
184 document.replace(document.getLength(), 0, text);
186 } catch (BadLocationException e) {
188 // viewer.setDocument(document);
191 public static PHPConsole getInstance() {
192 IWorkbenchPage page =
195 .getActiveWorkbenchWindow()
197 PHPConsole console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
201 .getPreferenceStore()
202 .getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE)
206 page.showView(PHPConsole.CONSOLE_ID);
207 if (console == null) {
208 console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
210 } catch (PartInitException e) {
211 PHPeclipsePlugin.getDefault().getLog().log(
214 PHPeclipsePlugin.getPluginId(),
216 PHPActionMessages.getString(
217 "PHPStartApacheAction.consoleViewOpeningProblem"),
226 * Prints out the string represented by the string buffer
228 public synchronized void write(String output) {
229 appendOutputText(output);
233 * Creates a string buffer from the given input stream
235 public static String getStringFromStream(InputStream stream)
237 StringBuffer buffer = new StringBuffer();
238 byte[] b = new byte[100];
240 while (finished != -1) {
241 finished = stream.read(b);
242 if (finished != -1) {
243 String current = new String(b, 0, finished);
244 buffer.append(current);
247 return buffer.toString();