//  Created by Joe Becker on 3/7/05.
//  Copyright 2005 __MyCompanyName__. All rights reserved.

#import "PrefController.h"
#import "Controller.h"
#import "ColorGraphView.h"
#import "defaults.h"


@implementation PrefController

- (IBAction)okay:(id)sender
{

    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

    [defs setObject:DataForColor([axesColorWell color])
          forKey:GP_AxesColor];

    [defs setObject:DataForColor([labelColorWell color])
          forKey:GP_LabelColor];

    [defs setObject:DataForColor([predictedColorWell color])
          forKey:GP_PredictedColor];
          
    [defs setObject:DataForColor([flagColorWell color])
			 forKey:GP_FlaggedColor];
	
	[defs setObject:DataForColor([measuredColorWell color])
			 forKey:GP_MeasuredColor];
	
    [defs setObject:[xminCell objectValue] forKey:GP_xmin];
    [defs setObject:[xmaxCell objectValue] forKey:GP_xmax];
    [defs setObject:[yminCell objectValue] forKey:GP_ymin];
    [defs setObject:[ymaxCell objectValue] forKey:GP_ymax];
    [defs setObject:[xstepCell objectValue] forKey:GP_xstep];

    [window orderOut:nil];

}


- (IBAction)revert:(id)sender
{
        
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    
    NSLog(@"PrefController:revert\n");


    [self setUpWell:axesColorWell ];
    [self setUpWell:labelColorWell ];
    [self setUpWell:predictedColorWell ];
    [self setUpWell:measuredColorWell ];
    [self setUpWell:flagColorWell ];

    [xminCell  setObjectValue:[defs objectForKey:GP_xmin]];
    [xmaxCell  setObjectValue:[defs objectForKey:GP_xmax]];
    [xstepCell setObjectValue:[defs objectForKey:GP_xstep]];
    [yminCell  setObjectValue:[defs objectForKey:GP_ymin]];
    [ymaxCell  setObjectValue:[defs objectForKey:GP_ymax]];

    
}


- (NSWindow *)window
{
    return window;
}


- (void)setUpWell:(NSColorWell *)well
{
    id colorGraphView = [ [NSApp delegate] graphView] ;

    [well setTarget:colorGraphView];
    [well setAction:@selector(setObjectsToColor:) ];
    [well setColor:[colorGraphView colorForTag:[well tag]]];
}


- (void)awakeFromNib
{
    [ [NSColorPanel sharedColorPanel] setContinuous:YES];

//    [self setUpWell:axesColorWell ];   // moved to revert:
//    [self setUpWell:labelColorWell ];
//    [self setUpWell:predictedColorWell ];

    [window setFrameUsingName:@"Preferences"];   // Chapter 21
    [window setFrameAutosaveName:@"Preferences"];
    [self revert:nil];

}


-(NSColorWell *)colorWellForTag:(int)aTag
{
    return [[window contentView] viewWithTag:aTag];
}




@end
