Skip to main content
summaryrefslogtreecommitdiffstats
blob: fab28c1609575ced46dd25ec8b1ec67a0cc44ff4 (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
/*****************************************************************************
 * Copyright (c) 2011 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.menu.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.common.ui.util.DisplayUtils;
import org.eclipse.gmf.runtime.diagram.ui.actions.internal.ShowConnectionLabelsAction;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ConnectionLabelsEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.services.editpart.EditPartService;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Location;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.uml.diagram.common.commands.ShowHideLabelsRequest;
import org.eclipse.papyrus.uml.diagram.common.editparts.ILabelRoleProvider;
import org.eclipse.papyrus.uml.diagram.common.util.DiagramEditPartsUtil;
import org.eclipse.papyrus.uml.diagram.menu.dialogs.ShowHideLabelSelectionDialog;
import org.eclipse.papyrus.uml.diagram.menu.messages.Messages;
import org.eclipse.papyrus.uml.tools.providers.UMLLabelProvider;

/**
 * Adapted code from {@link ShowConnectionLabelsAction}
 * 
 * This action allows to manage connection labels. 3 parameters are available for this actio :
 * <ul>
 * <li> {@link #HIDE_PARAMETER} : hide all labels for the selected elements</li>
 * <li>  {@link #SHOW_PARAMETER} : show all labels for the selected elements</li>
 * <li> {@link #MANAGE_PARAMETER} : open a dialog to choose labels to display and labels to hide for the selected element</li>
 * </ul>
 * 
 */
public class ShowHideLabelsAction extends AbstractGraphicalParametricAction {

	/**
	 * Parameters for this action
	 */
	public static final String MANAGE_PARAMETER = "manage"; //$NON-NLS-1$

	public static final String HIDE_PARAMETER = "hide"; //$NON-NLS-1$

	public static final String SHOW_PARAMETER = "show"; //$NON-NLS-1$


	/**
	 * 
	 * Constructor.
	 * 
	 * @param parameter
	 *        the parameter for this action
	 * @param selectedEditPart
	 *        the list of selected {@link EditPart}
	 */
	public ShowHideLabelsAction(String parameter, List<IGraphicalEditPart> selectedEditPart) {
		super(parameter, selectedEditPart);
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.uml.diagram.menu.actions.AbstractGraphicalParametricAction#getBuildedCommand()
	 * 
	 * @return
	 */
	@Override
	protected Command getBuildedCommand() {

		List<IGraphicalEditPart> selections = getSelection();
		CompoundCommand cmd = new CompoundCommand("ShowHideConnectionLabelCommand"); //$NON-NLS-1$
		if(!selections.isEmpty()) {
			if(getParameter().equals(MANAGE_PARAMETER)) {
				ManageLabelsAction action = new ManageLabelsAction(Messages.ShowHideConnectionLabelsAction_LabelsManager, Messages.ShowHideConnectionLabelsAction_SelectTheLabelToDisplay, getSelection());
				cmd.add(action.getActionCommand());
			} else {
				/*
				 * we have a problem with the hide action for Hide All Label,
				 * because, this request hide the labels and the port too!
				 * so, we need to use the CustomRequest!
				 */
				ContentProvider provider = new ContentProvider(DiagramEditPartsUtil.getDiagramEditPart(selections.get(0)));
				for(IGraphicalEditPart current : selections) {
					Object[] children = provider.getChildren(current);
					for(Object currentObj : children) {
						if(currentObj instanceof View) {
							ShowHideLabelsRequest request = null;
							if(getParameter().equals(HIDE_PARAMETER)) {
								request = new ShowHideLabelsRequest(false, (View)currentObj);
							} else if(getParameter().equals(SHOW_PARAMETER)) {
								request = new ShowHideLabelsRequest(true, (View)currentObj);
							}
							if(request != null) {
								Command tmp = current.getCommand(request);
								if(tmp != null) {
									cmd.add(tmp);
								}
							}
						}
					}

				}
			}
		}
		return (cmd.canExecute() && !cmd.isEmpty()) ? cmd : UnexecutableCommand.INSTANCE;
	}

	/**
	 * Returns the selected connections for the actions
	 * 
	 * @return
	 *         the selected connections for the actions
	 */
	protected List<ConnectionEditPart> getConnections() {
		List<ConnectionEditPart> connections = new ArrayList<ConnectionEditPart>();
		for(IGraphicalEditPart current : getSelection()) {
			if(current instanceof DiagramEditPart && getSelection().size() == 1) {
				connections.addAll(((DiagramEditPart)current).getConnections());
				break;
			} else if(current instanceof ConnectionEditPart) {
				connections.add((ConnectionEditPart)current);
			}
		}
		return connections;
	}

	/**
	 * This code comes from {@link ConnectionLabelsEditPolicy}
	 * 
	 * determines if the passed view is a label view or not
	 * the default provided implementation is just an educated/generic guss
	 * clients can override this method to provide more specific response
	 * 
	 * @param node
	 * @return
	 */
	protected boolean isLabelView(EditPart containerEditPart, View parentView, View view) {
		// labels are not compartments
		// labels contained by Node Shape Edit Parts or connection edit parts
		// labels had location constrain
		// labels had the string Type set on them 
		if((containerEditPart instanceof ShapeNodeEditPart || containerEditPart instanceof ConnectionEditPart) && view instanceof Node) {
			Node node = (Node)view;
			String nodeType = node.getType();
			if(!isCompartment(node) && (nodeType != null && nodeType.length() > 0)) {
				LayoutConstraint lContraint = node.getLayoutConstraint();
				if(lContraint instanceof Location) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 
	 * This code comes from {@link ConnectionLabelsEditPolicy}
	 * 
	 * determines if the passed view is a compartment view or not
	 * the default provided implementation is just an educated/generic guss
	 * clients can override this method to provide more specific response
	 * 
	 * @param node
	 * @return
	 */
	protected boolean isCompartment(Node node) {
		if(node.getStyle(NotationPackage.eINSTANCE.getDrawerStyle()) != null) {
			return true;
		}
		return false;
	}

	//	@Override
	//	public boolean isEnabled() {
	//		if(super.isEnabled()) {
	//			for(IGraphicalEditPart current : getSelection()) {
	//				if(current.getEditPolicy(ShowHideLabelEditPolicy.SHOW_HIDE_LABEL_ROLE) == null) {
	//					return false;
	//				}
	//			}
	//		}
	//		return true;
	//	}

	/**
	 * This class provides a dialog to manage the displaying of the labels for connections
	 * 
	 * 
	 * 
	 */
	public class ManageLabelsAction {

		/**
		 * This map is used to store the new states of the labels
		 * the {@link View} are the keys for this map and the associated value is a {@link Boolean}.
		 * This Boolean represents the new value for the display state.
		 */
		protected Map<View, Boolean> viewStatus = new HashMap<View, Boolean>();

		/** Title for the dialog */
		private String title;

		/** Message for the dialog */
		private String message;

		/** the list of connections to manage */
		private List<IGraphicalEditPart> editparts;

		/**
		 * 
		 * Constructor.
		 * 
		 * @param title
		 * @param message
		 * @param editPolicyKey
		 */
		public ManageLabelsAction(String title, String message, List<IGraphicalEditPart> editparts) {
			this.title = title;
			this.message = message;
			this.editparts = editparts;
			initMap();
		}

		/**
		 * Returns the command for this action
		 * 
		 * @return
		 *         the command for this action
		 */
		protected Command getActionCommand() {
			CompoundCommand cmd = new CompoundCommand("Manage Conection Labels "); //$NON-NLS-1$

			DiagramEditPart diagramEP = DiagramEditPartsUtil.getDiagramEditPart(editparts.get(0));
			ShowHideLabelSelectionDialog selectionDialog = new ShowHideLabelSelectionDialog(DisplayUtils.getDisplay().getActiveShell(), new UMLLabelProvider(), new ContentProvider(diagramEP));
			selectionDialog.setTitle(this.title);
			selectionDialog.setMessage(this.message);
			selectionDialog.setContainerMode(true);
			selectionDialog.setInput(editparts);
			selectionDialog.setExpandedElements(editparts.toArray());
			selectionDialog.setInitialElementSelections(getInitialSelection());
			selectionDialog.open();
			if(selectionDialog.getReturnCode() == Dialog.OK) {
				Object[] userSelection = selectionDialog.getResult();
				//fill the map with the new values
				for(Object current : userSelection) {
					if(current instanceof EditPart) {
						//do nothing
					} else if(current instanceof View) {
						viewStatus.put((View)current, new Boolean(true));
					}
				}

				/**
				 * Creates the commands for this action
				 */
				for(View view : viewStatus.keySet()) {
					boolean oldStatus = view.isVisible();
					boolean newStatus = viewStatus.get(view).booleanValue();
					if(oldStatus == newStatus) {
						//do nothing
					} else {
						if(oldStatus) {
							EditPart ep = DiagramEditPartsUtil.getEditPartFromView(view, getSelection().get(0));
							int dummy = 0;
							dummy++;
						}
						ShowHideLabelsRequest request = new ShowHideLabelsRequest(newStatus, view);
						//						EditPart ep = DiagramEditPartsUtil.getEditPartFromView(view, getConnections().get(0));
						EditPart ep = DiagramEditPartsUtil.getEditPartFromView(view, getSelection().get(0));

						//						EditPart ep = DiagramEditPartsUtil.getEditPartFromView(view, diagramEP);
						//Command command = ep.getParent().getCommand(request);
						Command command = ep.getParent().getCommand(request);
						if(command != null) {
							cmd.add(command);
						}
					}
				}

				return cmd;
			}
			return UnexecutableCommand.INSTANCE;
		}

		/**
		 * Returns the initial selection
		 * 
		 * @return
		 *         the initial selection : the view that are currently displayed
		 */
		public List<View> getInitialSelection() {
			List<View> selection = new ArrayList<View>();
			//			for(ConnectionEditPart current : getConnections()) {
			//				View model = (View)(current).getModel();
			//				Iterator<?> iter = model.getChildren().iterator();
			//				while(iter.hasNext()) {
			//					View childView = (View)iter.next();
			//					if(isLabelView(current, model, childView)) {
			//						if(childView.isVisible()) {
			//							selection.add(childView);
			//						}
			//					}
			//				}
			//			}

			for(IGraphicalEditPart current : getSelection()) {
				View model = (View)(current).getModel();
				Iterator<?> iter = model.getChildren().iterator();
				while(iter.hasNext()) {
					View childView = (View)iter.next();
					if(isLabelView(current, model, childView)) {
						if(childView.isVisible()) {
							selection.add(childView);
						}
					}
				}
			}
			return selection;
		}

		/**
		 * put all the available views in the map, with the value <code>false</code>
		 */
		public void initMap() {
			viewStatus.clear();
			//			for(EditPart current : getConnections()) {
			for(EditPart current : getSelection()) {
				View model = (View)((IGraphicalEditPart)current).getModel();
				Iterator<?> iter = model.getChildren().iterator();
				while(iter.hasNext()) {
					View childView = (View)iter.next();
					if(isLabelView(current, model, childView)) {
						viewStatus.put(childView, new Boolean(false));
					}
				}
			}
		}
	}


	/**
	 * 
	 * Provide the element to fill the tree
	 * 
	 */
	protected class ContentProvider implements ITreeContentProvider {

		/**
		 * the diagram EditPart. It's used to find the editpart corresponding to a view
		 */
		private DiagramEditPart diagramEP;

		/**
		 * 
		 * Constructor.
		 * 
		 * @param diagramEP
		 *        the diagram editpart. Used to find editpart corresponding to a view
		 */
		public ContentProvider(DiagramEditPart diagramEP) {
			this.diagramEP = diagramEP;
		}

		/**
		 * 
		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
		 * 
		 */
		public void dispose() {
			// TODO Auto-generated method stub

		}

		/**
		 * 
		 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
		 * 
		 * @param viewer
		 * @param oldInput
		 * @param newInput
		 */
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub

		}

		/**
		 * 
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object)
		 * 
		 * @param inputElement
		 * @return
		 */
		public Object[] getElements(Object inputElement) {
			if(inputElement instanceof List) {
				return ((List<?>)inputElement).toArray();
			}
			return new Object[0];
		}

		/**
		 * 
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
		 * 
		 * @param parentElement
		 * @return
		 */
		public Object[] getChildren(Object parentElement) {
			List<View> children = new ArrayList<View>();
			//			if(parentElement instanceof ConnectionEditPart) {
			if(parentElement instanceof EditPart) {
				View model = (View)((EditPart)parentElement).getModel();
				Iterator<?> iter = model.getChildren().iterator();
				while(iter.hasNext()) {
					View childView = (View)iter.next();
					if(isLabelView((EditPart)parentElement, model, childView)) {
						children.add(childView);
					}
				}
			}
			Collections.sort(children, new LabelRoleComparator());
			return children.toArray();
		}

		/**
		 * 
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
		 * 
		 * @param element
		 * @return
		 */
		public Object getParent(Object element) {
			if(element instanceof View) {
				EditPart part = DiagramEditPartsUtil.getEditPartFromView((View)element, diagramEP);
				return part.getParent();
			}
			return null;
		}

		/**
		 * 
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
		 * 
		 * @param element
		 * @return
		 */
		public boolean hasChildren(Object element) {
			Object[] children = getChildren(element);
			return children != null && children.length != 0;
		}
	}

	public class LabelRoleComparator implements Comparator<View> {

		public int compare(View o1, View o2) {
			EditPart ep1 = EditPartService.getInstance().createGraphicEditPart(o1);
			EditPart ep2 = EditPartService.getInstance().createGraphicEditPart(o2);

			if(ep1 instanceof ILabelRoleProvider && ep2 instanceof ILabelRoleProvider) {
				String role1 = ((ILabelRoleProvider)ep1).getLabelRole();
				String role2 = ((ILabelRoleProvider)ep2).getLabelRole();
				return role1.compareToIgnoreCase(role2);

			}
			return 0;
		}
	}

	@Override
	public List<IGraphicalEditPart> getSelection() {
		List<IGraphicalEditPart> selection = super.getSelection();
		List<IGraphicalEditPart> delete = new ArrayList<IGraphicalEditPart>();
		Iterator<IGraphicalEditPart> iter = selection.iterator();
		//		while(iter.hasNext()) {
		//			IGraphicalEditPart current = iter.next();
		//			if(Util.isAffixedChildNode(current)) {
		//				delete.remove(current);
		//			}
		//		}
		selection.removeAll(delete);
		return selection;
	}
}

Back to the top