Dynamically validating undo menu item availability (NSUndoManager)

  • Hi Cocoabuilders,

    I've got a situation where I'm performing operations on the file
    system and I want those operations to be undoable. The tricky part is
    that because the file system can be changed by anyone at anytime, I
    can't necessarily assume that it will be in a state which is
    consistent with how I made my change. I have routines which can check
    for this and determine whether or not I am able to undo, but I don't
    know the best way to work with the UndoManager on this.

    How can I proactively get my validation routine called and disable to
    undo menu item when it is not available?

    Cheers,
    Lach
  • The -validateMenuItem: method in an appropriate controller should do
    wonders.

    Mike.

    On 11 Jan 2008, at 22:22, Lachlan Cotter wrote:

    > Hi Cocoabuilders,
    >
    >
    > I've got a situation where I'm performing operations on the file
    > system and I want those operations to be undoable. The tricky part
    > is that because the file system can be changed by anyone at anytime,
    > I can't necessarily assume that it will be in a state which is
    > consistent with how I made my change. I have routines which can
    > check for this and determine whether or not I am able to undo, but I
    > don't know the best way to work with the UndoManager on this.
    >
    > How can I proactively get my validation routine called and disable
    > to undo menu item when it is not available?
    >
    >
    > Cheers,
    > Lach
  • On Jan 11, 2008, at 2:46 PM, Mike Abdullah wrote:

    > The -validateMenuItem: method in an appropriate controller should do
    > wonders.
    >
    You should typically use validateUserInterfaceItem: instead, unless
    there's a good reason not to:
    <http://developer.apple.com/documentation/Cocoa/Conceptual/MenuList/Articles
    /EnablingMenuItems.html#//apple_ref/doc/uid/20000261-74653
    >

    mmalc
  • Thanks guys.

    This seems to be half the solution... however there are still some
    complications. I learned in this thread:

    http://www.cocoabuilder.com/archive/message/cocoa/2002/8/8/55198

    how to validate the undo menu item, which seems to require some
    special treatment. But even though I'm now getting validateMenuItem:
    called for the undo item, I can't see how I can tell what target is
    on top of the undo stack in order to validate it.

    It's not sufficient for me to just check the name of the undo item. I
    need to get the undo target in order to call it's validation method.
    NSUndoManager doesn't seem to have methods to check this.

    Any ideas?

    Incidentally, it looks like the undo item gets its special context
    sensitive name somewhere inside NSWindow's implementation of
    validateMenuItem: since overriding this method causes this
    functionality to disappear. My solution is to call super's
    implementation anyway but just ignore the result.

    Cheers,
    Lach

    On 12/01/2008, at 11:52 AM, mmalc crawford wrote:

    >
    >> The -validateMenuItem: method in an appropriate controller should
    >> do wonders.
    >>
    > You should typically use validateUserInterfaceItem: instead, unless
    > there's a good reason not to:
    > <http://developer.apple.com/documentation/Cocoa/Conceptual/
    > MenuList/Articles/EnablingMenuItems.html#//apple_ref/doc/uid/
    > 20000261-74653>
    >
    > mmalc
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today