Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8dbbc9eae41a69e65c53aaeb30a0ff9b96ff55b8 (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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*****************************************************************************
 * Copyright (c) 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.ui.dialogs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.EdgeWithNoSemanticElementRepresentationImpl;
import org.eclipse.papyrus.uml.diagram.common.dialogs.AbstractCheckedTreeColumnViewerSelectionDialog;
import org.eclipse.papyrus.uml.diagram.common.messages.Messages;
import org.eclipse.papyrus.uml.diagram.common.util.LinkEndsMapper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Constraint;

/**
 *
 * The dialog used to select the link to show hide according to the selected gmf nodes
 *
 */
public class ShowHideRelatedLinkSelectionDialog extends AbstractCheckedTreeColumnViewerSelectionDialog {

	/**
	 * the width for the first column
	 */
	private final int FIRST_COLUMN_WIDTH = 180;

	/**
	 * the width for the others columns
	 */
	private final int OTHERS_COLUMN_WIDTH = 100;

	/**
	 * the titles of the columns
	 */
	private static final String[] columnsTitles = new String[] { Messages.ShowHideRelatedLinkSelectionDialog_LinksToDisplay, Messages.ShowHideRelatedLinkSelectionDialog_LinkKind, Messages.ShowHideRelatedLinkSelectionDialog_Ends,
			Messages.ShowHideRelatedLinkSelectionDialog_Sources, Messages.ShowHideRelatedLinkSelectionDialog_Targets };

	/**
	 * the mapping between model links and link end mapper
	 */
	private final Map<EObject, LinkEndsMapper> mapLink;

	/**
	 * the checkStateProvider to use
	 */
	private ICheckStateProvider checkedStateProvider;

	/**
	 * the checkStateListener used to refresh selection
	 */
	private ICheckStateListener checkedStateListener;

	/**
	 * a map between the gmf graphical elements and the availables model links
	 */
	private final Map<EditPart, Set<EObject>> availableLinks;

	/**
	 * the selection
	 */
	private final Set<EObject> selectedElements;


	/**
	 *
	 * Constructor.
	 *
	 * @param parent
	 *            the parent shell
	 * @param labelProvider
	 *            the label provider
	 * @param contentProvider
	 *            the content provider
	 * @param availableLinks
	 *            a map linking selected editparts and their available links
	 * @param linkEndsMapping
	 *            a map between the links and their ends
	 */
	public ShowHideRelatedLinkSelectionDialog(final Shell parent, final ILabelProvider labelProvider, final ITreeContentProvider contentProvider, final Map<EditPart, Set<EObject>> availableLinks, final Map<EObject, LinkEndsMapper> linkEndsMapping) {
		super(parent, labelProvider, contentProvider, SWT.CENTER | SWT.BORDER | SWT.FULL_SELECTION, columnsTitles.length);
		this.mapLink = linkEndsMapping;
		this.availableLinks = new HashMap<EditPart, Set<EObject>>(availableLinks);
		this.selectedElements = new HashSet<EObject>();
		setContainerMode(true);
		init();
	}

	/**
	 *
	 * @see org.eclipse.papyrus.uml.diagram.common.dialogs.CustomCheckedTreeSelectionDialog#setInitialSelection(java.lang.Object)
	 *
	 * @param selection
	 */
	public void setInitialSelection(final Collection<EObject> initialSelection) {
		this.selectedElements.addAll(initialSelection);
		super.setInitialSelection(initialSelection.toArray());
	}


	/**
	 *
	 * @param columnIndex
	 * @return
	 */
	@Override
	protected EditingSupport getEditingSupport(int columnIndex) {
		return null;
	}

	/**
	 * Initialize the columns Width, the columns Titles and the LabelProvider for the viewer.
	 *
	 */
	protected void init() {
		setColumnTitles(columnsTitles);
		int[] columnWidth = new int[columnsTitles.length];
		columnWidth[0] = FIRST_COLUMN_WIDTH;
		for (int i = 1; i < columnsTitles.length; i++) {
			columnWidth[i] = OTHERS_COLUMN_WIDTH;
		}
		setColumnWidths(columnWidth);
		setColumnCellLabelProvider(new CellLabelProvider[] { new EObjectLabelProvider(), new LinkEClassLabelProvider(), new EndsElementLabelProvider(), new SourceElementLabelProvider(), new TargetElementLabelProvider() });
	}

	/**
	 *
	 * @see org.eclipse.papyrus.uml.diagram.common.dialogs.AbstractCheckedTreeColumnViewerSelectionDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 *
	 * @param parent
	 * @return
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Control ctrl = super.createDialogArea(parent);
		this.checkedStateProvider = getOrCreateCheckStateProvider();
		getTreeViewer().setCheckStateProvider(this.checkedStateProvider);
		this.checkedStateListener = getOrCreateCheckStateListener();
		getTreeViewer().addCheckStateListener(this.checkedStateListener);
		updateTree(getTreeViewer());
		return ctrl;
	}

	@Override
	public void create() {
		super.create();
		updateTree(getTreeViewer());
	}

	/**
	 *
	 * @return
	 *         the checkedStateListener to use
	 */
	protected ICheckStateListener getOrCreateCheckStateListener() {
		if (this.checkedStateListener == null) {
			this.checkedStateListener = new ICheckStateListener() {

				/**
				 *
				 * @see org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent)
				 *
				 * @param event
				 */
				@Override
				public void checkStateChanged(CheckStateChangedEvent event) {
					final Object currentElement = event.getElement();
					boolean isChecked = event.getChecked();
					if (currentElement instanceof EditPart) {
						// we update the selected element list
						if (isChecked) {
							selectedElements.addAll(availableLinks.get(currentElement));
						} else {
							selectedElements.removeAll(availableLinks.get(currentElement));
						}

						// in some case, the EditPart can be a link which appears as possible elements of an other selected edit part
						final View view = (View) ((EditPart) currentElement).getAdapter(View.class);
						if (view instanceof Edge) {
							final EObject element = view.getElement();
							for (final Set<EObject> currentCol : availableLinks.values()) {
								if (currentCol.contains(element)) {
									if (isChecked) {
										selectedElements.add(element);
									} else {
										selectedElements.remove(element);
									}
								}
							}
						}
					} else {
						// we update the selected element list
						if (isChecked) {
							selectedElements.add((EObject) currentElement);
						} else {
							selectedElements.remove(currentElement);
						}
					}
					updateTree(getTreeViewer());
				}
			};
		}
		return this.checkedStateListener;
	}

	/**
	 *
	 * @param treeViewer
	 */
	// TODO : probably not the best way...
	protected final void updateTree(final TreeViewer treeViewer) {
		for (final TreeItem items : getTreeViewer().getTree().getItems()) {
			updateTreeItems(getTreeViewer(), items, getOrCreateCheckStateProvider());
		}
	}

	/**
	 *
	 * @param treeViewer
	 * @param item
	 * @param stateProvider
	 */
	private void updateTreeItems(final TreeViewer treeViewer, final TreeItem item, final ICheckStateProvider stateProvider) {
		boolean isChecked = stateProvider.isChecked(item.getData());
		boolean isGrayed = stateProvider.isGrayed(item.getData());
		item.setChecked(isChecked);
		item.setGrayed(isGrayed);
		for (final TreeItem child : item.getItems()) {
			updateTreeItems(treeViewer, child, stateProvider);
		}
	}

	/**
	 *
	 * @return
	 *         the checkedStateProvider to use for this dialog
	 */
	protected ICheckStateProvider getOrCreateCheckStateProvider() {
		if (this.checkedStateProvider == null) {
			this.checkedStateProvider = new ICheckStateProvider() {

				/**
				 *
				 * @see org.eclipse.jface.viewers.ICheckStateProvider#isGrayed(java.lang.Object)
				 *
				 * @param element
				 * @return
				 */
				@Override
				public boolean isGrayed(Object element) {
					boolean isGrayed = false;
					if (element instanceof EditPart) {
						Set<EObject> values = availableLinks.get(element);
						final Collection<EObject> tmp = new ArrayList<EObject>(selectedElements);
						tmp.retainAll(values);
						isGrayed = tmp.size() != values.size();
					}
					return isGrayed;
				}

				/**
				 *
				 * @see org.eclipse.jface.viewers.ICheckStateProvider#isChecked(java.lang.Object)
				 *
				 * @param element
				 * @return
				 */
				@Override
				public boolean isChecked(Object element) {
					boolean isChecked = false;
					if (element instanceof EditPart) {
						final Collection<?> possibleLinks = availableLinks.get(element);
						Collection<EObject> tmp = new ArrayList<EObject>(selectedElements);
						tmp.retainAll(possibleLinks);
						isChecked = tmp.size() > 0;


						final View view = (View) ((EditPart) element).getAdapter(View.class);
						if (!isChecked && view instanceof Edge) {// the edge edit part could be selected as grayed because the link is already selected for an another edit part
							final EObject eobject = view.getElement();
							isChecked = selectedElements.contains(eobject);
						}


					} else if (element instanceof EObject) {
						isChecked = selectedElements.contains(element);
					}
					return isChecked;
				}
			};
		}
		return this.checkedStateProvider;
	}

	/**
	 * This provider is used by the 2nd column
	 *
	 */
	protected class LinkEClassLabelProvider extends ColumnLabelProvider {

		/** label provider */
		private final ILabelProvider labelProvider;

		public LinkEClassLabelProvider() {
			labelProvider = getLabelProvider();
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public Image getImage(final Object element) {
			if (element instanceof EdgeWithNoSemanticElementRepresentationImpl) {
				return null;
			}
			if (element instanceof EObject) {
				return labelProvider.getImage(element);
			}
			return null;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String getText(final Object element) {
			if (element instanceof EdgeWithNoSemanticElementRepresentationImpl) {
				final EObject source = ((EdgeWithNoSemanticElementRepresentationImpl) element).getSource();
				if (source instanceof Comment) {
					return Messages.ShowHideRelatedLinkSelectionDialog_CommentLink;
				}
				if (source instanceof Constraint) {
					return Messages.ShowHideRelatedLinkSelectionDialog_ConstraintLink;
				}
			}
			if (element instanceof EObject) {
				return ((EObject) element).eClass().getName();
			}
			return ""; //$NON-NLS-1$
		}

		/**
		 *
		 * @see org.eclipse.jface.viewers.CellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
		 *
		 * @param cell
		 */
		@Override
		public void update(ViewerCell cell) {
			cell.setImage(getImage(cell.getElement()));
			cell.setText(getText(cell.getElement()));
		}
	}


	/**
	 * This provider is used by the 2nd column
	 *
	 */
	protected class EObjectLabelProvider extends ColumnLabelProvider {

		/** label provider */
		private final ILabelProvider labelProvider;

		public EObjectLabelProvider() {
			labelProvider = getLabelProvider();
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public Image getImage(Object element) {
			if (element instanceof EdgeWithNoSemanticElementRepresentationImpl) {
				final EObject source = ((EdgeWithNoSemanticElementRepresentationImpl) element).getSource();
				if (source instanceof Comment || source instanceof Constraint) {
					return labelProvider.getImage(source);
				}
			}
			return labelProvider.getImage(element);
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String getText(Object element) {
			if (element instanceof EdgeWithNoSemanticElementRepresentationImpl) {
				final EObject source = ((EdgeWithNoSemanticElementRepresentationImpl) element).getSource();
				if (source instanceof Comment) {
					return Messages.ShowHideRelatedLinkSelectionDialog_CommentLink;
				} else if (source instanceof Constraint) {
					return Messages.ShowHideRelatedLinkSelectionDialog_ConstraintLink;
				}
			}
			return labelProvider.getText(element);
		}


		/**
		 *
		 * @see org.eclipse.jface.viewers.CellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
		 *
		 * @param cell
		 */
		@Override
		public void update(ViewerCell cell) {
			cell.setImage(getImage(cell.getElement()));
			cell.setText(getText(cell.getElement()));
		}
	}

	/**
	 * This provider is used by the 2nd column
	 *
	 */
	protected class TargetElementLabelProvider extends AbstractLinkEndColumnsLabelProvider {

		/**
		 *
		 * Constructor.
		 *
		 * @param labelProvider
		 */
		public TargetElementLabelProvider(final ILabelProvider labelProvider) {
			super(labelProvider);
		}

		/**
		 *
		 * Constructor.
		 *
		 */
		public TargetElementLabelProvider() {
			super(getLabelProvider());
		}

		/**
		 *
		 * @see org.eclipse.papyrus.uml.diagram.menu.dialogs.ShowHideRelatedLinkSelectionDialog.AbstractLinkEndColumnsLabelProvider#getValues(java.lang.Object)
		 *
		 * @param element
		 * @return
		 */
		@Override
		protected Collection<?> getValues(Object element) {
			final LinkEndsMapper mapper = mapLink.get(element);
			if (mapper != null) {
				return mapper.getTargets();
			}
			return Collections.emptyList();
		}
	}

	/**
	 * This provider is used by the 2nd column
	 *
	 */
	protected class SourceElementLabelProvider extends AbstractLinkEndColumnsLabelProvider {

		/**
		 *
		 * Constructor.
		 *
		 * @param labelProvider
		 */
		public SourceElementLabelProvider(final ILabelProvider labelProvider) {
			super(labelProvider);
		}

		/**
		 *
		 * Constructor.
		 *
		 */
		public SourceElementLabelProvider() {
			super(getLabelProvider());
		}

		/**
		 *
		 * @param element
		 * @return
		 */
		@Override
		protected Collection<?> getValues(Object element) {
			final LinkEndsMapper mapper = mapLink.get(element);
			if (mapper != null) {
				return mapper.getSources();
			}
			return Collections.emptyList();
		}
	}

	public abstract class AbstractLinkEndColumnsLabelProvider extends ColumnLabelProvider {

		/**
		 * utils constants for label provider
		 */
		public static final String START_LIST = "["; //$NON-NLS-1$

		public static final String END_LIST = "]"; //$NON-NLS-1$

		public static final String VALUE_SEPARATOR = ","; //$NON-NLS-1$

		/**
		 * the label provider to use
		 */
		private final ILabelProvider labelProvider;

		/**
		 *
		 * Constructor.
		 *
		 * @param labelProvider
		 */
		public AbstractLinkEndColumnsLabelProvider(final ILabelProvider labelProvider) {
			this.labelProvider = labelProvider;
		}

		/**
		 *
		 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getImage(java.lang.Object)
		 *
		 * @param element
		 * @return
		 */
		@Override
		public Image getImage(Object element) {
			return null;
		}

		/**
		 *
		 * @see org.eclipse.jface.viewers.CellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
		 *
		 * @param cell
		 */
		@Override
		public void update(ViewerCell cell) {
			cell.setImage(getImage(cell.getElement()));
			cell.setText(getText(cell.getElement()));
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String getText(Object element) {
			final StringBuilder builder = new StringBuilder();
			final Collection<?> ends = getValues(element);
			if (!ends.isEmpty()) {
				builder.append(START_LIST);
				final Iterator<?> iter = ends.iterator();
				while (iter.hasNext()) {
					final Object current = iter.next();
					if (current instanceof Comment) {
						builder.append(Messages.ShowHideRelatedLinkSelectionDialog_Comment_);
					}
					builder.append(this.labelProvider.getText(current));
					if (iter.hasNext()) {
						builder.append(VALUE_SEPARATOR);
					}
				}
				builder.append(END_LIST);
			}
			return builder.toString();
		}

		protected abstract Collection<?> getValues(final Object element);
	}

	/**
	 * This provider is used by the 2nd column
	 *
	 */
	protected class EndsElementLabelProvider extends AbstractLinkEndColumnsLabelProvider {

		/**
		 *
		 * Constructor.
		 *
		 * @param labelProvider
		 */
		public EndsElementLabelProvider(final ILabelProvider labelProvider) {
			super(labelProvider);
		}

		/**
		 *
		 * Constructor.
		 *
		 */
		public EndsElementLabelProvider() {
			super(getLabelProvider());
		}

		/**
		 *
		 * @see org.eclipse.papyrus.uml.diagram.menu.dialogs.ShowHideRelatedLinkSelectionDialog.AbstractLinkEndColumnsLabelProvider#getValues(java.lang.Object)
		 *
		 * @param element
		 * @return
		 */
		@Override
		protected Collection<?> getValues(Object element) {
			final LinkEndsMapper mapper = mapLink.get(element);
			if (mapper != null) {
				return mapper.getEnds();
			}
			return Collections.emptyList();
		}
	}


}

Back to the top