Top Banner
NSTableView + Cocoa Bindings + Core Data + Drag & Drop HMDT Makoto Kinoshita
23

NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Apr 22, 2018

Download

Documents

haque
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

NSTableView + Cocoa Bindings + Core Data +

Drag & Drop

HMDT Makoto Kinoshita

Page 2: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

NSTableView + Cocoa Bindings

NSArrayController NSMutableArray

bindingcontent

Page 3: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

NSTableView + Cocoa Bindings + Core Data

NSArrayController delegate

bindingmanagedObjectContext

Core Data

mutableSetForKey

NSMutableSet

Page 4: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Array or Set

NSArray has order

NSSet has NO ORDER

Table view uses order!

Page 5: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Not Ordered Data

Page 6: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Ordered Data

Page 7: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Drag And Drop

date source

writeRows:toPasteboard:

acceptDrop:row:dropOperation

paste board

Page 8: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

What We Need

Core Data model with ordering

Copy managed objects to a pasteboard

Page 9: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Example Model

Entity Name: MailAttributes: to (String) title (String) from (String)

Page 10: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Type 1. Add An Attribute For Ordering

New Attributes: order (Int32)

Page 11: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Sort By Order

NSSortDescriptor key: order ascending: YES selector: compare:

Page 12: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Dragging

date source

Core Data

NSArrayController

1. Write to pboard request

2. Get selected managed objects

paste board

3. Copy data for managed objects

Page 13: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

… and Dropping

date source

NSArrayController

6. Update order values

7. Refresh view

paste board

4. Read data

5. Get data from the persistent coordinator

Core Data

Page 14: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Get NSArrayController

- (BOOL)tableView:(NSTableView*)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard{ // Get array controller NSDictionary* bindingInfo; NSArrayController* arrayController; bindingInfo = [tableView infoForBinding:NSContentBinding]; arrayController = [bindingInfo valueForKey:NSObservedObjectKey];

Page 15: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Get Managed Objects Data // Get arranged objects, they are managed object NSArray* arrangedObjects; arrangedObjects = [arrayController arrangedObjects]; // Collect URI representation of managed objects NSMutableArray* objectURIs; NSEnumerator* enumerator; NSNumber* rowNumber; objectURIs = [NSMutableArray array]; enumerator = [rows objectEnumerator]; while (rowNumber = [enumerator nextObject]) { int row = [rowNumber intValue]; if (row >= [arrangedObjects count]) { continue; } // Get URI representation of managed object NSManagedObject* object; NSManagedObjectID* objectID; NSURL* objectURI; object = [arrangedObjects objectAtIndex:row]; objectID = [object objectID]; objectURI = [objectID URIRepresentation]; [objectURIs addObject:objectURI]; }

Page 16: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

URI Representation

x-coredata://2D1E267F-6430-4636-A7E8-444786806A35/Mail/p109

Page 17: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Get With URI Representation // Get managed object context and persistent store coordinator NSManagedObjectContext* context; NSPersistentStoreCoordinator* coordinator; context = [self managedObjectContext]; coordinator = [context persistentStoreCoordinator]; // Collect manged objects with URIs NSMutableArray* managedObjects; NSEnumerator* enumerator; NSURL* objectURI; managedObjects = [NSMutableArray array]; enumerator = [objectURIs objectEnumerator]; while (objectURI = [enumerator nextObject]) { // Get managed object NSManagedObjectID* objectID; NSManagedObject* object; objectID = [coordinator managedObjectIDForURIRepresentation:objectURI]; object = [context objectWithID:objectID]; if (!object) { continue; } [managedObjects addObject:object]; }

Page 18: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Re-order // Get managed objects NSMutableArray* objects; objects = [NSMutableArray arrayWithArray:[arrayController arrangedObjects]]; if (!objects || [objects count] == 0) { return NO; } // Replace dragged objects with null objects as placeholder enumerator = [draggedObjects objectEnumerator]; while (object = [enumerator nextObject]) { int index; index = [objects indexOfObject:object]; if (index == NSNotFound) { // Error NSLog(@"Not found dragged link in links"); continue; } [objects replaceObjectAtIndex:index withObject:[NSNull null]]; } // Insert dragged objects at row if (row < [objects count]) { [objects insertObjects:draggedObjects atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(row, [draggedObjects count])]]; } else { [objects addObjectsFromArray:draggedObjects]; } // Remove null objeccts [objects removeObject:[NSNull null]]; // Re-order objects int i; for (i = 0; i < [objects count]; i++) { object = [objects objectAtIndex:i]; [object setValue:[NSNumber numberWithInt:i] forKey:@"order"]; } // Reload data [arrayController rearrangeObjects];

Page 19: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Problem

Page 20: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Type 2. Add An Entity For Ordering

Page 21: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Solution

Page 22: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Mail Addition

- (void)add:(id)sender{ // Create and insert Mail NSManagedObject* mail; mail = [NSEntityDescription insertNewObjectForEntityForName:@"Mail" inManagedObjectContext:[self managedObjectContext]]; // Create and insert Order NSManagedObject* order; order = [NSEntityDescription insertNewObjectForEntityForName:@"Order" inManagedObjectContext:[self managedObjectContext]]; // Set order to mail [mail setValue:order forKey:@"order"];}

Page 23: NSTableView + Cocoa Bindings + Core Data + Drag & …hmdt.jp/archives/image/2005_11/DNDCoreData.pdfNSTableView + Cocoa Bindings + Core Data NSArrayController delegate binding managedObjectContext

Demo