-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathControllerBrowsing.m
491 lines (368 loc) · 15.4 KB
/
ControllerBrowsing.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
/*
File: ControllerBrowsing.m
Abstract: Implementation of the data source object (MyImageObject) which
represents one item in the browser. Contains code to implement the
following protocols:
- IKImageBrowserDataSource informal protocol, which declares the
methods that an instance of IKImageBrowserView uses to access
the contents of its data source object.
- IKImageBrowserItem informal protocol, which declares the
methods that an instance of IKImageBrowserView uses to access
the contents of its data source for a given item.
Version: 1.1
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Computer, Inc. ("Apple") in consideration of your agreement to the
following terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of these
terms. If you do not agree with these terms, please do not use,
install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Computer,
Inc. may be used to endorse or promote products derived from the Apple
Software without specific prior written permission from Apple. Except
as expressly stated in this notice, no other rights or licenses, express
or implied, are granted by Apple herein, including but not limited to
any patent rights that may be infringed by your derivative works or by
other works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright © 2007-2009 Apple Inc., All Rights Reserved
*/
#import "Controller.h"
#import "ControllerBrowsing.h"
#import "ImageBrowserBAckgroundLayer.h"
/* a simple C function that open an NSOpenPanel and return an array of selected filepath */
static NSArray *openFiles()
{
NSOpenPanel *panel;
panel = [NSOpenPanel openPanel];
[panel setFloatingPanel:YES];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:YES];
int i = [panel runModalForTypes:nil];
if(i == NSOKButton){
return [panel filenames];
}
return nil;
}
/* Our datasource object : represents one item in the browser */
@interface MyImageObject : NSObject{
NSString *path;
}
@end
@implementation MyImageObject
- (void) dealloc
{
[path release];
[super dealloc];
}
- (void) setPath:(NSString *) aPath
{
if(path != aPath){
[path release];
path = [aPath retain];
}
}
#pragma mark -
#pragma mark item data source protocol
- (NSString *) imageRepresentationType
{
return IKImageBrowserPathRepresentationType;
}
- (id) imageRepresentation
{
return path;
}
- (NSString *) imageUID
{
return path;
}
- (id) imageTitle
{
return [path lastPathComponent];
}
@end
/* the controller */
@implementation Controller(Browsing)
#pragma mark -
#pragma mark import images from file system
/*
code that parse a repository and add all entries to our datasource array,
*/
- (void) addFileWithPath:(NSString *) path
{
[self willChangeValueForKey: @"totalNumberOfItems"];
MyImageObject *item;
NSString *filename = [path lastPathComponent];
NSString* ramDiskName = [ramDiskNameTextField stringValue];
// A shell script wrapped in AppleScript wrapped in Cocoa. Nasty, but cleaner than NSTask.
NSString *copyScript = [NSString stringWithFormat:@"do shell script \"cp \\\"%@\\\" \\\"/Volumes/RAMDisk%@/%@\\\"\"", path, ramDiskName, filename];
NSAppleScript *copyScriptAS = [[NSAppleScript alloc] initWithSource: copyScript];
NSAppleEventDescriptor* returnValue = [copyScriptAS executeAndReturnError:nil];
// Get the returned value as a string
NSString* returnText = [returnValue stringValue];
// A shell script wrapped in AppleScript wrapped in Cocoa. Nasty, but cleaner than NSTask.
NSString *openScript = [NSString stringWithFormat:@"do shell script \"open \\\"/Volumes/RAMDisk%@/%@\\\"\"", ramDiskName, filename];
NSAppleScript *openScriptAS = [[NSAppleScript alloc] initWithSource: openScript];
NSAppleEventDescriptor* openReturnValue = [openScriptAS executeAndReturnError:nil];
/* skip '.*' */
if([filename length] > 0)
{
char *ch = (char*) [filename UTF8String];
if(ch)
{
if(ch[0] == '.')
{
return;
}
}
}
item = [[MyImageObject alloc] init];
[item setPath:path];
[images addObject:item];
[item release];
[self didChangeValueForKey: @"totalNumberOfItems"];
}
- (void) addFilesFromDirectory:(NSString *) path
{
int i, n;
BOOL dir;
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&dir];
if(dir){
NSArray *content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
n = [content count];
for(i=0; i<n; i++)
[self addFileWithPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]]];
}
else
{
[self addFileWithPath:path];
}
[imageBrowser reloadData];
}
#pragma mark -
#pragma mark setupBrowsing
- (void) setupBrowsing
{
//allocate our datasource array: will contain instances of MyImageObject
images = [[NSMutableArray alloc] init];
//-- add two directory to the datasource at launch
// [self addFilesFromDirectory:@"/Library/Desktop Pictures/Nature/"];
// [self addFilesFromDirectory:@"/Library/Desktop Pictures/"];
//-- initial coordinate set in IB is wrong. reset it to [0;0]
[imageBrowser setFrameOrigin:NSZeroPoint];
//-- change the appearance of the view
[imageBrowser setCellsStyleMask:IKCellsStyleTitled | IKCellsStyleOutlined];
//-- add a backgrund layer
// ImageBrowserBackgroundLayer *backgroundLayer = [[[ImageBrowserBackgroundLayer alloc] init] autorelease];
// [imageBrowser setBackgroundLayer:backgroundLayer];
// backgroundLayer.owner = imageBrowser;
//-- change default font
// create a centered paragraph style
NSMutableParagraphStyle *paraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[paraphStyle setAlignment:NSCenterTextAlignment];
NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
[attributes setObject:[NSFont systemFontOfSize:12] forKey:NSFontAttributeName];
[attributes setObject:paraphStyle forKey:NSParagraphStyleAttributeName];
[attributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
[imageBrowser setValue:attributes forKey:IKImageBrowserCellsTitleAttributesKey];
attributes = [[[NSMutableDictionary alloc] init] autorelease];
[attributes setObject:[NSFont boldSystemFontOfSize:12] forKey:NSFontAttributeName];
[attributes setObject:paraphStyle forKey:NSParagraphStyleAttributeName];
[attributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
[imageBrowser setValue:attributes forKey:IKImageBrowserCellsHighlightedTitleAttributesKey];
//change selection color
[imageBrowser setValue:[NSColor colorWithCalibratedRed:0 green:0.2 blue:0.5 alpha:1.0] forKey:IKImageBrowserSelectionColorKey];
}
#pragma mark -
#pragma mark actions
/* "add" button was clicked */
- (IBAction) addFileButtonClicked:(id) sender
{
NSArray *path = openFiles();
if(!path){
NSLog(@"No path selected, return...");
return;
}
int i, n;
n = [path count];
for(i=0; i<n; i++)
[self addFilesFromDirectory:[path objectAtIndex:i]];
}
- (IBAction) zoomSliderDidChange:(id)sender
{
[imageBrowser setZoomValue:[sender floatValue]];
}
#pragma mark -
#pragma mark drag'n drop
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
return [self draggingUpdated:sender];
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
if ([sender draggingSource] == imageBrowser)
return NSDragOperationMove;
return NSDragOperationCopy;
}
- (BOOL) performDragOperation:(id <NSDraggingInfo>)sender
{
[self willChangeValueForKey: @"totalNumberOfItems"];
NSData *data = nil;
NSString *errorDescription;
// if we are dragging from the browser itself, ignore it
if ([sender draggingSource] == imageBrowser)
return NO;
NSPasteboard *pasteboard = [sender draggingPasteboard];
if ([[pasteboard types] containsObject:NSFilenamesPboardType])
data = [pasteboard dataForType:NSFilenamesPboardType];
if(data){
NSArray *filenames = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:kCFPropertyListImmutable format:nil errorDescription:&errorDescription];
int i, n;
n = [filenames count];
for(i=0; i<n; i++){
MyImageObject *item = [[MyImageObject alloc] init];
NSString* path = [filenames objectAtIndex:i];
[item setPath:path];
NSString *filename = [path lastPathComponent];
NSString* ramDiskName = [ramDiskNameTextField stringValue];
// A shell script wrapped in AppleScript wrapped in Cocoa. Nasty, but cleaner than NSTask.
NSString *copyScript = [NSString stringWithFormat:@"do shell script \"cp \\\"%@\\\" \\\"/Volumes/RAMDisk%@/%@\\\"\"", path, ramDiskName, filename];
NSAppleScript *copyScriptAS = [[NSAppleScript alloc] initWithSource: copyScript];
NSAppleEventDescriptor* returnValue = [copyScriptAS executeAndReturnError:nil];
// Get the returned value as a string
NSString* returnText = [returnValue stringValue];
// A shell script wrapped in AppleScript wrapped in Cocoa. Nasty, but cleaner than NSTask.
NSString *openScript = [NSString stringWithFormat:@"do shell script \"open \\\"/Volumes/RAMDisk%@/%@\\\"\"", ramDiskName, filename];
NSAppleScript *openScriptAS = [[NSAppleScript alloc] initWithSource: openScript];
NSAppleEventDescriptor* openReturnValue = [openScriptAS executeAndReturnError:nil];
[images insertObject:item atIndex:[imageBrowser indexAtLocationOfDroppedItem]];
[item release];
}
[imageBrowser reloadData];
}
[self didChangeValueForKey: @"totalNumberOfItems"];
return YES;
}
#pragma mark -
#pragma mark IKImageBrowserDataSource
/* implement image-browser's datasource protocol
Our datasource representation is a simple mutable array
*/
- (NSUInteger) numberOfItemsInImageBrowser:(IKImageBrowserView *) view
{
return [images count];
}
- (id) imageBrowser:(IKImageBrowserView *) view itemAtIndex:(NSUInteger) index
{
return [images objectAtIndex:index];
}
#pragma mark -
#pragma mark optional datasource methods : reordering / removing
- (void) imageBrowser:(IKImageBrowserView *) aBrowser removeItemsAtIndexes: (NSIndexSet *) indexes
{
[self willChangeValueForKey: @"totalNumberOfItems"];
[images removeObjectsAtIndexes:indexes];
[imageBrowser reloadData];
[self didChangeValueForKey: @"totalNumberOfItems"];
}
- (BOOL) imageBrowser:(IKImageBrowserView *) aBrowser moveItemsAtIndexes: (NSIndexSet *)indexes toIndex:(unsigned int)destinationIndex
{
NSArray *tempArray = [images objectsAtIndexes:indexes];
[images removeObjectsAtIndexes:indexes];
destinationIndex -= [indexes countOfIndexesInRange:NSMakeRange(0, destinationIndex)];
[images insertObjects:tempArray atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(destinationIndex, [tempArray count])]];
[imageBrowser reloadData];
return YES;
}
#pragma mark -
#pragma mark misc
- (void)setNumberOfDisplayedItems: (int)value{}
- (void)setTotalNumberOfItems: (int)value{}
- (void)setHasFilteredItems: (BOOL)value{}
- (int) numberOfDisplayedItems
{
return [images count];
}
- (int) totalNumberOfItems
{
return [filteredOutImages count] + [images count];
}
- (BOOL)hasFilteredItems
{
return ([filteredOutImages count] > 0);
}
#pragma mark -
#pragma mark search
/*
this code filters the "images" array depending on the current search field value. All items that are filtered-out are kept in the
"filteredOutImages" array (and corresponding indexes are kept in "filteredOutIndexes" in order to restore these indexes when the search field is cleared
*/
- (BOOL) keyword:(NSString *) aKeyword matchSearch:(NSString *) search
{
NSRange r = [aKeyword rangeOfString:search options:NSCaseInsensitiveSearch];
return (r.length>0 && r.location>=0);
}
- (IBAction) searchFieldChanged:(id) sender
{
[self willChangeValueForKey: @"numberOfDisplayedItems"];
[self willChangeValueForKey: @"hasFilteredItems"];
if(filteredOutImages == nil){
//first time we use the search field
filteredOutImages = [[NSMutableArray alloc] init];
filteredOutIndexes = [[NSMutableIndexSet alloc] init];
}
else{
//restore the original datasource, and restore the initial ordering if possible
NSUInteger lastIndex = [filteredOutIndexes lastIndex];
if(lastIndex >= [images count] + [filteredOutImages count]){
//can't restore previous indexes, just insert filtered items at the beginning
[images insertObjects:filteredOutImages atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [filteredOutImages count])]];
}
else
[images insertObjects:filteredOutImages atIndexes:filteredOutIndexes];
[filteredOutImages removeAllObjects];
[filteredOutIndexes removeAllIndexes];
}
//add filtered images to the filteredOut array
NSString *searchString = [sender stringValue];
if(searchString != nil && [searchString length] > 0){
int i, n;
n = [images count];
for(i=0; i<n; i++){
MyImageObject *anItem = [images objectAtIndex:i];
if([self keyword:[anItem imageTitle] matchSearch:searchString] == NO){
[filteredOutImages addObject:anItem];
[filteredOutIndexes addIndex:i];
}
}
}
//remove filtered-out images from the datasource array
[images removeObjectsInArray:filteredOutImages];
//reflect changes in the browser
[imageBrowser reloadData];
if ([images count] > 0)
[imageBrowser setSelectionIndexes: [NSIndexSet indexSetWithIndex: 0] byExtendingSelection:NO];
[self didChangeValueForKey: @"numberOfDisplayedItems"];
[self didChangeValueForKey: @"hasFilteredItems"];
}
@end