*** 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.phpdt.internal.ui.PHPUiImages;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.actions.PHPActionMessages;
21
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;
45
46 /**
47  * The PHPConsole is used to display the output if you start MySQL/Apache
48  * @see ViewPart
49  */
50 public class PHPConsole extends ViewPart {
51
52         public static final String CONSOLE_ID =
53                 "net.sourceforge.phpeclipse.views.phpconsoleview";
54
55         private TextViewer viewer = null;
56         private Document document = null;
57         private StyledText widget;
58         private Action cutAction = new Action() {
59                 public void run() {
60                         viewer.getTextWidget().cut();
61                 }
62         };
63         private Action copyAction = new Action() {
64                 public void run() {
65                         widget.copy();
66                 }
67         };
68         private Action pasteAction = new Action() {
69                 public void run() {
70                         viewer.getTextWidget().paste();
71                 }
72         };
73         private Action selectAllAction = new Action() {
74                 public void run() {
75                         widget.selectAll();
76                 }
77         };
78         private Action clearAction = new Action() {
79                 public void run() {
80                         widget.setText("");
81                 }
82         };
83         /**
84          * The constructor.
85          */
86         public PHPConsole() {
87         }
88
89         /**
90          * Insert the method's description here.
91          * @see ViewPart#createPartControl
92          */
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);
98
99                 widget = viewer.getTextWidget();
100                 widget.setFont(
101                         JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
102
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");
110                                 
111                 IActionBars bars = this.getViewSite().getActionBars();
112                 bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
113                 bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
114                 bars.setGlobalActionHandler(
115                         IWorkbenchActionConstants.PASTE,
116                         pasteAction);
117
118                 hookContextMenu();
119                 //      hookDoubleClickAction();
120                 contributeToActionBars();
121                 
122                 
123         }
124
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);
131                         }
132                 });
133                 Menu menu = menuMgr.createContextMenu(viewer.getControl());
134                 viewer.getControl().setMenu(menu);
135                 getSite().registerContextMenu(menuMgr, viewer);
136         }
137
138         private void contributeToActionBars() {
139                 IActionBars bars = getViewSite().getActionBars();
140                 fillLocalPullDown(bars.getMenuManager());
141                 fillLocalToolBar(bars.getToolBarManager());
142         }
143
144         private void fillLocalPullDown(IMenuManager manager) {
145                 manager.add(cutAction);
146                 manager.add(copyAction);
147                 manager.add(pasteAction);
148                 manager.add(selectAllAction);
149         }
150         
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"));
158         }
159         
160         private void fillLocalToolBar(IToolBarManager manager) {
161                 manager.add(clearAction);
162         }
163         /**
164          * Insert the method's description here.
165          * @see ViewPart#setFocus
166          */
167         public void setFocus() {
168         }
169
170         /**
171          * Set the text for the viewer
172          */
173         public void setOutputText(String text) {
174                 document = new Document(text);
175                 viewer.setDocument(document);
176         }
177
178         public void appendOutputText(String text) {
179                 try {
180                         if (document == null) {
181                                 document = new Document(text);
182                                 viewer.setDocument(document);
183                         } else {
184                                 document.replace(document.getLength(), 0, text);
185                         }
186                 } catch (BadLocationException e) {
187                 }
188                 //  viewer.setDocument(document);
189         }
190
191         public static PHPConsole getInstance() {
192                 IWorkbenchPage page =
193                         PlatformUI
194                                 .getWorkbench()
195                                 .getActiveWorkbenchWindow()
196                                 .getActivePage();
197                 PHPConsole console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
198
199                 if (PHPeclipsePlugin
200                         .getDefault()
201                         .getPreferenceStore()
202                         .getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE)
203                         == true) {
204
205                         try {
206                                 page.showView(PHPConsole.CONSOLE_ID);
207                                 if (console == null) {
208                                         console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
209                                 }
210                         } catch (PartInitException e) {
211                                 PHPeclipsePlugin.getDefault().getLog().log(
212                                         new Status(
213                                                 IStatus.ERROR,
214                                                 PHPeclipsePlugin.getPluginId(),
215                                                 0,
216                                                 PHPActionMessages.getString(
217                                                         "PHPStartApacheAction.consoleViewOpeningProblem"),
218                                                 e));
219                         }
220
221                 }
222                 return console;
223         }
224
225         /**
226          * Prints out the string represented by the string buffer
227          */
228         public synchronized void write(String output) {
229                 appendOutputText(output);
230         }
231
232         /**
233          * Creates a string buffer from the given input stream
234          */
235         public static String getStringFromStream(InputStream stream)
236                 throws IOException {
237                 StringBuffer buffer = new StringBuffer();
238                 byte[] b = new byte[100];
239                 int finished = 0;
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);
245                         }
246                 }
247                 return buffer.toString();
248         }
249
250 }