1 17 package org.eclipse.emf.codegen.action; 18 19 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.jface.action.IAction; 27 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 28 import org.eclipse.jface.operation.IRunnableWithProgress; 29 import org.eclipse.jface.viewers.ISelection; 30 import org.eclipse.jface.viewers.IStructuredSelection; 31 import org.eclipse.ui.IActionDelegate; 32 33 import org.eclipse.emf.codegen.jet.JETCompileTemplateOperation; 34 import org.eclipse.emf.codegen.jet.JETNature; 35 import org.eclipse.emf.codegen.presentation.CodeGenUIPlugin; 36 37 38 41 public class CompileTemplateAction implements IActionDelegate 42 { 43 protected ISelection selection; 44 45 public void run(IAction action) 46 { 47 if (action.isEnabled()) 48 { 49 IRunnableWithProgress op = 50 new IRunnableWithProgress() 51 { 52 public void run(IProgressMonitor monitor) 53 { 54 try 55 { 56 for (Iterator i = getSelectedObjects().iterator(); i.hasNext(); ) 57 { 58 IFile file = (IFile)i.next(); 59 JETNature jetNature = JETNature.getRuntime(file.getProject()); 60 if (jetNature != null) 61 { 62 JETCompileTemplateOperation compileTemplate = 63 new JETCompileTemplateOperation(file.getProject(), jetNature.getTemplateContainers(), Collections.singleton(file)); 64 compileTemplate.run(monitor); 65 } 66 } 67 } 68 catch (Exception e) 69 { 70 CodeGenUIPlugin.write(e); 71 } 72 } 73 }; 74 75 try 76 { 77 ProgressMonitorDialog dialog = 78 new ProgressMonitorDialog(CodeGenUIPlugin.getPlugin().getWorkbench().getActiveWorkbenchWindow().getShell()); 79 dialog.run(false, true, op); 80 } 81 catch (Exception e) 82 { 83 CodeGenUIPlugin.write(e); 84 } 85 } 86 } 87 88 public void selectionChanged(IAction action, ISelection selection) 89 { 90 this.selection = selection; 91 setActionState(action); 92 } 93 94 protected void setActionState(IAction action) 95 { 96 action.setEnabled(true); 97 } 98 99 protected List getSelectedObjects() 100 { 101 return 102 selection instanceof IStructuredSelection ? 103 ((IStructuredSelection)selection).toList() : 104 Collections.EMPTY_LIST; 105 } 106 107 protected boolean isSupportedAction(Object object) 108 { 109 return object instanceof IFile && ((IFile)object).isAccessible(); 110 } 111 } 112 | Popular Tags |