/*
iPhly: Radio Control on the iPhone

Copyright (C) 2010-2011 Ari Krupnik & Associates

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License V2
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#import "ModelNameViewController.h"


@implementation ModelNameViewController

@synthesize nameField;
@synthesize errorLabel;

- (id) initWithProcessor: (ModelProcessor *) p {
	if (self = [super initWithNibName:@"ModelNameViewController" bundle:nil]) {
		processor = [p retain];
		[self setTitle: @"Rename"];
	}
	return self;
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[nameField setText: [processor name]];
	[nameField setDelegate:self];
	[nameField becomeFirstResponder];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[processor release];
    [super dealloc];
}


- (IBAction) hideErrorLabel {
	[errorLabel setTitle:@"" forState: UIControlStateNormal];
	[errorLabel setHidden: YES];
}
/* UITextFieldDelegate */
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	if (textField == nameField) {
		NSString *renameError = [processor rename:[textField text]];
		if (renameError) {
			[errorLabel setTitle:renameError forState:UIControlStateNormal];
			[errorLabel setHidden: NO];
		} else {
			[textField resignFirstResponder];
			[errorLabel setTitle:@"" forState: UIControlStateNormal];
			[errorLabel setHidden: YES];
			[[self navigationController] popViewControllerAnimated:YES];
		}
	}
	return YES;
}


@end
