Re: Java and Objective-C
Re: Java and Objective-C
- Subject: Re: Java and Objective-C
- From: Denis Bohm <email@hidden>
- Date: Sat, 7 Jun 2008 13:54:00 -0700
On Jun 7, 2008, at 1:49 PM, Bill Bumgarner wrote:
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];
That is handled by the Java example above (via the "Object... args").
A method with any number of arguments can be passed to
registerUndoWithTarget. So you could do something like:
undoManager.registerUndoWithTarget(this, "setFrame", true,
splineStruct);
Denis
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden