
#import <Foundation/Foundation.h>
#import <MiscAppKit/MiscTreeDiagramView.h>
#import <MiscAppKit/MiscTreeDiagram.h>
#import <MiscAppKit/MiscDiagramTree.h>
#import <MiscAppKit/MiscNodeStyle.h>
#import <MiscAppKit/MiscTreeStyle.h>
#import "TDStyleWell.h"
#import "InfoView.h"

#import "AppController.h"
 
@implementation AppController

- (void)updateControls
{
    	id diagram, node, style;

     	diagram = [treeView diagram];
        node = [diagram root];
        style = [diagram treeStyle];
        [borderField setFloatValue:[node border]];
        [borderSlider setFloatValue:[node border]];
        [distanceToParentField setFloatValue:[node distanceToParent]];
        [distanceToParentSlider setFloatValue:[node distanceToParent]];
        [bendingFactorField setFloatValue:[style bendingFactor]];
        [bendingFactorSlider setFloatValue:[style bendingFactor]];
        [bendingFactorField setEnabled:[style lineType]];
        [bendingFactorSlider setEnabled:[style lineType]];        	
        [linkButton setState:[style lineType]];
        [orientationButton setState:[node layoutType]];
        [shadowToggle setState:[style shadow]];
        [shadowColorWell setColor:[style shadowColor]];
        node = [diagram selectedNode];
        if(node){
            	style = [node nodeStyle];
                [shapeMatrix selectCellWithTag:[node shapeType]];
                [outlineToggle setState:[style outline]];
                [fillColorWell setColor:[style fillColor]];
                [outlineColorWell setColor:[style outlineColor]];
                [outlineLinewidthField setFloatValue:[style linewidth]];
                [outlineLinewidthSlider setFloatValue:[style linewidth]];
                [childEndingMatrix selectCellWithTag:[style childEnding]];
                [parentEndingMatrix selectCellWithTag:[style parentEnding]];
                [textPositionMatrix selectCellWithTag:[style textPlacement]];
                [automaticResizeToggle setState:[style automaticResize]];

        }
}

- (void)awakeFromNib
{
	[[treeView diagram] setDoc:self];
        [styleWell setWellClient:self];
        [nodeWell setOperationMode:MiscPaletteMode];
}

- (void)changeShape:(id)sender
{
    	id diagram, node;

     	diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node)
     		[diagram setShapeType:[[sender selectedCell] tag] of:node];
}

- (void)changeParentEnding:(id)sender
{
        id diagram, node;

        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node)
                [diagram setParentEnding:[[sender selectedCell] tag] of:node];
}

- (void)changeChildEnding:(id)sender
{
        id diagram, node;

        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node)
                [diagram setChildEnding:[[sender selectedCell] tag] of:node];
}

- (void)changeOutlineColor:(id)sender
{
        id diagram, node;

        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node)
                [diagram setOutlineColor:[sender color] of:node];
}

- (void)changeFillColor:(id)sender
{
        id diagram, node;

        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node)
                [diagram setFillColor:[sender color] of:node];
}

- (void)changeShadowColor:(id)sender
{
        id diagram;

        diagram = [treeView diagram];
        [diagram setShadowColor:[sender color]];
}

- (void)changeLineColor:(id)sender
{
        return;
}

- (void)changeOutline:(id)sender
{
        return;
}

- (void)changeFill:(id)sender
{
        return;
}

- (void)changeShadow:(id)sender
{
        id diagram;

        diagram = [treeView diagram];
        [diagram setShadow:[sender state]];
}

- (void)changeLine:(id)sender
{
        return;
}

- (void)changeOutlineLinewidth:(id)sender
{
        id diagram, node;

        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node){
            	[diagram setLinewidth:[sender floatValue] of:node];
                if(sender == outlineLinewidthSlider)
                    	[outlineLinewidthField setFloatValue:[sender floatValue]];
                else
                    	[outlineLinewidthSlider setFloatValue:[sender floatValue]];
        }
}

- (void)changeLineLinewith:(id)sender
{
        return;
}

- (void)changeDistanceToParent:(id)sender
{
        id diagram;

        diagram = [treeView diagram];
        [diagram setDistanceToParent:[sender floatValue]];
        if(sender == distanceToParentField)
                [distanceToParentSlider setFloatValue:[sender floatValue]];
        else
                [distanceToParentField setFloatValue:[sender floatValue]];
}

- (void)changeBorder:(id)sender
{
    	id diagram;

	diagram = [treeView diagram];
	[diagram setBorder:[sender floatValue]];
        if(sender == borderField)
                [borderSlider setFloatValue:[sender floatValue]];
        else
                [borderField setFloatValue:[sender floatValue]];
}

- (void)changeOrientation:(id)sender
{
    	id diagram;

	diagram = [treeView diagram];
       	[diagram setLayoutType:[sender state]];
}

- (void)changeLink:(id)sender
{
    	id diagram;

     	diagram = [treeView diagram];
    	[diagram setLineType:[sender state]];
    	[bendingFactorField setEnabled:[sender state]];
        [bendingFactorSlider setEnabled:[sender state]];
}

- (void)changeBendingFactor:(id)sender
{
    	id diagram;

	diagram = [treeView diagram];
	[diagram setBendingFactor:[sender floatValue]];
        if(sender == bendingFactorSlider)
            	[bendingFactorField setFloatValue:[sender floatValue]];
        else
            	[bendingFactorSlider setFloatValue:[sender floatValue]];
}

- (void)changeTextPlacement:(id)sender
{
        id diagram, node;

        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node)
                [diagram setTextPlacement:[[sender selectedCell] tag] of:node];
}

- (void)changeAutomaticResize:(id)sender
{
        id diagram, node;

        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node)
                [diagram setAutomaticResize:[sender state] of:node];
}


- (void)setDirty:(BOOL)aBOOL
{
        return;
}

- (void)selectionChangedTo:anObject
{
    	id diagram, node;
        
    	[self updateControls];
        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node)
            	[styleWell setEnabled:YES];
        else
            	[styleWell setEnabled:NO];
        return;
}

- (BOOL)hasSelection
{
   	id diagram, node;

     	diagram = [treeView diagram];
     	node = [diagram selectedNode];
     	if(node)
        	return YES;
        else
            	return NO;
}

- styleSelection
{
        NSDictionary *inspectorContents;
        NSString *keys[2];
        NSObject *values[2];
        id diagram, node;

        diagram = [treeView diagram];
        node = [diagram selectedNode];
        if(node){
            	keys[0] = @"NodeStyle";
            	keys[1] = @"ShapeType";
            	values[0] = [node nodeStyle];
            	values[1] = [NSNumber numberWithInt:[node shapeType]];
            	inspectorContents = [NSDictionary dictionaryWithObjects:(id *)values forKeys:keys count:2];
            	return inspectorContents;
        } else
            	return nil;
}

- (void)showInfoPanel:(id)sender
{
    	if(!infoPanel)
            	[NSBundle loadNibNamed:@"Info" owner:self];
	[infoPanel makeKeyAndOrderFront:self];
        [infoView start:self];
}

- (void)saveDG:(id)sender
{
        NSSavePanel *savePanel = [NSSavePanel savePanel];

        [savePanel setRequiredFileType:@"diagram2"];
        [savePanel setTitle:@"Save DG"];
        if([savePanel runModal])
            	[treeView writeDiagramToDGFile:[savePanel filename]];
}

@end


