1 package org.eclipse.emf.examples.jet.article2.actionexample; 2 3 4 import java.io.ByteArrayInputStream ; 5 import java.io.File ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.PrintWriter ; 9 import java.io.StringWriter ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.util.Collections ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.StringTokenizer ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.MultiStatus; 23 import org.eclipse.core.runtime.Path; 24 import org.eclipse.core.runtime.Status; 25 import org.eclipse.emf.codegen.jet.JETEmitter; 26 import org.eclipse.emf.examples.jet.article2.TypesafeEnumPlugin; 27 28 import org.eclipse.jface.action.IAction; 29 import org.eclipse.jface.dialogs.ErrorDialog; 30 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 31 import org.eclipse.jface.viewers.ISelection; 32 import org.eclipse.jface.viewers.IStructuredSelection; 33 import org.eclipse.swt.widgets.Shell; 34 import org.eclipse.ui.IActionDelegate; 35 import org.eclipse.ui.actions.WorkspaceModifyOperation; 36 37 38 39 46 public class EmitAction implements IActionDelegate 47 { 48 protected ISelection selection; 49 50 public void selectionChanged(IAction action, ISelection selection) 51 { 52 this.selection = selection; 53 action.setEnabled(true); 54 } 55 56 public void run(IAction action) 57 { 58 List files = (selection instanceof IStructuredSelection) ? ((IStructuredSelection)selection).toList() : Collections.EMPTY_LIST; 59 60 for (Iterator i = files.iterator(); i.hasNext();) 61 { 62 IFile file = (IFile)i.next(); 63 IPath fullPath = file.getFullPath(); 64 65 String templateURI = "platform:/resource" + fullPath; 66 ClassLoader classloader = getClass().getClassLoader(); 67 JETEmitter emitter = new JETEmitter(templateURI, classloader); 68 69 73 try 74 { 75 String [] arguments = new String []{ "hi" }; 76 generate(emitter, arguments, file); 77 78 } 79 catch (Exception e) 80 { 81 handleException(e); 82 } 83 } 84 } 85 86 100 private void generate(final JETEmitter emitter, final Object [] arguments, final IFile file) throws CoreException, 101 InvocationTargetException , 102 InterruptedException 103 { 104 105 WorkspaceModifyOperation op = new WorkspaceModifyOperation() 106 { 107 public void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException , InterruptedException 108 { 109 try 110 { 111 String generated = emitter.generate(monitor, arguments); 112 saveGenerated(generated, file, monitor); 113 114 } 115 catch (IOException ioe) 116 { 117 throw new InvocationTargetException (ioe); 118 } 119 } 120 }; 121 122 Shell shell = TypesafeEnumPlugin.getActiveWorkbenchShell(); 123 ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell); 124 dialog.run(false, true, op); 125 } 126 127 139 private void saveGenerated(String generated, IFile file, IProgressMonitor monitor) throws IOException , CoreException 140 { 141 142 InputStream contents = new ByteArrayInputStream (generated.getBytes()); 143 144 IContainer parent = file.getParent(); 145 IFile target = parent.getFile(new Path(file.getName() + ".txt")); 146 147 if (target.exists()) 148 { 149 target.setContents(contents, true, false, monitor); 150 } 151 else 152 { 153 File systemFile = target.getLocation().toFile(); 154 if (systemFile.exists()) 155 { parent.refreshLocal(1, monitor); target.setContents(contents, true, false, monitor); 159 } 160 else 161 { 162 target.create(contents, false, monitor); 163 } 164 } 165 } 166 167 173 private void handleException(Throwable t) 174 { 175 if (t instanceof InvocationTargetException ) 176 { 177 t = ((InvocationTargetException )t).getTargetException(); 178 } 179 TypesafeEnumPlugin.log(t); 180 MultiStatus status = createStatus(t); 181 182 Shell shell = TypesafeEnumPlugin.getActiveWorkbenchShell(); 183 String title = "Error while emitting template"; 184 ErrorDialog.openError(shell, title, null, status); 185 } 186 187 197 private MultiStatus createStatus(Throwable t) 198 { 199 String id = TypesafeEnumPlugin.getPluginId(); 200 MultiStatus result = new MultiStatus(id, IStatus.ERROR, "Unexpected Error", t); 201 202 StringWriter trace = new StringWriter (); 203 t.printStackTrace(new PrintWriter (trace)); 204 StringTokenizer tok = new StringTokenizer (trace.toString(), "\r\n"); 205 206 while (tok.hasMoreTokens()) 207 { 208 String line = tok.nextToken(); 209 if (line.charAt(0) == '\t') 210 { 211 line = " " + line.trim(); 212 } 213 result.add(new Status(IStatus.ERROR, id, IStatus.ERROR, line, null)); 214 } 215 return result; 216 } 217 } | Popular Tags |