-
Notifications
You must be signed in to change notification settings - Fork 8
/
DataManager.m
391 lines (313 loc) · 12 KB
/
DataManager.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
/*
* R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
*
* R.app Copyright notes:
* Copyright (C) 2004-5 The R Foundation
* written by Stefano M. Iacus and Simon Urbanek
*
*
* R Copyright notes:
* Copyright (C) 1995-1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1998-2012 The R Development Core Team
* Copyright (C) 2002-2004 The R Foundation
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* A copy of the GNU General Public License is available via WWW at
* http://www.gnu.org/copyleft/gpl.html. You can also obtain it by
* writing to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA.
*/
#import "DataManager.h"
#import "RController.h"
#import "REngine/REngine.h"
#import "RegexKitLite.h"
#import <WebKit/WebKit.h>
#import <WebKit/WebFrame.h>
#define kDataManagerData @"data"
#define kDataManagerPackage @"package"
#define kDataManagerDescription @"description"
#define kDataManagerURL @"URL"
#define kSortModeNone 0
#define kSortModeAsc 1
#define kSortModeDesc 2
static DataManager* sharedController;
@implementation DataManager
+ (DataManager*) sharedController{
return sharedController;
}
- (void)awakeFromNib
{
[RDataSource setDoubleAction:@selector(loadRData:)];
[dataInfoView setFrameLoadDelegate:self];
[self enableGUIActions:NO];
}
- (id)init
{
self = [super init];
if (self) {
sharedController = self;
datasets = [[NSMutableArray alloc] initWithCapacity:500];
filteredDatasets = [[NSMutableArray alloc] initWithCapacity:500];
sortMode = kSortModeNone;
sortedColumn = nil;
}
return self;
}
- (void)dealloc {
if(datasets) [datasets release], datasets = nil;
if(filteredDatasets) [filteredDatasets release], filteredDatasets = nil;
[super dealloc];
}
#pragma mark -
- (NSWindow*)window
{
return DataManagerWindow;
}
- (void)show
{
[RDataSource reloadData];
[DataManagerWindow makeKeyAndOrderFront:self];
}
#pragma mark -
- (void)updateDatasets:(int)count withNames:(char**)name descriptions:(char**)desc packages:(char**)pkg URLs:(char**)url
{
[self enableGUIActions:NO];
[[self window] display];
[self show];
[self resetDatasets];
NSInteger i = 0;
for(i = 0; i < count; i++) {
NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithUTF8String: name[i]], kDataManagerData,
[NSString stringWithUTF8String: desc[i]], kDataManagerDescription,
[NSString stringWithUTF8String: pkg[i]], kDataManagerPackage,
[NSString stringWithUTF8String: url[i]], kDataManagerURL,
nil
];
[datasets addObject:entry];
[filteredDatasets addObject:entry];
}
[self filterTable:nil];
[RDataSource reloadData];
[self enableGUIActions:YES];
}
- (void)resetDatasets
{
[datasets removeAllObjects];
[filteredDatasets removeAllObjects];
}
- (NSInteger)count
{
return [datasets count];
}
- (void)enableGUIActions:(BOOL)enabled
{
[loadButton setEnabled:enabled];
[refreshButton setEnabled:enabled];
[searchField setEnabled:enabled];
[RDataSource setEnabled:enabled];
}
#pragma mark -
- (IBAction)loadRData:(id)sender
{
NSIndexSet *selectedRows = [RDataSource selectedRowIndexes];
if(![selectedRows count]) return;
NSInteger anIndex = [selectedRows firstIndex];
NSMutableArray *data = [[NSMutableArray alloc] initWithCapacity:[selectedRows count]];
NSMutableArray *pkgs = [[NSMutableArray alloc] initWithCapacity:[selectedRows count]];
while(anIndex != NSNotFound) {
[data addObject:[[filteredDatasets objectAtIndex:anIndex] objectForKey:kDataManagerData]];
[pkgs addObject:[[filteredDatasets objectAtIndex:anIndex] objectForKey:kDataManagerPackage]];
anIndex = [selectedRows indexGreaterThanIndex:anIndex];
}
[[REngine mainEngine] evaluateString:[NSString stringWithFormat:@"data(%@,package=c(\"%@\"))",
[data componentsJoinedByString:@","],
[pkgs componentsJoinedByString:@"\",\""]]];
}
- (IBAction)filterTable:(id)sender
{
NSString *searchPattern = [searchField stringValue];
if(![searchPattern length]) {
[filteredDatasets setArray:datasets];
[RDataSource deselectAll:nil];
} else {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"data CONTAINS[cd] %@ OR package CONTAINS[cd] %@ OR description CONTAINS[cd] %@",
searchPattern, searchPattern, searchPattern];
[filteredDatasets setArray:[datasets filteredArrayUsingPredicate:predicate]];
}
if(sortedColumn) {
[RDataSource setIndicatorImage:(sortMode == kSortModeAsc) ? [NSImage imageNamed:@"NSAscendingSortIndicator"] : [NSImage imageNamed:@"NSDescendingSortIndicator"] inTableColumn:[RDataSource tableColumnWithIdentifier:sortedColumn]];
[RDataSource setHighlightedTableColumn:[RDataSource tableColumnWithIdentifier:sortedColumn]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortedColumn ascending:(sortMode == kSortModeAsc)
selector:@selector(localizedCaseInsensitiveCompare:)];
[filteredDatasets sortUsingDescriptors:
[NSArray arrayWithObjects:sortDescriptor, nil]];
[sortDescriptor release];
}
[RDataSource reloadData];
}
- (IBAction)reloadDatasets:(id)sender
{
[self enableGUIActions:NO];
[[self window] display];
[[REngine mainEngine] executeString:@"data.manager()"];
}
- (IBAction)showHelp:(id)sender
{
if([RDataSource numberOfSelectedRows] != 1) return;
NSInteger row = [RDataSource selectedRow];
NSDictionary *selectedItem = [filteredDatasets objectAtIndex:row];
SLog(@"DataManager showHelp: %@", selectedItem);
NSString *urlText = nil;
int port = [[RController sharedController] helpServerPort];
if (port == 0) {
NSRunInformationalAlertPanel(NLS(@"Cannot start HTML help server."), NLS(@"Help"), NLS(@"Ok"), nil, nil);
return;
}
NSString *topic = [selectedItem objectForKey:kDataManagerData];
NSRange r = [topic rangeOfString:@" ("];
if (r.length > 0 && [topic length] - r.length > 3) // some datasets have the topic in parents
topic = [topic substringWithRange: NSMakeRange(r.location + 2, [topic length] - r.location - 3)];
urlText = [NSString stringWithFormat:@"http://localhost:%d/library/%@/html/%@.html", port, [selectedItem objectForKey:kDataManagerPackage], topic];
[[dataInfoView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
}
- (IBAction)printDocument:(id)sender
{
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(showHelp:)
object:RDataSource];
NSPrintInfo *printInfo;
NSPrintOperation *printOp;
printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo setHorizontalPagination: NSFitPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];
printOp = [NSPrintOperation printOperationWithView:[[[dataInfoView mainFrame] frameView] documentView]
printInfo:printInfo];
[printOp setShowPanels:YES];
[printOp runOperationModalForWindow:[self window]
delegate:self
didRunSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:@""];
}
- (IBAction)executeSelection:(id)sender
{
DOMRange *dr = [dataInfoView selectedDOMRange];
if (dr) { /* we don't do line-exec since we don't get the text outside the selection */
NSString *stx = [dr markupString];
// Ok, some simple processing here - it may not work in all cases
stx = [stx stringByReplacingOccurrencesOfRegex:@"(?i)<br[^>]*?>" withString:@"\n"];
stx = [stx stringByReplacingOccurrencesOfRegex:@"<[^>]*?>" withString:@""];
stx = [stx stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
stx = [stx stringByReplacingOccurrencesOfString:@">" withString:@">"];
stx = [stx stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
[[RController sharedController] sendInput:stx];
}
}
#pragma mark -
#pragma mark tableView delegates
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [filteredDatasets count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
{
if([[tableColumn identifier] isEqualToString:kDataManagerPackage])
return [[filteredDatasets objectAtIndex:rowIndex] objectForKey:kDataManagerPackage];
else if([[tableColumn identifier] isEqualToString:kDataManagerData])
return [[filteredDatasets objectAtIndex:rowIndex] objectForKey:kDataManagerData];
else if([[tableColumn identifier] isEqualToString:kDataManagerDescription])
return [[filteredDatasets objectAtIndex:rowIndex] objectForKey:kDataManagerDescription];
return @"...";
}
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return NO;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
// Check our notification object is our table
if ([aNotification object] != RDataSource) return;
// Update Info delayed
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(showHelp:)
object:RDataSource];
[self performSelector:@selector(showHelp:) withObject:RDataSource afterDelay:0.5];
}
- (void)tableView:(NSTableView*)tableView didClickTableColumn:(NSTableColumn *)tableColumn
{
[tableView deselectAll:nil];
// Tri-state sorting none -> asc -> desc -> none -> ...
if(!sortedColumn || (sortedColumn && [sortedColumn isEqualToString:[tableColumn identifier]])) {
sortMode++;
if (sortMode > kSortModeDesc) sortMode = kSortModeNone;
} else {
sortMode = kSortModeAsc;
}
// remove sort indicator of old column
if(sortedColumn) {
[self filterTable:nil];
[tableView setIndicatorImage:nil inTableColumn:[tableView tableColumnWithIdentifier:sortedColumn]];
}
// remember last to be sorted column
sortedColumn = [tableColumn identifier];
NSSortDescriptor* sortDescriptor;
switch(sortMode) {
case kSortModeNone:
[tableView setIndicatorImage:nil inTableColumn:tableColumn];
[tableView setHighlightedTableColumn:nil];
[filteredDatasets removeAllObjects];
[filteredDatasets setArray:datasets];
sortedColumn = nil;
break;
case kSortModeAsc:
[tableView setIndicatorImage:[NSImage imageNamed:@"NSAscendingSortIndicator"] inTableColumn:tableColumn];
[tableView setHighlightedTableColumn:tableColumn];
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortedColumn ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
[filteredDatasets sortUsingDescriptors:
[NSArray arrayWithObjects:sortDescriptor, nil]];
[sortDescriptor release];
break;
case kSortModeDesc:
[tableView setIndicatorImage:[NSImage imageNamed:@"NSDescendingSortIndicator"] inTableColumn:tableColumn];
[tableView setHighlightedTableColumn:tableColumn];
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortedColumn ascending:NO
selector:@selector(localizedCaseInsensitiveCompare:)];
[filteredDatasets sortUsingDescriptors:
[NSArray arrayWithObjects:sortDescriptor, nil]];
[sortDescriptor release];
break;
}
[RDataSource reloadData];
}
#pragma mark -
- (void)sheetDidEnd:(id)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo
{
SLog(@"DataManager: sheetDidEnd: returnCode: %d contextInfo: %@", returnCode, contextInfo);
// Order out the sheet - could be a NSPanel or NSWindow
if ([sheet respondsToSelector:@selector(orderOut:)]) {
[sheet orderOut:nil];
}
else if ([sheet respondsToSelector:@selector(window)]) {
[[sheet window] orderOut:nil];
}
[DataManagerWindow makeKeyAndOrderFront:nil];
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if ([menuItem action] == @selector(executeSelection:)) {
return ([dataInfoView selectedDOMRange] == nil) ? NO : YES;
}
return YES;
}
@end