Listening to transaction commit events on session closing
Every session creating and closing starts and commits the model editing transaction. The com.nomagic.uml2.transaction.TransactionCommitListener is a special listener, which is notified when the all changes inside a transaction are done and the transaction is closed.
The TransactionCommitListener contains transactionCommited(java.util.Collection<java.beans.PropertyChangeEvent>) method, which provides a collection of all java.beans.PropertyChangeEvent(s) that were executed in a transaction.
Create a custom transaction commit listener:
public
class
MyTransactionListener
implements
TransactionCommitListener
{
public
Runnable transactionCommited(
final
Collection<PropertyChangeEvent> events)
{
return
new
Runnable()
{
public
void
run()
{
for
(PropertyChangeEvent event : events)
{
if
(UML2MetamodelConstants.INSTANCE_CREATED.equals(event.getPropertyName()))
{
Object source = event.getSource();
if
(source
instanceof
Property)
{
Property property = (Property) source; Element owner = property.getOwner();
if
(owner
instanceof
Classifier)
{
Classifier propertyOwner = (Classifier) owner;
propertyOwner.setName(
"Contains ("
+ propertyOwner.getAttribute().size() +
") attributes"
);
}
// additionally for this Property we register listener to listen for any property changes in this Element properties.
property.addPropertyChangeListener(
new
DerivedValuePropertyChangeListener());
}
}
}
}
};
}
}
This “transaction commit listener” checks, if a new property is created in a classifier and updates the classifier’s (a property owner) name. All changes are done in the same session.
Register the custom “transaction commit listener” into the project
MyTransactionListener transactionListener =
new
MyTransactionListener();
TransactionManager transactionManager = project.getRepository().getTransactionManager();
transactionManager.addTransactionCommitListener(transactionListener);
Related pages