SQL Plugin copied from Quantum plugin and refactored for PHPEclipse
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / PHPSourceConsole.java
1 package net.sourceforge.phpdt.sql.view;
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 import net.sourceforge.phpdt.sql.PHPEclipseSQLPlugin;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.Document;
22 import org.eclipse.jface.text.TextViewer;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.StyledText;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IWorkbenchActionConstants;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.PartInitException;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.part.ViewPart;
33
34 /**
35  * The PHPSourceConsole is used to display the output from the PHP SQL wizards
36  * @see ViewPart
37  */
38 public class PHPSourceConsole extends ViewPart {
39
40   public static final String CONSOLE_ID =
41     "net.sourceforge.phpdt.sql.view.phpsourceconsoleview";
42
43   private TextViewer viewer = null;
44   private Document document = null;
45
46   /**
47    * The constructor.
48    */
49   public PHPSourceConsole() {
50   }
51
52   /**
53    * Insert the method's description here.
54    * @see ViewPart#createPartControl
55    */
56   public void createPartControl(Composite parent) {
57     viewer = new TextViewer(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
58     GridData viewerData = new GridData(GridData.FILL_BOTH);
59     viewer.getControl().setLayoutData(viewerData);
60     viewer.setEditable(false);
61
62     StyledText widget = viewer.getTextWidget();
63     widget.setFont(
64       JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
65     Action cutAction = new Action() {
66       public void run() {
67         viewer.getTextWidget().cut();
68       }
69     };
70     Action copyAction = new Action() {
71       public void run() {
72         viewer.getTextWidget().copy();
73       }
74     };
75     Action pasteAction = new Action() {
76       public void run() {
77         viewer.getTextWidget().paste();
78       }
79     };
80
81     IActionBars bars = this.getViewSite().getActionBars();
82     bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
83     bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
84     bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
85   }
86
87   /**
88    * Insert the method's description here.
89    * @see ViewPart#setFocus
90    */
91   public void setFocus() {
92   }
93
94   /**
95    * Set the text for the viewer
96    */
97   private void setOutputText(String text) {
98     document = new Document(text);
99     viewer.setDocument(document);
100   }
101
102   private void appendOutputText(String text) {
103     try {
104       if (document == null) {
105         document = new Document(text);
106         viewer.setDocument(document);
107       }
108       document.replace(document.getLength(), 0, text);
109     } catch (BadLocationException e) {
110     }
111     //  viewer.setDocument(document);
112   }
113
114   public static PHPSourceConsole getInstance() {
115     IWorkbenchPage page =
116       PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
117     PHPSourceConsole console = (PHPSourceConsole) page.findView(PHPSourceConsole.CONSOLE_ID);
118     // if (PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
119
120     try {
121       page.showView(PHPSourceConsole.CONSOLE_ID);
122       if (console == null) { 
123         console = (PHPSourceConsole) page.findView(PHPSourceConsole.CONSOLE_ID);
124       }
125     } catch (PartInitException e) {
126       PHPEclipseSQLPlugin.getDefault().getLog().log(
127         new Status(
128           IStatus.ERROR,
129           PHPEclipseSQLPlugin.PLUGIN_ID,
130           0,
131           Messages.getString("sqlconsole.viewopeningproblem"),
132           e));
133     }
134
135     //   }
136     return console;
137   }
138
139   /**
140    * Prints out the string represented by the string buffer
141    */
142   public synchronized void print(String output) {
143     appendOutputText(output);
144   }
145
146   /**
147    * Prints out the string represented by the string buffer
148    */
149   public synchronized void println(String output) {
150     appendOutputText(output+'\n');
151   }
152
153   public synchronized void clear() {
154     setOutputText("");
155   }
156 }