Search code examples
javaswingaopundojava-3d

State-based undo in Swing/Java3D application: AOP solution?


I am in charge of maintenance of an old application written in Swing, combined with a CAD-like tool written in Java3D. We are having problems with memory usage. After profiling, this is related to the undo functionality in the application.

All undo functionality is state-based, with a basic concept like this:

public class UndoAction {
  private UndoTarget target;
  private Object old_data;
  private Object new_data;
}

Code to create these UndoActions is basically littered throughout the application. Because there is no distinction between modifications of new objects, modifications of existing objects and modifications of subtrees, the following happens:

What happens is a single action is the following:

  • Create a new object A.
  • Modify field foo of the object. A new UndoAction is placed on the stack, which contains foo_old and foo_new.
  • Modify field bar of the object. A new UndoAction is placed on the stack, which contains bar_old and bar_new.
  • Execute B.setField(A). A new UndoAction is placed on the stack, which contains field_old and field_new (== A).

There is no granularity or any control over this at all. This does not help maintainability at all.

I want to refactor this system so it becomes maintainable and memory-friendly. Unfortunately, implementing the Undo system using the Command pattern is not possible; the actions are too impacting to revert. I want to implement the following:

  1. Use annotations to provide "Undo demarcation". @Undoable() would mark a method as generating an UndoAction which is put on the stack. This can be parametrised just like transactions: REQUIRE, NEST, JOIN... The full object graph is cloned upon entering the Undoable method.
  2. When a Transaction (=method) finishes, an algorithm should compare the new state with the old state and save a diff.
  3. To implement this, we can use AOP. This allows us to keep the core code very clean.

An now, my question: Do any of the above 3 functionalities already exist in Java? I can imagine I am not the first to wrestle with state-based undo and the problems linked to it (Undo demarcation, state compare, ...)


Solution

  • After this question has been open for quite some time, it seems the question is: "No, no such framework already exists."

    As a guide for other people, I am looking into Eclipse Modeling Framework and the EMF.Edit framework. In this framework, you define the model in a descriptor language, and the framework handles the model and any manipulations for you. This automatically results in Actions and Undo/Redo being created.