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: */
4
/**********************************************************
5
* This software is part of the graphviz package *
6
* http://www.graphviz.org/ *
8
* Copyright (c) 1994-2008 AT&T Corp. *
9
* and is licensed under the *
10
* Common Public License, Version 1.0 *
13
* Information and Software Systems Research *
14
* AT&T Research, Florham Park NJ *
15
**********************************************************/
17
#import "GVAttributeInspectorController.h"
18
#import "GVAttributeSchema.h"
20
#import "GVWindowController.h"
22
@implementation GVAttributeInspectorController
26
if (self = [super initWithWindowNibName: @"Attributes"]) {
28
_allAttributes = [[NSMutableDictionary alloc] init];
29
_inspectedGraph = nil;
30
_otherChangedGraph = YES;
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],
44
[componentToolbar setSelectedItemIdentifier:[graphToolbarItem itemIdentifier]];
45
[self toolbarItemDidSelect:nil];
47
/* start observing whenever a window becomes main */
48
[self graphWindowDidBecomeMain:nil];
49
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(graphWindowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:nil];
52
- (IBAction)toolbarItemDidSelect:(id)sender
54
/* reload the table */
55
[attributeTable reloadData];
58
- (void)graphWindowDidBecomeMain:(NSNotification *)notification
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];
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];
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];
79
[_allAttributes removeAllObjects];
83
[[self window] setTitle:[NSString stringWithFormat:@"%@ Attributes", [mainWindow title]]];
84
[attributeTable reloadData];
89
- (void)graphDidChange:(NSNotification *)notification
91
/* if we didn't instigate the change, update the UI */
92
if (_otherChangedGraph)
93
[attributeTable reloadData];
96
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
98
/* which toolbar items are selectable */
99
return [NSArray arrayWithObjects:
100
[graphToolbarItem itemIdentifier],
101
[nodeDefaultToolbarItem itemIdentifier],
102
[edgeDefaultToolbarItem itemIdentifier],
106
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
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];
115
/* use the default cell (usually a text field) for other columns */
119
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
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/"]];
126
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
128
return [[_allSchemas objectForKey:[componentToolbar selectedItemIdentifier]] count];
131
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
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];
144
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
146
if ([[tableColumn identifier] isEqualToString:@"value"])
148
NSString *selectedComponentIdentifier = [componentToolbar selectedItemIdentifier];
149
NSString *attributeName = [[[_allSchemas objectForKey:selectedComponentIdentifier] objectAtIndex:rowIndex] name];
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;
155
[[_allAttributes objectForKey:selectedComponentIdentifier] setValue:anObject forKey:attributeName];
158
_otherChangedGraph = YES;
167
[_allSchemas release];
168
[_allAttributes release];
170
[_inspectedGraph release];