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
13 **********************************************************************/
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
20 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
21 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22 import net.sourceforge.phpeclipse.actions.PHPActionMessages;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.IMenuListener;
28 import org.eclipse.jface.action.IMenuManager;
29 import org.eclipse.jface.action.IToolBarManager;
30 import org.eclipse.jface.action.MenuManager;
31 import org.eclipse.jface.action.Separator;
32 import org.eclipse.jface.resource.JFaceResources;
33 import org.eclipse.jface.text.BadLocationException;
34 import org.eclipse.jface.text.Document;
35 import org.eclipse.jface.text.TextViewer;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.custom.StyledText;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Menu;
41 import org.eclipse.ui.IActionBars;
42 import org.eclipse.ui.IWorkbenchActionConstants;
43 import org.eclipse.ui.IWorkbenchPage;
44 import org.eclipse.ui.PartInitException;
45 import org.eclipse.ui.part.ViewPart;
48 * The PHPConsole is used to display the output if you start MySQL/Apache
51 public class PHPConsole extends ViewPart {
53 public static final String CONSOLE_ID =
54 "net.sourceforge.phpeclipse.views.phpconsoleview";
55 private int COMMAND_COMBO_SIZE = 10;
57 private TextViewer fViewer = null;
58 private Document fDocument = null;
59 private StyledText fStyledText;
60 // private Combo fCommandCombo;
61 // private ProcessOutputWriter consoleOut;
62 // private ProcessOutputWriter consoleErr;
64 // private Action goAction;
66 private Action cutAction = new Action() {
68 fViewer.getTextWidget().cut();
71 private Action copyAction = new Action() {
76 // private Action pasteAction = new Action() {
77 // public void run() {
78 // fViewer.getTextWidget().paste();
81 private Action selectAllAction = new Action() {
83 fStyledText.selectAll();
86 private Action clearAction = new Action() {
88 fStyledText.setText("");
98 * Insert the method's description here.
99 * @see ViewPart#createPartControl
101 public void createPartControl(Composite parent) {
102 // Composite container = new Composite(parent, SWT.NULL);
103 // // control = container;
104 // GridLayout layout = new GridLayout();
105 // layout.marginWidth = 0;
106 // layout.marginHeight = 0;
107 // layout.verticalSpacing = 0;
108 // container.setLayout(layout);
109 // Composite navContainer = new Composite(container, SWT.NONE);
110 // layout = new GridLayout();
111 // layout.numColumns = 2;
112 // layout.marginHeight = 1;
113 // navContainer.setLayout(layout);
114 // createCommandBar(navContainer);
115 // navContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117 fViewer = new TextViewer(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
118 GridData viewerData = new GridData(GridData.FILL_BOTH);
119 fViewer.getControl().setLayoutData(viewerData);
120 fViewer.setEditable(false);
122 fStyledText = fViewer.getTextWidget();
124 JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
126 cutAction.setText("Cut");
127 copyAction.setText("Copy");
128 // pasteAction.setText("Paste");
129 selectAllAction.setText("Select All");
130 clearAction.setText("Clear PHP Console");
131 clearAction.setImageDescriptor(PHPUiImages.DESC_CLEAR);
132 clearAction.setToolTipText("Clear PHP Console");
134 IActionBars bars = this.getViewSite().getActionBars();
135 bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
136 bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
137 // bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
140 // hookDoubleClickAction();
141 contributeToActionBars();
143 // appendOutputText("This is the PHP console.\n");
144 // appendOutputText("Type: \"php $f\" to run the current editor file.\n");
148 private void createCommandBar(Composite parent) {
149 // Label addressLabel = new Label(parent, SWT.NONE);
150 // addressLabel.setText("Command:");
152 // fCommandCombo = new Combo(parent, SWT.DROP_DOWN | SWT.BORDER);
153 // fCommandCombo.addModifyListener(new ModifyListener() {
154 // public void modifyText(ModifyEvent e) {
155 // String text = fCommandCombo.getText();
156 // // goAction.setEnabled(text.length() > 0);
159 // fCommandCombo.addSelectionListener(new SelectionListener() {
160 // public void widgetSelected(SelectionEvent e) {
161 // String text = fCommandCombo.getItem(fCommandCombo.getSelectionIndex());
162 // if (text.length() > 0) {
163 // fCommandCombo.setText(text);
164 // // executeCommand(text);
167 // public void widgetDefaultSelected(SelectionEvent e) {
168 // executeCommand(fCommandCombo.getText());
171 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
172 // fCommandCombo.setLayoutData(gd);
175 // private void executeCommand(String command) {
177 // if (command.equals("")) {
178 // fCommandCombo.forceFocus();
183 // fCommandCombo.forceFocus();
184 // // add to Combo history
185 // String[] items = fCommandCombo.getItems();
187 // String normURL = command;
188 // for (int i = 0; i < items.length; i++) {
189 // String normItem = items[i];
190 // if (normURL.equals(normItem)) {
197 // fCommandCombo.remove(loc);
199 // fCommandCombo.add(command, 0);
200 // if (fCommandCombo.getItemCount() > COMMAND_COMBO_SIZE) {
201 // fCommandCombo.remove(fCommandCombo.getItemCount() - 1);
203 // fCommandCombo.getParent().layout(true);
206 // private void execute(String command) {
208 // ArrayList args = new ArrayList();
210 // command = command.replace('\\', '§');
212 // StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(command));
213 // tokenizer.resetSyntax();
215 // tokenizer.whitespaceChars(0, ' ');
216 // tokenizer.wordChars('!', 255);
218 // tokenizer.quoteChar('"');
219 // tokenizer.quoteChar('\'');
223 // while ((token = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) {
224 // if (token == StreamTokenizer.TT_WORD) {
225 // args.add(tokenizer.sval);
228 // } catch (IOException e) {
232 // // replace variables in arguments
234 //// IFile file = getFile();
235 // IFile file = PHPeclipsePlugin.getDefault().getLastEditorFile();
236 // if (file != null) {
237 // String fileLocation = file.getLocation().toString();
238 // for (int i = 0; i < args.size(); i++) {
239 // arg = args.get(i).toString();
240 // if (arg.equals("$f")) {
241 // //current php editor file
242 // if (File.separatorChar == '\\') {
243 // fileLocation = fileLocation.replace('/', '\\');
245 // args.set(i, fileLocation);
250 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
254 // StringBuffer commandBuffer = new StringBuffer(1024);
255 // // Program.launch(command);
256 // if (args.size() > 0) {
257 // arg0 = (String) args.get(0);
258 // arg0 = arg0.replace('§', '\\');
260 // if (arg0.equals("php")) {
261 // temp = store.getString(PHPeclipsePlugin.PHP_RUN_PREF);
262 // if (temp != null) {
266 // commandBuffer.append(arg0 + " ");
268 // String[] stringArgs = new String[args.size()];
269 // for (int i = 0; i < args.size(); i++) {
270 // arg = (String) args.get(i);
271 // arg = arg.replace('§', '\\');
272 // stringArgs[i] = arg;
273 // commandBuffer.append(arg + " ");
275 // commandBuffer.append("\n");
278 // command = commandBuffer.toString();
279 // write(command+"\n");
280 // Runtime runtime = Runtime.getRuntime();
282 // // runs the command
283 // Process process = runtime.exec(command);
285 // consoleOut = new ProcessOutputWriter(process.getInputStream());
286 // consoleOut.start();
287 // consoleErr = new ProcessOutputWriter(process.getErrorStream());
288 // consoleErr.start();
290 // //process.waitFor();
291 //// InputStream in = process.getInputStream();
292 //// String output = getStringFromStream(in);
295 //// } catch (InterruptedException e) {
296 //// write(e.toString());
297 // } catch (IOException e) {
298 // write(e.toString());
302 private void hookContextMenu() {
303 MenuManager menuMgr = new MenuManager("#PopupMenu");
304 menuMgr.setRemoveAllWhenShown(true);
305 menuMgr.addMenuListener(new IMenuListener() {
306 public void menuAboutToShow(IMenuManager manager) {
307 PHPConsole.this.fillContextMenu(manager);
310 Menu menu = menuMgr.createContextMenu(fViewer.getControl());
311 fViewer.getControl().setMenu(menu);
312 getSite().registerContextMenu(menuMgr, fViewer);
315 private void contributeToActionBars() {
316 IActionBars bars = getViewSite().getActionBars();
317 fillLocalPullDown(bars.getMenuManager());
318 fillLocalToolBar(bars.getToolBarManager());
321 private void fillLocalPullDown(IMenuManager manager) {
322 manager.add(cutAction);
323 manager.add(copyAction);
324 // manager.add(pasteAction);
325 manager.add(selectAllAction);
328 private void fillContextMenu(IMenuManager manager) {
329 manager.add(cutAction);
330 manager.add(copyAction);
331 // manager.add(pasteAction);
332 manager.add(selectAllAction);
333 // Other plug-ins can contribute there actions here
334 manager.add(new Separator("Additions"));
337 private void fillLocalToolBar(IToolBarManager manager) {
338 manager.add(clearAction);
341 * Insert the method's description here.
342 * @see ViewPart#setFocus
344 public void setFocus() {
345 // fCommandCombo.forceFocus();
349 * Set the text for the viewer
351 public void setOutputText(String text) {
352 fDocument = new Document(text);
353 fViewer.setDocument(fDocument);
356 public void appendOutputText(String text) {
358 if (fDocument == null) {
359 fDocument = new Document(text);
360 fViewer.setDocument(fDocument);
362 fDocument.replace(fDocument.getLength(), 0, text);
364 } catch (BadLocationException e) {
366 // viewer.setDocument(document);
369 public static PHPConsole getInstance() {
370 // IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
371 IWorkbenchPage page =
375 .getActiveWorkbenchWindow()
377 PHPConsole console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
381 .getPreferenceStore()
382 .getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE)
385 page.showView(PHPConsole.CONSOLE_ID);
386 if (console == null) {
387 console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
389 } catch (PartInitException e) {
390 PHPeclipsePlugin.getDefault().getLog().log(
393 PHPeclipsePlugin.getPluginId(),
395 PHPActionMessages.getString(
396 "PHPStartApacheAction.consoleViewOpeningProblem"),
404 * Prints out the string represented by the string
406 public synchronized void write(String output) {
407 appendOutputText(output);
411 * Creates a string buffer from the given input stream
413 public static String getStringFromStream(InputStream stream)
415 StringBuffer buffer = new StringBuffer();
416 byte[] b = new byte[100];
418 while (finished != -1) {
419 finished = stream.read(b);
420 if (finished != -1) {
421 String current = new String(b, 0, finished);
422 buffer.append(current);
425 return buffer.toString();
429 * Finds the file that's currently opened in the PHP Text Editor
431 // protected IFile getFile() {
432 // ITextEditor editor = PHPeclipsePlugin.getDefault().getLastEditorFile();
434 // IEditorInput editorInput = null;
435 // if (editor != null) {
436 // editorInput = editor.getEditorInput();
439 // if (editorInput instanceof IFileEditorInput)
440 // return ((IFileEditorInput) editorInput).getFile();
442 // // if nothing was found, which should never happen
446 class ProcessOutputWriter extends Thread {
447 boolean fStreamClosed;
448 InputStream fInputStream;
450 ProcessOutputWriter(InputStream inputStream) {
451 fInputStream = inputStream;
452 fStreamClosed = false;
455 public void closeStream() {
456 fStreamClosed = true;
458 fInputStream.close();
459 } catch (IOException io) {
466 new BufferedReader(new InputStreamReader(fInputStream));
469 while ((line = in.readLine()) != null) {
473 } catch (Exception e) {
474 e.printStackTrace(System.out);
475 if (!fStreamClosed) {
476 // write("\nPHP Console Exception: "+ e.toString() );