added command line (Combobox) to PHPConsole - no functionality yet
[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.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;
52
53 /**
54  * The PHPConsole is used to display the output if you start MySQL/Apache
55  * @see ViewPart
56  */
57 public class PHPConsole extends ViewPart {
58
59   public static final String CONSOLE_ID = "net.sourceforge.phpeclipse.views.phpconsoleview";
60   private int COMMAND_COMBO_SIZE = 10;
61
62   private TextViewer fViewer = null;
63   private Document fDocument = null;
64   private StyledText fStyledText;
65   private Combo fCommandCombo;
66   // private Action goAction;
67
68   private Action cutAction = new Action() {
69     public void run() {
70       fViewer.getTextWidget().cut();
71     }
72   };
73   private Action copyAction = new Action() {
74     public void run() {
75       fStyledText.copy();
76     }
77   };
78   private Action pasteAction = new Action() {
79     public void run() {
80       fViewer.getTextWidget().paste();
81     }
82   };
83   private Action selectAllAction = new Action() {
84     public void run() {
85       fStyledText.selectAll();
86     }
87   };
88   private Action clearAction = new Action() {
89     public void run() {
90       fStyledText.setText("");
91     }
92   };
93   /**
94    * The constructor.
95    */
96   public PHPConsole() {
97   }
98
99   /**
100    * Insert the method's description here.
101    * @see ViewPart#createPartControl
102    */
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));
118
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);
123
124     fStyledText = fViewer.getTextWidget();
125     fStyledText.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
126
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");
134
135     IActionBars bars = this.getViewSite().getActionBars();
136     bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
137     bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
138     bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
139
140     hookContextMenu();
141     //  hookDoubleClickAction();
142     contributeToActionBars();
143
144   }
145
146   private void createCommandBar(Composite parent) {
147     Label addressLabel = new Label(parent, SWT.NONE);
148     addressLabel.setText("Command:");
149
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);
155       }
156     });
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);
163         }
164       }
165       public void widgetDefaultSelected(SelectionEvent e) {
166         executeCommand(fCommandCombo.getText());
167       }
168     });
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);
173     //    makeActions();
174     //    IToolBarManager localBar =
175     //      getViewSite().getActionBars().getToolBarManager();
176     //    localBar.add(backwardAction);
177     //    localBar.add(forwardAction);
178   }
179
180   private void executeCommand(String command) {
181     command.trim();
182     write( "Test: "+command );
183     String[] items = fCommandCombo.getItems();
184     int loc = -1;
185     String normURL = command;
186     for (int i = 0; i < items.length; i++) {
187       String normItem = items[i];
188       if (normURL.equals(normItem)) {
189         // match 
190         loc = i;
191         break;
192       }
193     }
194     if (loc != -1) {
195       fCommandCombo.remove(loc);
196     }
197     fCommandCombo.add(command, 0);
198     if (fCommandCombo.getItemCount() > COMMAND_COMBO_SIZE) {
199       fCommandCombo.remove(fCommandCombo.getItemCount() - 1);
200     }
201     fCommandCombo.getParent().layout(true);
202   }
203
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);
210       }
211     });
212     Menu menu = menuMgr.createContextMenu(fViewer.getControl());
213     fViewer.getControl().setMenu(menu);
214     getSite().registerContextMenu(menuMgr, fViewer);
215   }
216
217   private void contributeToActionBars() {
218     IActionBars bars = getViewSite().getActionBars();
219     fillLocalPullDown(bars.getMenuManager());
220     fillLocalToolBar(bars.getToolBarManager());
221   }
222
223   private void fillLocalPullDown(IMenuManager manager) {
224     manager.add(cutAction);
225     manager.add(copyAction);
226     manager.add(pasteAction);
227     manager.add(selectAllAction);
228   }
229
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"));
237   }
238
239   private void fillLocalToolBar(IToolBarManager manager) {
240     manager.add(clearAction);
241   }
242   /**
243    * Insert the method's description here.
244    * @see ViewPart#setFocus
245    */
246   public void setFocus() {
247   }
248
249   /**
250    * Set the text for the viewer
251    */
252   public void setOutputText(String text) {
253     fDocument = new Document(text);
254     fViewer.setDocument(fDocument);
255   }
256
257   public void appendOutputText(String text) {
258     try {
259       if (fDocument == null) {
260         fDocument = new Document(text);
261         fViewer.setDocument(fDocument);
262       } else {
263         fDocument.replace(fDocument.getLength(), 0, text);
264       }
265     } catch (BadLocationException e) {
266     }
267     //  viewer.setDocument(document);
268   }
269
270   public static PHPConsole getInstance() {
271     IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
272     PHPConsole console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
273
274     if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
275
276       try {
277         page.showView(PHPConsole.CONSOLE_ID);
278         if (console == null) {
279           console = (PHPConsole) page.findView(PHPConsole.CONSOLE_ID);
280         }
281       } catch (PartInitException e) {
282         PHPeclipsePlugin.getDefault().getLog().log(
283           new Status(
284             IStatus.ERROR,
285             PHPeclipsePlugin.getPluginId(),
286             0,
287             PHPActionMessages.getString("PHPStartApacheAction.consoleViewOpeningProblem"),
288             e));
289       }
290
291     }
292     return console;
293   }
294
295   /**
296    * Prints out the string represented by the string buffer
297    */
298   public synchronized void write(String output) {
299     appendOutputText(output);
300   }
301
302   /**
303    * Creates a string buffer from the given input stream
304    */
305   public static String getStringFromStream(InputStream stream) throws IOException {
306     StringBuffer buffer = new StringBuffer();
307     byte[] b = new byte[100];
308     int finished = 0;
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);
314       }
315     }
316     return buffer.toString();
317   }
318
319 }