-
Notifications
You must be signed in to change notification settings - Fork 2
/
BToolbarController.m
193 lines (150 loc) · 7.24 KB
/
BToolbarController.m
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// BToolbarController.m
// BUserInterface
//
// Created by Jesse Grosjean on 9/11/07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "BToolbarController.h"
#import "BToolbar.h"
@interface BToolbarController (BToolbarControllerPrivate)
- (id <BToolbarContributerProtocol>)toolbarContributerFor:(NSString *)itemIdentifier;
@end
@implementation BToolbarController
#pragma mark Init
- (id)initWithToolbarIdentifier:(NSString *)aToolbarIdentifier window:(NSWindow *)aWindow {
if (self = [super init]) {
window = aWindow;
toolbarIdentifier = aToolbarIdentifier;
}
return self;
}
#pragma mark Accessors
@synthesize window;
- (NSToolbar *)toolbar {
if (!toolbar) {
toolbar = [[BToolbar alloc] initWithIdentifier:toolbarIdentifier];
allowedItemIdentifiers = [[NSMutableArray alloc] init];
defaultItemIdentifiers = [[NSMutableArray alloc] init];
selectableItemIdentifiers = [[NSMutableArray alloc] init];
itemIdentifiersToContributers = [[NSMutableDictionary alloc] init];
itemIdentifiersToConfigurationElements = [[NSMutableDictionary alloc] init];
NSDictionary *definedIdentifierLookup = [NSDictionary dictionaryWithObjectsAndKeys:
NSToolbarSeparatorItemIdentifier, @"NSToolbarSeparatorItemIdentifier",
NSToolbarSpaceItemIdentifier, @"NSToolbarSpaceItemIdentifier",
NSToolbarFlexibleSpaceItemIdentifier, @"NSToolbarFlexibleSpaceItemIdentifier",
NSToolbarShowColorsItemIdentifier, @"NSToolbarShowColorsItemIdentifier",
NSToolbarShowFontsItemIdentifier, @"NSToolbarShowFontsItemIdentifier",
NSToolbarCustomizeToolbarItemIdentifier, @"NSToolbarCustomizeToolbarItemIdentifier",
NSToolbarPrintItemIdentifier, @"NSToolbarPrintItemIdentifier",
nil];
for (BConfigurationElement *eachToolbar in [[BExtensionRegistry sharedInstance] configurationElementsFor:BUserInterfaceToolbarsExtensionPoint]) {
if ([[eachToolbar name] isEqualToString:@"toolbar"] && [[[eachToolbar attributes] objectForKey:@"id"] isEqualToString:[toolbar identifier]]) {
NSEnumerator *itemEnumerator = [[eachToolbar children] objectEnumerator];
BConfigurationElement *eachItem;
while (eachItem = [itemEnumerator nextObject]) {
NSString *identifier = [eachItem attributeForKey:@"id"];
if ([definedIdentifierLookup objectForKey:identifier]) {
identifier = [definedIdentifierLookup objectForKey:identifier];
}
if ([itemIdentifiersToConfigurationElements objectForKey:identifier]) {
[allowedItemIdentifiers removeObject:identifier];
[selectableItemIdentifiers removeObject:identifier];
BLogInfo([NSString stringWithFormat:@"toolbar item identifier %@ contributer is being replaced by new configuration element with same identifier %@", identifier, eachItem]);
}
[itemIdentifiersToConfigurationElements setObject:eachItem forKey:identifier];
[allowedItemIdentifiers addObject:identifier];
if ([eachItem booleanAttributeForKey:@"default"]) {
[defaultItemIdentifiers addObject:identifier];
}
if ([eachItem booleanAttributeForKey:@"selectable"]) {
[selectableItemIdentifiers addObject:identifier];
}
}
}
}
[toolbar setDelegate:self];
}
return toolbar;
}
- (BConfigurationElement *)configurationElementFor:(NSString *)itemIdentifier {
return [itemIdentifiersToConfigurationElements objectForKey:itemIdentifier];
}
#pragma mark Toolbar Delegate
- (NSToolbarItem *)toolbar:(NSToolbar *)aToolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
BConfigurationElement *configurationElement = [self configurationElementFor:itemIdentifier];
NSToolbarItem *defaultToolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
[defaultToolbarItem setLabel:[configurationElement localizedAttributeForKey:@"label"]];
[defaultToolbarItem setPaletteLabel:[configurationElement localizedAttributeForKey:@"paletteLabel"]];
[defaultToolbarItem setToolTip:[configurationElement localizedAttributeForKey:@"toolTip"]];
[defaultToolbarItem setTag:[configurationElement integerAttributeForKey:@"tag"]];
[defaultToolbarItem setTarget:[configurationElement executableExtensionAttributeForKey:@"target"]];
[defaultToolbarItem setAction:[configurationElement selectorAttributeForKey:@"action"]];
[defaultToolbarItem setImage:[configurationElement imageAttributeForKey:@"image"]];
id <BToolbarContributerProtocol> toolbarContributer = [self toolbarContributerFor:itemIdentifier];
if (toolbarContributer) {
return [toolbarContributer toolbarController:self itemForItemIdentifier:itemIdentifier defaultToolbarItem:defaultToolbarItem willBeInsertedIntoToolbar:flag];
} else {
return defaultToolbarItem;
}
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
return defaultItemIdentifiers;
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
return allowedItemIdentifiers;
}
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
return selectableItemIdentifiers;
}
- (void)toolbarWillAddItem:(NSNotification *)notification {
NSString *itemIdentifier = [notification valueForKeyPath:@"userInfo.item.itemIdentifier"];
[[self toolbarContributerFor:itemIdentifier] toolbarController:self willAddItem:notification];
}
- (void)toolbarDidRemoveItem:(NSNotification *)notification {
NSString *itemIdentifier = [notification valueForKeyPath:@"userInfo.item.itemIdentifier"];
[[self toolbarContributerFor:itemIdentifier] toolbarController:self didRemoveItem:notification];
}
- (void)toolbarWillShow:(NSToolbar *)aToolbar {
for (id <BToolbarContributerProtocol> each in [itemIdentifiersToContributers allValues]) {
if ([each conformsToProtocol:@protocol(BToolbarContributerProtocol)]) {
[each toolbarController:self willShowToolbar:aToolbar];
}
}
}
- (void)toolbarWillHide:(NSToolbar *)aToolbar {
for (id <BToolbarContributerProtocol> each in [itemIdentifiersToContributers allValues]) {
if ([each conformsToProtocol:@protocol(BToolbarContributerProtocol)]) {
[each toolbarController:self willHideToolbar:aToolbar];
}
}
}
- (void)toolbarWillRunCustomizationPalette:(NSToolbar *)aToolbar {
for (id <BToolbarContributerProtocol> each in [itemIdentifiersToContributers allValues]) {
if ([each conformsToProtocol:@protocol(BToolbarContributerProtocol)]) {
[each toolbarController:self willRunCustomizationPaletteForToolbar:aToolbar];
}
}
}
@end
@implementation BToolbarController (BToolbarControllerPrivate)
- (id <BToolbarContributerProtocol>)toolbarContributerFor:(NSString *)itemIdentifier {
id contributer = [itemIdentifiersToContributers objectForKey:itemIdentifier];
if (!contributer) {
BConfigurationElement *configurationElement = [self configurationElementFor:itemIdentifier];
if ([configurationElement attributeForKey:@"contributer"]) {
contributer = [configurationElement createExecutableExtensionFromAttribute:@"contributer" conformingToClass:nil conformingToProtocol:@protocol(BToolbarContributerProtocol) respondingToSelectors:nil];
}
if (!contributer) {
contributer = [NSNull null];
}
[itemIdentifiersToContributers setObject:contributer forKey:itemIdentifier];
}
if (contributer == [NSNull null]) {
return nil;
} else {
return contributer;
}
}
@end
NSString *BUserInterfaceToolbarsExtensionPoint = @"com.blocks.BUserInterface.toolbars";