Home Forums FlexGantt Removing all events from the Command Stack

  • Author
    Posts
  • Brian
    Post count: 0
    #12750 |

    Let’s say I would like to provide a button to undo all the commands on the command stack.
    I was expecting that I could use some code that looks like this:
    while (cmdStack.isUndoable()) {
    ganttChart.commandUndo();
    }

    But, when I approach the command stack like this or with similar code I see two problems. One is that my loop runs forever, basically crashing my program. Or two, I have managed to control the end of the loop to prevent running forever, and have seen the following error.

    java.lang.IllegalArgumentException: argument is not a child
    at javax.swing.tree.DefaultMutableTreeNode.remove(DefaultMutableTreeNode.java:381)
    at com.dlsc.flexgantt.command.treetable.DefaultNodeDragAndDropCommand.undoCommand(SourceFile)
    at com.dlsc.flexgantt.command.DefaultCommandStack$a.run(SourceFile)

    I’m guessing I am approaching this wrong, curios for a pointer on the right approach.

  • Dirk Lemmermann
    Keymaster
    Post count: 64
    #12751 |

    The problem in your scenario is that the undo will eventually be performed by the command stack method undo(IProgressMonitor) which will undo the command by creating a new thread and invoking Command.undoCommand() inside this thread. So your while loop creates a whole bunch of concurrent threads and that messes up the application.

    What you can do instead is to call DefaultCommandStack.getUndoableCommands(), iterate it backwards, and invoke undoCommand() on each command yourself. After that call DefaultCommandStack().clear().

    Dirk

    • Brian
      Post count: 0
      #12753 |

      the exact method of getUndoableCommands does not exist. I expect what you are asking me to do is call cmdStack.getExecutedCommands(), then iterate over that list backwards. As I iterate, I need to ask the command if it is undoable before calling the undoCommand method.

    • Dirk Lemmermann
      Keymaster
      Post count: 64
      #12756 |

      That is actually right. Or what you could do is to implement a while loop that calls getUndoableCommand() which always returns the next command that can be undone or NULL if there is none.

      ICommand cmd;
      do {
      cmd = stack.getUndoableCommand();
      cmd.undoCommand();
      } while (cmd == null);

      stack.clear();

    • Brian
      Post count: 0
      #12757 |

      would you expect the undo buttons to behave appropriately after we follow the above steps? They don’t seem to be catching the event, as I would have expected. Those do seem to work, though I am seeing off behavior when I introduce debug logging in the middle of that loop.

    • Dirk Lemmermann
      Keymaster
      Post count: 64
      #12758 |

      What behavior do you mean? Their enabled / disabled state?

  • Brian
    Post count: 0
    #12755 |

    The method you reference is not exposed to me: DefaultCommandStack.getUndoableCommands
    I assume that what I should be doing is calling: cmdStack.getExecutedCommands()
    Then I will iterate over them backwards. I would need to check each command to see if it is undoable before I call the undoCommand() method.

You must be logged in to reply to this topic.