This project has moved and is read-only. For the latest updates, please go here.

Detect ANY changes in diagram

Jan 9, 2013 at 10:25 AM

Is there a way to detect ANY changes in a diagram?

I tried PropertyController.ObjectsModified event - but it's not fired when I modify Models of my shapes.

I need to detect the following changes:

  • any changes in any Models,
  • any changes in any Shapes - made through PropertyGrid + made with mouse + made from code,

(changes in the diagram itself are not interesting in my case)

Jan 15, 2013 at 10:37 AM

I would recommend to handle the following events of IRepository:

event EventHandler<RepositoryModelObjectsEventArgs> ModelObjectsInserted;
event EventHandler<RepositoryModelObjectsEventArgs> ModelObjectsUpdated;
event EventHandler<RepositoryModelObjectsEventArgs> ModelObjectsDeleted;

event EventHandler<RepositoryShapesEventArgs> ShapesInserted;
event EventHandler<RepositoryShapesEventArgs> ShapesUpdated;
event EventHandler<RepositoryShapesEventArgs> ShapesDeleted;
event EventHandler<RepositoryShapeConnectionEventArgs> ConnectionInserted;
event EventHandler<RepositoryShapeConnectionEventArgs> ConnectionDeleted;

Jan 17, 2013 at 9:25 AM

Thank you for the answer.

I'll try to use it soon. My purpose is to create kind of "IsDirty" attribute for my project - this will help to prevent data loss when user is about to close an unsaved project from UI.

Jan 17, 2013 at 10:00 AM

Try using IRepository.IsModified property.

Feb 13, 2013 at 10:10 AM
Edited Feb 13, 2013 at 10:11 AM
Hi,

as far as same delegate is used for more than one Model-related event - is it possible to extend RepositoryModelEventArgs with information describing which exactly event has happened?

(same for shapes and connections)
Feb 13, 2013 at 10:32 AM
Edited Feb 13, 2013 at 10:33 AM
I don't see the problem, even if you want to handle all events with a single method of your host applicaion:
(...)
    private RegisterModelEvents() {
        Project.Repository.ModelObjectsInserted += repository_ModelObjectsInserted;
        Project.Repository.ModelObjectsUpdated += repository_ModelObjectsUpdated;
        Project.Repository.ModelObjectsDeleted += repository_ModelObjectsDeleted;
    }
(...)
    private enum KindOfChange { Insert, Modify, Delete }

    private void HandleModelObjectChangeEvent(KindOfChange kindOfChange, RepositoryModelObjectsEventArgs e) {
        switch (kindOfChange) {
            case KindOfChange.Insert: 
                MessageBox.Show("A model object was inserted."); 
                break;
            case KindOfChange.Insert: 
                MessageBox.Show("A model object was modified."); 
                break;
            case KindOfChange.Insert: 
                MessageBox.Show("A model object was deleted."); 
                break;
            default: 
                Debug.Fail("Unhandled switch case!"); 
                break;
        }
    }

    private void repository_ModelObjectsInserted(object sender, RepositoryModelObjectsEventArgs e) {
        HandleModelObjectChangeEvent(KindOfChange.Insert, e);
    }
    private void repository_ModelObjectsUpdated(object sender, RepositoryModelObjectsEventArgs e) {
        HandleModelObjectChangeEvent(KindOfChange.Modify, e);
    }
    private void repository_ModelObjectsDeleted(object sender, RepositoryModelObjectsEventArgs e) {
        HandleModelObjectChangeEvent(KindOfChange.Delete, e);
    }
(...)
For performance reasons, the repository does not care about the detailed changes when modifying an object.
Feb 13, 2013 at 10:56 AM
Sure we can use this approach :)
But should the ***EventArgs classes contain some extra field (e.g. KindOfChange) - our code would be shorter.
I don't think this extra field will introduce any performance problem.

However, it's really not a problem to discuss.