PageRenderTime 4437ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/velocity/plugins/com.liferay.ide.velocity/src/com/liferay/ide/velocity/vaulttec/ui/editor/actions/FormatResourceAction.java

https://gitlab.com/4615833/liferay-ide
Java | 249 lines | 190 code | 24 blank | 35 comment | 17 complexity | fa92bed5742a6491c48a061ecd2ab653 MD5 | raw file
  1. package com.liferay.ide.velocity.vaulttec.ui.editor.actions;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.Set;
  12. import org.eclipse.core.resources.IContainer;
  13. import org.eclipse.core.resources.IFile;
  14. import org.eclipse.core.resources.IFolder;
  15. import org.eclipse.core.resources.IProject;
  16. import org.eclipse.core.resources.IResource;
  17. import org.eclipse.core.resources.IWorkspaceRoot;
  18. import org.eclipse.core.runtime.CoreException;
  19. import org.eclipse.jface.action.IAction;
  20. import org.eclipse.jface.dialogs.MessageDialog;
  21. import org.eclipse.jface.text.Document;
  22. import org.eclipse.jface.text.IDocument;
  23. import org.eclipse.jface.viewers.ISelection;
  24. import org.eclipse.jface.viewers.ISelectionProvider;
  25. import org.eclipse.jface.viewers.IStructuredSelection;
  26. import org.eclipse.jface.viewers.StructuredSelection;
  27. import org.eclipse.swt.widgets.Shell;
  28. import org.eclipse.ui.IObjectActionDelegate;
  29. import org.eclipse.ui.IWorkbenchPart;
  30. import org.eclipse.ui.IWorkbenchPartSite;
  31. import com.liferay.ide.velocity.editor.VelocityEditor;
  32. import com.liferay.ide.velocity.vaulttec.ui.VelocityPlugin;
  33. /**
  34. * DOCUMENT ME!
  35. *
  36. * @author <a href="mailto:akmal.sarhan@gmail.com">Akmal Sarhan </a>
  37. * @version $Revision: 14 $
  38. */
  39. public class FormatResourceAction implements IObjectActionDelegate
  40. {
  41. private Object _selected = null;
  42. /**
  43. *
  44. */
  45. public FormatResourceAction()
  46. {
  47. // TODO Auto-generated constructor stub
  48. }
  49. private String lineSep = System.getProperty("line.separator");
  50. /**
  51. * @param document
  52. */
  53. private IDocument format(final IDocument document)
  54. {
  55. Formatter formatter=new Formatter();
  56. return formatter.format(document);
  57. }
  58. /*
  59. * (non-Javadoc)
  60. * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
  61. * org.eclipse.ui.IWorkbenchPart)
  62. */
  63. public void setActivePart(IAction action, IWorkbenchPart targetPart)
  64. {
  65. _part = targetPart;
  66. }
  67. private IWorkbenchPart _part;
  68. /*
  69. * (non-Javadoc)
  70. * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
  71. */
  72. public void run(IAction action)
  73. {
  74. IWorkbenchPartSite site = _part.getSite();
  75. provider = site.getSelectionProvider();
  76. if (_selected == null)
  77. {
  78. MessageDialog.openInformation(new Shell(), "VelocityPlugin", "Unable to open file");
  79. // VelocityPlugin.log("Unable to open file");
  80. return;
  81. }
  82. if (_selected instanceof IStructuredSelection)
  83. {
  84. //
  85. try
  86. {
  87. Object[] items = ((IStructuredSelection) _selected).toArray();
  88. Set files = new HashSet(items.length, 1.0F);
  89. try
  90. {
  91. for (int i = 0; i < items.length; i++)
  92. {
  93. if (items[i] instanceof IResource)
  94. {
  95. IResource resource = (IResource) items[i];
  96. switch (resource.getType())
  97. {
  98. case IResource.FOLDER:
  99. case IResource.PROJECT:
  100. IContainer folder = (IContainer) items[i];
  101. getChildren(folder, files);
  102. break;
  103. case IResource.FILE:
  104. files.add((IFile) items[i]);
  105. // ((IFile) items[i]).getProject()
  106. break;
  107. default:
  108. /**
  109. * @todo use logger to print warning about
  110. * invalid type
  111. */
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. catch (CoreException ex)
  118. {
  119. ex.printStackTrace();
  120. }
  121. for (Iterator iter = files.iterator(); iter.hasNext();)
  122. {
  123. IFile directory = (IFile) iter.next();
  124. formatFile(directory);
  125. }
  126. }
  127. catch (Exception e)
  128. {
  129. VelocityPlugin.log(e);
  130. }
  131. } else
  132. {
  133. MessageDialog.openInformation(new Shell(), "VelocityPlugin", "Unable to open file");
  134. // VelocityPlugin.log("Unable to open shell");
  135. return;
  136. }
  137. }
  138. /*
  139. * (non-Javadoc)
  140. * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
  141. * org.eclipse.jface.viewers.ISelection)
  142. */
  143. public void selectionChanged(IAction action, ISelection selection)
  144. {
  145. _selected = null;
  146. if (selection instanceof IStructuredSelection)
  147. {
  148. _selected = (IStructuredSelection) selection;
  149. }
  150. }
  151. private void getChildren(IContainer resource, Set files) throws CoreException
  152. {
  153. IResource[] children = resource.members();
  154. for (int i = 0; i < children.length; i++)
  155. {
  156. IResource child = children[i];
  157. switch (child.getType())
  158. {
  159. case IResource.FILE:
  160. if (child.getName().matches(".*?(html|vm|jspx|jspf)"))
  161. {
  162. files.add((IFile) child);
  163. }
  164. break;
  165. case IResource.FOLDER:
  166. getChildren((IFolder) child, files);
  167. break;
  168. case IResource.PROJECT:
  169. getChildren((IProject) child, files);
  170. break;
  171. case IResource.ROOT:
  172. getChildren((IWorkspaceRoot) child, files);
  173. break;
  174. }
  175. }
  176. }
  177. ISelectionProvider provider = null;
  178. private void formatFile(IFile file)
  179. {
  180. if (file.getFileExtension().matches(".*?(html|vm|jspx|jspf)"))
  181. {
  182. ISelection s = new StructuredSelection(file);
  183. provider.setSelection(s);
  184. IDocument document = new Document();
  185. BufferedWriter awriter = null;
  186. String line = null;
  187. StringBuffer b = new StringBuffer();
  188. try
  189. {
  190. BufferedReader in = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(file.getLocation().toFile()))));
  191. while ((line = in.readLine()) != null)
  192. {
  193. b.append(line);
  194. b.append(lineSep);
  195. }
  196. document.set(b.toString());
  197. document = format(document);
  198. if (file instanceof IResource)
  199. {
  200. file.setContents(new BufferedInputStream(new ByteArrayInputStream(document.get().getBytes())), IFile.FORCE | IFile.KEEP_HISTORY, null);
  201. ((IResource) file).refreshLocal(IResource.DEPTH_ZERO, null);
  202. }
  203. }
  204. catch (Exception e)
  205. {
  206. VelocityPlugin.log(e);
  207. }
  208. finally
  209. {
  210. if (awriter != null)
  211. {
  212. try
  213. {
  214. awriter.close();
  215. }
  216. catch (IOException e)
  217. {
  218. VelocityPlugin.log(e);
  219. }
  220. }
  221. }
  222. VelocityEditor.openInEditor(file);
  223. }
  224. }
  225. }