1 package net.sourceforge.phpdt.tidy;
 
   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
 
  12     Klaus Hartlage - www.eclipseproject.de
 
  13 **********************************************************************/
 
  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;
 
  32  * The JTidyConsole is used to display the low level JTidy output
 
  36 public class JTidyConsole extends ViewPart {
 
  38   public static final String CONSOLE_ID = "net.sourceforge.phpdt.tidy.consoleview";
 
  40   private TextViewer viewer = null;
 
  41   private Document document = null;
 
  46   public JTidyConsole() {
 
  50    * Insert the method's description here.
 
  51    * @see ViewPart#createPartControl
 
  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);
 
  59     StyledText widget = viewer.getTextWidget();
 
  60     widget.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
 
  61     Action cutAction = new Action() {
 
  63         viewer.getTextWidget().cut();
 
  66     Action copyAction = new Action() {
 
  68         viewer.getTextWidget().copy();
 
  71     Action pasteAction = new Action() {
 
  73         viewer.getTextWidget().paste();
 
  77     IActionBars bars = this.getViewSite().getActionBars();
 
  78     bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction);
 
  79     bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction);
 
  80     bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
 
  84    * Insert the method's description here.
 
  85    * @see ViewPart#setFocus
 
  87   public void setFocus() {
 
  91    * Set the text for the viewer
 
  93   private void setOutputText(String text) {
 
  94     document = new Document(text);
 
  95     viewer.setDocument(document);
 
  98   private void appendOutputText(String text) {
 
 100       if (document == null) {
 
 101         document = new Document(text);
 
 102         viewer.setDocument(document);
 
 104       document.replace(document.getLength(), 0, text);
 
 105     } catch (BadLocationException e) {
 
 107     //  viewer.setDocument(document);
 
 111    * Prints out the string represented by the string buffer
 
 113   public static void print(String output) {
 
 115       IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
 
 116       JTidyConsole console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID);
 
 118       if (console != null) {
 
 119         console.appendOutputText(output);
 
 121         page.showView(JTidyConsole.CONSOLE_ID);
 
 122         console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID);
 
 123         console.setOutputText(output);
 
 125     } catch (PartInitException e) {
 
 126         System.err.println("Problems occured then opening console view");
 
 127 //              JtidyPlugin.getDefault().getLog().log(
 
 130 //              JtidyPlugin.getPluginId(),
 
 132 //              JtidyPlugin.getString("consoleViewOpeningProblem"),
 
 137   public static void clear() {
 
 139                 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
 
 140                 JTidyConsole console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID);
 
 142                 if (console != null) {
 
 143                         console.setOutputText("");
 
 145                   page.showView(JTidyConsole.CONSOLE_ID);
 
 146                   console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID);
 
 147                   console.setOutputText("");
 
 149           } catch (PartInitException e) {
 
 150                   System.err.println("Problems occured then opening console view");
 
 153   public static void println(String output) {
 
 157    * Creates a string buffer from the given input stream
 
 159 //  public static String getStringFromStream(InputStream stream) throws IOException {
 
 160 //    StringBuffer buffer = new StringBuffer();
 
 161 //    byte[] b = new byte[100];
 
 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);
 
 170 //    return buffer.toString();