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