Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb77e00f263281ad943f5d12ece0ec930a43b004 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Florian Noyrit (CEA LIST) florian.noyrit@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.properties.tabbed.customization.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.properties.runtime.state.IState;
import org.eclipse.papyrus.properties.tabbed.core.view.SectionDescriptorState;
import org.eclipse.papyrus.properties.tabbed.core.view.SectionDescriptorState.ReplacedSectionState;
import org.eclipse.papyrus.properties.tabbed.core.view.SectionSetDescriptorState;
import org.eclipse.papyrus.properties.tabbed.core.view.StatesStore;
import org.eclipse.papyrus.properties.tabbed.core.view.TabDescriptorState;
import org.eclipse.papyrus.properties.tabbed.customization.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.views.properties.tabbed.ISectionDescriptor;



/**
 * Validator for configuration that output report for errors and warnings.
 */
public class ConfigurationValidator {

	protected Shell shell;

	/** problem found during validation */
	protected ArrayList<AbstractValidationProblem> errorsFound = new ArrayList<AbstractValidationProblem>();

	/** problem found during validation */
	protected ArrayList<AbstractValidationProblem> warningsFound = new ArrayList<AbstractValidationProblem>();

	TableViewer errorsTableViewer;

	TableViewer warningsTableViewer;

	/**
	 * A specific validation problem for duplicates
	 */
	protected class Duplicate extends AbstractValidationProblem {

		protected IState stateA;

		protected IState stateB;

		/**
		 * Getter for the first state concerned by this problem
		 * 
		 * @return
		 *         the state concerned by this problem
		 */
		public IState getStateA() {
			return stateA;
		}

		/**
		 * Setter for the first state concerned by this problem
		 * 
		 * @param state
		 *        the state concerned by this problem
		 */
		public void setStateA(IState state) {
			this.stateA = state;
		}

		/**
		 * Getter for the second state concerned by this problem
		 * 
		 * @return
		 *         the state concerned by this problem
		 */
		public IState getStateB() {
			return stateB;
		}

		/**
		 * Setter for the second state concerned by this problem
		 * 
		 * @param state
		 *        the state concerned by this problem
		 */
		public void setStateB(IState state) {
			this.stateB = state;
		}

		/**
		 * 
		 * Constructor.
		 * 
		 * @param stateA
		 *        the first state concerned by this problem
		 * @param stateB
		 *        the second state concerned by this problem
		 */
		public Duplicate(IState stateA, IState stateB) {
			setMessage("Duplicate : ");
			this.stateA = stateA;
			this.stateB = stateB;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Duplicate) {
				if(((Duplicate)obj).getStateA() == stateA || ((Duplicate)obj).getStateB() == stateA) {
					return true;
				} else {
					return false;
				}
			}

			return super.equals(obj);
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String getText() {
			return message + stateA.getText() + " <-> " + stateB.getText();
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public Image getImage() {
			return Activator.getImage("/icons/Duplicate.gif");
		}
	}

	/**
	 * A specific validation problem for broken link (broken references among elements)
	 */
	protected class BrokenLink extends AbstractValidationProblem {

		protected IState state;

		/**
		 * Getter for the state concerned by this problem
		 * 
		 * @return
		 *         the state concerned by this problem
		 */
		public IState getState() {
			return state;
		}

		/**
		 * Setter for the state concerned by this problem
		 * 
		 * @param state
		 *        the state concerned by this problem
		 */
		public void setStateA(IState state) {
			this.state = state;
		}

		/**
		 * 
		 * Constructor.
		 * 
		 * @param state
		 *        the state concerned by this problem
		 */
		public BrokenLink(IState state) {
			setMessage("BrokenLink : ");
			this.state = state;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String getText() {
			return message + state.getText();
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public Image getImage() {
			return Activator.getImage("/icons/BrokenLink.gif");
		}
	}

	public ConfigurationValidator(Shell shell) {
		this.shell = shell;
	}

	/**
	 * Validates the configuration
	 * 
	 * @param forSave
	 *        if set to true, the report dialog will be adapted for saving purpose
	 * @param verbose
	 *        if set to true, report is given even when configuration is valid
	 * @return true if the configuration is valid, false otherwise
	 */
	public boolean validateConfig(boolean forSave, boolean verbose) {

		errorsFound.clear();
		warningsFound.clear();
		//Find duplicates

		//For tabs 
		for(TabDescriptorState tabDescriptorState : StatesStore.getTabDescriptorStates()) {
			for(TabDescriptorState tabDescriptorState2 : StatesStore.getTabDescriptorStates()) {
				if(tabDescriptorState != tabDescriptorState2) {
					if(tabDescriptorState.getId().equals(tabDescriptorState2.getId())) {
						if(!errorsFound.contains(new Duplicate(tabDescriptorState, tabDescriptorState2))) {
							Duplicate duplicate = new Duplicate(tabDescriptorState, tabDescriptorState2);
							duplicate.setMessage("Duplicated tabID: ");
							errorsFound.add(duplicate);
						}
					}
				}
			}
		}

		//For sections
		List<SectionDescriptorState> sectionDescriptorStates = new ArrayList<SectionDescriptorState>();
		for(SectionSetDescriptorState sectionSetDescriptorState : StatesStore.getSectionSetDescriptorStates()) {
			sectionDescriptorStates.addAll(sectionSetDescriptorState.getSectionDescriptorStates());
		}
		for(SectionDescriptorState sectionDescriptorState : sectionDescriptorStates) {
			for(SectionDescriptorState sectionDescriptorState2 : sectionDescriptorStates) {
				if(sectionDescriptorState != sectionDescriptorState2) {
					if(sectionDescriptorState.getId().equals(sectionDescriptorState2.getId())) {
						if(!errorsFound.contains(new Duplicate(sectionDescriptorState, sectionDescriptorState2))) {
							Duplicate duplicate = new Duplicate(sectionDescriptorState, sectionDescriptorState2);
							duplicate.setMessage("Duplicated sectionID: ");
							errorsFound.add(duplicate);
						}
					}
				}
			}
		}

		//For fragments
		//		List<IFragmentDescriptorState> fragmentDescriptorStates = new ArrayList<IFragmentDescriptorState>();
		//		for(SectionDescriptorState sectionDescriptorState : sectionDescriptorStates) {
		//			fragmentDescriptorStates.addAll(sectionDescriptorState.getFragmentDescriptorStates());
		//		}
		//		for(IFragmentDescriptorState fragmentDescriptorState : fragmentDescriptorStates) {
		//			for(IFragmentDescriptorState fragmentDescriptorState2 : fragmentDescriptorStates) {
		//				if(fragmentDescriptorState != fragmentDescriptorState2) {
		//					if(fragmentDescriptorState.getId().equals(fragmentDescriptorState2.getId())) {
		//						if(!IDduplicates.containsValue(fragmentDescriptorState) && !IDduplicates.containsKey(fragmentDescriptorState)) {
		//							IDduplicates.put(fragmentDescriptorState, fragmentDescriptorState2);
		//						}
		//					}
		//				}
		//			}
		//		}

		//Check tabs label 
		for(TabDescriptorState tabDescriptorState : StatesStore.getTabDescriptorStates()) {
			for(TabDescriptorState tabDescriptorState2 : StatesStore.getTabDescriptorStates()) {
				if(tabDescriptorState != tabDescriptorState2) {
					if(tabDescriptorState.getLabel().equals(tabDescriptorState2.getLabel())) {
						if(!warningsFound.contains(new Duplicate(tabDescriptorState, tabDescriptorState2))) {
							Duplicate duplicate = new Duplicate(tabDescriptorState, tabDescriptorState2);
							duplicate.setMessage("Confusing tab label: ");
							warningsFound.add(duplicate);
						}
					}
				}
			}
		}




		//Check references
		boolean found;
		//TabIdentifier in sections
		for(SectionDescriptorState sectionDescriptorState : sectionDescriptorStates) {
			found = false;
			for(TabDescriptorState tabDescriptorState : StatesStore.getTabDescriptorStates()) {
				if(sectionDescriptorState.getTargetTab().equals(tabDescriptorState.getId())) {
					found = true;
				}
			}
			if(!found) {
				BrokenLink brokenLink = new BrokenLink(sectionDescriptorState);
				brokenLink.setMessage("Broken link for TargetTab in : ");
				errorsFound.add(brokenLink);
			}
		}

		//AfterSection in sections
		for(SectionDescriptorState sectionDescriptorState : sectionDescriptorStates) {
			found = false;
			if(!sectionDescriptorState.getAfterSection().equals(ISectionDescriptor.TOP)) {
				for(SectionDescriptorState sectionDescriptorState2 : sectionDescriptorStates) {
					if(sectionDescriptorState.getAfterSection().equals(sectionDescriptorState2.getId())) {
						found = true;
					}
				}
				if(!found) {
					BrokenLink brokenLink = new BrokenLink(sectionDescriptorState);
					brokenLink.setMessage("Broken link for AfterSection in : ");
					warningsFound.add(brokenLink);
				}
			}
		}

		//Identifier in ReplacedSection
		List<ReplacedSectionState> replacedSectionStates = new ArrayList<ReplacedSectionState>();
		for(SectionDescriptorState sectionDescriptorState : sectionDescriptorStates) {
			replacedSectionStates.addAll(sectionDescriptorState.getReplacedSectionStates());
		}
		for(ReplacedSectionState replacedSectionState : replacedSectionStates) {
			found = false;
			for(SectionDescriptorState sectionDescriptorState : sectionDescriptorStates) {
				if(replacedSectionState.getId().equals(sectionDescriptorState.getId())) {
					found = true;
				}
			}
			if(!found) {
				BrokenLink brokenLink = new BrokenLink(replacedSectionState);
				brokenLink.setMessage("Broken link for Identifier in : ");
				warningsFound.add(brokenLink);
			}
		}

		if(errorsFound.isEmpty() && warningsFound.isEmpty()) {
			if(verbose) {
				MessageDialog.openInformation(shell, "Validation report", "No error and no warning found");
			}
			return true;
		} else {
			ReportDialog dialog = new ReportDialog(shell, forSave);
			dialog.open();
			//If forSave and save anyway
			if(forSave && dialog.getReturnCode() == IDialogConstants.OK_ID) {
				return true;
			} else {
				return false;
			}
		}
	}

	/**
	 * The report dialog opened to display the errors and warnings found in the configuration
	 */
	protected class ReportDialog extends TitleAreaDialog {

		private boolean forSave;

		/**
		 * 
		 * Constructor.
		 * 
		 * @param parentShell
		 *        the shell to use
		 * @param forSave
		 *        if set to true, the report dialog will be adapted for saving purpose
		 */
		public ReportDialog(Shell parentShell, boolean forSave) {
			super(parentShell);
			this.setShellStyle(getShellStyle() | SWT.RESIZE);
			this.forSave = forSave;
		}

		/**
		 * {@inheritDoc}
		 */
		public Control createContents(Composite parent) {
			Control contents = super.createContents(parent);

			// Set the title
			setTitle("Validation report");

			// Set the message
			setMessage("Please reconsider the following issues.", IMessageProvider.NONE);

			return contents;
		}

		protected Control createProblemReportArea(Composite conposite) {
			Composite contents = new Composite(conposite, SWT.BORDER);
			contents.setLayout(new GridLayout(1, true));
			contents.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

			ILabelProvider labelProvider = new LabelProvider() {

				/**
				 * {@inheritDoc}
				 */
				public Image getImage(Object element) {
					if(element instanceof AbstractValidationProblem) {
						return ((AbstractValidationProblem)element).getImage();
					}
					return null;
				}

				/**
				 * {@inheritDoc}
				 */
				public String getText(Object element) {
					if(element instanceof AbstractValidationProblem) {
						return ((AbstractValidationProblem)element).getText();
					}
					return element.toString();
				}
			};

			IContentProvider contentProvider = new IStructuredContentProvider() {

				public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
					// TODO Auto-generated method stub

				}

				public void dispose() {
					// TODO Auto-generated method stub

				}

				public Object[] getElements(Object inputElement) {
					return ((ArrayList<Object>)inputElement).toArray();
				}
			};

			// Create Label
			Label labelErrors = new Label(contents, SWT.NONE);
			labelErrors.setText("Error(s) found: ");


			int flags = SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL;
			errorsTableViewer = new TableViewer(contents, flags);
			GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
			gridData.widthHint = convertWidthInCharsToPixels(80);
			gridData.heightHint = convertHeightInCharsToPixels(18);
			errorsTableViewer.getControl().setLayoutData(gridData);
			errorsTableViewer.setLabelProvider(labelProvider);
			errorsTableViewer.setContentProvider(contentProvider);


			Label labelWarnings = new Label(contents, SWT.NONE);
			labelWarnings.setText("Warning(s) found: ");

			warningsTableViewer = new TableViewer(contents, flags);
			GridData gridData2 = new GridData(SWT.FILL, SWT.FILL, true, true);
			gridData2.widthHint = convertWidthInCharsToPixels(80);
			gridData2.heightHint = convertHeightInCharsToPixels(18);
			warningsTableViewer.getControl().setLayoutData(gridData2);
			warningsTableViewer.setLabelProvider(labelProvider);
			warningsTableViewer.setContentProvider(contentProvider);


			return contents;

		}


		/**
		 * {@inheritDoc}
		 */
		protected Control createDialogArea(Composite parent) {

			Composite parentComposite = (Composite)super.createDialogArea(parent);

			initializeDialogUnits(parentComposite);

			createProblemReportArea(parentComposite);

			errorsTableViewer.setInput(errorsFound);
			warningsTableViewer.setInput(warningsFound);

			return parentComposite;
		}

		/**
		 * {@inheritDoc}
		 */
		protected void createButtonsForButtonBar(Composite parent) {
			if(forSave) {
				createButton(parent, IDialogConstants.CANCEL_ID, "Don't save", true);
				createButton(parent, IDialogConstants.OK_ID, "Save anyway", false);
			} else {
				createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
			}
		}
	}
}

Back to the top