PageRenderTime 29ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 25@import "Bookmark.j"
 26
 27@implementation MyDocument : CPDocument
 28{
 29    CPString            name @accessors;
 30    CPString            collectionDescription @accessors;
 31    CPArray             collection @accessors;
 32
 33    @outlet CPTableView tableView;
 34    @outlet CPTextField nameField;
 35
 36    @outlet CPTextField selectedBookmarkTitleField;
 37    @outlet CPTextField selectedBookmarkURLField;
 38}
 39
 40- (id)init
 41{
 42    if (self = [super init])
 43    {
 44        collection = [CPArray array];
 45    }
 46    return self;
 47}
 48
 49- (void)windowControllerDidLoadCib:(CPWindowController)aController
 50{
 51    [super windowControllerDidLoadCib:aController];
 52
 53    [self updateSelectionDetailFields];
 54}
 55
 56- (@action)nameFieldChanged:(id)sender
 57{
 58    [self setName:[nameField stringValue]];
 59}
 60
 61- (@action)selectedBookmarkTitleFieldChanged:(id)sender
 62{
 63    var selectedRow = [tableView selectedRow];
 64
 65    if (selectedRow === CPNotFound)
 66        return;
 67
 68    var selectedBookmark = [collection objectAtIndex:selectedRow];
 69    [selectedBookmark setTitle:[selectedBookmarkTitleField stringValue]];
 70    [tableView reloadData];
 71}
 72
 73- (@action)selectedBookmarkURLFieldChanged:(id)sender
 74{
 75    var selectedRow = [tableView selectedRow];
 76
 77    if (selectedRow === CPNotFound)
 78        return;
 79
 80    var URLString = [selectedBookmarkURLField stringValue],
 81        URL = [CPURL URLWithString:URLString],
 82        selectedBookmark = [collection objectAtIndex:selectedRow];
 83
 84    [selectedBookmark setURL:URL];
 85    [tableView reloadData];
 86}
 87
 88- (void)updateSelectionDetailFields
 89{
 90    var selectedRow = [tableView selectedRow];
 91
 92    if (selectedRow === CPNotFound)
 93    {
 94        [selectedBookmarkTitleField setStringValue:@"No selection"];
 95        [selectedBookmarkTitleField setSelectable:NO];
 96        [selectedBookmarkURLField setStringValue:@"No selection"];
 97        [selectedBookmarkURLField setSelectable:NO];
 98    }
 99    else
100    {
101        var selectedBookmark = [collection objectAtIndex:selectedRow];
102
103        [selectedBookmarkTitleField setStringValue:[selectedBookmark title]];
104        [selectedBookmarkTitleField setEditable:YES];
105
106        var URL = [selectedBookmark URL],
107            URLString = @"No URL";
108
109        if (URL)
110            URLString = [URL absoluteString];
111
112        [selectedBookmarkURLField setStringValue:URLString];
113        [selectedBookmarkURLField setEditable:YES];
114    }
115}
116
117- (@action)addBookmark:(id)sender
118{
119    var newBookmark = [Bookmark new];
120    [newBookmark setCreationDate:[CPDate date]];
121    [collection addObject:newBookmark];
122
123    [tableView reloadData];
124    [self updateSelectionDetailFields];
125}
126
127- (@action)removeSelectedBookmarks:(id)sender
128{
129    var selectedRows = [tableView selectedRowIndexes],
130        currentIndex = [selectedRows lastIndex];
131
132    while (currentIndex != CPNotFound)
133    {
134        [collection removeObjectAtIndex:currentIndex];
135        currentIndex = [selectedRows indexLessThanIndex: currentIndex];
136    }
137
138    [tableView reloadData];
139    [self updateSelectionDetailFields];
140}
141
142- (CPData)dataRepresentationOfType:(CPString)aType
143{
144    var data = [CPData data],
145        archiver = [[CPKeyedArchiver alloc] initForWritingWithMutableData:data];
146
147    [archiver encodeObject:name forKey:@"name"];
148    [archiver encodeObject:collectionDescription forKey:@"collectionDescription"];
149    [archiver encodeObject:collection forKey:@"collection"];
150
151    [archiver finishEncoding];
152
153    return data;
154}
155
156- (BOOL)loadDataRepresentation:(CPData)data ofType:(CPString)aType
157{
158    var unarchiver = [[CPKeyedUnarchiver alloc] initForReadingWithData:data];
159
160    name = [unarchiver decodeObjectForKey:@"name"];
161    collectionDescription = [unarchiver decodeObjectForKey:@"collectionDescription"];
162    collection = [unarchiver decodeObjectForKey:@"collection"];
163
164    [unarchiver finishDecoding];
165
166    return YES;
167}
168
169- (void)setCollection:(CPArray)aCollection
170{
171    if (collection !== aCollection)
172        collection = [aCollection copy];
173}
174
175@end
176
177//@import "TableViewDataSource.j"
178