Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8918cea877bdf43c998c6cc1bb2465a99b7195f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*****************************************************************************
 * Copyright (c) 2010, 2013 CEA LIST.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - refactor for non-workspace abstraction of problem markers (CDO)
 *  Patrick Tessier (CEA LIST) refacor to add allowing adding validation specific to UML
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.validation.commands;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.papyrus.infra.services.validation.EcoreDiagnostician;
import org.eclipse.papyrus.infra.services.validation.IPapyrusDiagnostician;
import org.eclipse.papyrus.infra.services.validation.ValidationTool;
import org.eclipse.papyrus.infra.services.validation.ValidationUtils;
import org.eclipse.papyrus.infra.services.validation.preferences.PreferenceUtils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * Abstract validation command. While being abstract (and refined by concrete validation commands)
 * it contains all validation related code.
 * 
 * 
 * @author Ansgar Radermacher (CEA LIST)
 */
abstract public class AbstractValidateCommand extends AbstractTransactionalCommand {

	final String modelValidationViewID = "org.eclipse.papyrus.views.validation.ModelValidationView"; //$NON-NLS-1$

	protected TransactionalEditingDomain domain;

	protected EObject selectedElement;

	/**
	 * Current diagnostic within a validation run
	 */
	protected Diagnostic diagnostic;
	
	protected IPapyrusDiagnostician diagnostician;

	/**
	 * Creates a new ValidationCommand
	 *
	 * @param label
	 *        the command label
	 * @param domain
	 *        the editing domain
	 * @param selectedElement the selected element
	 */
	public AbstractValidateCommand(String label, TransactionalEditingDomain domain, EObject selectedElement) {
		this(label, domain, selectedElement, new EcoreDiagnostician());
	}
	
	/**
	 * Creates a new ImportLibraryFromRepositoryCommand
	 * 
	 * @param label
	 *        the command label
	 * @param domain
	 *        the editing domain
	 * @param selectedElement
	 *        the selected element
	 * @param diagnostician
	 *        a diagnocstician adapted to a domain see {@link IPapyrusDiagnostician}
	 */
	public AbstractValidateCommand(String label, TransactionalEditingDomain domain, EObject selectedElement, IPapyrusDiagnostician diagnostician) {
		super(domain, label, Collections.EMPTY_LIST);
		this.domain = domain;
		this.selectedElement = selectedElement;
		this.diagnostician= diagnostician;
	}

	/**
	 * @return The resource on which markers should be applied.
	 */
	protected Resource getValidationResource() {
		return ValidationUtils.getValidationResource(selectedElement);
	}

	protected void runValidation(final EObject validateElement) {
		final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

		IRunnableWithProgress runValidationWithProgress = new IRunnableWithProgress()
		{

			public void run(final IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException
			{
				try {
					diagnostic = validate(progressMonitor, validateElement);
				}
				finally {
					progressMonitor.done();
				}
			}
		};

		IRunnableWithProgress createMarkersWithProgress = new IRunnableWithProgress()
		{

			public void run(final IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException
			{
				try {
					handleDiagnostic(progressMonitor, diagnostic, validateElement, shell);
					
				}
				finally {
					progressMonitor.done();
				}
			}
		};

		createMarkersWithProgress = new ValidationTool(validateElement)
			.wrap(createMarkersWithProgress);

		try {
			// runs the operation, and shows progress.
			diagnostic = null;
			new ProgressMonitorDialog(shell).run(true, true, runValidationWithProgress);
			if(diagnostic != null) {
				int markersToCreate = diagnostic.getChildren().size();
				if((markersToCreate > 0) && PreferenceUtils.getAutoShowValidation()) {
					// activate model view, if activated in configuration
					// IViewRegistry viewRegistry = PlatformUI.getWorkbench().getViewRegistry();
					// IViewDescriptor desc = viewRegistry.find(modelValidationViewID);
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(modelValidationViewID);
					// HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().showView(modelValidationViewID);
					
				}
				// don't fork this dialog, i.e. run it in the UI thread. This avoids that the diagrams are constantly refreshing *while*
				// markers/decorations are changing. This greatly enhances update performance. See also bug 400593
				new ProgressMonitorDialog(shell).run(false, true, createMarkersWithProgress);
			}
		} catch (Exception exception) {
			EMFEditUIPlugin.INSTANCE.log(exception);
		}
	}

	/**
	 * This simply executes the command.
	 */
	protected Diagnostic validate(IProgressMonitor progressMonitor, EObject validateElement)
	{
		int validationSteps = 0;
		for(Iterator<?> i = validateElement.eAllContents(); i.hasNext(); i.next()) {
			++validationSteps;
		}

		progressMonitor.beginTask("", validationSteps); //$NON-NLS-1$
		AdapterFactory adapterFactory =
			domain instanceof AdapterFactoryEditingDomain ? ((AdapterFactoryEditingDomain)domain).getAdapterFactory() : null;
		diagnostician.initialize(adapterFactory, progressMonitor);

		BasicDiagnostic diagnostic = diagnostician.createDefaultDiagnostic(validateElement);
		Map<Object, Object> context = diagnostician.createDefaultContext();

		progressMonitor.setTaskName(EMFEditUIPlugin.INSTANCE.getString("_UI_Validating_message", new Object[]{ diagnostician.getObjectLabel(validateElement) })); //$NON-NLS-1$
		diagnostician.validate(validateElement, diagnostic, context);

		if(progressMonitor.isCanceled()) {
			return null;
		}
		return diagnostic;
	}




	protected void handleDiagnostic(IProgressMonitor monitor, Diagnostic diagnostic, final EObject validateElement, final Shell shell)
	{
		// Do not show a dialog, as in the original version since the user sees the result directly
		// in the model explorer
		Resource resource = getValidationResource();
		// final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		if(resource != null) {
			if(validateElement != null) {
				SubMonitor sub = SubMonitor.convert(monitor, 2);
				
				ValidationTool vt = new ValidationTool(validateElement, resource);
				int markersToCreate = diagnostic.getChildren().size();

				sub.beginTask("Delete existing markers", 1);
				flushDisplayEvents(shell.getDisplay());

				vt.deleteSubMarkers(sub.newChild(1));

				monitor.setTaskName("Create markers (total: " + markersToCreate + " markers) and refresh diagrams"); //$NON-NLS-1$
				flushDisplayEvents(shell.getDisplay());

				vt.createMarkers(diagnostic, sub.newChild(1));
				
				sub.done();
			}
		}
	}

	protected void flushDisplayEvents(Display display) {
		while (display.readAndDispatch());
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean canExecute() {
		return (selectedElement != null);
	}
}

Back to the top