Guest User

Untitled

a guest
May 22nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. /*
  2. * AppController.j
  3. * imdbdemo
  4. *
  5. * Created by Randy Luecke on July 2, 2010.
  6. * Copyright 2010, RCLConcepts, LLC All rights reserved.
  7. */
  8.  
  9. @import <Foundation/CPObject.j>
  10. @import <AppKit/CPScrollView.j>
  11. @import <AppKit/CPTableView.j>
  12. @import <AppKit/CPTableColumn.j>
  13.  
  14. @implementation AppController : CPObject
  15. {
  16. CPDictionary data;
  17. }
  18. - (void)applicationDidFinishLaunching:(CPNotification)aNotification
  19. {
  20. // convert the data to make it easier to deal with
  21. data = [ ];
  22. // loop through everything and create a dictionary in place of the JSObject adding it to the array
  23. for (var i = 0; i < window.DATA.length; i++)
  24. data[i] = [CPDictionary dictionaryWithJSObject:window.DATA[i] recursively:NO];
  25.  
  26. var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
  27. contentView = [theWindow contentView],
  28. bounds = [contentView bounds];
  29.  
  30. [contentView setBackgroundColor:[CPColor colorWithHexString:"d7d6d8"]];
  31.  
  32. var label = [[CPTextField alloc] initWithFrame:CGRectMake(15, 15, 0,0)];
  33. [label setStringValue:"IMDB Ratings"];
  34. [label setFont:[CPFont boldSystemFontOfSize:24]];
  35. [label setTextColor:[CPColor whiteColor]];
  36. [label setTextShadowColor:[CPColor blackColor]];
  37. [label setTextShadowOffset:CGSizeMake(1,1)];
  38. [label sizeToFit];
  39. [contentView addSubview:label];
  40.  
  41. // give the scrollview a 15px margin on all sides to conform with the HIG
  42. var start = CGRectGetMaxY([label frame]) + 15,
  43. scroll = [[CPScrollView alloc] initWithFrame:CGRectMake(0, start, bounds.size.width, bounds.size.height - start)],
  44. table = [[CPTableView alloc] initWithFrame:CGRectMakeZero()];
  45.  
  46. // the delegate is notified about behavior stuff
  47. [table setDelegate:self];
  48.  
  49. // the data source gives the tableview all it's data
  50. [table setDataSource:self];
  51.  
  52. [table setAllowsMultipleSelection:YES];
  53. [table setUsesAlternatingRowBackgroundColors:YES];
  54.  
  55. // in order to use Drag and Drop we register for items of the type "myItems"
  56. [table registerForDraggedTypes:["myItems"]];
  57.  
  58. // We add columns like so
  59. var column = [[CPTableColumn alloc] initWithIdentifier:"title"],
  60. descriptor = [CPSortDescriptor sortDescriptorWithKey:"title" ascending:NO]; // this is used for sorting
  61. [[column headerView] setStringValue:"Title"];
  62. [column setWidth:555.0]; // you can also set a min/max width if you want
  63.  
  64. // apply the sort descriptor to this column so it knows how to sort.
  65. [column setSortDescriptorPrototype:descriptor];
  66.  
  67. // finally add it to the table
  68. [table addTableColumn:column];
  69.  
  70.  
  71. // we do this two more times
  72. var column2 = [[CPTableColumn alloc] initWithIdentifier:"rating"],
  73. descriptor = [CPSortDescriptor sortDescriptorWithKey:"rating" ascending:NO];
  74. [[column2 headerView] setStringValue:"Rating"];
  75. [column2 setWidth:150.0];
  76. [column2 setSortDescriptorPrototype:descriptor];
  77. [table addTableColumn:column2];
  78.  
  79. var column3 = [[CPTableColumn alloc] initWithIdentifier:"votes"],
  80. descriptor = [CPSortDescriptor sortDescriptorWithKey:"votes" ascending:NO];
  81. [[column3 headerView] setStringValue:"Votes"];
  82. [column3 setWidth:150.0];
  83. [column3 setSortDescriptorPrototype:descriptor];
  84. [table addTableColumn:column3];
  85.  
  86. // when then tell the scrollview that it needs to scroll the tableview
  87. [scroll setDocumentView:table];
  88. // and resize so that it fills the screen always
  89. [scroll setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
  90. // and who wants scroll bars when they're not needed?
  91. [scroll setAutohidesScrollers:YES];
  92.  
  93. // finally add the scrollview to the window's contentview
  94. [contentView addSubview:scroll];
  95.  
  96.  
  97. // bring the window to the front.
  98. [theWindow orderFront:self];
  99. }
  100.  
  101.  
  102. // since the app controller is the data source we must implement a couple of required data source methods
  103.  
  104. // first the data source is asked how many rows the tableview contains
  105. - (int)numberOfRowsInTableView:(CPTableView)atableView
  106. {
  107. return [data count];
  108. }
  109.  
  110.  
  111. // Then the data source needs to supply the data to the tableview.
  112. - (id)tableView:(CPTableView)aTableView objectValueForTableColumn:(CPTableColumn)aColumn row:(int)aRow
  113. {
  114. // the key is the identifier we assigned to the column
  115. var key = [aColumn identifier];
  116.  
  117. // the object at our specific row is what we want, but we want a specific value from that dictionary.
  118. return [data[aRow] objectForKey:key];
  119. }
  120.  
  121. // when the sort descriptor changes we need to sort our data
  122. - (void)tableView:(CPTableView)aTableView sortDescriptorsDidChange:(CPArray)oldDescriptors
  123. {
  124. // first we figure out how we're suppose to sort, then sort the data array
  125. var newDescriptors = [aTableView sortDescriptors];
  126. [data sortUsingDescriptors:newDescriptors];
  127.  
  128. // the reload the table data
  129. [aTableView reloadData];
  130. }
  131.  
  132. // when you drag a row (or group of rows) the datasource writes the values to the pasteboard and returns wheter or not to allow the drag.
  133. - (BOOL)tableView:(CPTableView)aTableView writeRowsWithIndexes:(CPIndexSet)rowIndexes toPasteboard:(CPPasteboard)pboard
  134. {
  135. // using the keyed archiver we archive the data; in this case the row indexes we're dragging
  136. var encodedData = [CPKeyedArchiver archivedDataWithRootObject:rowIndexes];
  137. // we then declare the pasteboard types
  138. [pboard declareTypes:["myItems"] owner:self];
  139. // and then set the data to the pasteboard
  140. [pboard setData:encodedData forType:"myItems"];
  141.  
  142. // and return YES to allow the drag.
  143. return YES;
  144. }
  145.  
  146. // as you drag the tableview needs to validate the drop
  147. - (CPDragOperation)tableView:(CPTableView)aTableView validateDrop:(id)info proposedRow:(CPInteger)row proposedDropOperation:(CPTableViewDropOperation)operation
  148. {
  149. // with this method you can set whether you're dropping ABOVE or ON (as in on top of) the row of your choosing.
  150. [aTableView setDropRow:row dropOperation:CPTableViewDropAbove];
  151.  
  152. // you then return the drag operation (move, copy, none, etc)
  153. return CPDragOperationMove;
  154. }
  155.  
  156. // when you finally drop on the tableview this method is called
  157. - (BOOL)tableView:(CPTableView)aTableView acceptDrop:(id)info row:(int)row dropOperation:(CPTableViewDropOperation)operation
  158. {
  159. // get the data you added to your pasteboard
  160. var pboard = [info draggingPasteboard],
  161. rowData = [pboard dataForType:"myItems"];
  162.  
  163. rowData = [CPKeyedUnarchiver unarchiveObjectWithData:rowData];
  164.  
  165. // we're using the method we added via a category below to move items around the array
  166. [data moveIndexes:rowData toIndex:row];
  167.  
  168. // if the number of rows were to change (i.e. you dropped items onto the tablview from someowhere else, rather than reordering them)
  169. // then you would want to call reloadData in order to select the correct indexes in the following methods
  170. //[aTableView reloadData];
  171.  
  172. // then we want to select the row indexes we just dropped
  173. var destIndexes = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(row, [rowData count])];
  174. [aTableView selectRowIndexes:destIndexes byExtendingSelection:NO];
  175.  
  176. // returning YES tells the tableview to reload the data and completes the drop.
  177. return YES;
  178. }
  179.  
  180. @end
  181.  
  182. // a simple helper extention to CPArray to make it easier to move indexes around.
  183. @implementation CPArray (MoveIndexes)
  184.  
  185. - (void)moveIndexes:(CPIndexSet)indexes toIndex:(int)insertIndex
  186. {
  187. var aboveCount = 0,
  188. object,
  189. removeIndex;
  190.  
  191. var index = [indexes lastIndex];
  192.  
  193. while (index != CPNotFound)
  194. {
  195. if (index >= insertIndex)
  196. {
  197. removeIndex = index + aboveCount;
  198. aboveCount ++;
  199. }
  200. else
  201. {
  202. removeIndex = index;
  203. insertIndex --;
  204. }
  205.  
  206. object = [self objectAtIndex:removeIndex];
  207. [self removeObjectAtIndex:removeIndex];
  208. [self insertObject:object atIndex:insertIndex];
  209.  
  210. index = [indexes indexLessThanIndex:index];
  211. }
  212. }
  213.  
  214. @end
Add Comment
Please, Sign In to add comment