1 package org.hibernate.eclipse.console.actions; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.util.Iterator ; 8 9 import org.eclipse.core.resources.IFile; 10 import org.eclipse.core.runtime.CoreException; 11 import org.eclipse.jface.action.IAction; 12 import org.eclipse.jface.dialogs.MessageDialog; 13 import org.eclipse.jface.viewers.ISelection; 14 import org.eclipse.jface.viewers.IStructuredSelection; 15 import org.eclipse.ui.IActionDelegate; 16 import org.eclipse.ui.IObjectActionDelegate; 17 import org.eclipse.ui.IWorkbenchPart; 18 import org.eclipse.ui.internal.PluginAction; 19 import org.hibernate.eclipse.console.HibernateConsolePlugin; 20 import org.hibernate.tool.hbm2x.XMLPrettyPrinter; 21 22 public class JTidyFormatAction implements IObjectActionDelegate { 23 24 private IWorkbenchPart targetPart; 25 26 29 public JTidyFormatAction() { 30 super(); 31 32 33 34 } 35 36 39 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 40 this.targetPart = targetPart; 41 } 42 43 46 public void run(IAction action) { 47 IStructuredSelection selection = (IStructuredSelection) ((PluginAction)action).getSelection(); 48 49 if(selection!=null && MessageDialog.openQuestion(targetPart.getSite().getShell(), "Format with JTidy", "Do you want to format " + selection.size() + " xml files with JTidy ?")) { 50 Iterator iterator = selection.iterator(); 51 try { 52 while(iterator.hasNext()) { 53 IFile file = (IFile) iterator.next(); 54 InputStream contents = null ; 55 ByteArrayOutputStream bos = null; 56 ByteArrayInputStream stream = null; 57 try { 58 contents = file.getContents(); 59 bos = new ByteArrayOutputStream (); 60 XMLPrettyPrinter.prettyPrint(contents, bos); 61 stream = new ByteArrayInputStream (bos.toByteArray()); 62 file.setContents(stream, true, true, null); 63 } finally { 64 if(stream!=null) stream.close(); 65 if(bos!=null) bos.close(); 66 if(contents!=null) contents.close(); 67 } 68 } 69 } catch (CoreException e) { 70 HibernateConsolePlugin.getDefault().showError(targetPart.getSite().getShell(), "Error while running JTidy", e); 71 } catch (IOException io) { 72 HibernateConsolePlugin.getDefault().showError(targetPart.getSite().getShell(), "Error while running JTidy", io); 73 } 74 } 75 } 76 77 80 public void selectionChanged(IAction action, ISelection selection) { 81 83 } 84 85 } 86 | Popular Tags |