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.events.ModifyEvent;
37 import org.eclipse.swt.events.ModifyListener;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.events.SelectionListener;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Label;
45 import org.eclipse.swt.widgets.Menu;
46 import org.eclipse.ui.IActionBars;
47 import org.eclipse.ui.IWorkbenchActionConstants;
48 import org.eclipse.ui.IWorkbenchPage;
49 import org.eclipse.ui.PartInitException;
50 import org.eclipse.ui.PlatformUI;
51 import org.eclipse.ui.part.ViewPart;
54 * The PHPConsole is used to display the output if you start MySQL/Apache
57 public class PHPConsole extends ViewPart {
59 public static final String CONSOLE_ID = "net.sourceforge.phpeclipse.views.phpconsoleview";
60 private int COMMAND_COMBO_SIZE = 10;
62 private TextViewer fViewer = null;
63 private Document fDocument = null;
64 private StyledText fStyledText;
65 private Combo fCommandCombo;
66 // private Action goAction;
68 private Action cutAction = new Action() {
70 fViewer.getTextWidget().cut();
73 private Action copyAction = new Action() {
78 private Action pasteAction = new Action() {
80 fViewer.getTextWidget().paste();
83 private Action selectAllAction = new Action() {
85 fStyledText.selectAll();
88 private Action clearAction = new Action() {
90 fStyledText.setText("");
100 * Insert the method's description here.
101 * @see ViewPart#createPartControl
103 public void createPartControl(Composite parent) {
104 Composite container = new Composite(parent, SWT.NULL);
105 // control = container;
106 GridLayout layout = new GridLayout();
107 layout.marginWidth = 0;
108 layout.marginHeight = 0;
109 layout.verticalSpacing = 0;
110 container.setLayout(layout);
111 Composite navContainer = new Composite(container, SWT.NONE);
112 layout = new GridLayout();
113 layout.numColumns = 2;
114 layout.marginHeight = 1;
115 navContainer.setLayout(layout);
116 createCommandBar(navContainer);
117 navContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
119 fViewer = new TextViewer(container, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
120 GridData viewerData = new GridData(GridData.FILL_BOTH);
121 fViewer.getControl().setLayoutData(viewerData);
122 fViewer.setEditable(true);
124 fStyledText = fViewer.getTextWidget();
125 fStyledText.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
127 cutAction.setText("Cut");
128 copyAction.setText("Copy");
129 pasteAction.setText("Paste");
130 selectAllAction.setText("Select All");
131 clearAction.setText("Clear PHP Console");
132 clearAction.setImageDescriptor(PHPUiImages.DESC_CLEAR);
133 clearAction.setToolTipText("Clear PHP Console");
135 IActionBars bars = this.getViewSite().getActionBars();
136 bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
137 bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
138 bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
141 // hookDoubleClickAction();
142 contributeToActionBars();
146 private void createCommandBar(Composite parent) {
147 Label addressLabel = new Label(parent, SWT.NONE);
148 addressLabel.setText("Command:");
150 fCommandCombo = new Combo(parent, SWT.DROP_DOWN | SWT.BORDER);
151 fCommandCombo.addModifyListener(new ModifyListener() {
152 public void modifyText(ModifyEvent e) {
153 String text = fCommandCombo.getText();
154 // goAction.setEnabled(text.length() > 0);
157 fCommandCombo.addSelectionListener(new SelectionListener() {
158 public void widgetSelected(SelectionEvent e) {
159 String text = fCommandCombo.getItem(fCommandCombo.getSelectionIndex());
160 if (text.length() > 0) {
161 fCommandCombo.setText(text);
162 // executeCommand(text);
165 public void widgetDefaultSelected(SelectionEvent e) {
166 executeCommand(fCommandCombo.getText());
169 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
170 fCommandCombo.setLayoutData(gd);
171 // ToolBar toolbar = new ToolBar(parent, SWT.FLAT | SWT.HORIZONTAL);
172 // toolBarManager = new ToolBarManager(toolbar);
174 // IToolBarManager localBar =
175 // getViewSite().getActionBars().getToolBarManager();
176 // localBar.add(backwardAction);
177 // localBar.add(forwardAction);
180 private void executeCommand(String command) {
182 write( "Test: "+command );
183 String[] items = fCommandCombo.getItems();
185 String normURL = command;
186 for (int i = 0; i < items.length; i++) {
187 String normItem = items[i];
188 if (normURL.equals(normItem)) {
195 fCommandCombo.remove(loc);
197 fCommandCombo.add(command, 0);
198 if (fCommandCombo.getItemCount() > COMMAND_COMBO_SIZE) {
199 fCommandCombo.remove(fCommandCombo.getItemCount() - 1);
201 fCommandCombo.getParent().layout(true);
204 private void hookContextMenu() {
205 MenuManager menuMgr = new MenuManager("#PopupMenu");
206 menuMgr.setRemoveAllWhenShown(true);
207 menuMgr.addMenuListener(new IMenuListener() {
208 public void menuAboutToShow(IMenuManager manager) {
209 PHPConsole.this.fillContextMenu(manager);
212 Menu menu = menuMgr.createContextMenu(fViewer.getControl());
213 fViewer.getControl().setMenu(menu);
214 getSite().registerContextMenu(menuMgr, fViewer);
217 private void contributeToActionBars() {
218 IActionBars bars = getViewSite().getActionBars();
219 fillLocalPullDown(bars.getMenuManager());
220 fillLocalToolBar(bars.getToolBarManager());
223 private void fillLocalPullDown(IMenuManager manager) {
224 manager.add(cutAction);
225 manager.add(copyAction);
226 manager.add(pasteAction);
227 manager.add(selectAllAction);
230 private void fillContextMenu(IMenuManager manager) {
231 manager.add(cutAction);
232 manager.add(copyAction);
233 manager.add(pasteAction);
234 manager.add(selectAllAction);
235 // Other plug-ins can contribute there actions here
236 manager.add(new Separator("Additions"));
239 private void fillLocalToolBar(IToolBarManager manager) {
240 manager.add(clearAction);
243 * Insert the method's description here.
244 * @see ViewPart#setFocus
246 public void setFocus() {
250 * Set the text for the viewer
252 public void setOutputText(String text) {
253 fDocument = new Document(text);
254 fViewer.setDocument(fDocument);
257 public void appendOutputText(String text) {
259 if (fDocument == null) {
260 fDocument = new Document(text);
261 fViewer.setDocument(fDocument);
263 fDocument.replace(fDocument.getLength(), 0, text);
265 } catch (BadLocationException e) {
267 // viewer.setDocument(document);
270 public static PHPConsole getInstance() {
271 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
272 PHPConsole console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
274 if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
277 page.showView(PHPConsole.CONSOLE_ID);
278 if (console == null) {
279 console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
281 } catch (PartInitException e) {
282 PHPeclipsePlugin.getDefault().getLog().log(
285 PHPeclipsePlugin.getPluginId(),
287 PHPActionMessages.getString("PHPStartApacheAction.consoleViewOpeningProblem"),
296 * Prints out the string represented by the string buffer
298 public synchronized void write(String output) {
299 appendOutputText(output);
303 * Creates a string buffer from the given input stream
305 public static String getStringFromStream(InputStream stream) throws IOException {
306 StringBuffer buffer = new StringBuffer();
307 byte[] b = new byte[100];
309 while (finished != -1) {
310 finished = stream.read(b);
311 if (finished != -1) {
312 String current = new String(b, 0, finished);
313 buffer.append(current);
316 return buffer.toString();