1/** <title>NSNibBindingConnector</title>
2
3   <abstract>
4   </abstract>
5
6   Copyright (C) 2007 Free Software Foundation, Inc.
7
8   Author: Gregory John Casamento
9   Date: 2007
10   Author: Fred Kiefer <fredkiefer@gmx.de>
11   Date: Nov 2007
12
13   This file is part of the GNUstep GUI Library.
14
15   This library is free software; you can redistribute it and/or
16   modify it under the terms of the GNU Lesser General Public
17   License as published by the Free Software Foundation; either
18   version 2 of the License, or (at your option) any later version.
19
20   This library is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
23   Lesser General Public License for more details.
24
25   You should have received a copy of the GNU Lesser General Public
26   License along with this library; see the file COPYING.LIB.
27   If not, see <http://www.gnu.org/licenses/> or write to the
28   Free Software Foundation, 51 Franklin Street, Fifth Floor,
29   Boston, MA 02110-1301, USA.
30*/
31
32#import <GNUstepGUI/GSNibLoading.h>
33#import <Foundation/NSString.h>
34#import <Foundation/NSDictionary.h>
35#import <AppKit/NSKeyValueBinding.h>
36
37@implementation NSNibBindingConnector
38
39- (id) init
40{
41  if((self = [super init]) != nil)
42    {
43      _binding = @"";
44      _keyPath = @"";
45      _options = RETAIN([NSMutableDictionary dictionary]);
46    }
47  return self;
48}
49
50- (void) dealloc
51{
52  RELEASE(_binding);
53  RELEASE(_keyPath);
54  RELEASE(_options);
55  [super dealloc];
56}
57
58- (NSString *) binding
59{
60  return _binding;
61}
62
63- (NSString *) keyPath
64{
65  return _keyPath;
66}
67
68- (NSDictionary *) options
69{
70  return _options;
71}
72
73- (void) setBinding: (NSString *)binding
74{
75  ASSIGN(_binding, binding);
76}
77
78- (void) setKeyPath: (NSString *)keyPath
79{
80  ASSIGN(_keyPath, keyPath);
81}
82
83- (void) setOptions: (NSDictionary *)options
84{
85  ASSIGN(_options, options);
86}
87
88- (void) replaceObject: (id)anObject withObject: (id)anotherObject
89{
90  if (_binding == anObject)
91    {
92      ASSIGN(_binding, anotherObject);
93    }
94  if (_keyPath == anObject)
95    {
96      ASSIGN(_keyPath, anotherObject);
97    }
98  if (_options == anObject)
99    {
100      ASSIGN(_options, anotherObject);
101    }
102
103  [super replaceObject: anObject withObject: anotherObject];
104}
105
106- (void) establishConnection
107{
108  [_src bind: _binding
109        toObject: _dst
110        withKeyPath: _keyPath
111        options: _options];
112}
113
114- (void) encodeWithCoder: (NSCoder*)aCoder
115{
116  if ([aCoder allowsKeyedCoding])
117    {
118      if (_binding != nil)
119        {
120          [aCoder encodeObject: _binding forKey: @"NSBinding"];
121        }
122      if (_keyPath != nil)
123        {
124          [aCoder encodeObject: _keyPath forKey: @"NSKeyPath"];
125        }
126      if (_options != nil)
127        {
128          [aCoder encodeObject: _options forKey: @"NSOptions"];
129        }
130    }
131  else
132    {
133      [aCoder encodeObject: _binding];
134      [aCoder encodeObject: _keyPath];
135      [aCoder encodeObject: _options];
136    }
137}
138
139- (id) initWithCoder: (NSCoder*)aDecoder
140{
141  if (!(self = [super initWithCoder: aDecoder]))
142    {
143      return nil;
144    }
145
146  if ([aDecoder allowsKeyedCoding])
147    {
148      if ([aDecoder containsValueForKey: @"NSNibBindingConnectorVersion"])
149        {
150          int version = [aDecoder decodeIntForKey: @"NSNibBindingConnectorVersion"];
151          if (version != 2)
152            {
153              NSLog(@"Unexpected NSNibBindingConnectorVersion %d", version);
154              RELEASE(self);
155              return nil;
156            }
157        }
158      if ([aDecoder containsValueForKey: @"NSBinding"])
159        {
160          ASSIGN(_binding, [aDecoder decodeObjectForKey: @"NSBinding"]);
161        }
162      if ([aDecoder containsValueForKey: @"NSKeyPath"])
163        {
164          ASSIGN(_keyPath, [aDecoder decodeObjectForKey: @"NSKeyPath"]);
165        }
166      if ([aDecoder containsValueForKey: @"NSOptions"])
167        {
168          ASSIGN(_options, [aDecoder decodeObjectForKey: @"NSOptions"]);
169        }
170    }
171  else
172    {
173      ASSIGN(_binding,[aDecoder decodeObject]);
174      ASSIGN(_keyPath,[aDecoder decodeObject]);
175      ASSIGN(_options,[aDecoder decodeObject]);
176    }
177
178  return self;
179}
180
181- (NSString *) description
182{
183  return [NSString stringWithFormat: @"<%@ binding=%@ keypath=%@ options=%@>",
184                   [super description],
185                   [self binding],
186                   [self keyPath],
187                   [self options]];
188}
189
190@end
191