blob: 9230027689eb48fe1d8e4b065d63a4beb6ccb269 (
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
|
/*******************************************************************************
* Copyright (c) 2010, 2017 THALES GLOBAL SERVICES.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.testers;
import java.util.Iterator;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.business.api.query.DDiagramElementQuery;
import org.eclipse.sirius.diagram.ui.edit.api.part.IDiagramElementEditPart;
/**
* Tester to know if at least one element of the selected is kind of IDiagramElementEditPart and has a visible label.
*
* @author <a href="mailto:laurent.redor@obeo.fr">Laurent Redor</a>
*/
public class CanHideLabelTester extends PropertyTester {
/**
* {@inheritDoc}
*
* @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object,
* java.lang.String, java.lang.Object[], java.lang.Object)
*/
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
boolean result = false;
if ("canHideLabel".equals(property)) { //$NON-NLS-1$
if (receiver instanceof IStructuredSelection) {
result = testStructuredSelection((IStructuredSelection) receiver);
} else if (receiver instanceof IDiagramElementEditPart) {
result = testDiagramElementEditPart((IDiagramElementEditPart) receiver);
}
}
return result;
}
/**
* @param selectedElement
* The current selection
* @return true if the selected element has a visible label.
*/
private boolean testDiagramElementEditPart(IDiagramElementEditPart selectedElement) {
DDiagramElement diagramElement = selectedElement.resolveDiagramElement();
if (diagramElement == null) {
return false;
}
DDiagramElementQuery query = new DDiagramElementQuery(diagramElement);
return query.canHideLabel() && !query.isLabelHidden();
}
/**
* @param selectedElements
* The current selection
* @return true if at least one element of the selected is kind of IDiagramElementEditPart and has a visible label .
*/
private boolean testStructuredSelection(IStructuredSelection selectedElements) {
boolean atLeastOneIsOK = false;
final Iterator<?> iterator = selectedElements.iterator();
while (!atLeastOneIsOK && iterator.hasNext()) {
final Object next = iterator.next();
if (next instanceof IDiagramElementEditPart) {
atLeastOneIsOK = testDiagramElementEditPart((IDiagramElementEditPart) next);
}
}
return atLeastOneIsOK;
}
}
|