-
Notifications
You must be signed in to change notification settings - Fork 7
Events
KMF proposes an option in its Maven pluggin.
If this option is set to true
, the code generator generates all necessary methods and class to provide a listener mechanism to all the features of your Metamodel.
Events are generated when:
- Attributes are
set
- References are
set
,added
orremoved
(the two last are only available for references with unlimited max cardinality) - An opposite element is modified (only if the opposite relation is set).
NB: The source
of an event is identified by its path()
. As a consequence, you need to specify an ID
attribute for, at least, the element you want to listen and all its parents (in the containment hierarchy).
There are two types of listeners you can place on a model element:
- ModelElementListener is used to get informed about changes in the attributes or in the relations of a specific model element
- ModelTreeListener is used to get informed each time an attribute or a reference is modified or set, or if a model element is added or removed somewhere under (according to the containment hierarchy) the element you place the listener on.
For instance, considering a very simple Finite State Machine metamodel (FSM<>--State<>--Transition<>--Action).
fsm.addModelTreeListener(new ModelTreeListener() {
@Override
public void elementChanged(ModelEvent evt) {
System.out.println("FSM-Tree::" + evt.toString());
}
});
Would print a message each time something is set, added or removed in the entire FSM ! But, if you want to listen to events concerning the FSM only (States added or removed, don't care about Transitions and Actions), a ModelElementListener would be sufficient.
fsm.addModelElementListener(new ModelElementListener() {
@Override
public void elementChanged(ModelEvent evt) {
System.out.println("FSM::" + evt.toString());
}
});