Track change notifications [message #1863962] |
Fri, 08 March 2024 03:50  |
Eclipse User |
|
|
|
Hi,
From what I know it is possible in EMF to implement listeners that track changes made to a model .The idea is that I want to implement listeners on some attributes, references etc, and based on whether it was changed or not, trigger another action. Is there any available resource that helps with that? Should I modify any of the files generated for the editor or create new ones?
Thanks!
|
|
|
|
|
|
Re: Track change notifications [message #1863971 is a reply to message #1863968] |
Fri, 08 March 2024 07:29   |
Eclipse User |
|
|
|
By trigger actions I mean, for instance, if a certain attribute is set to a certain value , then, the user will have readonly permissions on the name attribute of the same element ). for instance in the following I have made changes to the BookItemProvider, so that if the year attribute is set to "2007", the name attribute of that particular Book will be readonly. For other Books where the year is != 2007, the name attribute should be able to be edited.
@Override
public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) {
if (itemPropertyDescriptors == null) {
super.getPropertyDescriptors(object);
// Dynamically determine if the name property should be read-only
boolean isNameReadOnly = false;
if (object instanceof Book) {
Book book = (Book) object;
if (book.getYear() == 2007) {
isNameReadOnly = true;
}
}
addNamePropertyDescriptor(object, isNameReadOnly);
addYearPropertyDescriptor(object);
}
return itemPropertyDescriptors;
}
// Updated to include dynamic read-only logic
protected void addNamePropertyDescriptor(Object object, boolean isReadOnly) {
itemPropertyDescriptors.add(createItemPropertyDescriptor(
((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(),
getResourceLocator(), getString("_UI_Book_name_feature"),
getString("_UI_PropertyDescriptor_description", "_UI_Book_name_feature", "_UI_Book_type"),
LibraryProjectPackage.Literals.BOOK__NAME, true, false, isReadOnly,
ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, null));
}
@Override
public void notifyChanged(Notification notification) {
super.notifyChanged(notification);
if (notification.getFeatureID(Book.class) == LibraryProjectPackage.BOOK__YEAR) {
// Year has changed, so refresh property descriptors to potentially update read-only status
itemPropertyDescriptors = null;
}
switch (notification.getFeatureID(Book.class)) {
case LibraryProjectPackage.BOOK__NAME:
case LibraryProjectPackage.BOOK__YEAR:
fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
return;
}
}
|
|
|
|
Re: Track change notifications [message #1863973 is a reply to message #1863972] |
Fri, 08 March 2024 08:23  |
Eclipse User |
|
|
|
I made the following changes, and it seems to be working. At least for now :)
@Override
public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) {
// Always clear the old descriptors to ensure they are re-evaluated for the current object state
itemPropertyDescriptors = null;
super.getPropertyDescriptors(object);
// Note: We do not need to pass isReadOnly flag here anymore
addNamePropertyDescriptor(object); // Adjust this method to evaluate read-only condition internally
addYearPropertyDescriptor(object);
return itemPropertyDescriptors;
}
// Adjusted to evaluate read-only condition internally
protected void addNamePropertyDescriptor(Object object) {
boolean editName = true;
if (object instanceof Book) {
Book book = (Book) object;
if (book.getYear() == 2007) {
editName = false;
}
}
itemPropertyDescriptors.add(createItemPropertyDescriptor(
((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(),
getResourceLocator(), getString("_UI_Book_name_feature"),
getString("_UI_PropertyDescriptor_description", "_UI_Book_name_feature", "_UI_Book_type"),
LibraryProjectPackage.Literals.BOOK__NAME, editName, false, false,
ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, null));
}
|
|
|
Powered by
FUDForum. Page generated in 0.77115 seconds