-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMangoWindowController.m
485 lines (395 loc) · 15.7 KB
/
MangoWindowController.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//
// MangoWindowController.m
// Mango
//
// Created by Juan Carlos Moreno on 4/4/14.
// Copyright (c) 2014 Juan Carlos Moreno. All rights reserved.
//
#import "MangoWindowController.h"
#import <INAppStoreWindow/INAppStoreWindow.h>
@interface MangoWindowController ()
@end
@implementation MangoWindowController
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
[window setDelegate:self];
MangoDataManager *dataManager = [[MangoDataManager alloc] init];
[dataManager setDelegate:self];
[self setDataManager:dataManager];
[self setPluginManager:[[MangoPluginManager alloc] init]];
[[self pluginManager] setDelegate: self];
}
return self;
}
#pragma mark - Server Actions
- (IBAction)serverInfoButtonPressed:(id)sender {
InfoWindowController *mangowindow = [[InfoWindowController alloc] initWithWindowNibName:@"InfoWindow"];
[[mangowindow window] setTitle:[[[self dataManager] ConnectionManager] connectionDecriptionString]];
[mangowindow setDataManager: [self dataManager]];
[self setInfoWindowController:mangowindow];
[mangowindow showWindow: self];
[mangowindow getServerInfo];
}
#pragma mark - Database Actions
- (IBAction)removeCollectionWasPressed:(id)sender {
NSAlert *alert = [NSAlert alertWithMessageText:@"Do you really want to drop this collection?"
defaultButton:@"Drop Collection"
alternateButton:@"Learn more"
otherButton:@"Cancel"
informativeTextWithFormat:@"Deleting this item will erase all associated data in the collection. This action cannot be undone."];
[self setPopupAlert:alert];
NSButton *sButton = (NSButton *) sender;
[alert runAsPopoverForView:sButton withCompletionBlock:^(NSInteger result) {
// handle result
if (result==NSAlertFirstButtonReturn)
{
BOOL result = [[[self dataManager] ConnectionManager] dropCollectionNamed:[self getSelectedCollectionName] onDB: (NSString *)[self getSelectedDatabase]];
if(result)
{
[self refreshCollectionOutline];
}
else
{
NSAlert *alert2 = [[NSAlert alloc] init];
[alert2 setMessageText: [NSString stringWithFormat:@"Unable to drop collection %@", [self getSelectedCollectionName]]];
[alert2 runModal];
}
}
}];
}
- (IBAction)editCollectionWasPressed:(id)sender {
[self showPopOver:[self renameCollectionPopover] withSender:sender];
}
- (IBAction)addCollectionWasPressed:(id)sender {
[self showPopOver:[self createCollectionPopover] withSender:sender];
}
-(void) showPopOver: ( NSPopover *) po withSender: (id) sender
{
if ([po isShown])
{
[po close];
}
else
{
[po showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
}
}
- (IBAction)createCollectionAction:(id)sender {
CreateItemPopover *pvc = (CreateItemPopover *) [[self createCollectionPopover] contentViewController];
NSString *newDBName = [[pvc inputTextField] stringValue];
if (newDBName && [newDBName length]>0)
{
newDBName = [newDBName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if([[[self dataManager] ConnectionManager] createCollectionNamed:newDBName onDB:[self getSelectedDatabase]])
{
[self refreshCollectionOutline];
[[pvc inputTextField] setStringValue:@""];
[[self createCollectionPopover] close];
}
}
}
- (IBAction)renameDBWasPressed:(id)sender {
CreateItemPopover *pvc = (CreateItemPopover *) [[self renameCollectionPopover] contentViewController];
NSString *newCollName = [[pvc inputTextField] stringValue];
if (newCollName && [newCollName length]>0)
{
newCollName = [newCollName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//if([[[self dataManager] ConnectionManager] createCollectionNamed:newCollName onDB:[self getSelectedDatabase]])
//{
//[self setupDBsPopUpButton];
[self refreshCollectionOutline];
[[pvc inputTextField] setStringValue:@""];
[[self renameCollectionPopover] close];
//}
}
}
- (IBAction)cancelCreateNewCollectionWasPressed:(id)sender {
[[self createCollectionPopover] close];
}
- (IBAction)cancelCreateNewDBWasPressed:(id)sender {
[[self createDBPopover] close];
}
- (IBAction)cancelRenameCollectionWasPressed:(id)sender {
[[self renameCollectionPopover] close];
}
- (IBAction)createDBAction:(id)sender {
// TODO: Fix that when the control loses focus, it adds DB
CreateItemPopover *pvc = (CreateItemPopover *) [[self createDBPopover] contentViewController];
NSString *newDBName = [[pvc inputTextField] stringValue];
if (newDBName && [newDBName length]>0)
{
newDBName = [newDBName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if([[[self dataManager] ConnectionManager] createDBNamed:newDBName])
{
if ([[self createDBPopover] isShown])
{
[[self createDBPopover] close];
}
[self setupDBsPopUpButton];
[[pvc inputTextField] setStringValue:@""];
}
}
}
- (IBAction)showCreateDBPopoverWasPressed:(id)sender {
[self showPopOver:[self createDBPopover] withSender:sender];
}
- (IBAction)dropDBWasPressed:(id)sender {
NSAlert *alert = [NSAlert alertWithMessageText:@"Do you really want to drop database?"
defaultButton:@"Drop"
alternateButton:@"Learn more"
otherButton:@"Cancel"
informativeTextWithFormat:@"Deleting this item will erase all associated data in the database. This action cannot be undone."];
[self setPopupAlert:alert];
NSButton *sButton = (NSButton *) sender;
[alert runAsPopoverForView:sButton withCompletionBlock:^(NSInteger result) {
// handle result
if (result==NSAlertFirstButtonReturn)
{
BOOL result = [[[self dataManager] ConnectionManager] dropDB:[self getSelectedDatabase]];
if(result)
{
[self setupDBsPopUpButton];
[self refreshCollectionOutline];
}
else
{
NSAlert *alert2 = [[NSAlert alloc] init];
[alert2 setMessageText: [NSString stringWithFormat:@"Unable to drop database %@", [self getSelectedDatabase]]];
[alert2 runModal];
}
}
}];
}
- (IBAction)showUsersWasPressed:(id)sender {
}
- (IBAction)dbInfoButtonPressed:(id)sender {
[[[self dataManager] ConnectionManager] getDBStats: [self getSelectedDatabase]];
}
- (IBAction)dbsPopUpButtonAction:(id)sender
{
[[self collectionSearchField] setStringValue:@""];
[self refreshCollectionOutline];
}
-(void) refreshCollectionOutline
{
[self setCollectionData:@[]];
[[self dataManager] fetchCollectionNamesForDB: [self getSelectedDatabase]];
[[self dataManager] processCollectionsWithFilter:[[self collectionSearchField] stringValue]];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:0];
[[self collectionListView] selectRowIndexes:indexSet byExtendingSelection:NO];
}
#pragma mark - Collection Actions
- (IBAction)collectionInfoButtonPressed:(id)sender{
//[[self connMgr] getCollectionInfo: [NSString stringWithFormat:@"%@.%@", [self getSelectedDatabase], [self getSelectedCollectionName]]];
}
- (void)controlTextDidChange:(NSNotification *)aNotification {
if ([[aNotification object] isKindOfClass: [NSSearchField class ]])
{
[[self dataManager] processCollectionsWithFilter:[[self collectionSearchField] stringValue]];
}
}
#pragma mark - Document UI Actions
-(void) outlineViewSelectionDidChange:(NSNotification *)notification
{
NSString *selectedCollection = [self getSelectedCollectionName];
NSString *selectedDB = [self getSelectedDatabase];
NSTabViewItem *selectedItem = [[self tabView] selectedTabViewItem];
NSString *tabName = [selectedItem label];
id<MangoPlugin> plugin = [[self pluginManager] activePluginNamed: tabName];
NSArray *nodes = [[self collectionListTC] selectedNodes];
if (nodes && [nodes count])
{
NSDictionary *rObj = [[nodes firstObject] representedObject];
NSNumber *gridfs = [rObj valueForKey:@"GridFS"];
if (gridfs && [gridfs boolValue])
{
[plugin setIsGridFS:YES];
}
else
{
[plugin setIsGridFS:NO];
}
}
[plugin refreshDataFromDB:selectedDB withCollection:selectedCollection andDataManager:[self dataManager]];
}
- (void) connectAndShow
{
[[[self dataManager] ConnectionManager ] openConnection];
[self window];
}
- (IBAction)debugConn:(id)sender {
}
#pragma mark - UI Setup
-(void) setupDBsPopUpButton
{
NSArray * dbs = [[[self dataManager] ConnectionManager] getDatabases];
[[self dbsPopUpButton] removeAllItems];
[[self dbsPopUpButton] addItemsWithTitles:dbs];
}
-(void) setupSideBar
{
[[self sideBar] setConnectionURL: [[[self dataManager] ConnectionManager] connectionDecriptionString] ];
}
-(void) setupTabs
{
[[self tabBarView] setTabView: [self tabView]];
MMSafariTabStyle *style = [[MMSafariTabStyle alloc] init];
[[self tabBarView] setStyle:style];
[[self tabBarView] setDelegate:self];
[[self tabBarView] setCanCloseOnlyTab:YES]; // The only tab left does not have a button
MangoBrowserViewController *tabContent = [[MangoBrowserViewController alloc] initWithNibName:@"MangoBrowserViewController" bundle:[NSBundle bundleForClass:[self class]]];
NSTabViewItem *currentTab = [[self tabView] tabViewItemAtIndex:0];
[[tabContent view] setFrame:[[currentTab view]frame]];
[[currentTab view] addSubview:[tabContent view]];
[[self pluginManager] setActivePlugin:tabContent withName: currentTab.label];
}
-(void) setupWindow
{
INAppStoreWindow *window = (INAppStoreWindow *) [self window];
window.trafficLightButtonsLeftMargin = 7.0;
window.fullScreenButtonRightMargin = 7.0;
window.centerFullScreenButton = YES;
window.titleBarHeight = 60.0;
window.verticalTrafficLightButtons = YES;
// set checkboxes
window.showsTitle = NO;
[self setupCloseButton: window];
[self setupMinimizeButton: window];
[self setupZoomButton: window];
NSRect rect = [[self popUpContainerView] frame];
rect.origin.x += 70;
[[self popUpContainerView] setFrame:rect];
[window.titleBarView addSubview:[self popUpContainerView]];
[window setTitle:[[[self dataManager] ConnectionManager] connectionDecriptionString]];
}
- (void)setupCloseButton:(INAppStoreWindow *) window
{
INWindowButton *closeButton = [INWindowButton windowButtonWithSize:NSMakeSize(14, 16) groupIdentifier:nil];
closeButton.activeImage = [NSImage imageNamed:@"close-active-color"];
closeButton.activeNotKeyWindowImage = [NSImage imageNamed:@"close-activenokey-color"];
closeButton.inactiveImage = [NSImage imageNamed:@"close-inactive-disabled-color"];
closeButton.pressedImage = [NSImage imageNamed:@"close-pd-color"];
closeButton.rolloverImage = [NSImage imageNamed:@"close-rollover-color"];
window.closeButton = closeButton;
}
- (void)setupMinimizeButton:(INAppStoreWindow *) window
{
INWindowButton *button = [INWindowButton windowButtonWithSize:NSMakeSize(14, 16) groupIdentifier:nil];
button.activeImage = [NSImage imageNamed:@"minimize-active-color"];
button.activeNotKeyWindowImage = [NSImage imageNamed:@"minimize-activenokey-color"];
button.inactiveImage = [NSImage imageNamed:@"minimize-inactive-disabled-color"];
button.pressedImage = [NSImage imageNamed:@"minimize-pd-color"];
button.rolloverImage = [NSImage imageNamed:@"minimize-rollover-color"];
window.minimizeButton = button;
}
- (void)setupZoomButton:(INAppStoreWindow *) window
{
INWindowButton *button = [INWindowButton windowButtonWithSize:NSMakeSize(14, 16) groupIdentifier:nil];
button.activeImage = [NSImage imageNamed:@"zoom-active-color"];
button.activeNotKeyWindowImage = [NSImage imageNamed:@"zoom-activenokey-color"];
button.inactiveImage = [NSImage imageNamed:@"zoom-inactive-disabled-color"];
button.pressedImage = [NSImage imageNamed:@"zoom-pd-color"];
button.rolloverImage = [NSImage imageNamed:@"zoom-rollover-color"];
window.zoomButton = button;
}
- (void)windowDidLoad
{
[super windowDidLoad];
[self setupWindow];
[self setupDBsPopUpButton];
[self refreshCollectionOutline];
[self setupSideBar];
[self setupTabs];
}
#pragma mark - Collection List
- (void)setCollectionList:(NSArray *)cl
{
}
# pragma mark - Utility functions
-(NSString *) getSelectedCollectionName
{
if ([[[self collectionListTC] selectedNodes] count])
{
NSTreeNode *selectedNode = [[[self collectionListTC] selectedNodes] objectAtIndex:0];
NSString *prefix =@"";
NSTreeNode *parent = [selectedNode parentNode];
while(parent)
{
NSString *name = [[parent representedObject] valueForKey:@"name"];
if (name)
{
prefix = [NSString stringWithFormat:@"%@.%@", name ,prefix];
}
parent = [parent parentNode];
}
NSDictionary *selectedNodeObj = [selectedNode representedObject];
NSString *selectedCollection = selectedNodeObj[@"name"];
if (![prefix isEqualToString:@""])
{
selectedCollection = [NSString stringWithFormat:@"%@%@", prefix, selectedCollection];
}
return selectedCollection;
}
return @"";
}
-(NSString *) getSelectedDatabase
{
return [[[self dbsPopUpButton] selectedItem] title];
}
# pragma mark - DataManager callbacks
-(void) setCollectionData: (NSArray *)data
{
[self setCollectionListItems: data];
}
# pragma mark - MangoPluginDelegate
-(void) addPluginNamed: (NSString *) name withInstance:(id<MangoPlugin>) plugin
{
MangoPluginTabItem *item = [[MangoPluginTabItem alloc] init];
[item setTitle:name];
[item setHasCloseButton:YES];
NSTabViewItem *newTab = [[NSTabViewItem alloc] initWithIdentifier:item];
[[self tabView] addTabViewItem:newTab];
[[self tabView] selectTabViewItem:newTab];
if ([plugin isKindOfClass:[NSViewController class]])
{
NSViewController *pVC = (NSViewController *)plugin;
[[pVC view] setFrame:[[newTab view]frame]];
[[newTab view] addSubview:[pVC view]];
}
[[self tabBarView] setNeedsUpdate:YES];
[[self pluginManager] setActivePlugin:plugin withName: name];
}
-(id<MangoPlugin>) createPluginInstanceWithClass:(Class)cls andInstanceName:(NSString *)name
{
id<MangoPlugin> newPlugin = [[cls alloc] init];
return newPlugin;
}
- (void)tabView:(NSTabView *)aTabView didCloseTabViewItem:(NSTabViewItem *)tabViewItem
{
[[self pluginManager] removePluginNamed:[tabViewItem label]];
[[self tabBarView] setNeedsUpdate:YES];
}
#pragma mark - Outline delegate
- (NSCell*) outlineView:(NSOutlineView*) outlineView dataCellForTableColumn:(NSTableColumn*) tableColumn item:(id) item
{
CollectionListCell *cell = [[CollectionListCell alloc] init];
NSTreeNode *t_item = (NSTreeNode *) item;
NSNumber * isGFS = [[t_item representedObject] valueForKey:@"GridFS"];
if (isGFS && [isGFS boolValue])
{
[cell setIsGridFS:YES];
}
else
{
[cell setIsGridFS:NO];
}
return cell;
}
- (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
{
return 25;
}
@end