1 17 package org.eclipse.emf.edit.ui.action; 18 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.eclipse.core.resources.IFile; 27 import org.eclipse.core.resources.IMarker; 28 import org.eclipse.core.resources.IResource; 29 import org.eclipse.core.resources.ResourcesPlugin; 30 import org.eclipse.core.runtime.CoreException; 31 import org.eclipse.core.runtime.IProgressMonitor; 32 import org.eclipse.core.runtime.Path; 33 import org.eclipse.core.runtime.Platform; 34 import org.eclipse.jface.action.Action; 35 import org.eclipse.jface.dialogs.ErrorDialog; 36 import org.eclipse.jface.dialogs.MessageDialog; 37 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 38 import org.eclipse.jface.operation.IRunnableWithProgress; 39 import org.eclipse.jface.viewers.ISelectionChangedListener; 40 import org.eclipse.jface.viewers.ISelectionProvider; 41 import org.eclipse.jface.viewers.IStructuredSelection; 42 import org.eclipse.jface.viewers.SelectionChangedEvent; 43 import org.eclipse.jface.viewers.StructuredSelection; 44 import org.eclipse.jface.viewers.Viewer; 45 import org.eclipse.jface.window.Window; 46 import org.eclipse.swt.widgets.Shell; 47 import org.eclipse.ui.IEditorPart; 48 import org.eclipse.ui.IWorkbenchPart; 49 import org.eclipse.ui.PlatformUI; 50 import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation; 51 import org.eclipse.ui.part.ISetSelectionTarget; 52 53 import org.eclipse.emf.common.notify.AdapterFactory; 54 import org.eclipse.emf.common.ui.viewer.IViewerProvider; 55 import org.eclipse.emf.common.util.BasicDiagnostic; 56 import org.eclipse.emf.common.util.Diagnostic; 57 import org.eclipse.emf.common.util.DiagnosticChain; 58 import org.eclipse.emf.common.util.URI; 59 import org.eclipse.emf.ecore.EClass; 60 import org.eclipse.emf.ecore.EObject; 61 import org.eclipse.emf.ecore.EValidator; 62 import org.eclipse.emf.ecore.resource.Resource; 63 import org.eclipse.emf.ecore.util.Diagnostician; 64 import org.eclipse.emf.ecore.util.EcoreUtil; 65 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; 66 import org.eclipse.emf.edit.domain.EditingDomain; 67 import org.eclipse.emf.edit.domain.IEditingDomainProvider; 68 import org.eclipse.emf.edit.provider.IItemLabelProvider; 69 import org.eclipse.emf.edit.ui.EMFEditUIPlugin; 70 71 72 74 public class ValidateAction extends Action implements ISelectionChangedListener 75 { 76 public static class EclipseResourcesUtil 77 { 78 public IRunnableWithProgress getWorkspaceModifyOperation(IRunnableWithProgress runnableWithProgress) 79 { 80 return new WorkspaceModifyDelegatingOperation(runnableWithProgress); 81 } 82 83 public void createMarkers(IFile file, Diagnostic diagnostic) 84 { 85 EObject eObject = null; 86 List data = diagnostic.getData(); 87 if (!data.isEmpty()) 88 { 89 Object target = data.get(0); 90 if (target instanceof EObject) 91 { 92 eObject = (EObject)target; 93 } 94 } 95 96 if (diagnostic.getChildren().isEmpty()) 97 { 98 try 99 { 100 IMarker marker = file.createMarker(EValidator.MARKER); 101 int severity = diagnostic.getSeverity(); 102 if (severity < Diagnostic.WARNING) 103 { 104 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO); 105 } 106 else if (severity < Diagnostic.ERROR) 107 { 108 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING); 109 } 110 else 111 { 112 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); 113 } 114 marker.setAttribute(IMarker.MESSAGE, diagnostic.getMessage()); 115 if (eObject != null) 116 { 117 marker.setAttribute(EValidator.URI_ATTRIBUTE, EcoreUtil.getURI(eObject).toString()); 118 } 119 } 120 catch (CoreException exception) 121 { 122 EMFEditUIPlugin.INSTANCE.log(exception); 123 } 124 } 125 else 126 { 127 String parentMessage = diagnostic.getMessage() + ". "; 128 for (Iterator i = diagnostic.getChildren().iterator(); i.hasNext(); ) 129 { 130 Diagnostic childDiagnostic = (Diagnostic)i.next(); 131 try 132 { 133 IMarker marker = file.createMarker(EValidator.MARKER); 134 int severity = childDiagnostic.getSeverity(); 135 if (severity < Diagnostic.WARNING) 136 { 137 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO); 138 } 139 else if (severity < Diagnostic.ERROR) 140 { 141 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING); 142 } 143 else 144 { 145 marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); 146 } 147 marker.setAttribute(IMarker.MESSAGE, parentMessage + childDiagnostic.getMessage()); 148 if (eObject != null) 149 { 150 marker.setAttribute(EValidator.URI_ATTRIBUTE, EcoreUtil.getURI(eObject).toString()); 151 } 152 } 153 catch (CoreException exception) 154 { 155 EMFEditUIPlugin.INSTANCE.log(exception); 156 } 157 } 158 } 159 } 160 } 161 162 protected ISelectionProvider selectionProvider; 163 protected List selectedObjects; 164 protected EditingDomain domain; 165 protected EclipseResourcesUtil eclipseResourcesUtil = 166 Platform.getBundle("org.eclipse.core.resources") != null ? 167 new EclipseResourcesUtil() : null; 168 169 172 public ValidateAction() 173 { 174 super(EMFEditUIPlugin.INSTANCE.getString("_UI_Validate_menu_item")); 175 setDescription(EMFEditUIPlugin.INSTANCE.getString("_UI_Validate_simple_description")); 176 } 177 178 public void run() 179 { 180 final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); 181 IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() 182 { 183 public void run(final IProgressMonitor progressMonitor) throws InvocationTargetException , InterruptedException 184 { 185 try 186 { 187 final Diagnostic diagnostic = validate(progressMonitor); 188 shell.getDisplay().asyncExec 189 (new Runnable () 190 { 191 public void run() 192 { 193 if (progressMonitor.isCanceled()) 194 { 195 handleDiagnostic(Diagnostic.CANCEL_INSTANCE); 196 } 197 else 198 { 199 handleDiagnostic(diagnostic); 200 } 201 } 202 }); 203 } 204 finally 205 { 206 progressMonitor.done(); 207 } 208 } 209 }; 210 211 if(eclipseResourcesUtil != null) 212 { 213 runnableWithProgress = eclipseResourcesUtil.getWorkspaceModifyOperation(runnableWithProgress); 214 } 215 216 try 217 { 218 new ProgressMonitorDialog(shell).run(true, true, runnableWithProgress); 222 } 223 catch (Exception exception) 224 { 225 EMFEditUIPlugin.INSTANCE.log(exception); 226 } 227 } 228 229 232 protected Diagnostic validate(final IProgressMonitor progressMonitor) 233 { 234 EObject eObject = (EObject)selectedObjects.iterator().next(); 235 int count = 0; 236 for (Iterator i = eObject.eAllContents(); i.hasNext(); i.next()) 237 { 238 ++count; 239 } 240 241 progressMonitor.beginTask("", count); 242 243 final AdapterFactory adapterFactory = 244 domain instanceof AdapterFactoryEditingDomain ? ((AdapterFactoryEditingDomain)domain).getAdapterFactory() : null; 245 246 Diagnostician diagnostician = 247 new Diagnostician() 248 { 249 public String getObjectLabel(EObject eObject) 250 { 251 if (adapterFactory != null && !eObject.eIsProxy()) 252 { 253 IItemLabelProvider itemLabelProvider = (IItemLabelProvider)adapterFactory.adapt(eObject, IItemLabelProvider.class); 254 if (itemLabelProvider != null) 255 { 256 return itemLabelProvider.getText(eObject); 257 } 258 } 259 260 return super.getObjectLabel(eObject); 261 } 262 263 public boolean validate(EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map context) 264 { 265 progressMonitor.worked(1); 266 return super.validate(eClass, eObject, diagnostics, context); 267 } 268 }; 269 270 progressMonitor.setTaskName 271 (EMFEditUIPlugin.INSTANCE.getString("_UI_Validating_message", new Object [] {diagnostician.getObjectLabel(eObject)})); 272 273 return diagnostician.validate(eObject); 274 } 275 276 protected void handleDiagnostic(Diagnostic diagnostic) 277 { 278 int result = 279 ErrorDialog.openError 280 (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 281 EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationProblems_title"), 282 EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationProblems_message"), 283 BasicDiagnostic.toIStatus(diagnostic)); 284 285 if (diagnostic.getSeverity() == Diagnostic.OK) 288 { 289 MessageDialog.openInformation 290 (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 291 EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationOK_title"), 292 EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationOK_message")); 293 result = Window.CANCEL; 294 } 295 296 if (Platform.getBundle("org.eclipse.core.resources") != null) 297 { 298 final IFile file = getFile(); 299 if (file != null) 300 { 301 try 302 { 303 file.deleteMarkers(EValidator.MARKER, true, IResource.DEPTH_ZERO); 304 } 305 catch (CoreException exception) 306 { 307 EMFEditUIPlugin.INSTANCE.log(exception); 308 } 309 } 310 311 if (result == Window.OK) 312 { 313 if (!diagnostic.getChildren().isEmpty()) 314 { 315 List data = ((Diagnostic)diagnostic.getChildren().get(0)).getData(); 316 if (!data.isEmpty() && data.get(0) instanceof EObject) 317 { 318 Object part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 319 if (part instanceof ISetSelectionTarget) 320 { 321 ((ISetSelectionTarget)part).selectReveal(new StructuredSelection(data.get(0))); 322 } 323 else if (part instanceof IViewerProvider) 324 { 325 Viewer viewer = ((IViewerProvider)part).getViewer(); 326 if (viewer != null) 327 { 328 viewer.setSelection(new StructuredSelection(data.get(0)), true); 329 } 330 } 331 } 332 } 333 334 for (Iterator i = diagnostic.getChildren().iterator(); i.hasNext(); ) 335 { 336 Diagnostic childDiagnostic = (Diagnostic)i.next(); 337 createMarkers(file, childDiagnostic); 338 } 339 } 340 } 341 } 342 343 protected void createMarkers(IFile file, Diagnostic diagnostic) 344 { 345 eclipseResourcesUtil.createMarkers(file, diagnostic); 346 } 347 348 protected IFile getFile() 349 { 350 Resource resource = (Resource)domain.getResourceSet().getResources().get(0); 351 if (resource != null) 352 { 353 URI uri = resource.getURI(); 354 uri = resource.getResourceSet().getURIConverter().normalize(uri); 355 String scheme = uri.scheme(); 356 if ("platform".equals(scheme) && uri.segmentCount() > 1 && "resource".equals(uri.segment(0))) 357 { 358 StringBuffer platformResourcePath = new StringBuffer (); 359 for (int j = 1, size = uri.segmentCount(); j < size; ++j) 360 { 361 platformResourcePath.append('/'); 362 platformResourcePath.append(uri.segment(j)); 363 } 364 return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourcePath.toString())); 365 } 366 } 367 return null; 368 } 369 370 public void selectionChanged(SelectionChangedEvent event) 371 { 372 selectionProvider = event.getSelectionProvider(); 373 if (event.getSelection() instanceof IStructuredSelection) 374 { 375 setEnabled(updateSelection((IStructuredSelection)event.getSelection())); 376 } 377 else 378 { 379 setEnabled(false); 380 } 381 } 382 383 public boolean updateSelection(IStructuredSelection selection) 384 { 385 selectedObjects = new ArrayList (); 386 for (Iterator objects = selection.iterator(); objects.hasNext(); ) 387 { 388 selectedObjects.add(AdapterFactoryEditingDomain.unwrap(objects.next())); 389 } 390 391 return 392 selectedObjects.size() == 1 && 393 selectedObjects.get(0) instanceof EObject; 394 } 395 396 399 public void setActiveEditor(IEditorPart editorPart) 400 { 401 setActiveWorkbenchPart(editorPart); 402 } 403 404 407 public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart) 408 { 409 if (workbenchPart instanceof IEditingDomainProvider) 410 { 411 domain = ((IEditingDomainProvider)workbenchPart).getEditingDomain(); 412 } 413 } 414 } 415
| Popular Tags
|