-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGVAttributeInspectorController.m
More file actions
159 lines (138 loc) · 5.79 KB
/
Copy pathGVAttributeInspectorController.m
File metadata and controls
159 lines (138 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at http://www.graphviz.org/
*************************************************************************/
#import "GVAttributeInspectorController.h"
#import "GVAttributeSchema.h"
#import "GVDocument.h"
#import "GVZGraph.h"
#import "GVWindowController.h"
@implementation GVAttributeInspectorController
- (instancetype)init
{
if (self = [super initWithWindowNibName: @"Attributes"]) {
_allAttributes = [[NSMutableDictionary alloc] init];
_otherChangedGraph = YES;
}
return self;
}
- (void)awakeFromNib
{
/* set component toolbar */
_allSchemas = [[NSDictionary alloc] initWithObjectsAndKeys:
[GVAttributeSchema attributeSchemasWithComponent:@"graph"], _graphToolbarItem.itemIdentifier,
[GVAttributeSchema attributeSchemasWithComponent:@"node"], _nodeDefaultToolbarItem.itemIdentifier,
[GVAttributeSchema attributeSchemasWithComponent:@"edge"], _edgeDefaultToolbarItem.itemIdentifier,
nil];
_componentToolbar.selectedItemIdentifier = _graphToolbarItem.itemIdentifier;
[self toolbarItemDidSelect:nil];
/* start observing whenever a window becomes main */
[self graphWindowDidBecomeMain:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(graphWindowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:nil];
}
- (IBAction)toolbarItemDidSelect:(id)sender
{
/* reload the table */
[_attributeTable reloadData];
}
- (void)graphWindowDidBecomeMain:(NSNotification *)notification
{
NSWindow *mainWindow = notification ? notification.object : NSApp.mainWindow;
GVDocument *mainWindowDocument = mainWindow.windowController.document;
/* update and observe referenced document */
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
if (_inspectedDocument)
[defaultCenter removeObserver:self name:@"GVGraphDocumentDidChange" object:_inspectedDocument];
_inspectedDocument = mainWindowDocument;
[defaultCenter addObserver:self selector:@selector(graphDocumentDidChange:) name:@"GVGraphDocumentDidChange" object:mainWindowDocument];
[self reloadAttributes];
/* update the UI */
self.window.title = [NSString stringWithFormat:@"%@ Attributes", mainWindow.title];
[_attributeTable reloadData];
}
- (void)graphDocumentDidChange:(NSNotification *)notification
{
/* if we didn't instigate the change, update the UI */
if (_otherChangedGraph) {
[self reloadAttributes];
[_attributeTable reloadData];
}
}
- (void)reloadAttributes
{
/* reload the attributes from the inspected document's graph */
[_allAttributes removeAllObjects];
if ([_inspectedDocument respondsToSelector:@selector(graph)]) {
GVZGraph *graph = _inspectedDocument.graph;
_allAttributes[_graphToolbarItem.itemIdentifier] = graph.graphAttributes;
_allAttributes[_nodeDefaultToolbarItem.itemIdentifier] = graph.defaultNodeAttributes;
_allAttributes[_edgeDefaultToolbarItem.itemIdentifier] = graph.defaultEdgeAttributes;
}
}
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
/* which toolbar items are selectable */
return @[_graphToolbarItem.itemIdentifier,
_nodeDefaultToolbarItem.itemIdentifier,
_edgeDefaultToolbarItem.itemIdentifier];
}
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if ([tableColumn.identifier isEqualToString:@"value"]) {
/* use the row's schema's cell */
NSCell *cell = _allSchemas[_componentToolbar.selectedItemIdentifier][row].cell;
cell.enabled = _allAttributes.count > 0;
return cell;
}
else
/* use the default cell (usually a text field) for other columns */
return nil;
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSInteger selectedRow = [notification.object selectedRow];
NSString *documentation = selectedRow == -1 ? nil : _allSchemas[_componentToolbar.selectedItemIdentifier][selectedRow].documentation;
[_documentationWeb loadHTMLString:documentation baseURL:[NSURL URLWithString:@"http://www.graphviz.org/"]];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return _allSchemas[_componentToolbar.selectedItemIdentifier].count;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *selectedComponentIdentifier = _componentToolbar.selectedItemIdentifier;
NSString *attributeName = _allSchemas[selectedComponentIdentifier][row].name;
if ([tableColumn.identifier isEqualToString:@"key"])
return attributeName;
else if ([tableColumn.identifier isEqualToString:@"value"])
/* return the inspected graph's attribute value, if any */
return _allAttributes[selectedComponentIdentifier][attributeName];
else
return nil;
}
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if ([tableColumn.identifier isEqualToString:@"value"]) {
NSString *selectedComponentIdentifier = _componentToolbar.selectedItemIdentifier;
NSString *attributeName = _allSchemas[selectedComponentIdentifier][row].name;
/* set or remove the key-value on the selected attributes */
/* NOTE: to avoid needlessly reloading the table in graphDocumentDidChange:, we fence this change with _otherChangedGraph = NO */
_otherChangedGraph = NO;
@try {
_allAttributes[selectedComponentIdentifier][attributeName] = object;
}
@finally {
_otherChangedGraph = YES;
}
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeMainNotification object:nil];
}
@end