File: ConnectionDictionaryInspector.m

package info (click to toggle)
gnustep-dl2 0.12.0%2Bgit20250112-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,092 kB
  • sloc: objc: 68,223; makefile: 60; sh: 48
file content (224 lines) | stat: -rw-r--r-- 5,946 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

/*
 ConnectionDictionaryInspector.m
 
 Author: David Wetzel <dave@turbocat.de>
 Date: 2010
 
 This file is part of EOModelEditor.
 
 EOModelEditor is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 EOModelEditor is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with EOModelEditor; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "ConnectionDictionaryInspector.h"

#include <EOAccess/EOEntity.h>
#include <EOAccess/EORelationship.h>
#include <EOAccess/EOModel.h>
#include <EOAccess/EOAttribute.h>

#include <EOModeler/EOModelerApp.h>
#include "../EOMEDocument.h"
#include "../EOMEEOAccessAdditions.h"

#include <Foundation/Foundation.h>

#import "../TableViewController.h"

#ifndef GNUSTEP
#include <GNUstepBase/GNUstep.h>
#endif

@implementation ConnectionDictionaryInspector

- (NSString *) displayName
{
  return @"Connection Dictionary";
}

- (void) dealloc
{
  DESTROY(_dataArray);
  DESTROY(_tableViewController);
  DESTROY(_selectedDict);

  [super dealloc];
}


- (void) awakeFromGSMarkup
{
  _tableViewController = [TableViewController new];

  [_tableView setDataSource:_tableViewController];
  [_tableView setDelegate:_tableViewController];
  [_textView setRichText:NO];
  
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(tableViewSelectionDidChange:) 
                                               name:NSTableViewSelectionDidChangeNotification 
                                             object:_tableView];

  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(textDidChange:) 
                                               name:NSTextDidChangeNotification 
                                             object:_textView];
  
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(tableViewDataDidChange:) 
                                               name:TableViewDataHasChangedNotification 
                                             object:_tableViewController];
  
}

- (float) displayOrder
{
  return 1;
}

- (BOOL) canInspectObject:(id)anObject
{
  BOOL weCan = [anObject isKindOfClass:[EOModel class]];
  return weCan;
}

- (void) commitChanges
{
  NSMutableDictionary * mDict = [NSMutableDictionary dictionary];
  
  if ((_dataArray) && ([_dataArray count])) {
    NSEnumerator * aEnumer = [_dataArray objectEnumerator];
    NSDictionary * myDict = nil;
    
    while ((myDict = [aEnumer nextObject])) {
      [mDict setObject:[myDict objectForKey:@"value"]
                forKey:[myDict objectForKey:@"key"]];
    }

  }
  [(EOModel *)[self selectedObject] setConnectionDictionary:mDict];
}


- (void) setDataFromDictionary:(NSDictionary*) aDictionary
{
  NSEnumerator * keyEnumer;
  NSString     * currentKey;
    
  DESTROY(_dataArray);
  _dataArray = [NSMutableArray array];
  RETAIN(_dataArray);
  
  // make sure we are using ordered keys.
  keyEnumer = [[[aDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)] objectEnumerator];
  
  while ((currentKey = [keyEnumer nextObject])) {
    NSMutableDictionary * mDict;
    
    mDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
             [aDictionary objectForKey:currentKey], @"value",
             currentKey,@"key", nil];
    
    [_dataArray addObject:mDict];
  }
  
  [_tableViewController setRepresentedObject:_dataArray];
  [_tableView reloadData];
  [_textView setString:@""];        

}

- (void) refresh
{  
  [self setDataFromDictionary:[(EOModel *)[self selectedObject] connectionDictionary]];
  DESTROY(_selectedDict);

}

- (void) textDidChange:(NSNotification *)notif
{
  id obj = [notif object];
  
  if (_selectedDict) {
    if (obj == _textView) {
      // I dont know why that stringWithString is needed here,
      // but it seems to fix an OSX bug that messes up values -- dw
      [_selectedDict setObject:[NSString stringWithString:[_textView string]]
                        forKey:@"value"];
      [_tableView reloadData];
      [self commitChanges];
    } 
  }
  
}

- (IBAction) add:(id) sender
{
  NSMutableDictionary * mDict;
  NSUInteger  aCount = [_dataArray count];
  
  mDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
           @"newValue", @"value",
           [NSString stringWithFormat:@"newKey%"PRIuPTR, aCount],@"key", nil];
  
  [_dataArray addObject:mDict];
  [_tableView reloadData];
}

- (IBAction) remove:(id) sender
{
  [_dataArray removeObjectsInArray:[_tableViewController selectedObjects]];
  [_tableView reloadData];
  [self commitChanges];
}

- (IBAction) tableViewClicked:(id) sender
{
}

- (void)tableViewSelectionDidChange:(NSNotification *) notification
{
  NSArray * selectedObjects = nil;
  
  selectedObjects = [_tableViewController selectedObjects];
  
  if ([selectedObjects count] > 0) {
    NSMutableDictionary * dict = [selectedObjects objectAtIndex:0];
    NSString * value = [dict objectForKey:@"value"];
    
    ASSIGN(_selectedDict, dict);
    
    [_textView setString:(value) ? value : @""];        
  } else {
    DESTROY(_selectedDict);
  }

  
}

- (void) tableViewDataDidChange: (NSNotification*) notification
{
  NSDictionary * userInfo = [notification userInfo];

  if ([[userInfo objectForKey:@"keyPath"] isEqual:@"value"]) {
    [_textView setString:[userInfo objectForKey:@"newValue"]];        
  }
  [self commitChanges];
  
}


@end