forked from bububa/MongoHub-Mac
/
JsonWindowController.mm
153 lines (135 loc) · 4.92 KB
/
JsonWindowController.mm
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
//
// JsonWindowController.m
// MongoHub
//
// Created by Syd on 10-12-27.
// Copyright 2010 ThePeppersStudio.COM. All rights reserved.
//
#import "JsonWindowController.h"
#import "Configure.h"
#import "NSProgressIndicator+Extras.h"
#import "DatabasesArrayController.h"
#import "Connection.h"
#import "MongoDB.h"
#import "NSString+Extras.h"
@implementation JsonWindowController
@synthesize managedObjectContext;
@synthesize databasesArrayController;
@synthesize mongoDB;
@synthesize conn;
@synthesize dbname;
@synthesize collectionname;
@synthesize jsonDict;
@synthesize myTextView;
- (id)init {
if (![super initWithWindowNibName:@"JsonWindow"]) return nil;
return self;
}
- (void)dealloc {
[managedObjectContext release];
[databasesArrayController release];
[conn release];
[mongoDB release];
[dbname release];
[collectionname release];
[jsonDict release];
[myTextView release];
[syntaxColoringController setDelegate: nil];
[syntaxColoringController release];
syntaxColoringController = nil;
[progress release];
[super dealloc];
}
- (void)windowWillClose:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:kJsonWindowWillClose object:nil];
[super release];
}
- (void)windowDidLoad {
[super windowDidLoad];
NSString *title = [[NSString alloc] initWithFormat:@"%@.%@ _id:%@", dbname, collectionname, [jsonDict objectForKey:@"value"]];
[self.window setTitle:title];
[title release];
[myTextView setString:[jsonDict objectForKey:@"beautified"]];
syntaxColoringController = [[UKSyntaxColoredTextViewController alloc] init];
[syntaxColoringController setDelegate: self];
[syntaxColoringController setView: myTextView];
}
-(void) textViewControllerWillStartSyntaxRecoloring: (UKSyntaxColoredTextViewController*)sender
{
// Show your progress indicator.
[progress startAnimation: self];
[progress display];
}
-(void) textViewControllerDidFinishSyntaxRecoloring: (UKSyntaxColoredTextViewController*)sender
{
// Hide your progress indicator.
[progress stopAnimation: self];
[progress display];
}
-(NSString *)syntaxDefinitionFilenameForTextViewController: (UKSyntaxColoredTextViewController*)sender
{
return @"JSON";
}
-(void) selectionInTextViewController: (UKSyntaxColoredTextViewController*)sender // Update any selection status display.
changedToStartCharacter: (NSUInteger)startCharInLine endCharacter: (NSUInteger)endCharInLine
inLine: (NSUInteger)lineInDoc startCharacterInDocument: (NSUInteger)startCharInDoc
endCharacterInDocument: (NSUInteger)endCharInDoc;
{
NSString* statusMsg = nil;
if( startCharInDoc < endCharInDoc )
{
statusMsg = NSLocalizedString(@"character %lu to %lu of line %lu (%lu to %lu in document).",@"selection description in syntax colored text documents.");
statusMsg = [NSString stringWithFormat: statusMsg, startCharInLine +1, endCharInLine +1, lineInDoc +1, startCharInDoc +1, endCharInDoc +1];
}
else
{
statusMsg = NSLocalizedString(@"character %lu of line %lu (%lu in document).",@"insertion mark description in syntax colored text documents.");
statusMsg = [NSString stringWithFormat: statusMsg, startCharInLine +1, lineInDoc +1, startCharInDoc +1];
}
[status setStringValue: statusMsg];
[status display];
}
/* -----------------------------------------------------------------------------
recolorCompleteFile:
IBAction to do a complete recolor of the whole friggin' document.
-------------------------------------------------------------------------- */
-(IBAction) recolorCompleteFile: (id)sender
{
[syntaxColoringController recolorCompleteFile: sender];
}
-(IBAction) save:(id)sender
{
[NSThread detachNewThreadSelector:@selector(doSave) toTarget:self withObject:nil];
}
-(void) doSave
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[status setStringValue: @"Saving..."];
[status display];
[progress startAnimation: self];
[progress display];
NSString *user=nil;
NSString *password=nil;
Database *db = [databasesArrayController dbInfo:conn name:dbname];
if (db) {
user = db.user;
password = db.password;
}
[db release];
NSString *_id = nil;
if ([[jsonDict objectForKey:@"type"] isEqualToString:@"ObjectId"]) {
_id = [NSString stringWithFormat:@"ObjectId(\"%@\")", [jsonDict objectForKey:@"value"]];
}else {
_id = [NSString stringWithFormat:@"\"%@\"", [jsonDict objectForKey:@"value"]];
}
NSMutableString *json = [[NSMutableString alloc] initWithString:[myTextView string]];
[mongoDB saveInDB:dbname collection:collectionname user:user password:password jsonString:json _id:_id];
[json release];
[progress stopAnimation: self];
[progress display];
[status setStringValue: @"Saved"];
[status display];
[[NSNotificationCenter defaultCenter] postNotificationName:kJsonWindowSaved object:nil];
[pool release];
}
@end