package codeStatistics.actions;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.MessageConsole;
import codeStatistics.Utils;
import ast.XmlVisitor;
/**
* Our sample action implements workbench action delegate. The action proxy will
* be created by the workbench and shown in the UI. When the user tries to use
* the action, this delegate will be created and execution will be delegated to
* it.
*
* @see IWorkbenchWindowActionDelegate
*/
public class PrintXml implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
private ITreeSelection selection;
private MessageConsole findConsole(String name) {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (int i = 0; i < existing.length; i++)
if (name.equals(existing[i].getName()))
return (MessageConsole) existing[i];
// no console found, so create a new one
MessageConsole myConsole = new MessageConsole(name, null);
conMan.addConsoles(new IConsole[] { myConsole });
return myConsole;
}
/**
* The action has been activated. The argument of the method represents the
* 'real' action sitting in the workbench UI.
*
* @see IWorkbenchWindowActionDelegate#run
*/
public void run(IAction action) {
if (selection == null)
return;
try {
MessageConsole console = findConsole("Code Statistics: XML OUTPUT");
String id = IConsoleConstants.ID_CONSOLE_VIEW;
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IConsoleView view = (IConsoleView) page.showView(id);
view.display(console);
PrintWriter out = new PrintWriter(new BufferedOutputStream(console.newMessageStream()));
for (ICompilationUnit icu : Utils.getSelectedIcu(selection)) {
CompilationUnit cu = Utils.parse(icu);
XmlVisitor visitor;
visitor = new XmlVisitor();
cu.accept(visitor);
String xmlDoc = "FILE: " + cu.getJavaElement().getElementName() + "\n" + Utils.print(visitor.getDocument());
for(String line:xmlDoc.split("\n"))
out.println(line);
out.flush();
}
} catch (ParserConfigurationException e) {
Utils.displayError(window, e);
} catch (TransformerException e) {
Utils.displayError(window, e);
} catch (JavaModelException e) {
Utils.displayError(window, e);
} catch (PartInitException e) {
Utils.displayError(window, e);
}
}
/**
* Selection in the workbench has been changed. We can change the state of
* the 'real' action here if we want, but this can only happen after the
* delegate has been created.
*
* @see IWorkbenchWindowActionDelegate#selectionChanged
*/
public void selectionChanged(IAction action, ISelection selection) {
if (selection instanceof ITreeSelection) {
this.selection = (ITreeSelection) selection;
}
}
/**
* We can use this method to dispose of any system resources we previously
* allocated.
*
* @see IWorkbenchWindowActionDelegate#dispose
*/
public void dispose() {
}
/**
* We will cache window object in order to be able to provide parent shell
* for the message dialog.
*
* @see IWorkbenchWindowActionDelegate#init
*/
public void init(IWorkbenchWindow window) {
this.window = window;
}
}