/*
 Oaxaca: Useful Cocoa Library
 Copyright (C) 2005, Niko Matsakis (niko@alum.mit.edu)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 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 "OADataWatch.h"

@interface OADWEntity
@end

@interface OADWInstance
@end

@interface OADataWatch (Private)
- initWithContext:(NSManagedObjectContext*)context model:(NSManagedObjectModel*)model;
- (void) awakeFromNib;
- (void) dealloc;
@end

@implementation OADataWatch

+ (void) displayContext:(NSManagedObjectContext*)context 
                  model:(NSManagedObjectModel*)model
{
    [[OADataWatch alloc] initWithContext:context model:model];
    // have to figure out how this thing is retained or something...
    // ...for now who cares
}

@end

@implementation OADataWatch (Private)

- initWithContext:(NSManagedObjectContext*)context model:(NSManagedObjectModel*)model
{
    if ((self = [super init])) {
        m_context = [context retain];
        m_model = [model retain];
        
        [NSBundle loadNibNamed:@"OADataWatch" owner:self];
        
        [m_entityController addObserver:self 
                             forKeyPath:@"selection" 
                                options:NSKeyValueObservingOptionNew
                                context:nil];
        [m_window makeKeyAndOrderFront:self];
        
    }
    return self;
}

- (void) dealloc
{
    [m_entityController removeObserver:self forKeyPath:@"selection"];
    [m_context release];
    [m_model release];
    [super dealloc];
}

- (NSManagedObjectContext*) managedObjectContext
{
    return m_context;
}

- (NSManagedObjectModel*) managedObjectModel
{
    return m_model;
}

- (void) awakeFromNib
{
}

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:object change:(NSDictionary *)change context:(void *)context
{
    if (object == m_entityController) {
        id desc = [[m_entityController arrangedObjects] objectAtIndex:[m_entityController selectionIndex]];
        if (desc) {
            NSString *entityName = [desc name];
            [m_objectController setEntityName:entityName];
            [m_objectController prepareContent];
        }
    }
}

@end
