package net.sourceforge.phpdt.tidy; /********************************************************************** Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html Contributors: IBM Corporation - Initial implementation Klaus Hartlage - www.eclipseproject.de **********************************************************************/ import org.eclipse.jface.action.Action; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.TextViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.IActionBars; import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart; /** * The JTidyConsole is used to display the low level JTidy output * * @see ViewPart */ public class JTidyConsole extends ViewPart { public static final String CONSOLE_ID = "net.sourceforge.phpdt.tidy.consoleview"; private TextViewer viewer = null; private Document document = null; /** * The constructor. */ public JTidyConsole() { } /** * Insert the method's description here. * @see ViewPart#createPartControl */ public void createPartControl(Composite parent) { viewer = new TextViewer(parent, SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL); GridData viewerData = new GridData(GridData.FILL_BOTH); viewer.getControl().setLayoutData(viewerData); viewer.setEditable(false); StyledText widget = viewer.getTextWidget(); widget.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT)); Action cutAction = new Action() { public void run() { viewer.getTextWidget().cut(); } }; Action copyAction = new Action() { public void run() { viewer.getTextWidget().copy(); } }; Action pasteAction = new Action() { public void run() { viewer.getTextWidget().paste(); } }; IActionBars bars = this.getViewSite().getActionBars(); bars.setGlobalActionHandler(IWorkbenchActionConstants.CUT, cutAction); bars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyAction); bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction); } /** * Insert the method's description here. * @see ViewPart#setFocus */ public void setFocus() { } /** * Set the text for the viewer */ private void setOutputText(String text) { document = new Document(text); viewer.setDocument(document); } private void appendOutputText(String text) { try { if (document == null) { document = new Document(text); viewer.setDocument(document); } document.replace(document.getLength(), 0, text); } catch (BadLocationException e) { } // viewer.setDocument(document); } /** * Prints out the string represented by the string buffer */ public static void print(String output) { try { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); JTidyConsole console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID); if (console != null) { console.appendOutputText(output); } else { page.showView(JTidyConsole.CONSOLE_ID); console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID); console.setOutputText(output); } } catch (PartInitException e) { System.err.println("Problems occured then opening console view"); // JtidyPlugin.getDefault().getLog().log( // new Status( // IStatus.ERROR, // JtidyPlugin.getPluginId(), // 0, // JtidyPlugin.getString("consoleViewOpeningProblem"), // e)); } } public static void clear() { try { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); JTidyConsole console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID); if (console != null) { console.setOutputText(""); } else { page.showView(JTidyConsole.CONSOLE_ID); console = (JTidyConsole) page.findView(JTidyConsole.CONSOLE_ID); console.setOutputText(""); } } catch (PartInitException e) { System.err.println("Problems occured then opening console view"); } } public static void println(String output) { print(output+"\n"); } /** * Creates a string buffer from the given input stream */ // public static String getStringFromStream(InputStream stream) throws IOException { // StringBuffer buffer = new StringBuffer(); // byte[] b = new byte[100]; // int finished = 0; // while (finished != -1) { // finished = stream.read(b); // if (finished != -1) { // String current = new String(b, 0, finished); // buffer.append(current); // } // } // return buffer.toString(); // } }