forked from karelia/iMedia
/
IMBNodeViewController.m
1659 lines (1194 loc) · 48.2 KB
/
IMBNodeViewController.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
iMedia Browser Framework <http://karelia.com/imedia/>
Copyright (c) 2005-2012 by Karelia Software et al.
iMedia Browser is based on code originally developed by Jason Terhorst,
further developed for Sandvox by Greg Hulands, Dan Wood, and Terrence Talbot.
The new architecture for version 2.0 was developed by Peter Baumgartner.
Contributions have also been made by Matt Gough, Martin Wennerberg and others
as indicated in source files.
The iMedia Browser Framework is licensed under the following terms:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in all or substantial portions of the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following
conditions:
Redistributions of source code must retain the original terms stated here,
including this list of conditions, the disclaimer noted below, and the
following copyright notice: Copyright (c) 2005-2012 by Karelia Software et al.
Redistributions in binary form must include, in an end-user-visible manner,
e.g., About window, Acknowledgments window, or similar, either a) the original
terms stated here, including this list of conditions, the disclaimer noted
below, and the aforementioned copyright notice, or b) the aforementioned
copyright notice and a link to karelia.com/imedia.
Neither the name of Karelia Software, nor Sandvox, nor the names of
contributors to iMedia Browser may be used to endorse or promote products
derived from the Software without prior and express written permission from
Karelia Software or individual contributors, as appropriate.
Disclaimer: THE SOFTWARE IS PROVIDED BY THE COPYRIGHT OWNER AND CONTRIBUTORS
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH, THE
SOFTWARE OR THE USE OF, OR OTHER DEALINGS IN, THE SOFTWARE.
*/
// Author: Peter Baumgartner
//----------------------------------------------------------------------------------------------------------------------
#pragma mark HEADERS
#import "IMBNodeViewController.h"
#import "IMBObjectViewController.h"
#import "IMBLibraryController.h"
#import "IMBNodeTreeController.h"
#import "IMBOutlineView.h"
#import "IMBConfig.h"
#import "IMBParser.h"
#import "IMBNode.h"
#import "IMBNodeCell.h"
#import "IMBFlickrNode.h"
#import "NSView+iMedia.h"
#import "NSFileManager+iMedia.h"
//----------------------------------------------------------------------------------------------------------------------
#pragma mark CONSTANTS
static NSString* kArrangedObjectsKey = @"arrangedObjects";
static NSString* kSelectionKey = @"selection";
static NSString* kIMBRevealNodeWithIdentifierNotification = @"IMBRevealNodeWithIdentifierNotification";
static NSString* kIMBSelectNodeWithIdentifierNotification = @"IMBSelectNodeWithIdentifierNotification";
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
// Private methods...
@interface IMBNodeViewController ()
- (void) _startObservingLibraryController;
- (void) _stopObservingLibraryController;
- (NSMutableDictionary*) _preferences;
- (void) _setPreferences:(NSMutableDictionary*)inDict;
- (void) _saveStateToPreferences;
- (void) _loadStateFromPreferences;
- (NSMutableArray*) _expandedNodeIdentifiers;
- (void) _nodesWillChange;
- (void) _nodesDidChange;
- (void) _updatePopupMenu;
- (void) __updatePopupMenu;
- (void) _syncPopupMenuSelection;
- (void) __syncPopupMenuSelection;
- (CGFloat) minimumNodeViewWidth;
- (CGFloat) minimumLibraryViewHeight;
- (CGFloat) minimumObjectViewHeight;
- (NSSize) minimumViewSize;
- (NSViewController*) _customHeaderViewControllerForNode:(IMBNode*)inNode;
- (NSViewController*) _customObjectViewControllerForNode:(IMBNode*)inNode;
- (NSViewController*) _customFooterViewControllerForNode:(IMBNode*)inNode;
@end
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
@implementation IMBNodeViewController
@synthesize libraryController = _libraryController;
@synthesize nodeTreeController = ibNodeTreeController;
@synthesize selectedNodeIdentifier = _selectedNodeIdentifier;
@synthesize expandedNodeIdentifiers = _expandedNodeIdentifiers;
@synthesize selectedParser = _selectedParser;
@synthesize nodeOutlineView = ibNodeOutlineView;
@synthesize nodePopupButton = ibNodePopupButton;
@synthesize objectHeaderView = ibObjectHeaderView;
@synthesize objectContainerView = ibObjectContainerView;
@synthesize objectFooterView = ibObjectFooterView;
@synthesize standardObjectView = _standardObjectView;
@synthesize customObjectView = _customObjectView;
//----------------------------------------------------------------------------------------------------------------------
// Set default preferences to make sure that group nodes are initially expanded and the user sees all root nodes...
+ (void) initialize
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSArray* expandedNodeIdentifiers = [NSArray arrayWithObjects:
@"group://LIBRARIES",
@"group://FOLDERS",
@"group://SEARCHES",
@"group://INTERNET",
@"group://DEVICES",
nil];
NSMutableDictionary* stateDict = [NSMutableDictionary dictionary];
[stateDict setObject:expandedNodeIdentifiers forKey:@"expandedNodeIdentifiers"];
NSMutableDictionary* classDict = [IMBConfig prefsForClass:self.class];
[classDict setObject:stateDict forKey:kIMBMediaTypeImage];
[classDict setObject:stateDict forKey:kIMBMediaTypeAudio];
[classDict setObject:stateDict forKey:kIMBMediaTypeMovie];
[classDict setObject:stateDict forKey:kIMBMediaTypeLink];
[classDict setObject:stateDict forKey:kIMBMediaTypeContact];
[IMBConfig registerDefaultPrefs:classDict forClass:self.class];
[pool drain];
}
//----------------------------------------------------------------------------------------------------------------------
+ (NSBundle*) bundle
{
return [NSBundle bundleForClass:[self class]];
}
+ (NSString*) nibName
{
return @"IMBLibraryView";
}
+ (IMBNodeViewController*) viewControllerForLibraryController:(IMBLibraryController*)inLibraryController
{
IMBNodeViewController* controller = [[[self alloc] initWithNibName:[self nibName] bundle:[self bundle]] autorelease];
[controller view]; // Load the view *before* setting the libraryController,
controller.libraryController = inLibraryController; // so that outlets are set before we load the preferences.
return controller;
}
//----------------------------------------------------------------------------------------------------------------------
- (id) initWithNibName:(NSString*)inNibName bundle:(NSBundle*)inBundle
{
if (self = [super initWithNibName:inNibName bundle:inBundle])
{
_selectedNodeIdentifier = nil;
_expandedNodeIdentifiers = nil;
_isRestoringState = NO;
}
return self;
}
- (void) awakeFromNib
{
// We need to save preferences before tha app quits...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(_saveStateToPreferences)
name:NSApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(_revealNodeWithIdentifier:)
name:kIMBRevealNodeWithIdentifierNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(_selectNodeWithIdentifier:)
name:kIMBSelectNodeWithIdentifierNotification
object:nil];
// Observe changes to the libary node tree...
[ibNodeTreeController retain];
[ibNodeTreeController addObserver:self forKeyPath:kArrangedObjectsKey options:0 context:(void*)kArrangedObjectsKey];
[ibNodeTreeController addObserver:self forKeyPath:kSelectionKey options:0 context:(void*)kSelectionKey];
// Set the cell class on the outline view...
NSArray* columns = [ibNodeOutlineView tableColumns];
if ([columns count] > 0)
{
NSTableColumn* column = [[ibNodeOutlineView tableColumns] objectAtIndex:0];
IMBNodeCell* cell = [[[IMBNodeCell alloc] init] autorelease];
[column setDataCell:cell];
}
// Build the initial contents of the node popup...
[ibNodePopupButton removeAllItems];
[self __updatePopupMenu];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self _stopObservingLibraryController];
[ibNodeTreeController removeObserver:self forKeyPath:kArrangedObjectsKey];
[ibNodeTreeController removeObserver:self forKeyPath:kSelectionKey];
[ibNodeTreeController release];
IMBRelease(_libraryController);
IMBRelease(_selectedNodeIdentifier);
IMBRelease(_expandedNodeIdentifiers);
IMBRelease(_standardObjectView);
IMBRelease(_customObjectView);
IMBRelease(_customHeaderViewControllers);
IMBRelease(_customObjectViewControllers);
IMBRelease(_customFooterViewControllers);
[super dealloc];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
- (void) setLibraryController:(IMBLibraryController*)inLibraryController
{
[self _stopObservingLibraryController];
id old = _libraryController;
_libraryController = [inLibraryController retain];
[old release];
[self _startObservingLibraryController];
[self _loadStateFromPreferences];
// Register the the outline view as a dragging destination if we want to accept new folders
if ([[_libraryController delegate] respondsToSelector:@selector(allowsFolderDropForMediaType:)]) {
// This method returns a BOOL, and seeing as the delegate doesn't have a protocol and we don't
// have one to cast to, we can't use performSelector.., so will use an invocation.
BOOL allowsDrop;
NSString *mediaType = _libraryController.mediaType;
NSMethodSignature *methodSignature = [[_libraryController delegate] methodSignatureForSelector:@selector(allowsFolderDropForMediaType:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setSelector:@selector(allowsFolderDropForMediaType:)];
[invocation setArgument:&mediaType atIndex:2]; // First actual arg
[invocation invokeWithTarget:[_libraryController delegate]];
[invocation getReturnValue:&allowsDrop];
if (allowsDrop) {
[ibNodeOutlineView registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
}
} else {
[ibNodeOutlineView registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
}
}
- (NSString*) mediaType
{
return self.libraryController.mediaType;
}
- (void) _startObservingLibraryController
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(_nodesWillChange)
name:kIMBNodesWillChangeNotification
object:_libraryController];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(_nodesDidChange)
name:kIMBNodesDidChangeNotification
object:_libraryController];
}
- (void) _stopObservingLibraryController
{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:kIMBNodesWillChangeNotification
object:_libraryController];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:kIMBNodesDidChangeNotification
object:_libraryController];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
- (NSMutableDictionary*) _preferences
{
NSMutableDictionary* classDict = [IMBConfig prefsForClass:self.class];
return [NSMutableDictionary dictionaryWithDictionary:[classDict objectForKey:self.mediaType]];
}
- (void) _setPreferences:(NSMutableDictionary*)inDict
{
NSMutableDictionary* classDict = [IMBConfig prefsForClass:self.class];
if (inDict) [classDict setObject:inDict forKey:self.mediaType];
[IMBConfig setPrefs:classDict forClass:self.class];
}
- (void) _saveStateToPreferences
{
NSMutableDictionary* stateDict = [self _preferences];
if (self.expandedNodeIdentifiers)
{
[stateDict setObject:self.expandedNodeIdentifiers forKey:@"expandedNodeIdentifiers"];
}
if (self.selectedNodeIdentifier)
{
[stateDict setObject:self.selectedNodeIdentifier forKey:@"selectedNodeIdentifier"];
}
// Please note: Due to lack of position getter in NSSplitView we cannot set splitviewPosition here,
// but need to do it in NSSplitView delegate method instead...
[self _setPreferences:stateDict];
}
- (void) _loadStateFromPreferences
{
NSMutableDictionary* stateDict = [self _preferences];
self.expandedNodeIdentifiers = [NSMutableArray arrayWithArray:[stateDict objectForKey:@"expandedNodeIdentifiers"]];
self.selectedNodeIdentifier = [stateDict objectForKey:@"selectedNodeIdentifier"];
float splitviewPosition = [[stateDict objectForKey:@"splitviewPosition"] floatValue];
if (splitviewPosition > 0.0) [ibSplitView setPosition:splitviewPosition ofDividerAtIndex:0];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark NSSplitView Delegate
// Store the current divider position in the preferences. Since there is no getter for the current position we
// cannot do it a quit time and have to store it here in the delegate method...
- (CGFloat) splitView:(NSSplitView*)inSplitView constrainSplitPosition:(CGFloat)inPosition ofSubviewAt:(NSInteger)inIndex
{
// This constrains the bottom edge of the top section (the library view) so
// that it is guaranteed to be tall enough to accomodate its minimal popup-based
// chooser, and so that the bottom section (the object view) is tall enough to
// accommodate a reasonable row of items at a reasonable zoom level.
if (inIndex == 0)
{
double minPos = [self minimumLibraryViewHeight];
double maxPos = inSplitView.frame.size.height - [self minimumObjectViewHeight];
inPosition = MAX(inPosition,minPos);
inPosition = MIN(inPosition,maxPos);
}
NSMutableDictionary* stateDict = [self _preferences];
[stateDict setObject:[NSNumber numberWithFloat:inPosition] forKey:@"splitviewPosition"];
[self _setPreferences:stateDict];
return inPosition;
}
// When resising the splitview, then make sure that only the bottom part (object view) gets resized, and
// that the IMBOutlineView is not affected... UNLESS the bottom has already been resized to its minimum
// size.
- (void) splitView:(NSSplitView*)inSplitView resizeSubviewsWithOldSize:(NSSize)inOldSize
{
if ([inSplitView.subviews count] >= 2)
{
NSView* topView = [[inSplitView subviews] objectAtIndex:0];
NSView* bottomView = [[inSplitView subviews] objectAtIndex:1];
float dividerThickness = [inSplitView dividerThickness];
NSRect newFrame = [inSplitView frame];
NSRect topFrame = [topView frame];
topFrame.size.width = newFrame.size.width;
NSRect bottomFrame = [bottomView frame];
bottomFrame.origin = NSMakePoint(0,0);
bottomFrame.size.height = newFrame.size.height - topFrame.size.height - dividerThickness;
bottomFrame.size.width = newFrame.size.width;
bottomFrame.origin.y = topFrame.size.height + dividerThickness;
// If the bottom view is squeezed to its minimum, then we have to resort to shrinking the top.
// If our client heeded our minimumViewSize then we can't have been resized to a size that
// causes BOTH our top and bottom views to be shrunk beyond their minimums.
if (bottomFrame.size.height < [self minimumObjectViewHeight])
{
CGFloat bottomOverflow = [self minimumObjectViewHeight] - bottomFrame.size.height;
bottomFrame.size.height = [self minimumObjectViewHeight];
bottomFrame.origin.y -= bottomOverflow;
topFrame.size.height -= bottomOverflow;
}
[topView setFrame:topFrame];
[bottomView setFrame:bottomFrame];
}
}
// When the splitview moved up so far that the IMBOutlineView gets too small, then hide the outline and show
// the popup instead...
- (void) splitViewDidResizeSubviews:(NSNotification*)inNotification
{
NSRect frame = [[ibNodeOutlineView enclosingScrollView] frame];
BOOL collapsed = frame.size.height < 52.0;
[[ibNodeOutlineView enclosingScrollView] setHidden:collapsed];
[ibNodePopupButton setHidden:!collapsed];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark NSOutlineView Delegate
// If the user is expanding an item in the IMBOutlineView then ask the delegate of the library controller if
// we are allowed to expand the node. If expansion was not triggered by a user event, but by the controllers
// then always allow it...
- (BOOL) outlineView:(NSOutlineView*)inOutlineView shouldExpandItem:(id)inItem
{
BOOL shouldExpand = YES;
if (!_isRestoringState)
{
id delegate = self.libraryController.delegate;
if ([delegate respondsToSelector:@selector(libraryController:shouldPopulateNode:)])
{
IMBNode* node = [inItem representedObject];
shouldExpand = [delegate libraryController:self.libraryController shouldPopulateNode:node];
}
}
return shouldExpand;
}
// Expanding was allowed, so instruct the library controller to add subnodes to the node if necessary...
- (void) outlineViewItemWillExpand:(NSNotification*)inNotification
{
id item = [[inNotification userInfo] objectForKey:@"NSObject"];
IMBNode* node = [item representedObject];
[self.libraryController populateNode:node];
}
// When nodes were expanded or collapsed, then store the current state of the user interface. Also cancel any
// pending populate operation for the nodes that were just collapsed...
- (void) _setExpandedNodeIdentifiers
{
if (!_isRestoringState && !self.libraryController.isReplacingNode)
{
self.expandedNodeIdentifiers = [self _expandedNodeIdentifiers];
}
}
- (void) outlineViewItemDidExpand:(NSNotification*)inNotification
{
[self _setExpandedNodeIdentifiers];
}
- (void) outlineViewItemDidCollapse:(NSNotification*)inNotification
{
[self _setExpandedNodeIdentifiers];
id item = [[inNotification userInfo] objectForKey:@"NSObject"];
IMBNode* node = [item representedObject];
[self.libraryController stopPopulatingNodeWithIdentifier:node.identifier];
}
//----------------------------------------------------------------------------------------------------------------------
// Ask the library delegate if we may change the selection.
- (BOOL) outlineView:(NSOutlineView*)inOutlineView shouldSelectItem:(id)inItem
{
BOOL shouldSelect = YES;
if (!_isRestoringState)
{
IMBNode* node = [inItem representedObject];
id delegate = self.libraryController.delegate;
if ([delegate respondsToSelector:@selector(libraryController:shouldPopulateNode:)])
{
shouldSelect = [delegate libraryController:self.libraryController shouldPopulateNode:node];
}
if (node.isGroup)
{
shouldSelect = NO;
}
}
return shouldSelect;
}
// If the selection just changed due to a direct user event (clicking), then instruct the library controller
// to populate the node (if necessary) and remember the identifier of the selected node...
- (void) outlineViewSelectionDidChange:(NSNotification*)inNotification;
{
if (!_isRestoringState && !self.libraryController.isReplacingNode)
{
NSInteger row = [ibNodeOutlineView selectedRow];
id item = row>=0 ? [ibNodeOutlineView itemAtRow:row] : nil;
IMBNode* newNode = [item representedObject];
// Stop loading the old node's contents, assuming we don't need them anymore. Instead populated the
// newly selected node...
[self.libraryController stopPopulatingNodeWithIdentifier:self.selectedNodeIdentifier];
if (newNode)
{
[self.libraryController populateNode:newNode];
self.selectedNodeIdentifier = newNode.identifier;
}
// If the node has a custom object view, then install it now...
[self installObjectViewForNode:newNode];
// If a completely different parser was selected, then notify the previous parser, that it is most
// likely no longer needed any can get rid of its cached data...
if (self.selectedParser != newNode.parser)
{
[self.selectedParser didStopUsingParser];
self.selectedParser = newNode.parser;
}
}
// Sync the selection of the popup menu...
[self __syncPopupMenuSelection];
}
//----------------------------------------------------------------------------------------------------------------------
// Note: According to WWDC Session 110, this is called a LOT so it's not good for delayed loading...
- (void) outlineView:(NSOutlineView*)inOutlineView willDisplayCell:(NSCell*)inCell forTableColumn:(NSTableColumn*)inTableColumn item:(id)inItem
{
IMBNode* node = [inItem representedObject];
IMBNodeCell* cell = (IMBNodeCell*)inCell;
[cell setImage:node.icon];
[cell setBadgeType:node.badgeTypeNormal];
if ([node respondsToSelector:@selector(license)])
{
IMBFlickrNodeLicense license = [((IMBFlickrNode *)node) license];
if (license < IMBFlickrNodeLicense_Undefined) license = IMBFlickrNodeLicense_Undefined;
if (license > IMBFlickrNodeLicense_CommercialUse) license = IMBFlickrNodeLicense_CommercialUse;
// These are file names. Ideally we should put localized AX Descriptions on them.
NSArray *names = [NSArray arrayWithObjects:@"any", @"CC", @"remix", @"commercial", nil];
NSString *fileName = [names objectAtIndex:license];
if (license)
{
NSImage *image = [[[NSImage alloc] initByReferencingFile:[IMBBundle() pathForResource:fileName ofType:@"pdf"]] autorelease];
[image setScalesWhenResized:YES];
[image setSize:NSMakeSize(16.0,16.0)];
[cell setExtraImage:image];
}
else
{
[cell setExtraImage:nil];
}
}
else
{
[cell setExtraImage:nil];
}
}
//----------------------------------------------------------------------------------------------------------------------
-(BOOL) outlineView:(NSOutlineView*)inOutlineView isGroupItem:(id)inItem
{
IMBNode* node = [inItem representedObject];
return node.isGroup;
}
#pragma mark NSOutlineViewDataSource
//----------------------------------------------------------------------------------------------------------------------
// Check if we have any folder paths in the dragging pasteboard...
- (NSDragOperation) outlineView:(NSOutlineView*)inOutlineView validateDrop:(id<NSDraggingInfo>)inInfo proposedItem:(id)inItem proposedChildIndex:(NSInteger)inIndex
{
NSArray* paths = [[inInfo draggingPasteboard] propertyListForType:NSFilenamesPboardType];
BOOL exists,directory;
for (NSString* path in paths)
{
exists = [[NSFileManager imb_threadSafeManager] fileExistsAtPath:path isDirectory:&directory];
if (exists && directory)
{
[inOutlineView setDropItem:nil dropChildIndex:NSOutlineViewDropOnItemIndex]; // Target the whole view
return NSDragOperationCopy;
}
}
return NSDragOperationNone;
}
// For each folder path that was dropped onto the outline view create a new custom parser. Then reload the library...
- (BOOL) outlineView:(NSOutlineView*)inOutlineView acceptDrop:(id<NSDraggingInfo>)inInfo item:(id)inItem childIndex:(NSInteger)inIndex
{
BOOL result = NO;
NSArray* paths = [[inInfo draggingPasteboard] propertyListForType:NSFilenamesPboardType];
BOOL exists,directory;
for (NSString* path in paths)
{
exists = [[NSFileManager imb_threadSafeManager] fileExistsAtPath:path isDirectory:&directory];
if (exists && directory)
{
if (![IMBConfig isLibraryPath:path])
{
IMBParser* parser = [self.libraryController addCustomRootNodeForFolder:path];
self.selectedNodeIdentifier = [parser identifierForPath:path];
result = YES;
}
}
}
[inOutlineView.window makeFirstResponder:inOutlineView];
[self.libraryController reload];
return result;
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark Saving & Restoring State
// Get the identifiers of all currently expanded nodes. The result is a flat array, which is needed in the method
// _restoreUserInterfaceState to try to restore the state of the user interface...
- (NSMutableArray*) _expandedNodeIdentifiers
{
NSMutableArray* expandedNodeIdentifiers = [NSMutableArray array];
NSInteger n = [ibNodeOutlineView numberOfRows];
for (NSInteger i=0; i<n; i++)
{
id item = [ibNodeOutlineView itemAtRow:i];
if ([ibNodeOutlineView isItemExpanded:item])
{
IMBNode* node = [item representedObject];
[expandedNodeIdentifiers addObject:node.identifier];
}
}
return expandedNodeIdentifiers;
}
// Get IMBNode at specified table row...
- (IMBNode*) _nodeAtRow:(NSInteger)inRow
{
id item = [ibNodeOutlineView itemAtRow:inRow];
IMBNode* node = [item representedObject];
return node;
}
//----------------------------------------------------------------------------------------------------------------------
// Called in response to a IMBNodesWillChangeNotification notification. Set a flag that helps us to do the right
// thing in the IMBOutlineView delegate methods. If nodes are currently being replaced, then we will allow any
// changes, because those changes were not initiated by user events...
// And it will allow us to save any state information that might be unintentionally changed through the
// replacements of nodes (remember that the libary controller has a binding with the outline view)
- (void) _nodesWillChange
{
// Due to an error in NSTreeController a node to be replaced by another node will not be released
// by NSTreeController if it was currently selected. As a workaround we can safely unselect it here
// before nodes are exchanged by the library controller because the node identifier
// of the currently selected node is saved in _selectedNodeIdentifiers anyways.
[ibNodeTreeController setSelectionIndexPath:nil];
// Since the replacing of nodes in the library controller will lead to side effects
// regarding the current scroll position of the outline view we have to save it here
// for later restauration
_nodeOutlineViewSavedVisibleRectOrigin = [ibNodeOutlineView visibleRect].origin;
}
//----------------------------------------------------------------------------------------------------------------------
// Called in response to a IMBNodesDidChangeNotification notification. Restore expanded state from the saved info.
// We now have new node instances, but we can use the identifiers to locate the correct ones. First expand nodes
// as needed, then select the correct node...
- (void) _nodesDidChange
{
NSInteger i,rows,count;
IMBNode* node;
NSString* identifier;
// Update the internal data structures...
[ibNodeOutlineView reloadData];
// Temporarily disable storing of saved state (we only want that when the user actuall clicks in the UI...
_isRestoringState = YES;
// Restore the expanded nodes...
NSMutableArray* expandedNodeIdentifiers = [NSMutableArray arrayWithArray:self.expandedNodeIdentifiers];
while ((count = [expandedNodeIdentifiers count]))
{
rows = [ibNodeOutlineView numberOfRows];
for (i=0; i<rows; i++)
{
node = [self _nodeAtRow:i];
identifier = node.identifier;
if ([expandedNodeIdentifiers indexOfObject:identifier] != NSNotFound)
{
[ibNodeOutlineView expandItem:[ibNodeOutlineView itemAtRow:i]];
[expandedNodeIdentifiers removeObject:identifier];
break;
}
}
if ([expandedNodeIdentifiers count] == count)
{
break;
}
}
// Restore the selected node. Walk through all visible nodes. If we find the correct one then select it.
// Please note the special case where we do not have a selection yet. In this case we use the first available
// node (that is not a group) to make sure that the user sees something immediately...
NSString* selectedNodeIdentifier = self.selectedNodeIdentifier;
rows = [ibNodeOutlineView numberOfRows];
BOOL found = NO;
for (i=0; i<rows; i++)
{
node = [self _nodeAtRow:i];
identifier = node.identifier;
if (selectedNodeIdentifier == nil && node.isGroup == NO)
{
selectedNodeIdentifier = identifier;
}
if ([identifier isEqualToString:selectedNodeIdentifier])
{
[self selectNode:node];
found = YES;
break;
}
}
if (!found)
{
[self selectNode:nil];
[self installObjectViewForNode:nil];
}
// Rebuild the popup menu manually. Please note that the popup menu does not currently use bindings...
[self __updatePopupMenu];
[self __syncPopupMenuSelection];
// Restore scroll position of outline view (see _nodesWillChange for further explanation)
[ibNodeOutlineView scrollPoint:_nodeOutlineViewSavedVisibleRectOrigin];
// We are done, now the user is once again in charge...
_isRestoringState = NO;
}
//----------------------------------------------------------------------------------------------------------------------
// Selecting a node requires two parts. First the node needs to be selected in the NSTreeController. This will
// be directly reflected in the selection of the NSOutlineView and the NSPopUpButton. The second part is that
// a previously empty nodes needs to be populated by the libraryController...
- (void) selectNode:(IMBNode*)inNode
{
if (inNode)
{
if (inNode.isGroup)
{
[ibNodeTreeController setSelectionIndexPaths:nil];
}
else
{
NSIndexPath* indexPath = inNode.indexPath;
[ibNodeTreeController setSelectionIndexPath:indexPath];
// Not redundant! Needed if selection doesn't change due to previous line!
[self.libraryController populateNode:inNode];
// [self installCustomObjectView:[inNode customObjectView]];
[self installObjectViewForNode:inNode];
}
}
else
{
[ibNodeTreeController setSelectionIndexPaths:nil];
}
}
// Return the selected node. Here we assume that the NSTreeController was configured to only allow single
// selection or no selection...
- (IMBNode*) selectedNode
{
NSArray* selectedNodes = [ibNodeTreeController selectedObjects];
if ([selectedNodes count] > 0)
{
return [selectedNodes objectAtIndex:0];
}
return nil;
}
//----------------------------------------------------------------------------------------------------------------------
- (void) expandSelectedNode
{
// Expand the selected node...
NSInteger rows = [ibNodeOutlineView numberOfRows];
for (NSInteger i=0; i<rows; i++)
{
if ([ibNodeOutlineView isRowSelected:i])
{
id item = [ibNodeOutlineView itemAtRow:i];
[ibNodeOutlineView expandItem:item];
[self _setExpandedNodeIdentifiers];
break;
}
}
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark Popup Menu
- (void) observeValueForKeyPath:(NSString*)inKeyPath ofObject:(id)inObject change:(NSDictionary*)inChange context:(void*)inContext
{
if (inContext == (void*)kArrangedObjectsKey)
{
// [self __updatePopupMenu];
}
else if (inContext == (void*)kSelectionKey)
{
[self __syncPopupMenuSelection];
}
else
{
[super observeValueForKeyPath:inKeyPath ofObject:inObject change:inChange context:inContext];
}
}