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