carbidecpp22devenv/plugins/org.eclipse.gmf.templates.legacy_1.0.0.v20090614-0839/codegen.templates/xpt/providers/ValidationDecoratorProvider.xpt
changeset 422 033392511bf7
equal deleted inserted replaced
421:631a44165bcf 422:033392511bf7
       
     1 /*
       
     2  * Copyright (c) 2007 Borland Software Corporation
       
     3  * 
       
     4  * All rights reserved. This program and the accompanying materials
       
     5  * are made available under the terms of the Eclipse Public License v1.0
       
     6  * which accompanies this distribution, and is available at
       
     7  * http://www.eclipse.org/legal/epl-v10.html
       
     8  *
       
     9  * Contributors:
       
    10  *    Dmitry Stadnik (Borland) - initial API and implementation
       
    11  */
       
    12 
       
    13 «IMPORT "http://www.eclipse.org/gmf/2008/GenModel"»
       
    14 «IMPORT "http://www.eclipse.org/emf/2002/GenModel"»
       
    15 «IMPORT "http://www.eclipse.org/emf/2002/Ecore"»
       
    16 
       
    17 «DEFINE ValidationDecoratorProvider FOR gmfgen::GenDiagram-»
       
    18 «EXPAND xpt::Common::copyright FOR editorGen-»
       
    19 package «providersPackageName»;
       
    20 
       
    21 «EXPAND xpt::Common::generatedClassComment»
       
    22 public class «validationDecoratorProviderClassName»
       
    23 		extends org.eclipse.gmf.runtime.common.core.service.AbstractProvider
       
    24 		implements org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorProvider {
       
    25 
       
    26 	«EXPAND xpt::Common::generatedMemberComment»
       
    27 	private static final String KEY = "validationStatus"; «EXPAND xpt::Common::nonNLS»
       
    28 	«IF editorGen.application == null-»
       
    29 
       
    30 	«EXPAND xpt::Common::generatedMemberComment»
       
    31 	private static final String MARKER_TYPE = «editorGen.plugin.getActivatorQualifiedClassName()».ID +
       
    32 			".«getValidationDiagnosticMarkerType()»"; «EXPAND xpt::Common::nonNLS»
       
    33 
       
    34 	«EXPAND xpt::Common::generatedMemberComment»
       
    35 	private static MarkerObserver fileObserver;
       
    36 	«ENDIF-»
       
    37 
       
    38 	«EXPAND xpt::Common::generatedMemberComment»
       
    39 	private static java.util.Map/*<String, List<IDecorator>>*/ allDecorators = new java.util.HashMap();
       
    40 
       
    41 	«EXPAND xpt::Common::generatedMemberComment»
       
    42 	public void createDecorators(org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget decoratorTarget) {
       
    43 		org.eclipse.gef.EditPart editPart = (org.eclipse.gef.EditPart) decoratorTarget.getAdapter(org.eclipse.gef.EditPart.class);
       
    44 		if (editPart instanceof org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart ||
       
    45 				editPart instanceof org.eclipse.gef.editparts.AbstractConnectionEditPart) {
       
    46 			Object model = editPart.getModel();
       
    47 			if ((model instanceof org.eclipse.gmf.runtime.notation.View)) {
       
    48 				org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) model;
       
    49 				if (!(view instanceof org.eclipse.gmf.runtime.notation.Edge) && !view.isSetElement()) {
       
    50 					return;
       
    51 				}
       
    52 			}
       
    53 			org.eclipse.gef.EditDomain ed = editPart.getViewer().getEditDomain();
       
    54 			if (!(ed instanceof org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditDomain)) {
       
    55 				return;
       
    56 			}
       
    57 			if (((org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditDomain) ed).getEditorPart() instanceof
       
    58 					«editorGen.editor.getQualifiedClassName()») {
       
    59 				decoratorTarget.installDecorator(KEY, new StatusDecorator(decoratorTarget));
       
    60 			}
       
    61 		}
       
    62 	}
       
    63 
       
    64 	«EXPAND xpt::Common::generatedMemberComment»
       
    65 	public boolean provides(org.eclipse.gmf.runtime.common.core.service.IOperation operation) {
       
    66 		if (!(operation instanceof org.eclipse.gmf.runtime.diagram.ui.services.decorator.CreateDecoratorsOperation)) {
       
    67 			return false;
       
    68 		}
       
    69 		org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget decoratorTarget =
       
    70 				((org.eclipse.gmf.runtime.diagram.ui.services.decorator.CreateDecoratorsOperation) operation).getDecoratorTarget();
       
    71 		org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) decoratorTarget.getAdapter(
       
    72 				org.eclipse.gmf.runtime.notation.View.class);
       
    73 		return view != null && «EXPAND xpt::editor::VisualIDRegistry::modelID».equals(«EXPAND xpt::editor::VisualIDRegistry::getModelIDMethodCall»(view));
       
    74 	}
       
    75 
       
    76 	«EXPAND xpt::Common::generatedMemberComment»
       
    77 	public static void refreshDecorators(org.eclipse.gmf.runtime.notation.View view) {
       
    78 		refreshDecorators(org.eclipse.gmf.runtime.diagram.core.util.ViewUtil.getIdStr(view), view.getDiagram());
       
    79 	}
       
    80 
       
    81 	«EXPAND xpt::Common::generatedMemberComment»
       
    82 	private static void refreshDecorators(String viewId, org.eclipse.gmf.runtime.notation.Diagram diagram) {
       
    83 		final java.util.List decorators = viewId != null ? (java.util.List) allDecorators.get(viewId) : null;
       
    84 		if (decorators == null || decorators.isEmpty() || diagram == null) {
       
    85 			return;
       
    86 		}
       
    87 		final org.eclipse.gmf.runtime.notation.Diagram fdiagram = diagram;
       
    88 		org.eclipse.ui.PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
       
    89 
       
    90 			public void run() {
       
    91 				try {
       
    92 					org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(fdiagram).runExclusive(new Runnable() {
       
    93 
       
    94 						public void run() {
       
    95 							for (java.util.Iterator it = decorators.iterator(); it.hasNext();) {
       
    96 								org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecorator decorator =
       
    97 										(org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecorator) it.next();
       
    98 								decorator.refresh();
       
    99 							}
       
   100 						}
       
   101 					});
       
   102 				} catch (Exception e) {
       
   103 					«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
       
   104 							"Decorator refresh failure", e); «EXPAND xpt::Common::nonNLS»
       
   105 				}
       
   106 			}
       
   107 		});
       
   108 	}
       
   109 
       
   110 	«EXPAND xpt::Common::generatedMemberComment»
       
   111 	public static class StatusDecorator extends org.eclipse.gmf.runtime.diagram.ui.services.decorator.AbstractDecorator {
       
   112 
       
   113 		«EXPAND xpt::Common::generatedMemberComment»
       
   114 		private String viewId;
       
   115 
       
   116 		«EXPAND xpt::Common::generatedMemberComment»
       
   117 		public StatusDecorator(org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget decoratorTarget) {
       
   118 			super(decoratorTarget);
       
   119 			try {
       
   120 				final org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) getDecoratorTarget().getAdapter(
       
   121 						org.eclipse.gmf.runtime.notation.View.class);
       
   122 				org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(view).runExclusive(new Runnable() {
       
   123 
       
   124 					public void run() {
       
   125 						StatusDecorator.this.viewId = view != null ?
       
   126 								org.eclipse.gmf.runtime.diagram.core.util.ViewUtil.getIdStr(view) : null;
       
   127 					}
       
   128 				});
       
   129 			} catch (Exception e) {
       
   130 				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
       
   131 						"ViewID access failure", e); «EXPAND xpt::Common::nonNLS»			
       
   132 			}
       
   133 		}
       
   134 
       
   135 		«EXPAND xpt::Common::generatedMemberComment»
       
   136 		public void refresh() {
       
   137 			removeDecoration();
       
   138 			org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) getDecoratorTarget().getAdapter(
       
   139 					org.eclipse.gmf.runtime.notation.View.class);
       
   140 			if (view == null || view.eResource() == null) {
       
   141 				return;
       
   142 			}
       
   143 			org.eclipse.gef.EditPart editPart = (org.eclipse.gef.EditPart) getDecoratorTarget().getAdapter(org.eclipse.gef.EditPart.class);
       
   144 			if (editPart == null || editPart.getViewer() == null) {
       
   145 				return;
       
   146 			}
       
   147 
       
   148 			// query for all the validation markers of the current resource
       
   149 			«IF editorGen.application == null-»
       
   150 			String elementId = org.eclipse.gmf.runtime.diagram.core.util.ViewUtil.getIdStr(view);
       
   151 			if (elementId == null) {
       
   152 				return;
       
   153 			}
       
   154 			int severity = org.eclipse.core.resources.IMarker.SEVERITY_INFO;
       
   155 			org.eclipse.core.resources.IMarker foundMarker = null;
       
   156 			org.eclipse.core.resources.IResource resource = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(view.eResource());
       
   157 			if (resource == null || !resource.exists()) {
       
   158 				return;
       
   159 			}
       
   160 			org.eclipse.core.resources.IMarker[] markers = null;
       
   161 			try {
       
   162 				markers = resource.findMarkers(MARKER_TYPE, true, org.eclipse.core.resources.IResource.DEPTH_INFINITE);
       
   163 			} catch (org.eclipse.core.runtime.CoreException e) {
       
   164 				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
       
   165 						"Validation markers refresh failure", e); «EXPAND xpt::Common::nonNLS»
       
   166 			}
       
   167 			«ELSE-»
       
   168 			int severity = org.eclipse.core.runtime.IStatus.INFO;
       
   169 			«EXPAND xpt::editor::ValidationMarker::qualifiedClassName» foundMarker = null;
       
   170 			«EXPAND xpt::editor::ValidationMarker::qualifiedClassName»[] markers =
       
   171 					«EXPAND xpt::editor::ValidationMarker::qualifiedClassName».getMarkers(editPart.getViewer(), viewId);
       
   172 			«ENDIF-»
       
   173 			if (markers == null || markers.length == 0) {
       
   174 				return;
       
   175 			}
       
   176 			org.eclipse.draw2d.Label toolTip = null;
       
   177 			for (int i = 0; i < markers.length; i++) {
       
   178 				«IF editorGen.application == null-»
       
   179 				org.eclipse.core.resources.IMarker marker = markers[i];
       
   180 				String attribute = marker.getAttribute(org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID, ""); «EXPAND xpt::Common::nonNLS»
       
   181 				if (attribute.equals(elementId)) {
       
   182 					int nextSeverity = marker.getAttribute(org.eclipse.core.resources.IMarker.SEVERITY,
       
   183 							org.eclipse.core.resources.IMarker.SEVERITY_INFO);					
       
   184 				«ELSE-»
       
   185 				«EXPAND xpt::editor::ValidationMarker::qualifiedClassName» marker = markers[i];
       
   186 				int nextSeverity = marker.getStatusSeverity();
       
   187 				«ENDIF-»
       
   188 					org.eclipse.swt.graphics.Image nextImage = getImage(nextSeverity);	 
       
   189 					if (foundMarker == null) {				
       
   190 						foundMarker = marker;
       
   191 						toolTip = new org.eclipse.draw2d.Label(
       
   192 							«IF editorGen.application == null-»
       
   193 								marker.getAttribute(org.eclipse.core.resources.IMarker.MESSAGE, ""), «EXPAND xpt::Common::nonNLS»
       
   194 							«ELSE-»
       
   195 								marker.getMessage(),
       
   196 							«ENDIF-»
       
   197 								nextImage);						
       
   198 					} else {
       
   199 						if (toolTip.getChildren().isEmpty()) {
       
   200 							org.eclipse.draw2d.Label comositeLabel = new org.eclipse.draw2d.Label();
       
   201 							org.eclipse.draw2d.FlowLayout fl = new org.eclipse.draw2d.FlowLayout(false);
       
   202 							fl.setMinorSpacing(0);
       
   203 							comositeLabel.setLayoutManager(fl);
       
   204 							comositeLabel.add(toolTip);
       
   205 							toolTip = comositeLabel;
       
   206 						}
       
   207 						toolTip.add(new org.eclipse.draw2d.Label(
       
   208 							«IF editorGen.application == null-»
       
   209 								marker.getAttribute(org.eclipse.core.resources.IMarker.MESSAGE, ""),  «EXPAND xpt::Common::nonNLS»
       
   210 							«ELSE-»
       
   211 								marker.getMessage(),
       
   212 							«ENDIF-»
       
   213 								nextImage));
       
   214 					}
       
   215 					severity = (nextSeverity > severity) ? nextSeverity : severity;					
       
   216 				«IF editorGen.application == null-»
       
   217 				}
       
   218 				«ENDIF-»
       
   219 			}
       
   220 			if (foundMarker == null) {
       
   221 				return;
       
   222 			}
       
   223 
       
   224 			// add decoration
       
   225 			if (editPart instanceof org.eclipse.gef.GraphicalEditPart) {
       
   226 				if (view instanceof org.eclipse.gmf.runtime.notation.Edge) {
       
   227 					setDecoration(getDecoratorTarget().addConnectionDecoration(getImage(severity), 50, true));										
       
   228 				} else {
       
   229 					int margin = -1;
       
   230 					if (editPart instanceof org.eclipse.gef.GraphicalEditPart) {
       
   231 						margin = org.eclipse.gmf.runtime.draw2d.ui.mapmode.MapModeUtil.getMapMode(
       
   232 							((org.eclipse.gef.GraphicalEditPart) editPart).getFigure()).DPtoLP(margin);
       
   233 					}
       
   234 					setDecoration(getDecoratorTarget().addShapeDecoration(getImage(severity),
       
   235 							org.eclipse.gmf.runtime.diagram.ui.services.decorator.IDecoratorTarget.Direction.NORTH_EAST, margin, true));										
       
   236 				}
       
   237 				getDecoration().setToolTip(toolTip);
       
   238 			}
       
   239 		}
       
   240 
       
   241 		«EXPAND xpt::Common::generatedMemberComment»
       
   242 		private org.eclipse.swt.graphics.Image getImage(int severity) {
       
   243 			String imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_ERROR_TSK;
       
   244 			switch (severity) {
       
   245 			«IF editorGen.application == null-»
       
   246 			case org.eclipse.core.resources.IMarker.SEVERITY_ERROR:
       
   247 				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_ERROR_TSK;
       
   248 				break;
       
   249 			case org.eclipse.core.resources.IMarker.SEVERITY_WARNING:
       
   250 				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_WARN_TSK;
       
   251 				break;
       
   252 			«ELSE-»
       
   253 			case org.eclipse.core.runtime.IStatus.ERROR:
       
   254 				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_ERROR_TSK;
       
   255 				break;
       
   256 			case org.eclipse.core.runtime.IStatus.WARNING:
       
   257 				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_WARN_TSK;
       
   258 				break;
       
   259 			«ENDIF-»
       
   260 			default:
       
   261 				imageName = org.eclipse.ui.ISharedImages.IMG_OBJS_INFO_TSK;
       
   262 			}
       
   263 			return org.eclipse.ui.PlatformUI.getWorkbench().getSharedImages().getImage(imageName);
       
   264 		}
       
   265 
       
   266 		«EXPAND xpt::Common::generatedMemberComment»
       
   267 		public void activate() {
       
   268 			if (viewId == null) {
       
   269 				return;
       
   270 			}
       
   271 
       
   272 			// add self to global decorators registry
       
   273 			java.util.List list = (java.util.List) allDecorators.get(viewId);
       
   274 			if (list == null) {
       
   275 				list = new java.util.ArrayList(2);
       
   276 				list.add(this);
       
   277 				allDecorators.put(viewId, list);
       
   278 			} else if (!list.contains(this)) {
       
   279 				list.add(this);
       
   280 			}
       
   281 			«IF editorGen.application == null-»
       
   282 
       
   283 			// start listening to changes in resources
       
   284 			org.eclipse.gmf.runtime.notation.View view = (org.eclipse.gmf.runtime.notation.View) getDecoratorTarget().getAdapter(
       
   285 					org.eclipse.gmf.runtime.notation.View.class);
       
   286 			if (view == null) {
       
   287 				return;
       
   288 			}
       
   289 			org.eclipse.gmf.runtime.notation.Diagram diagramView = view.getDiagram();
       
   290 			if (diagramView == null) {
       
   291 				return;
       
   292 			}
       
   293 			if (fileObserver == null) {
       
   294 				org.eclipse.gmf.runtime.common.ui.resources.FileChangeManager.getInstance().addFileObserver(
       
   295 						fileObserver = new MarkerObserver(diagramView));
       
   296 			}
       
   297 			«ENDIF-»
       
   298 		}
       
   299 
       
   300 		«EXPAND xpt::Common::generatedMemberComment»
       
   301 		public void deactivate() {
       
   302 			if (viewId == null) {
       
   303 				return;
       
   304 			}
       
   305 
       
   306 			// remove self from global decorators registry
       
   307 			java.util.List list = (java.util.List) allDecorators.get(viewId);
       
   308 			if (list != null) {
       
   309 				list.remove(this);
       
   310 				if (list.isEmpty()) {
       
   311 					allDecorators.remove(viewId);
       
   312 				}
       
   313 			}
       
   314 			«IF editorGen.application == null-»
       
   315 
       
   316 			// stop listening to changes in resources if there are no more decorators
       
   317 			if (fileObserver != null && allDecorators.isEmpty()) {
       
   318 				org.eclipse.gmf.runtime.common.ui.resources.FileChangeManager.getInstance().removeFileObserver(fileObserver);
       
   319 				fileObserver = null;
       
   320 			}
       
   321 			«ENDIF-»
       
   322 			super.deactivate();
       
   323 		}
       
   324 	}
       
   325 	«IF editorGen.application == null-»
       
   326 
       
   327 	«EXPAND xpt::Common::generatedMemberComment»
       
   328 	static class MarkerObserver implements org.eclipse.gmf.runtime.common.ui.resources.IFileObserver {
       
   329 
       
   330 		«EXPAND xpt::Common::generatedMemberComment»
       
   331 		private org.eclipse.gmf.runtime.notation.Diagram diagram;
       
   332 
       
   333 		«EXPAND xpt::Common::generatedMemberComment»
       
   334 		private MarkerObserver(org.eclipse.gmf.runtime.notation.Diagram diagram) {
       
   335 			this.diagram = diagram;
       
   336 		}
       
   337 
       
   338 		«EXPAND xpt::Common::generatedMemberComment»
       
   339 		public void handleFileRenamed(org.eclipse.core.resources.IFile oldFile, org.eclipse.core.resources.IFile file) {
       
   340 		}
       
   341 
       
   342 		«EXPAND xpt::Common::generatedMemberComment»
       
   343 		public void handleFileMoved(org.eclipse.core.resources.IFile oldFile, org.eclipse.core.resources.IFile file) {
       
   344 		}
       
   345 
       
   346 		«EXPAND xpt::Common::generatedMemberComment»
       
   347 		public void handleFileDeleted(org.eclipse.core.resources.IFile file) {
       
   348 		}
       
   349 
       
   350 		«EXPAND xpt::Common::generatedMemberComment»
       
   351 		public void handleFileChanged(org.eclipse.core.resources.IFile file) {
       
   352 		}
       
   353 
       
   354 		«EXPAND xpt::Common::generatedMemberComment»
       
   355 		public void handleMarkerAdded(org.eclipse.core.resources.IMarker marker) {
       
   356 			if (marker.getAttribute(org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID, null) != null) {
       
   357 				handleMarkerChanged(marker);
       
   358 			}
       
   359 		}
       
   360 
       
   361 		«EXPAND xpt::Common::generatedMemberComment»
       
   362 		public void handleMarkerDeleted(org.eclipse.core.resources.IMarker marker, java.util.Map attributes) {
       
   363 			String viewId = (String) attributes.get(org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID);
       
   364 			refreshDecorators(viewId, diagram);
       
   365 		}
       
   366 
       
   367 		«EXPAND xpt::Common::generatedMemberComment»
       
   368 		public void handleMarkerChanged(org.eclipse.core.resources.IMarker marker) {
       
   369 			if (!MARKER_TYPE.equals(getType(marker))) {
       
   370 				return;
       
   371 			}
       
   372 			String viewId = marker.getAttribute(
       
   373 					org.eclipse.gmf.runtime.common.ui.resources.IMarker.ELEMENT_ID, ""); «EXPAND xpt::Common::nonNLS»
       
   374 			refreshDecorators(viewId, diagram);
       
   375 		}
       
   376 
       
   377 		«EXPAND xpt::Common::generatedMemberComment»
       
   378 		private String getType(org.eclipse.core.resources.IMarker marker) {
       
   379 			try {
       
   380 				return marker.getType();
       
   381 			} catch (org.eclipse.core.runtime.CoreException e) {
       
   382 				«editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError(
       
   383 						"Validation marker refresh failure", e); «EXPAND xpt::Common::nonNLS»
       
   384 				return ""; «EXPAND xpt::Common::nonNLS»
       
   385 			}
       
   386 		}
       
   387 	}
       
   388 	«ENDIF-»
       
   389 	«EXPAND additions-»
       
   390 }
       
   391 «ENDDEFINE»
       
   392 
       
   393 «DEFINE additions FOR gmfgen::GenDiagram»«ENDDEFINE»