~ubuntu-branches/ubuntu/lucid/graphviz/lucid-updates

« back to all changes in this revision

Viewing changes to macosx/GVAttributeInspectorController.m

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2008-06-19 20:23:23 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080619202323-ls23h96ntj9ny94m
Tags: 2.18-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build depend on liblualib50-dev instead of liblua5.1-0-dev.
  - Drop libttf-dev (libttf-dev is in universe) (LP: #174749).
  - Replace gs-common with ghostscript.
  - Build-depend on python-dev instead of python2.4-dev or python2.5-dev.
  - Mention the correct python version for the python bindings in the
    package description.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: GVAttributeInspectorController.m,v 1.1 2008/02/11 12:34:07 glenlow Exp $ $Revision: 1.1 $ */
 
2
/* vim:set shiftwidth=4 ts=8: */
 
3
 
 
4
/**********************************************************
 
5
*      This software is part of the graphviz package      *
 
6
*                http://www.graphviz.org/                 *
 
7
*                                                         *
 
8
*            Copyright (c) 1994-2008 AT&T Corp.           *
 
9
*                and is licensed under the                *
 
10
*            Common Public License, Version 1.0           *
 
11
*                      by AT&T Corp.                      *
 
12
*                                                         *
 
13
*        Information and Software Systems Research        *
 
14
*              AT&T Research, Florham Park NJ             *
 
15
**********************************************************/
 
16
 
 
17
#import "GVAttributeInspectorController.h"
 
18
#import "GVAttributeSchema.h"
 
19
#import "GVGraph.h"
 
20
#import "GVWindowController.h"
 
21
 
 
22
@implementation GVAttributeInspectorController
 
23
 
 
24
- (id)init
 
25
{
 
26
        if (self = [super initWithWindowNibName: @"Attributes"]) {
 
27
                _allSchemas = nil;
 
28
                _allAttributes = [[NSMutableDictionary alloc] init];
 
29
                _inspectedGraph = nil;
 
30
                _otherChangedGraph = YES;
 
31
        }
 
32
        return self;
 
33
}
 
34
 
 
35
- (void)awakeFromNib
 
36
{
 
37
        /* set component toolbar */
 
38
        [_allSchemas release];
 
39
        _allSchemas = [[NSDictionary alloc] initWithObjectsAndKeys:
 
40
                [GVAttributeSchema attributeSchemasWithComponent:@"graph"], [graphToolbarItem itemIdentifier],
 
41
                [GVAttributeSchema attributeSchemasWithComponent:@"node"], [nodeDefaultToolbarItem itemIdentifier],
 
42
                [GVAttributeSchema attributeSchemasWithComponent:@"edge"], [edgeDefaultToolbarItem itemIdentifier],
 
43
                nil];
 
44
        [componentToolbar setSelectedItemIdentifier:[graphToolbarItem itemIdentifier]];
 
45
        [self toolbarItemDidSelect:nil];
 
46
                
 
47
        /* start observing whenever a window becomes main */
 
48
        [self graphWindowDidBecomeMain:nil];
 
49
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(graphWindowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:nil];
 
50
}
 
51
 
 
52
- (IBAction)toolbarItemDidSelect:(id)sender
 
53
{
 
54
        /* reload the table */
 
55
        [attributeTable reloadData];
 
56
}
 
57
 
 
58
- (void)graphWindowDidBecomeMain:(NSNotification *)notification
 
59
{
 
60
        NSWindow* mainWindow = notification ? [notification object] : [NSApp mainWindow];
 
61
        NSWindowController* mainWindowController = [mainWindow windowController];
 
62
        if ([mainWindowController respondsToSelector:@selector(graph)]) {
 
63
                GVGraph *newGraph = [(GVWindowController *)mainWindowController graph];
 
64
                
 
65
                if (_inspectedGraph != newGraph) {
 
66
                        /* retain the inspected graph and start observing any changes from it */
 
67
                        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
 
68
                        if (_inspectedGraph) {
 
69
                                [defaultCenter removeObserver:self name:@"GVGraphDidChange" object:_inspectedGraph];
 
70
                                [_inspectedGraph release];
 
71
                        }
 
72
                        if (_inspectedGraph = [newGraph retain]) {
 
73
                                [_allAttributes setObject:newGraph.graphAttributes forKey:[graphToolbarItem itemIdentifier]];
 
74
                                [_allAttributes setObject:newGraph.defaultNodeAttributes forKey:[nodeDefaultToolbarItem itemIdentifier]];
 
75
                                [_allAttributes setObject:newGraph.defaultEdgeAttributes forKey:[edgeDefaultToolbarItem itemIdentifier]];
 
76
                                [defaultCenter addObserver:self selector:@selector(graphDidChange:) name:@"GVGraphDidChange" object:newGraph];
 
77
                        }
 
78
                        else
 
79
                                [_allAttributes removeAllObjects];
 
80
                                
 
81
                
 
82
                        /* update the UI */
 
83
                        [[self window] setTitle:[NSString stringWithFormat:@"%@ Attributes", [mainWindow title]]];
 
84
                        [attributeTable reloadData];
 
85
                }
 
86
        }
 
87
}
 
88
 
 
89
- (void)graphDidChange:(NSNotification *)notification
 
90
{
 
91
        /* if we didn't instigate the change, update the UI */
 
92
        if (_otherChangedGraph)
 
93
                [attributeTable reloadData];
 
94
}
 
95
 
 
96
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
 
97
{
 
98
        /* which toolbar items are selectable */
 
99
        return [NSArray arrayWithObjects:
 
100
                [graphToolbarItem itemIdentifier],
 
101
                [nodeDefaultToolbarItem itemIdentifier],
 
102
                [edgeDefaultToolbarItem itemIdentifier],
 
103
                nil];
 
104
}
 
105
 
 
106
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
 
107
{
 
108
        if ([[tableColumn identifier] isEqualToString:@"value"]) {
 
109
                /* use the row's schema's cell */
 
110
                NSCell *cell = [[[_allSchemas objectForKey:[componentToolbar selectedItemIdentifier]] objectAtIndex:row] cell];
 
111
                [cell setEnabled:[_allAttributes count] > 0];
 
112
                return cell;
 
113
        }
 
114
        else
 
115
                /* use the default cell (usually a text field) for other columns */
 
116
                return nil;
 
117
}
 
118
 
 
119
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
 
120
{
 
121
        int selectedRow = [[aNotification object] selectedRow];
 
122
        NSString* documentation = selectedRow == -1 ? nil : [[[_allSchemas objectForKey:[componentToolbar selectedItemIdentifier]] objectAtIndex: selectedRow] documentation];
 
123
        [[documentationWeb mainFrame] loadHTMLString:documentation baseURL:[NSURL URLWithString:@"http://www.graphviz.org/"]];
 
124
}
 
125
 
 
126
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
 
127
{
 
128
        return [[_allSchemas objectForKey:[componentToolbar selectedItemIdentifier]] count];
 
129
}
 
130
 
 
131
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
 
132
{
 
133
        NSString *selectedComponentIdentifier = [componentToolbar selectedItemIdentifier];
 
134
        NSString *attributeName = [[[_allSchemas objectForKey:selectedComponentIdentifier] objectAtIndex:rowIndex] name];
 
135
        if ([[tableColumn identifier] isEqualToString:@"key"])
 
136
                return attributeName;
 
137
        else if ([[tableColumn identifier] isEqualToString:@"value"])
 
138
                /* return the inspected graph's attribute value, if any */
 
139
                return [[_allAttributes objectForKey:selectedComponentIdentifier] valueForKey:attributeName];
 
140
        else
 
141
                return nil;
 
142
}
 
143
 
 
144
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
 
145
{
 
146
        if ([[tableColumn identifier] isEqualToString:@"value"])
 
147
                {
 
148
                        NSString *selectedComponentIdentifier = [componentToolbar selectedItemIdentifier];
 
149
                        NSString *attributeName = [[[_allSchemas objectForKey:selectedComponentIdentifier] objectAtIndex:rowIndex] name];
 
150
                        
 
151
                        /* set or remove the key-value on the selected attributes */
 
152
                        /* NOTE: to avoid needlessly reloading the table in graphDidChange:, we fence this change with _otherChangedGraph = NO */
 
153
                        _otherChangedGraph = NO;
 
154
                        @try {
 
155
                                [[_allAttributes objectForKey:selectedComponentIdentifier] setValue:anObject forKey:attributeName];
 
156
                        }
 
157
                        @finally {
 
158
                                _otherChangedGraph = YES;
 
159
                        }
 
160
                }
 
161
 
 
162
}
 
163
 
 
164
 
 
165
- (void)dealloc
 
166
{
 
167
        [_allSchemas release];
 
168
        [_allAttributes release];
 
169
        
 
170
        [_inspectedGraph release];
 
171
        
 
172
        [super dealloc];
 
173
}
 
174
 
 
175
@end