This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
/
DocEditor.m
395 lines (325 loc) · 11.1 KB
/
DocEditor.m
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// DocEditor.m
// TouchDB Viewer
//
// Created by Jens Alfke on 5/4/12.
// Copyright (c) 2012 Couchbase, Inc. All rights reserved.
//
#import "DocEditor.h"
#import "DBWindowController.h"
#import "JSONFormatter.h"
@implementation DocEditor
{
CouchDatabase* _db;
CouchRevision* _revision;
BOOL _readOnly;
BOOL _untitled;
NSDictionary* _originalProperties;
NSMutableDictionary* _properties;
NSMutableArray* _propNames;
BOOL _cancelingEdit;
IBOutlet DBWindowController* _dbWindowController;
IBOutlet NSTableView* _table;
IBOutlet NSButton *_addPropertyButton, *_removePropertyButton, *_addColumnButton;
IBOutlet NSButton *_saveButton, *_revertButton;
}
@synthesize database=_db, tableView=_table, readOnly=_readOnly;
- (void) awakeFromNib {
NSLayoutConstraint* c = [NSLayoutConstraint constraintWithItem: _addPropertyButton
attribute: NSLayoutAttributeLeft
relatedBy: NSLayoutRelationEqual
toItem: _table
attribute: NSLayoutAttributeLeft
multiplier: 1.0 constant: 0];
[_table.window.contentView addConstraint: c];
}
- (CouchRevision*) revision {
return _revision;
}
- (void) setRevision: (CouchRevision*)rev {
if (rev != _revision || _untitled) {
_revision = rev;
_untitled = NO;
[self revertDocumentToSaved: self];
}
}
- (void) editNewDocument {
if (!_untitled) {
_revision = nil;
_untitled = YES;
[self revertDocumentToSaved: self];
}
}
static BOOL isSpecialProperty(NSString* key) {
return [key hasPrefix: @"_"];
}
- (void) rebuildTable {
_propNames = _properties.allKeys.mutableCopy;
[self sortProperties];
}
- (void) sortProperties {
[_propNames sortUsingComparator: ^NSComparisonResult(NSString* key1, NSString* key2) {
int n = (isSpecialProperty(key1) != 0) - (isSpecialProperty(key2) != 0);
if (n)
return n;
return [key1 caseInsensitiveCompare: key2];
}];
[_table reloadData];
}
- (NSString*) selectedProperty {
NSInteger row = _table.selectedRow;
return row >= 0 ? _propNames[row] : nil;
}
- (void) setSelectedProperty: (NSString*)property {
NSUInteger row = [_propNames indexOfObject: property];
NSIndexSet* indexes = nil;
if (row != NSNotFound)
indexes = [NSIndexSet indexSetWithIndex: row];
else
indexes = [NSIndexSet indexSet];
[_table selectRowIndexes: indexes byExtendingSelection: NO];
[self enablePropertyButtons];
}
- (NSString*) selectedOrClickedProperty {
NSInteger row = _table.clickedRow;
if (row < 0)
row = _table.selectedRow;
return row >= 0 ? _propNames[row] : nil;
}
- (BOOL) saveDocument {
if (!_readOnly && _properties && ![_properties isEqual: _originalProperties]) {
CouchDocument* doc;
NSError* error;
if (_revision)
doc = _revision.document;
else
doc = _db[_properties[@"_id"]];
if (![[doc putProperties: _properties] wait: &error]) {
[_table presentError: error];
return NO;
}
self.revision = doc.currentRevision;
_properties = _revision.properties.mutableCopy;
_originalProperties = _properties.copy;
}
_saveButton.hidden = _revertButton.hidden = YES;
return YES;
}
- (IBAction) saveDocument: (id)sender {
[self saveDocument];
}
- (IBAction) revertDocumentToSaved:(id)sender {
NSString* selectedProperty = self.selectedProperty;
if (_untitled) {
NSString* docID = [[_db.server generateUUIDs: 1] lastObject];
_properties = [NSMutableDictionary dictionaryWithObject: docID forKey: @"_id"];
} else {
_properties = _revision.properties.mutableCopy;
}
_originalProperties = _properties.copy;
[self rebuildTable];
self.selectedProperty = selectedProperty;
_saveButton.hidden = _revertButton.hidden = !_untitled;
}
#pragma mark - ACTIONS:
- (IBAction) addProperty: (id)sender {
// Insert a placeholder empty-string property for the user to fill in:
if (_readOnly || [_propNames containsObject: @""] || _properties[@""]) {
NSBeep();
return;
}
[_propNames insertObject: @"" atIndex: 0];
[_table reloadData];
self.selectedProperty = @"";
[_table editColumn: 0 row: 0 withEvent: nil select: YES];
}
- (IBAction) removeProperty: (id)sender {
NSString* prop = self.selectedOrClickedProperty;
if (_readOnly || !prop || isSpecialProperty(prop)) {
NSBeep();
return;
}
[_properties removeObjectForKey: prop];
[self rebuildTable];
}
- (IBAction) addColumnForSelectedProperty:(id)sender {
NSString* property = self.selectedOrClickedProperty;
if (!property)
return;
if ([_dbWindowController hasColumnForProperty: property])
[_dbWindowController removeColumnForProperty: property];
else
[_dbWindowController addColumnForProperty: property];
[self enablePropertyButtons];
}
- (IBAction) cancelOperation: (id)sender {
if ([_table editedRow] >= 0) {
_cancelingEdit = YES;
@try {
[_table editColumn: -1 row: -1 withEvent: nil select: NO];
} @finally {
_cancelingEdit = NO;
}
[self rebuildTable];
}
}
- (IBAction) copy:(id)sender {
NSString* prop = self.selectedProperty;
if (!prop) {
NSBeep();
return;
}
id value = _properties[prop];
NSString* json = [JSONFormatter stringForObjectValue: value];
NSPasteboard* pb = [NSPasteboard generalPasteboard];
[pb clearContents];
[pb setString: json forType: NSStringPboardType];
}
- (BOOL) validateUserInterfaceItem: (id <NSValidatedUserInterfaceItem>)item {
NSLog(@"validate %@", item);//TEMP
NSString* property = self.selectedOrClickedProperty;
SEL action = [item action];
if (action == @selector(addColumnForSelectedProperty:)) {
NSString* title;
if (property && [_dbWindowController hasColumnForProperty: property])
title = @"Remove Column";
else
title = @"Add Column";
[(id)item setTitle: title];
return (property != nil);
} else if (action == @selector(removeProperty:)) {
return (!_readOnly && property != nil && !isSpecialProperty(property));
}
return YES;
}
- (void) enablePropertyButtons {
NSString* selectedProperty = self.selectedProperty;
BOOL canInsert = !_readOnly && (_revision != nil || _untitled);
BOOL canRemove = !_readOnly && selectedProperty && !isSpecialProperty(selectedProperty);
_addPropertyButton.enabled = canInsert;
_removePropertyButton.enabled = canRemove;
_addColumnButton.enabled = selectedProperty && !isSpecialProperty(selectedProperty);
_addColumnButton.title = (selectedProperty && [_dbWindowController hasColumnForProperty: selectedProperty]) ? @"Remove Column" : @"Add Column";
}
#pragma mark - TABLE VIEW DATA SOURCE / DELEGATE:
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return _propNames.count;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
id result = _propNames[row];
if (![tableColumn.identifier isEqualToString: @"key"])
result = _properties[result];
return result;
}
- (void)tableView:(NSTableView *)tableView
willDisplayCell:(NSTextFieldCell*)cell
forTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
NSString* key = _propNames[row];
NSColor* color;
if (isSpecialProperty(key))
color = [NSColor disabledControlTextColor];
else
color = [NSColor controlTextColor];
[cell setTextColor: color];
}
- (BOOL)tableView:(NSTableView *)tableView
shouldEditTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
if (_readOnly)
return NO;
NSString* key = _propNames[row];
if (!isSpecialProperty(key))
return YES;
if (_untitled && [key isEqualToString: @"_id"] &&
![[tableColumn identifier] isEqualToString: @"key"])
return YES;
else
return NO;
}
- (void)tableView:(NSTableView *)tableView
setObjectValue:(NSString*)newCellValue
forTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
if (_cancelingEdit)
return;
BOOL editingKey = [tableColumn.identifier isEqualToString: @"key"];
NSString* key = _propNames[row];
if (isSpecialProperty(key) && !_untitled)
return;
if (_properties[key]) {
// Yes, this is a real document property:
if (newCellValue == nil || [newCellValue isEqual: @""]) {
// User entered empty key or value: delete property
[_properties removeObjectForKey: key];
[self rebuildTable];
} else if (editingKey) {
// Change the key:
if ([newCellValue isEqualToString: key])
return; // no-op
if (isSpecialProperty(newCellValue)) {
NSBeep();
return;
}
if (_properties[newCellValue]) {
NSBeep(); // duplicate key!
return;
}
id value = _properties[key];
_properties[newCellValue] = value;
[_properties removeObjectForKey: key];
[self rebuildTable];
self.selectedProperty = newCellValue;
} else {
// Change the value:
if ([newCellValue isEqual: _properties[key]])
return; // no-op
_properties[key] = newCellValue;
}
} else {
// Editing the fake row inserted by addProperty:
if (newCellValue == nil || [newCellValue isEqual: @""]) {
// User entered empty key or value; delete property
[_propNames removeObjectAtIndex: row];
[self rebuildTable];
self.selectedProperty = nil;
} else if (editingKey) {
// Changing the key, so re-sort:
if (isSpecialProperty(newCellValue)) {
NSBeep();
return;
}
if (_properties[newCellValue]) {
NSBeep(); // duplicate key!
return;
}
_propNames[row] = newCellValue;
[self sortProperties];
self.selectedProperty = newCellValue;
} else {
// Changing the value:
_properties[key] = newCellValue;
}
}
_saveButton.hidden = _revertButton.hidden = NO;
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
[self enablePropertyButtons];
}
- (BOOL)control:(NSControl *)control
textView:(NSTextView *)textView
doCommandBySelector:(SEL)command
{
//NSLog(@"command: %@", NSStringFromSelector(command));
if (command == @selector(cancelOperation:)) { // Esc key
[self cancelOperation: self];
return YES;
}
return NO;
}
@end