KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > actions > JTidyFormatAction


1 package org.hibernate.eclipse.console.actions;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.util.Iterator JavaDoc;
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     /**
27      * Constructor for Action1.
28      */

29     public JTidyFormatAction() {
30         super();
31         
32         
33         
34     }
35
36     /**
37      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
38      */

39     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
40         this.targetPart = targetPart;
41     }
42
43     /**
44      * @see IActionDelegate#run(IAction)
45      */

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 JavaDoc iterator = selection.iterator();
51             try {
52             while(iterator.hasNext()) {
53                 IFile file = (IFile) iterator.next();
54                 InputStream JavaDoc contents = null ;
55                 ByteArrayOutputStream JavaDoc bos = null;
56                 ByteArrayInputStream JavaDoc stream = null;
57                 try {
58                     contents = file.getContents();
59                     bos = new ByteArrayOutputStream JavaDoc();
60                     XMLPrettyPrinter.prettyPrint(contents, bos);
61                     stream = new ByteArrayInputStream JavaDoc(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 JavaDoc io) {
72                 HibernateConsolePlugin.getDefault().showError(targetPart.getSite().getShell(), "Error while running JTidy", io);
73             }
74         }
75     }
76
77     /* (non-Javadoc)
78      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
79      */

80     public void selectionChanged(IAction action, ISelection selection) {
81         // TODO Auto-generated method stub
82

83     }
84
85 }
86
Popular Tags