-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCTableHtmlFormatter.m
236 lines (139 loc) · 5.43 KB
/
BCTableHtmlFormatter.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
//
// BCTableHtmlFormatter.m
// Bartender
//
// Created by Tom Houpt on 12/7/15.
// Copyright 2012 Behavioral Cybernetics LLC. All rights reserved.
//
#import "BCTableHtmlFormatter.h"
@implementation BCTableHtmlFormatter
-(id)initWithTableView:(NSTableView *)table andTableID:(NSString *)tid; {
self = [super init];
if (self) {
tableView = table;
tableID = tid;
useVerticalLines = YES;
useAlternatingRowColors = YES;
}
return self;
}
-(void) setTableView:(NSTableView *)table; { tableView = table; }
- (NSString *) htmlString; {
NSMutableString *htmlString = [[NSMutableString alloc] init];
[self appendHtmlToString:htmlString];
return htmlString;
}
-(void)appendCssToString:(NSMutableString *)buffer; {
NSMutableString *cssString = [[NSMutableString alloc] init];
[cssString appendString: @"<style>\n"];
NSString *prefixString;
if (nil == tableID) {
prefixString = @".datatable";
}
else {
prefixString = [NSString stringWithFormat:@"#%@",tableID];
}
// specify 12 pt fonts for table
[cssString appendFormat: @"%@ th, td {font-family: Helvetica, sans-serif; font-size:9pt;} \n", prefixString];
// [cssString appendFormat: @"%@ table { border-style: hidden } \n", prefixString];
// // to put vertical rules between columns, set
// td: { border-style: none solid none solid; border-width:thin; }
// or is this better?
// col { border-style: none solid }
//
// // to put rule under header row, set
// th: { border-style: none none solid none; }
// make san-serif
if (useAlternatingRowColors) {
// http://www.w3.org/Style/Examples/007/evenodd.en.html
[cssString appendFormat: @"%@ tr:nth-child(odd) { background-color:#ddd; } \n", prefixString];
[cssString appendFormat: @"%@ tr:nth-child(even) { background-color:#fff; } \n", prefixString];
// be sure to set up the webpreferences of the webview to print backgrounds, e.g.
// [myWebView setPreferencesIdentifier:@"myWebPreferences"];
// WebPreferences *prefs = [myWebView preferences];
// [prefs setShouldPrintBackgrounds:YES];
}
[cssString appendString: @"</style>\n"];
[buffer appendString:cssString];
}
- (void) appendHtmlToString:(NSMutableString *)buffer; {
[self appendCssToString:buffer];
[buffer appendFormat: @"<TABLE "];
if (nil == tableID) {
[buffer appendString: @"class = \"datatable\" "];
}
else {
[buffer appendFormat: @"id = \"%@\" ",tableID];
}
NSString *rulesString;
if (!useVerticalLines && !useHorizontalLines) {rulesString = @"rules=none ";}
if ( useVerticalLines && !useHorizontalLines) {rulesString = @"rules=cols "; }
if (!useVerticalLines && useHorizontalLines) {rulesString = @"rules=rows "; }
if ( useVerticalLines && useHorizontalLines) {rulesString = @"rules=all "; }
if (nil == tableID){
[buffer appendString: rulesString];
}
[buffer appendString: @">\n"];
[self appendHeadersToString:buffer];
NSInteger rowIndex;
NSInteger numRows = [tableView numberOfRows];
for (rowIndex=0;rowIndex<numRows;rowIndex++) {
[self appendRowAtIndex:rowIndex toString:buffer];
}
[buffer appendString: @"</TABLE>\n"];
// NSError *error;
//
// NSString *testFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0] stringByAppendingPathComponent:@"tableHtmlTest.html"];
//
// [buffer writeToFile:testFilePath atomically:YES encoding:NSUnicodeStringEncoding error:&error];
//
// if (nil != error) {
// NSLog(@"BCTabledHtmlFormatter appendHtmlToString Error Desc: %@",[error localizedDescription]);
// NSLog(@"BCTabledHtmlFormatter appendHtmlToString Error Sugg: %@",[error localizedRecoverySuggestion]);
// }
}
-(void)appendHeadersToString:(NSMutableString *)buffer; {
[buffer appendString: @"\t<TR frame=\"below\">\n"];
for (NSTableColumn *aTableColumn in [tableView tableColumns]) {
[buffer appendString: @"\t\t<TH>"];
[buffer appendString:[[aTableColumn headerCell] stringValue]];
[buffer appendString: @"</TH>\n"];
}
[buffer appendString: @"\t</TR>\n"];
}
-(void)appendRowAtIndex:(NSInteger)rowIndex toString:(NSMutableString *)buffer; {
[buffer appendString: @"\t<TR>\n"];
for (NSTableColumn *aTableColumn in [tableView tableColumns]) {
[buffer appendString: @"\t\t<TD>"];
NSString *objectValue = [[tableView dataSource] tableView:tableView objectValueForTableColumn:aTableColumn row:rowIndex];
if (objectValue) { [buffer appendString:objectValue]; }
else {
// if blank column, leave table cell blank
if ([[[aTableColumn headerCell] stringValue] isEqualToString:[NSString string]]){
[buffer appendString:kBlankCellText];
}
else { [buffer appendString:kNoDataCellText]; }
}
[buffer appendString: @"</TD>\n"];
}
[buffer appendString: @"\t</TR>\n"];
}
//- (NSString *)stringForObjectValue:(id)anObject; {
//
// // Textual representation of cell content
// // taken from "MyValueFormatter.m" of "QTMetadataEditor" sample code
//
// NSString *resultString;
//
// if ([anObject isMemberOfClass:[NSData class]]) {
// // resultString = [MyValueFormatter hexStringFromData:(NSData *)anObject];
// }
// else if ([anObject isMemberOfClass:[NSString class]]) {
// resultString = anObject;
// }
// else if ([anObject isMemberOfClass:[NSNumber class]]) {
// resultString = [anObject stringValue];
// }
// return resultString;
//}
@end