API  0.9.10
CPDictionaryController.j
Go to the documentation of this file.
1 /*
2  * CPDictionaryController.j
3  * AppKit
4  *
5  * Adapted from Cocotron, by Johannes Fortmann
6  *
7  * Created by Blair Duncan
8  * Copyright 2013, SGL Studio, BBDO Toronto All rights reserved.
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 
27 {
28  CPDictionary _contentDictionary;
29 
30  CPArray _includedKeys;
31  CPArray _excludedKeys;
32 
33  CPString _initialKey;
34  id _initialValue;
35 }
36 
37 - (id)init
38 {
39  self = [super init];
40 
41  if (self)
42  {
43  _initialKey = @"key";
44  _initialValue = @"value";
45  }
46 
47  return self;
48 }
49 
50 - (id)newObject
51 {
52  var keys = [_contentDictionary allKeys],
53  newKey = _initialKey,
54  count = 0;
55 
56  if ([keys containsObject:newKey])
57  while ([keys containsObject:newKey])
58  newKey = [CPString stringWithFormat:@"%@%i", _initialKey, ++count];
59 
60  return [self _newObjectWithKey:newKey value:_initialValue];
61 }
62 
63 - (id)_newObjectWithKey:(CPString)aKey value:(id)aValue
64 {
65  var aNewObject = [_CPDictionaryControllerKeyValuePair new];
66 
67  aNewObject._dictionary = _contentDictionary;
68  aNewObject._controller = self;
69  aNewObject._key = aKey;
70 
71  if (aValue !== nil)
72  [aNewObject setValue:aValue];
73 
74  return aNewObject;
75 }
76 
78 {
79  return _contentDictionary;
80 }
81 
82 - (void)setContentDictionary:(CPDictionary)aDictionary
83 {
84  if (aDictionary == _contentDictionary)
85  return;
86 
87  if ([aDictionary isKindOfClass:[CPDictionary class]])
88  _contentDictionary = aDictionary;
89  else
90  _contentDictionary = nil;
91 
92  var array = [CPArray array],
93  allKeys = [_contentDictionary allKeys];
94 
95  [allKeys addObjectsFromArray:_includedKeys];
96 
97  var iter = [[CPSet setWithArray:allKeys] objectEnumerator],
98  obj;
99 
100  while ((obj = [iter nextObject]) !== nil)
101  if (![_excludedKeys containsObject:obj])
102  [array addObject:[self _newObjectWithKey:obj value:nil]];
103 
104  [super setContent:array];
105 }
106 
107 
108 @end
109 
110 
111 var CPIncludedKeys = @"CPIncludedKeys",
112  CPExcludedKeys = @"CPExcludedKeys";
113 
115 
116 - (id)initWithCoder:(CPCoder)aCoder
117 {
118  self = [super initWithCoder:aCoder];
119 
120  if (self)
121  {
122  _includedKeys = [aCoder decodeObjectForKey:CPIncludedKeys];
123  _excludedKeys = [aCoder decodeObjectForKey:CPExcludedKeys];
124  _initialKey = @"key";
125  _initialValue = @"value";
126  }
127 
128  return self;
129 }
130 
131 - (void)encodeWithCoder:(CPCoder)aCoder
132 {
133  [super encodeWithCoder:aCoder];
134 
135  [aCoder encodeObject:_includedKeys forKey:CPIncludedKeys];
136  [aCoder encodeObject:_excludedKeys forKey:CPExcludedKeys];
137 }
138 
139 @end
140 
141 
142 
143 
144 @implementation _CPDictionaryControllerKeyValuePair : CPObject
145 {
146  CPString _key;
147  CPDictionary _dictionary;
148  CPDictionaryController _controller;
149 }
150 
151 - (id)value
152 {
153  return [_dictionary objectForKey:_key];
154 }
155 
156 - (void)setValue:(id)aValue
157 {
158  [_dictionary setObject:aValue forKey:_key];
159 }
160 
161 - (BOOL)isExplicitlyIncluded
162 {
163  return [[_controller _includedKeys] containsObject:_key];
164 }
165 
166 
167 @end
168 
170 
174 - (CPArray)includedKeys
175 {
176  return _includedKeys;
177 }
178 
182 - (void)setIncludedKeys:(CPArray)aValue
183 {
184  _includedKeys = aValue;
185 }
186 
190 - (CPArray)excludedKeys
191 {
192  return _excludedKeys;
193 }
194 
198 - (void)setExcludedKeys:(CPArray)aValue
199 {
200  _excludedKeys = aValue;
201 }
202 
207 {
208  return _initialKey;
209 }
210 
214 - (void)setInitialKey:(CPString)aValue
215 {
216  _initialKey = aValue;
217 }
218 
223 {
224  return _initialValue;
225 }
226 
230 - (void)setInitialValue:(id)aValue
231 {
232  _initialValue = aValue;
233 }
234 
235 @end
id initWithCoder:(CPCoder aCoder)
void encodeWithCoder:(CPCoder aCoder)
A mutable key-value pair collection.
Definition: CPDictionary.h:2
void setContent:(id value)
An immutable string (collection of characters).
Definition: CPString.h:2
var CPIncludedKeys
var CPExcludedKeys
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
id stringWithFormat:(CPString format, [,] ...)
Definition: CPString.j:166