|
|
1.1 ! root 1: // SortOrderSetter.m ! 2: ! 3: #import <eointerface/eointerface.h> ! 4: #import "SortOrderSetter.h" ! 5: #import "TableMagic.h" ! 6: ! 7: @implementation SortOrderSetter ! 8: - (NSArray *)orderingArrayForEntity:(EOEntity *)entity keys:(NSArray *)keys ! 9: ordering:(EOOrdering)ordering ! 10: { ! 11: int count, i; ! 12: NSMutableArray *orderarray; ! 13: ! 14: count = [keys count]; ! 15: orderarray = [NSMutableArray arrayWithCapacity:count]; ! 16: ! 17: for(i=0; i<count; i++) { ! 18: EOAttribute *attr = [entity attributeNamed:[keys objectAtIndex:i]]; ! 19: if (attr) ! 20: [orderarray addObject: [EOAttributeOrdering attributeOrderingWithAttribute:attr ! 21: ordering:ordering]]; ! 22: } ! 23: return orderarray; ! 24: } ! 25: ! 26: - applyOrderingToDataSourceAndFetch:(EOOrdering)ordering ! 27: { ! 28: id tableView = [NXTableView keyTableView]; ! 29: EOController *controller = [tableView eoController]; ! 30: id dataSource = [controller dataSource]; ! 31: if (controller && [dataSource respondsToSelector:@selector(setFetchOrder:)]) { ! 32: NSArray *keys = [tableView associationKeysForSelectedColumns]; ! 33: if (!keys) // if none selected, sort by first ! 34: keys = [NSArray arrayWithObject:[tableView associationKeyForColumn:0]]; ! 35: [dataSource setFetchOrder:[self orderingArrayForEntity:[dataSource entity] keys:keys ordering:ordering]]; ! 36: ! 37: // nil out the fetch order on the controller (since it will override this one) ! 38: [controller setSortOrdering:nil]; ! 39: ! 40: [controller fetch:nil]; ! 41: return self; ! 42: } ! 43: ! 44: NXBeep(); ! 45: return nil; ! 46: } ! 47: ! 48: - sortDataSourceAscending:sender ! 49: { ! 50: return [self applyOrderingToDataSourceAndFetch:EOAscendingOrder]; ! 51: } ! 52: ! 53: - sortDataSourceDescending:sender ! 54: { ! 55: return [self applyOrderingToDataSourceAndFetch:EODescendingOrder]; ! 56: } ! 57: ! 58: - (NSArray *)keyOrderingForKeys:(NSArray *)keys ordering:(NSComparisonResult)ordering ! 59: { ! 60: int i, count = [keys count]; ! 61: NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; ! 62: for(i=0; i<count; i++) { ! 63: [result addObject:[EOKeySortOrdering keyOrderingWithKey:[keys objectAtIndex:i] ordering:ordering]]; ! 64: } ! 65: return result; ! 66: } ! 67: ! 68: - applyOrderingToControllerAndFetch:(NSComparisonResult)ordering ! 69: { ! 70: id tableView = [NXTableView keyTableView]; ! 71: EOController *controller = [tableView eoController]; ! 72: if (controller) { ! 73: NSArray *keys = [tableView associationKeysForSelectedColumns]; ! 74: if (!keys) // if none selected, sort by first ! 75: keys = [NSArray arrayWithObject:[tableView associationKeyForColumn:0]]; ! 76: [controller setSortOrdering:[self keyOrderingForKeys:keys ordering:ordering]]; ! 77: [controller resort]; ! 78: ! 79: if ([[controller dataSource] respondsToSelector:@selector(setFetchOrder:)]) { ! 80: // unset the fetch order on the dataSource ! 81: [(EODatabaseDataSource *)[controller dataSource] setFetchOrder:nil]; ! 82: } ! 83: return self; ! 84: } ! 85: ! 86: NXBeep(); ! 87: return nil; ! 88: ! 89: } ! 90: ! 91: - sortControllerAscending:sender ! 92: { ! 93: return [self applyOrderingToControllerAndFetch:NSOrderedAscending]; ! 94: } ! 95: ! 96: - sortControllerDescending:sender ! 97: { ! 98: return [self applyOrderingToControllerAndFetch:NSOrderedDescending]; ! 99: } ! 100: ! 101: ! 102: // switch old style methods to use controller sorting ! 103: - setOrderAscending:sender ! 104: { ! 105: return [self sortControllerAscending:sender]; ! 106: } ! 107: ! 108: - setOrderDescending:sender ! 109: { ! 110: return [self sortControllerDescending:sender]; ! 111: } ! 112: @end
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.