Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab6e986727b313e592cd510d2e1a0f7378cbcf91 (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
package org.eclipse.papyrus.uml.diagram.common.actions;

/*****************************************************************************
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.ui.util.DisplayUtils;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.commands.wrappers.GEFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.emf.providers.EMFLabelProvider;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.util.ViewServiceUtil;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog;
import org.eclipse.ui.dialogs.SelectionDialog;

/**
 * 
 * Super Class for the ShowHide action
 * 
 */
public abstract class AbstractShowHideAction implements IActionDelegate, IWorkbenchWindowActionDelegate {

	/** the selected elements */
	protected List<IGraphicalEditPart> selectedElements;

	/** the initial selection */
	protected List<Object> initialSelection;

	/** key for the EditPolicy providing the action */
	private String editPolicyKey = null;

	/** title for the window */
	private String title = null;

	/** message for the window */
	private String message = null;

	/** label provider */
	private ILabelProvider labelProvider = null;

	/** content provider */
	private ITreeContentProvider contentProvider = null;

	/** the list of the views to destroy */
	protected List<EditPartRepresentation> viewsToDestroy;

	/** the list of the view to create */
	protected List<EditPartRepresentation> viewsToCreate;

	/** the list of the {@link EditPartRepresentation} */
	protected List<EditPartRepresentation> representations;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param title
	 *        title for the dialog
	 * @param message
	 *        message for the dialog
	 * @param editPolicyKey
	 *        the EditPolicy used for this action
	 */
	public AbstractShowHideAction(String title, String message, String editPolicyKey) {
		this.editPolicyKey = editPolicyKey;
		this.title = title;
		this.message = message;
		/* for further information, see bug 302555 */
		ViewServiceUtil.forceLoad();
	}

	/**
	 * {@inheritDoc}
	 */
	public void dispose() {
		// nothing here
	}

	/**
	 * {@inheritDoc}
	 */
	public void init(IWorkbenchWindow window) {
		// nothing here
	}

	/**
	 * {@inheritDoc}
	 */
	public void run(IAction action) {

		if(!canRun()) {
			return;
		}
		initAction();
		buildInitialSelection();
		SelectionDialog selectionDialog = getSelectionDialog();
		selectionDialog.open();
		if(selectionDialog.getReturnCode() == Dialog.OK) {
			buildShowHideElementsList(selectionDialog.getResult());
			final Command command = getActionCommand();
			final TransactionalEditingDomain domain = this.selectedElements.get(0).getEditingDomain();
			if(command.canExecute()) {
				try {
					domain.runExclusive(new Runnable() {

						public void run() {
							PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

								// executing the command
								public void run() {
									domain.getCommandStack().execute(new GEFtoEMFCommandWrapper(command));
								}
							});
						}
					});
				} catch (InterruptedException e) {
					Activator.log.error(e);
				}
			}
		}
	}

	/**
	 * Creates and returns the selection dialog displayed by this action
	 * 
	 * @return the created selection dialog
	 */
	protected SelectionDialog getSelectionDialog() {
		CheckedTreeSelectionDialog selectionDialog = new CheckedTreeSelectionDialog(DisplayUtils.getDisplay().getActiveShell(), getEditorLabelProvider(), getContentProvider());
		selectionDialog.setTitle(title);
		selectionDialog.setMessage(message);
		selectionDialog.setContainerMode(true);
		selectionDialog.setInput(getInput());
		selectionDialog.setExpandedElements(getExpandedElements());
		selectionDialog.setInitialElementSelections(getInitialSelection());
		return selectionDialog;
	}

	/**
	 * Returns the list of all elements
	 * 
	 * @return the list of all elements
	 */
	protected Object[] getExpandedElements() {
		List<Object> allElements = new ArrayList<Object>();
		for(EditPartRepresentation current : this.representations) {
			allElements.add(current);
			allElements.addAll(current.getPossibleElement());
		}
		return allElements.toArray();
	}

	/**
	 * 
	 * Test if the action can be run! Useful when we press F4 and it's the first
	 * action in Papyrus
	 * 
	 * @return if the action can be run or not
	 */
	public boolean canRun() {

		if(this.selectedElements.isEmpty()) {
			return false;
		}
		for(Object object : this.selectedElements) {
			if(!(object instanceof EditPart)) {
				return false;
			} else {
				EditPolicy policy = ((EditPart)object).getEditPolicy(editPolicyKey);
				if(policy == null) {
					return false;
				}
			}
		}

		return true;
	}

	/**
	 * 
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
	 * 
	 * @param action
	 *        the current action
	 * @param selection
	 *        the current selection
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		boolean enabled = false;
		if(editPolicyKey != null) {
			selectedElements = new ArrayList<IGraphicalEditPart>();
			if(selection instanceof StructuredSelection) {
				for(Object current : ((StructuredSelection)selection).toArray()) {
					if(current instanceof IGraphicalEditPart) {
						selectedElements.add((IGraphicalEditPart)current);
						EditPolicy policy = ((IGraphicalEditPart)current).getEditPolicy(editPolicyKey);
						if(policy != null) {
							enabled = true;
							break;
						}
					}
				}
			}

		}
		if(action != null) {
			action.setEnabled(enabled);
		}
	}

	/**
	 * Initialize
	 * <ul>
	 * <li>{@link #labelProvider}</li>
	 * <li> {@link #representations}</li>
	 * </ul>
	 * 
	 * This method should be override by subclasses to initialize {@link #contentProvider} for example.
	 * 
	 */
	protected void initAction() {
		this.labelProvider = new EditPartRepresentationLabelProvider();
		this.representations = new ArrayList<AbstractShowHideAction.EditPartRepresentation>();
	}

	/**
	 * Returns the initial selection for the Tree Dialog.
	 * 
	 * @return The initial selection for the Tree Dialog
	 */
	public List<Object> getInitialSelection() {
		return this.initialSelection;

	}

	/**
	 * Build the initial selection for the tree
	 */
	protected void buildInitialSelection() {
		this.initialSelection = new ArrayList<Object>();
		for(EditPartRepresentation current : this.representations) {
			contributeToInitialSelection(initialSelection, current);
		}
	}

	/**
	 * Complete the list of initial selection for the given representation and its potential children
	 * 
	 * @param listToComplete
	 *        the list of selected elements to complete
	 * @param representation
	 *        the edit part representation that completes the list
	 */
	protected void contributeToInitialSelection(List<Object> listToComplete, EditPartRepresentation representation) {
		listToComplete.addAll(representation.getInitialSelection());
		List<EditPartRepresentation> children = representation.getPossibleElement();
		if(children != null) {
			for(EditPartRepresentation child : children) {
				contributeToInitialSelection(listToComplete, child);
			}
		}
	}

	/**
	 * Returns the input for the TreeDialog
	 * 
	 * @return the input for the TreeDialog
	 */
	abstract protected List<Object> getInput();

	/**
	 * Fill the following fields with the elements to show and the elements to
	 * hide
	 * <ul>
	 * <li>{@link #viewsToCreate}</li>
	 * <li> {@link #viewsToDestroy}</li>
	 * </ul>
	 * 
	 * @param result
	 *        the interesting element selected in the tree
	 */
	protected void buildShowHideElementsList(Object[] result) {
		this.viewsToCreate = new ArrayList<EditPartRepresentation>();
		this.viewsToDestroy = new ArrayList<EditPartRepresentation>();
	}

	/**
	 * Returns the command done by this action
	 * 
	 * @return the command done by this action
	 */
	abstract protected Command getActionCommand();

	/**
	 * Setter for {@link #labelProvider}
	 * 
	 * @param provider
	 *        the label provider for the tree
	 */
	protected void setEditorLabelProvider(ILabelProvider provider) {
		this.labelProvider = provider;
	}

	/**
	 * Returns the label provider used by the dialog
	 * 
	 * @return the labelProvider
	 */
	protected ILabelProvider getEditorLabelProvider() {
		return labelProvider;
	}

	/**
	 * Setter for {@link #contentProvider}
	 * 
	 * @param provider
	 *        the provider for the tree
	 */
	protected void setContentProvider(ITreeContentProvider provider) {
		this.contentProvider = provider;
	}

	/**
	 * Returns the content provider for the selection dialog
	 * 
	 * @return the contentProvider
	 */
	protected ITreeContentProvider getContentProvider() {
		return contentProvider;
	}

	/**
	 * Setter for {@link #selectedElements}. When this action is called by a
	 * popup menu, {@link #selectedElements} is filled by {@link #selectionChanged(IAction, ISelection)} When this action is called
	 * by a Handler, {@link #selectedElements} is filled with this method
	 * 
	 * @param selection
	 *        the current selection
	 */
	public void setSelection(List<IGraphicalEditPart> selection) {
		this.selectedElements = selection;
	}

	/**
	 * Getter for {@link AbstractShowHideAction#editPolicyKey}
	 * 
	 * @return
	 * 
	 *         {@link AbstractShowHideAction#editPolicyKey}
	 */
	public String getEditPolicyKey() {
		return this.editPolicyKey;
	}

	/**
	 * 
	 * This class provides some facilities to build the action.
	 * 
	 */
	public class EditPartRepresentation {

		/** the editpart represented by this class */
		private IGraphicalEditPart representedEditPart;

		/** the initial selection */
		protected List<EditPartRepresentation> initialSelection;

		/** the possible element to show/hide */
		protected List<EditPartRepresentation> elementsToSelect;

		/** the semantic element represented by the EditPart */
		protected EObject eObject;

		/** parent edit part representation */
		final private EditPartRepresentation parentRepresentation;

		/**
		 * Constructor.
		 * 
		 * @param representedEditPart
		 *        the represented EditPart
		 * @param eObject
		 *        the semantic element represented by EditPartRepresentation
		 */
		public EditPartRepresentation(IGraphicalEditPart representedEditPart, EObject eObject) {
			this(representedEditPart, eObject, null);
		}

		/**
		 * 
		 * Constructor.
		 * 
		 * @param representedEditPart
		 *        the represented EditPart
		 * @param eObject
		 *        the semantic element represented by EditPartRepresentation
		 * @param parentRepresentation
		 *        parent representation of this edit part representation
		 */
		public EditPartRepresentation(IGraphicalEditPart representedEditPart, EObject eObject, EditPartRepresentation parentRepresentation) {
			this.setRepresentedEditPart(representedEditPart);
			this.eObject = eObject;
			this.parentRepresentation = parentRepresentation;
			initRepresentation();
		}

		/**
		 * Getter for {@link #representedEditPart}
		 * 
		 * @return the represented editpart, {@link #representedEditPart}
		 */
		public IGraphicalEditPart getRepresentedEditPart() {
			return this.representedEditPart;
		}

		/**
		 * Getter for {@link #eObject}
		 * 
		 * @return {@link #eObject}
		 */
		public EObject getSemanticElement() {
			return this.eObject;
		}

		/**
		 * Getter for {@link #initialSelection}
		 * 
		 * @return {@link #initialSelection}
		 */
		public List<EditPartRepresentation> getInitialSelection() {
			return this.initialSelection;
		}

		/**
		 * initialize the following fields
		 * <ul>
		 * <li> {@link #initialSelection}</li>
		 * <li>{@link #elementsToSelect}</li>
		 * </ul>
		 */
		protected void initRepresentation() {
			this.initialSelection = new ArrayList<EditPartRepresentation>();
			this.elementsToSelect = new ArrayList<EditPartRepresentation>();
		}

		/**
		 * Setter for {@link #elementsToSelect}
		 * 
		 * @return {@link #elementsToSelect}
		 */
		public List<EditPartRepresentation> getPossibleElement() {
			return this.elementsToSelect;
		}

		/**
		 * Returns the parent representation of this edit part representation
		 * 
		 * @return the parent representation of this edit part representation
		 */
		public EditPartRepresentation getParentRepresentation() {
			return parentRepresentation;
		}

		/**
		 * Setter for the {@link #representedEditPart}
		 * 
		 * @param representedEditPart
		 *        {@link #representedEditPart}
		 */
		public void setRepresentedEditPart(IGraphicalEditPart representedEditPart) {
			this.representedEditPart = representedEditPart;
		}

		/**
		 * Returns the display label for this edit part representation
		 * 
		 * @return the display label for this edit part representation
		 */
		public String getLabel() {
			EObject semanticElement = getSemanticElement();
			if(isElementInherited()) {
				StringBuffer buffer = new StringBuffer();
				buffer.append(getEditorLabelProvider().getText(getSemanticElement()));
				buffer.append(" ");
				buffer.append("(from ");
				buffer.append(getEditorLabelProvider().getText(getSemanticElement().eContainer()));
				buffer.append(")");
				return buffer.toString();
			}
			return getEditorLabelProvider().getText(semanticElement);
		}

		/**
		 * Returns the display label for this edit part representation
		 * 
		 * @return the display label for this edit part representation
		 */
		public Image getImage() {
			return getEditorLabelProvider().getImage(getSemanticElement());
		}

		/**
		 * Checks if the parent of the element linked to the given edit part representation is similar to the element of the parent representation
		 * 
		 * @param representation
		 *        the edit part representation to check
		 * @return <code>true</code> if the element is inherited
		 */
		protected boolean isElementInherited() {
			EditPartRepresentation parentRepresentation = getParentRepresentation();
			if(parentRepresentation == null) { // no parent => can not be inherited
				return false;
			}

			// no uml element in representation, can not check
			if(getSemanticElement() == null) {
				return false;
			}

			EObject parentElement = parentRepresentation.getSemanticElement();
			EObject elementOwner = getSemanticElement().eContainer(); // Null for rer.getUMLElement was already check before.
			if(parentElement == null || elementOwner == null) { // no element in the parent representation
				return false;
			}

			if(!parentElement.equals(elementOwner)) {
				return true;
			}
			return false;
		}
	}

	/**
	 * label provider that delegates to the edit part representation the label
	 */
	protected class EditPartRepresentationLabelProvider extends LabelProvider {

		/** editor label provider */
		protected EMFLabelProvider editorLabelProvider;

		/**
		 * Constructor.
		 */
		public EditPartRepresentationLabelProvider() {
			this.editorLabelProvider = new EMFLabelProvider();
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public String getText(Object element) {
			if(element instanceof EditPartRepresentation) {
				return ((EditPartRepresentation)element).getLabel();
			}
			return editorLabelProvider.getText(element);
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public Image getImage(Object element) {
			if(element instanceof EditPartRepresentation) {
				return ((EditPartRepresentation)element).getImage();
			}
			return editorLabelProvider.getImage(element);
		}
	}

	//	/**
	//	 * 
	//	 * EditorLabelProvider for the {@link CheckedTreeSelectionDialog}
	//	 * 
	//	 */
	//	public class CustomEditorLabelProvider extends EditorLabelProvider {
	//
	//		/**
	//		 * {@inheritDoc}
	//		 */
	//		@Override
	//		public Image getImage(Object element) {
	//			if(element instanceof EditPartRepresentation) {
	//				element = ((EditPartRepresentation)element).getSemanticElement();
	//			}
	//			return super.getImage(element);
	//		}
	//
	//		/**
	//		 * {@inheritDoc}
	//		 */
	//		@Override
	//		public String getText(Object element) {
	//			if(element instanceof CompartmentEditPartRepresentation) {
	//				return ((CompartmentEditPartRepresentation)element).getCompartmentName();
	//			} else if(element instanceof EditPartRepresentation) {
	//				EObject semanticElement = ((EditPartRepresentation)element).getSemanticElement();
	//				if(isElementInherited((EditPartRepresentation)element)) {
	//					StringBuffer buffer = new StringBuffer();
	//					buffer.append(super.getText(semanticElement));
	//					buffer.append(" ");
	//					buffer.append("(from ");
	//					buffer.append(super.getText(((EditPartRepresentation)element).getSemanticElement().eContainer()));
	//					buffer.append(")");
	//					return buffer.toString();
	//				} else {
	//					return super.getText(semanticElement);
	//				}
	//			}
	//			return super.getText(element);
	//		}
	//	}

}

Back to the top