/*
 *	Filename: RegExFormatterInspector.m
 *	Created : Wed Jan 22 09:44:01 1997
 *	Author  : Vince DeMarco
 *	LastEditDate Was "Wed Jan 22 13:08:05 1997"
 */

#import "RegExFormatterInspector.h"
#import <regex/RegExSyntaxFormatter.h>
#import <regex/RegExFormatter.h>
#import <regex/RegEx.h>

@implementation RegExFormatterInspector

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (BOOL)wantsButtons
{
    return NO;
}

- (NSWindow *)window
{
    id window = [super window];
    if (!window) {
        [NSBundle loadNibNamed:@"RegExFormatterInspector" owner:self];
        window = [super window];

        [[[[expressionTableView tableColumns] objectAtIndex:0] dataCell]
	    setFormatter:[[RegExSyntaxFormatter allocWithZone:[expressionTableView zone]] init]];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillUpdate:)
					      name:NSApplicationWillUpdateNotification object:NSApp];
    }
    return window;
}

- (void)revert:sender
{
    RegExFormatter *formatter = [self object];
    [super revert:sender];

    [allowsEmptyStringCheckBox setState:[formatter allowsEmptyString]];
    [formatPatternTextField setStringValue:[formatter formatPattern]];
    [expressionTableView reloadData];
    [self selectExpression:self];
}

- addExpression:sender
{
    if ([RegEx validRegexPattern:[expressionTextField stringValue]]){
	[[self object] addRegularExpression:[[RegEx allocWithZone:[[self object] zone]]
						   initWithPattern:[expressionTextField stringValue]]];
	[expressionTableView reloadData];
	[self selectExpression:self];
	[self ok:sender];
    }else{
	NSBeep();
    }
    return self;
}

- delExpression:sender
{
    if ([expressionTableView selectedRow] < [[[self object] expressions] count]){
	[[self object] removeRegularExpressionAtIndex:[expressionTableView selectedRow]];
	[self ok:sender];
    }
    [expressionTableView reloadData];
    [self selectExpression:self];
    return self;
}

- allowsEmptyString:sender
{
    BOOL flag = [allowsEmptyStringCheckBox state];
    [[self object] setAllowsEmptyString:flag];
    [self ok:sender];
    return self;
}

- selectExpression:sender
{
    if ([[[self object] expressions] count]){
	[[expressionButtons cellAtRow:0 column:1] setEnabled:YES];
	[expressionTextField setStringValue:[[[[self object] expressions]
						 objectAtIndex:[expressionTableView selectedRow]] pattern]];
    }else{
	[[expressionButtons cellAtRow:0 column:1] setEnabled:NO];
    }
    return self;
}

- (void)applicationWillUpdate:(NSNotification *)notification
{

}

/* TableView dataSource methods */

- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [[[self object] expressions] count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
    return [[self object] regularExpressionAtIndex:row];
}

/* TextField delegate class */
- (void)controlTextDidEndEditing:(NSNotification *)notification
{
    id notifier = [notification object];
    if (notifier == formatPatternTextField){
	[[self object] setFormatPattern:[notifier stringValue]];
    }
}

- (void)controlTextDidChange:(NSNotification *)notification
{
    id notifier = [notification object];
    if (notifier == expressionTextField){
	if (![[expressionTextField stringValue] isEqual:@""] &&
	    [RegEx validRegexPattern:[expressionTextField stringValue]]){
	    [[expressionButtons cellAtRow:0 column:0] setEnabled:YES];
	}else{
	    [[expressionButtons cellAtRow:0 column:0] setEnabled:NO];
	}
    }
}
@end
