intial version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.jtidy / src / net / sourceforge / phpdt / tidy / JTidyConsole.java
1 package net.sourceforge.phpdt.tidy;
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 org.eclipse.jface.action.Action;
16 import org.eclipse.jface.resource.JFaceResources;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.Document;
19 import org.eclipse.jface.text.TextViewer;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.StyledText;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IWorkbenchActionConstants;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.PartInitException;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.part.ViewPart;
30
31 /**
32  * The JTidyConsole is used to display the low level JTidy output
33  * 
34  * @see ViewPart
35  */
36 public class JTidyConsole extends ViewPart {
37
38   public static final String CONSOLE_ID = "net.sourceforge.phpdt.tidy.consoleview";
39
40   private TextViewer viewer = null;
41   private Document document = null;
42
43   /**
44    * The constructor.
45    */
46   public JTidyConsole() {
47   }
48
49   /**
50    * Insert the method's description here.
51    * @see ViewPart#createPartControl
52    */
53   public void createPartControl(Composite parent) {
54     viewer = new TextViewer(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
55     GridData viewerData = new GridData(GridData.FILL_BOTH);
56     viewer.getControl().setLayoutData(viewerData);
57     viewer.setEditable(false);
58     
59     StyledText widget = viewer.getTextWidget();
60     widget.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
61     Action cutAction = new Action() {
62       public void run() {
63         viewer.getTextWidget().cut();
64       }
65     };
66     Action copyAction = new Action() {
67       public void run() {
68         viewer.getTextWidget().copy();
69       }
70     };
71     Action pasteAction = new Action() {
72       public void run() {
73         viewer.getTextWidget().paste();
74       }
75     };
76
77     IActionBars bars = this.getViewSite().getActionBars();
78     bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
79     bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
80     bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
81   }
82
83   /**
84    * Insert the method's description here.
85    * @see ViewPart#setFocus
86    */
87   public void setFocus() {
88   }
89
90   /**
91    * Set the text for the viewer
92    */
93   private void setOutputText(String text) {
94     document = new Document(text);
95     viewer.setDocument(document);
96   }
97
98   private void appendOutputText(String text) {
99     try {
100       if (document == null) {
101         document = new Document(text);
102         viewer.setDocument(document);
103       }
104       document.replace(document.getLength(), 0, text);
105     } catch (BadLocationException e) {
106     }
107     //  viewer.setDocument(document);
108   }
109
110   /**
111    * Prints out the string represented by the string buffer
112    */
113   public static void print(String output) {
114     try {
115       IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
116       JTidyConsole console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID);
117
118       if (console != null) {
119         console.appendOutputText(output);
120       } else {
121         page.showView(JTidyConsole.CONSOLE_ID);
122         console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID);
123         console.setOutputText(output);
124       }
125     } catch (PartInitException e) {
126         System.err.println("Problems occured then opening console view");
127 //              JtidyPlugin.getDefault().getLog().log(
128 //        new Status(
129 //          IStatus.ERROR,
130 //              JtidyPlugin.getPluginId(),
131 //          0,
132 //              JtidyPlugin.getString("consoleViewOpeningProblem"),
133 //          e));
134     }
135
136   }
137   public static void clear() {
138           try {
139                 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
140                 JTidyConsole console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID);
141
142                 if (console != null) {
143                         console.setOutputText("");
144                 } else {
145                   page.showView(JTidyConsole.CONSOLE_ID);
146                   console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID);
147                   console.setOutputText("");
148                 }
149           } catch (PartInitException e) {
150                   System.err.println("Problems occured then opening console view");
151           }
152   }
153   public static void println(String output) {
154         print(output+"\n");
155   }
156   /**
157    * Creates a string buffer from the given input stream
158    */
159 //  public static String getStringFromStream(InputStream stream) throws IOException {
160 //    StringBuffer buffer = new StringBuffer();
161 //    byte[] b = new byte[100];
162 //    int finished = 0;
163 //    while (finished != -1) {
164 //      finished = stream.read(b);
165 //      if (finished != -1) {
166 //        String current = new String(b, 0, finished);
167 //        buffer.append(current);
168 //      }
169 //    }
170 //    return buffer.toString();
171 //  }
172
173 }