$search
00001 00004 package org.best_of_robotics.transform.ros.to.cplusplus.handler; 00005 00006 import java.util.ArrayList; 00007 00008 import org.best_of_robotics.transform.ros.to.cplusplus.jobs.EglTransformOperationJob; 00009 import org.best_of_robotics.transform.ros.to.cplusplus.jobs.RosMsgsTransformOperationJob; 00010 import org.eclipse.core.commands.AbstractHandler; 00011 import org.eclipse.core.commands.ExecutionEvent; 00012 import org.eclipse.core.commands.ExecutionException; 00013 import org.eclipse.core.resources.IMarker; 00014 import org.eclipse.core.resources.IProject; 00015 import org.eclipse.core.resources.IResourceRuleFactory; 00016 import org.eclipse.core.resources.IWorkspace; 00017 import org.eclipse.core.resources.ResourcesPlugin; 00018 import org.eclipse.core.runtime.CoreException; 00019 import org.eclipse.core.runtime.jobs.ISchedulingRule; 00020 import org.eclipse.core.runtime.jobs.Job; 00021 import org.eclipse.emf.ecore.resource.Resource; 00022 import org.eclipse.emf.ecore.resource.ResourceSet; 00023 import org.eclipse.gmf.runtime.emf.core.resources.GMFResource; 00024 import org.eclipse.jface.dialogs.MessageDialog; 00025 import org.eclipse.ui.IEditorPart; 00026 import org.eclipse.ui.PlatformUI; 00027 import org.eclipse.ui.part.FileEditorInput; 00028 import org.ros.model.ros.diagram.part.RosDiagramEditor; 00029 00034 public class RosMsgsTransform extends AbstractHandler { 00035 00036 private RosDiagramEditor rosDiagramEditor; 00037 private IProject project; 00038 private RosMsgsTransformOperationJob eglJob; 00039 private ArrayList<IMarker> problems = new ArrayList<IMarker>(); 00040 00044 public RosMsgsTransform() { 00045 // TODO Auto-generated constructor stub 00046 } 00047 00048 /* (non-Javadoc) 00049 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) 00050 */ 00051 @Override 00052 public Object execute(ExecutionEvent event) throws ExecutionException { 00053 IEditorPart editor = PlatformUI.getWorkbench() 00054 .getActiveWorkbenchWindow().getActivePage().getActiveEditor(); 00055 00056 if (!(editor instanceof RosDiagramEditor)) { 00057 MessageDialog 00058 .openError( 00059 PlatformUI.getWorkbench() 00060 .getActiveWorkbenchWindow().getShell(), 00061 "Error on Editor Selection", 00062 "Please select the editor from which you want to generate code and execute command again."); 00063 return null; 00064 } else { 00065 rosDiagramEditor = (RosDiagramEditor) editor; 00066 } 00067 00068 String commandName = "rosHandler"; 00069 // try { 00070 // commandName = event.getCommand().getName(); 00071 // } catch (NotDefinedException e) { 00072 // e.printStackTrace(); 00073 // } 00074 00075 Resource resource = getFirstSemanticModelResource(rosDiagramEditor 00076 .getEditingDomain().getResourceSet()); 00077 00078 //TODO This is error checking for setting the package name in the model. 00079 // if (!check(resource)) { 00080 // Shell shell = PlatformUI.getWorkbench().getDisplay() 00081 // .getActiveShell(); 00082 // MessageDialog 00083 // .openError( 00084 // shell, 00085 // "Model Not Valid Error", 00086 // "The are problems with model. Please check the Problem View for more information."); 00087 // try { 00088 // PlatformUI.getWorkbench().getActiveWorkbenchWindow() 00089 // .getActivePage() 00090 // .showView("org.eclipse.ui.views.ProblemView").setFocus(); 00091 // } catch (PartInitException e) { 00092 // e.printStackTrace(); 00093 // } 00094 // return null; 00095 // } 00096 00097 eglJob = new RosMsgsTransformOperationJob(commandName); 00098 00099 initializeJobs(resource); 00100 00101 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 00102 IResourceRuleFactory ruleFactory = workspace.getRuleFactory(); 00103 ISchedulingRule rule = ruleFactory.modifyRule(project); 00104 00105 eglJob.setUser(true); 00106 eglJob.setPriority(Job.INTERACTIVE); 00107 eglJob.setRule(rule); 00108 eglJob.schedule(); 00109 00110 return null; 00111 } 00112 00113 private void initializeJobs(Resource resource) { 00114 FileEditorInput fileEditorInput = (FileEditorInput) rosDiagramEditor 00115 .getEditorInput(); 00116 project = fileEditorInput.getFile().getProject(); 00117 eglJob.setProject(project); 00118 eglJob.createSource(resource); 00119 } 00120 00121 // private boolean check(Resource resource) { 00122 // if (resource == null) return false; 00123 // org.orocos.model.rtt.Package rttPackage = (org.orocos.model.rtt.Package) resource.getContents().get(0); 00124 // for (IMarker marker : problems) { 00125 // try { 00126 // marker.delete(); 00127 // } catch (CoreException e) { 00128 // e.printStackTrace(); 00129 // } 00130 // } 00131 // problems.clear(); 00132 // if (rttPackage.getName() == null || rttPackage.getName().equals("")) { 00133 // problems.add(createProblemMarker("Package name is not set in diagram.")); 00134 // } 00135 // EList<TaskContext> taskContexts = rttPackage.getTaskContext(); 00136 // for (TaskContext taskContext : taskContexts) { 00137 // if (taskContext.getNamespace() == null || taskContext.getNamespace().equals("")) { 00138 // problems.add(createProblemMarker("Namespace is not set for " + taskContext.getName())); 00139 // } 00140 // if (taskContext.getType() == null || taskContext.getType().equals("")) { 00141 // problems.add(createProblemMarker("Type is not set for " + taskContext.getName())); 00142 // } 00143 // } 00144 // if (!problems.isEmpty()) return false; 00145 // return true; 00146 // } 00147 00148 private IMarker createProblemMarker(String message) { 00149 try { 00150 IMarker problemMarker = ResourcesPlugin.getWorkspace().getRoot().createMarker(IMarker.PROBLEM); 00151 problemMarker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); 00152 problemMarker.setAttribute(IMarker.MESSAGE, message); 00153 problemMarker.setAttribute(IMarker.TRANSIENT, true); 00154 problemMarker.setAttribute(IMarker.LOCATION, "RTT Graphical Editor"); 00155 return problemMarker; 00156 } catch (CoreException e) { 00157 e.printStackTrace(); 00158 } 00159 return null; 00160 00161 } 00162 00163 public Resource getFirstSemanticModelResource(ResourceSet resourceSet) { 00164 for (Resource resource : resourceSet.getResources()) { 00165 if (!(resource instanceof GMFResource)) { 00166 return resource; 00167 } 00168 } 00169 return null; 00170 } 00171 00172 }