Re: Validation of a Custom View NSToolbarItem
Re: Validation of a Custom View NSToolbarItem
- Subject: Re: Validation of a Custom View NSToolbarItem
- From: "Carmen Cerino Jr." <email@hidden>
- Date: Thu, 18 Dec 2008 16:45:06 -0500
I apologize if I cam of snarky in my last email. I guess I may still
confused on how the validation process works. My impression from the
Toolbar Programming guide was that if you are dealing with image
based items you want to implement the validateToolbarItem in your
target. However if you are dealing with an item with a custom view you
need to go the subclassing route and override validate. They did not
mention in the guide that validate needs to make a call to any other
methods outside whatever you need to do in order to make the contents
of the view appear disabled or enabled. Another issue I am having is
that I read view based items don't get autovalidated, so by
autovalidation are they referring to the act of validating or the
process of checking if something is valid or not? I guess that would
be the reason why I made my first post. I was unsure if I was suppose
to ask the item to validate itself or if the toolbar should be doing
it.
I did try the class that Keith provided, but had no luck. I am a
breakpoint and an NSLog statement inside validate and neither are
getting hit. From this point on lets assume that I am using Keith's
custom item, but here is some of the rest of my code.
static void addToolbarItem(NSMutableDictionary *theDict,NSString
*identifier,NSString *label,NSString *paletteLabel,NSString
*toolTip,id target,SEL settingSelector, id itemContent,SEL action,
NSMenu * menu)
{
NSMenuItem *mItem;
NSToolbarItem *item;
if([identifier isEqualToString:@"uploads"])
{
item = [[[KBViewToolbarItem alloc]
initWithItemIdentifier:identifier] autorelease];
}
else
{
item = [[[NSToolbarItem alloc]
initWithItemIdentifier:identifier] autorelease];
}
[item setLabel:label];
[item setPaletteLabel:paletteLabel];
[item setToolTip:toolTip];
[item setTarget:target];
[item performSelector:settingSelector withObject:itemContent];
[item setAction:action];
if (menu!=NULL)
{
// we actually need an NSMenuItem here, so we construct one
mItem=[[[NSMenuItem alloc] init] autorelease];
[mItem setSubmenu: menu];
[mItem setTitle: [menu title]];
[item setMenuFormRepresentation:mItem];
}
[theDict setObject:item forKey:identifier];
}
+ (NSToolbar*)initialToolbarWithDelegate:(id)delegate
{
NSToolbar *toolbar=[[[NSToolbar alloc]
initWithIdentifier:@"myToolbar"] autorelease];
[toolbar setDelegate:delegate];
[toolbar setAllowsUserCustomization:YES];
[toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
[toolbar setSizeMode:NSToolbarSizeModeSmall];
return toolbar;
}
-(void)awakeFromNib
{
mToolbarItems = [[NSMutableDictionary alloc] init];
addToolbarItem(mToolbarItems,@"login",@"Login",@"Login/Logout",@"Login.",self,@selector(setImage:),[NSImage
imageNamed:NSImageNameUser],@selector(loginLogout:),NULL);
addToolbarItem(mToolbarItems,@"serverStatus",@"servername.domain.com",@"servername.domain.com",@"Connected
server status.",self,@selector(setImage:),[NSImage
imageNamed:@"ConnectionGood"],@selector(showWebsite:),NULL);
addToolbarItem(mToolbarItems,@"uploads",@"View Uploads",@"View
Uploads",@"View your pending
uploadds.",self,@selector(setView:),ibPendingUploadsItemView,
NULL,NULL);
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSString *)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
// We create and autorelease a new NSToolbarItem, and then go
through the process of setting up its
// attributes from the master toolbar item matching that identifier
in our dictionary of items.
NSToolbarItem *newItem = [[[NSToolbarItem alloc]
initWithItemIdentifier:itemIdentifier] autorelease];
NSToolbarItem *item=[mToolbarItems objectForKey:itemIdentifier];
[newItem setLabel:[item label]];
[newItem setPaletteLabel:[item paletteLabel]];
if ([item view]!=NULL)
{
[newItem setView:[item view]];
}
else
{
[newItem setImage:[item image]];
}
[newItem setToolTip:[item toolTip]];
[newItem setTarget:[item target]];
[newItem setAction:[item action]];
[newItem setMenuFormRepresentation:[item menuFormRepresentation]];
if ([newItem view]!=NULL)
{
[newItem setMinSize:[[item view] bounds].size];
[newItem setMaxSize:[[item view] bounds].size];
}
[newItem setEnabled:YES];
return newItem;
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
return [NSArray
arrayWithObjects:@"login",@"uploads",NSToolbarSeparatorItemIdentifier,NSToolbarFlexibleSpaceItemIdentifier,@"serverStatus",nil];
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
return [NSArray arrayWithObjects:@"login",@"uploads",
NSToolbarSeparatorItemIdentifier,NSToolbarFlexibleSpaceItemIdentifier,@"serverStatus",
NSToolbarSpaceItemIdentifier,nil];
}
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
{
if([[theItem itemIdentifier] isEqualToString:@"login"])
{
if((mLoggedIn && ![[theItem label] isEqualToString:@"Logout"])
|| (!mLoggedIn && ![[theItem label] isEqualToString:@"Login"]))
{
[self updateLoginInfoForCurrentUser:theItem];
}
}else if([[theItem itemIdentifier] isEqualToString:@"serverStatus"]
&& ![[theItem label] isEqualToString:mCurrentServerURL] ){
[theItem setLabel:mCurrentServerURL];
[theItem setToolTip:mCurrentServerURL];
NSImage *serverStatusImage = [mCurrentServerURL
isEqualToString:@"Offline Mode"] ? [NSImage
imageNamed:@"ConnectionWarning"] : [NSImage
imageNamed:@"ConnectionGood"];
[theItem setImage: serverStatusImage];
}
return YES;
}
-(BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
return YES;
}
_______________________________________________
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