Thank you -- this is the kind of side by side, purely code oriented, set of comparisons that I think are both largely missing and generally quite useful.

Comments inline.

On Jun 7, 2008, at 1:30 PM, Denis Bohm wrote:
The Objective-C example on that page is:

- (void)setGridVisible:(NSNumber *)flag {
   BOOL flagValue = [flag boolValue];
   if (gvFlags.showGrid != flagValue) {
NSNumber *currentValue = [NSNumber numberWithBool:gvFlags.showGrid];
       gvFlags.showGrid = flagValue;
       if (flagValue)
           [graphicView resetGUP];
       [graphicView cache:[graphicView bounds]];
       [undoManager registerUndoWithTarget:self
               selector:@selector(setGridVisible:)
               object:currentValue];

I would generally write this as:

[[NSUndoManager prepareWithInvocationTarget: self] setGridVisible: currentValue];

In particular, the above form allows one to undo any random state change and not just one that can be represented by a method that takes a single argument as a parameter.

For example:

[[NSUndoManager prepareWithInvocationTarget: self] setFrame: myRect animate: YES spline: splineStruct];

-setFrame:animate:spline: is a method that only exists in my application.

       [undoManager setActionName:GRID_OP];
   }

Something similar in Java could be done as:

void setGridVisible(boolean flagValue) {
        if (gvFlags.showGrid != flagValue) {
                boolean currentValue = gvFlags.showGrid;
                gvFlags.showGrid = flagValue;
                if (flagValue) {
                        graphicView.resetGUP();
                graphicView.cache(graphicView.bounds());
undoManager.registerUndoWithTarget(this, "setGridVisible", currentValue);
                undoManager.setActionName(GRID_OP);
        }

Where the methods in the Java UndoManager could be implemented something like:

public void registerUndoWithTarget(Object target, Method method, Object... args)

public void registerUndoWithTarget(Object target, String methodName, Object... args) {
   Method[] methods = target.getClass().getDeclaredMethods();
Method method = getBestMatch(target.getClass(), getTypesOf(args)); //
   ...
}

protected Method getBestMatch(Class targetClass, Class[] argClasses) {
   Method[] methods = targetClass.getDeclaredMethods();
   ...
}

The Objective-C and Java versions of the user code look pretty similar. What aspects of the Objective-C undo manager am I missing?

How would you support the more generically applicable invocation form in Java?

I.e. this form:

[[NSUndoManager prepareWithInvocationTarget: self] setFrame: myRect animate: YES spline: splineStruct];


b.bum

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to