/Tests/AppKit/WithAndWithoutBindingsIntegration/01_WithoutBindings/MyDocument.j

http://github.com/cacaodev/cappuccino · Unknown · 178 lines · 140 code · 38 blank · 0 comment · 0 complexity · 6337ac786e3ad9b41b884e58faba80cd MD5 · raw file

  1. /*
  2. * MyDocument.j
  3. * AppKit Tests
  4. *
  5. * Created by Alexander Ljungberg.
  6. * Copyright 2010, WireLoad, LLC.
  7. *
  8. * Adapted from MyDocument.m in WithAndWithoutBindings by Apple Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. @import "Bookmark.j"
  25. @implementation MyDocument : CPDocument
  26. {
  27. CPString name @accessors;
  28. CPString collectionDescription @accessors;
  29. CPArray collection @accessors;
  30. @outlet CPTableView tableView;
  31. @outlet CPTextField nameField;
  32. @outlet CPTextField selectedBookmarkTitleField;
  33. @outlet CPTextField selectedBookmarkURLField;
  34. }
  35. - (id)init
  36. {
  37. if (self = [super init])
  38. {
  39. collection = [CPArray array];
  40. }
  41. return self;
  42. }
  43. - (void)windowControllerDidLoadCib:(CPWindowController)aController
  44. {
  45. [super windowControllerDidLoadCib:aController];
  46. [self updateSelectionDetailFields];
  47. }
  48. - (@action)nameFieldChanged:(id)sender
  49. {
  50. [self setName:[nameField stringValue]];
  51. }
  52. - (@action)selectedBookmarkTitleFieldChanged:(id)sender
  53. {
  54. var selectedRow = [tableView selectedRow];
  55. if (selectedRow === CPNotFound)
  56. return;
  57. var selectedBookmark = [collection objectAtIndex:selectedRow];
  58. [selectedBookmark setTitle:[selectedBookmarkTitleField stringValue]];
  59. [tableView reloadData];
  60. }
  61. - (@action)selectedBookmarkURLFieldChanged:(id)sender
  62. {
  63. var selectedRow = [tableView selectedRow];
  64. if (selectedRow === CPNotFound)
  65. return;
  66. var URLString = [selectedBookmarkURLField stringValue],
  67. URL = [CPURL URLWithString:URLString],
  68. selectedBookmark = [collection objectAtIndex:selectedRow];
  69. [selectedBookmark setURL:URL];
  70. [tableView reloadData];
  71. }
  72. - (void)updateSelectionDetailFields
  73. {
  74. var selectedRow = [tableView selectedRow];
  75. if (selectedRow === CPNotFound)
  76. {
  77. [selectedBookmarkTitleField setStringValue:@"No selection"];
  78. [selectedBookmarkTitleField setSelectable:NO];
  79. [selectedBookmarkURLField setStringValue:@"No selection"];
  80. [selectedBookmarkURLField setSelectable:NO];
  81. }
  82. else
  83. {
  84. var selectedBookmark = [collection objectAtIndex:selectedRow];
  85. [selectedBookmarkTitleField setStringValue:[selectedBookmark title]];
  86. [selectedBookmarkTitleField setEditable:YES];
  87. var URL = [selectedBookmark URL],
  88. URLString = @"No URL";
  89. if (URL)
  90. URLString = [URL absoluteString];
  91. [selectedBookmarkURLField setStringValue:URLString];
  92. [selectedBookmarkURLField setEditable:YES];
  93. }
  94. }
  95. - (@action)addBookmark:(id)sender
  96. {
  97. var newBookmark = [Bookmark new];
  98. [newBookmark setCreationDate:[CPDate date]];
  99. [collection addObject:newBookmark];
  100. [tableView reloadData];
  101. [self updateSelectionDetailFields];
  102. }
  103. - (@action)removeSelectedBookmarks:(id)sender
  104. {
  105. var selectedRows = [tableView selectedRowIndexes],
  106. currentIndex = [selectedRows lastIndex];
  107. while (currentIndex != CPNotFound)
  108. {
  109. [collection removeObjectAtIndex:currentIndex];
  110. currentIndex = [selectedRows indexLessThanIndex: currentIndex];
  111. }
  112. [tableView reloadData];
  113. [self updateSelectionDetailFields];
  114. }
  115. - (CPData)dataRepresentationOfType:(CPString)aType
  116. {
  117. var data = [CPData data],
  118. archiver = [[CPKeyedArchiver alloc] initForWritingWithMutableData:data];
  119. [archiver encodeObject:name forKey:@"name"];
  120. [archiver encodeObject:collectionDescription forKey:@"collectionDescription"];
  121. [archiver encodeObject:collection forKey:@"collection"];
  122. [archiver finishEncoding];
  123. return data;
  124. }
  125. - (BOOL)loadDataRepresentation:(CPData)data ofType:(CPString)aType
  126. {
  127. var unarchiver = [[CPKeyedUnarchiver alloc] initForReadingWithData:data];
  128. name = [unarchiver decodeObjectForKey:@"name"];
  129. collectionDescription = [unarchiver decodeObjectForKey:@"collectionDescription"];
  130. collection = [unarchiver decodeObjectForKey:@"collection"];
  131. [unarchiver finishDecoding];
  132. return YES;
  133. }
  134. - (void)setCollection:(CPArray)aCollection
  135. {
  136. if (collection !== aCollection)
  137. collection = [aCollection copy];
  138. }
  139. @end
  140. //@import "TableViewDataSource.j"