blob: fc77bf13f79ae08b055cd2d8c5b6325dc7fd05c9 (
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
|
/*****************************************************************************
* Copyright (c) 2012 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:
* CEA LIST - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.util;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.NotFoundException;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.ui.util.EditorUtils;
import org.eclipse.papyrus.infra.ui.util.ServiceUtilsForSelection;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
/**
* This class is a Property tester used to check is current model (meaning the model currently opened in Papyrus) is a UML Model or a UML Profile.
* This class is used in order to create test for deciding whether a diagram creation command should be visible or not.
* This property tester assumes that currently active editor is Papyrus, it should be used with care (simultaneously with a test to ensure Papyrus is
* currently opened and active).
*/
public class UMLSelectionTester extends PropertyTester {
/** Tester ID for UML Model nature */
public final static String IS_UML_MODEL = "isUMLModel";
/** Tester ID for UML Profile nature */
public final static String IS_UML_PROFILE = "isUMLProfile";
/** Default constructor */
public UMLSelectionTester() {
}
/** Test the receiver against the selected property */
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
// Ensure Papyrus is the active editor
IMultiDiagramEditor editor = EditorUtils.getMultiDiagramEditor();
if (editor == null) {
return false;
}
Object currentValue = null;
if (IS_UML_MODEL.equals(property)) {
currentValue = testUMLModelNature(receiver);
return (currentValue == expectedValue);
} else if (IS_UML_PROFILE.equals(property)) {
currentValue = testUMLProfileNature(receiver);
return (currentValue == expectedValue);
}
return false;
}
/** True if root object is a UML Model */
protected boolean testUMLModelNature(Object receiver) {
EObject root = getRoot(receiver);
/*
* For controlled resources, it is very important to consider root of UML model can be a Package. Of course, we
* can still exclude Profile, which should be dedicated to profile diagrams.
*/
return root instanceof Package && !(root instanceof Profile);
}
/** True if root object is a UML Profile */
protected boolean testUMLProfileNature(Object receiver) {
return (getRoot(receiver) instanceof Profile);
}
private EObject getRoot(Object receiver) {
if (receiver instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection) receiver;
try {
ModelSet modelSet = ServiceUtilsForSelection.getInstance().getModelSet(selection);
EObject root = getRoot(modelSet);
return root;
} catch (ServiceException ex) {
return null;
}
}
return null;
}
/** Returns the root EObject of currently opened model */
private EObject getRoot(ModelSet modelSet) {
UmlModel openedModel = (UmlModel) modelSet.getModel(UmlModel.MODEL_ID);
if (openedModel != null) {
EObject root;
try {
root = openedModel.lookupRoot();
} catch (NotFoundException e) {
return null;
}
return root;
}
return null;
}
}
|