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
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/cocoa/table_model_array_controller.h"
#include "app/table_model.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/remove_rows_table_model.h"
@interface TableModelArrayController (PrivateMethods)
- (NSUInteger)offsetForGroupID:(int)groupID;
- (NSUInteger)offsetForGroupID:(int)groupID startingOffset:(NSUInteger)offset;
- (NSIndexSet*)controllerRowsForModelRowsInRange:(NSRange)range;
- (void)setModelRows:(RemoveRowsTableModel::Rows*)modelRows
fromControllerRows:(NSIndexSet*)rows;
- (void)modelDidChange;
- (void)modelDidAddItemsInRange:(NSRange)range;
- (void)modelDidRemoveItemsInRange:(NSRange)range;
- (NSDictionary*)columnValuesForRow:(NSInteger)row;
@end
// Observer for a RemoveRowsTableModel.
class RemoveRowsObserverBridge : public TableModelObserver {
public:
RemoveRowsObserverBridge(TableModelArrayController* controller)
: controller_(controller) {}
virtual ~RemoveRowsObserverBridge() {}
// TableModelObserver methods
virtual void OnModelChanged();
virtual void OnItemsChanged(int start, int length);
virtual void OnItemsAdded(int start, int length);
virtual void OnItemsRemoved(int start, int length);
private:
TableModelArrayController* controller_; // weak
};
void RemoveRowsObserverBridge::OnModelChanged() {
[controller_ modelDidChange];
}
void RemoveRowsObserverBridge::OnItemsChanged(int start, int length) {
OnItemsRemoved(start, length);
OnItemsAdded(start, length);
}
void RemoveRowsObserverBridge::OnItemsAdded(int start, int length) {
[controller_ modelDidAddItemsInRange:NSMakeRange(start, length)];
}
void RemoveRowsObserverBridge::OnItemsRemoved(int start, int length) {
[controller_ modelDidRemoveItemsInRange:NSMakeRange(start, length)];
}
@implementation TableModelArrayController
static NSString* const kIsGroupRow = @"_is_group_row";
static NSString* const kGroupID = @"_group_id";
- (void)bindToTableModel:(RemoveRowsTableModel*)model
withColumns:(NSDictionary*)columns
groupTitleColumn:(NSString*)groupTitleColumn {
model_ = model;
tableObserver_.reset(new RemoveRowsObserverBridge(self));
columns_.reset([columns copy]);
groupTitle_.reset([groupTitleColumn copy]);
model_->SetObserver(tableObserver_.get());
[self modelDidChange];
}
- (void)modelDidChange {
NSIndexSet* indexes = [NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(0, [[self arrangedObjects] count])];
[self removeObjectsAtArrangedObjectIndexes:indexes];
if (model_->HasGroups()) {
const TableModel::Groups& groups = model_->GetGroups();
DCHECK(groupTitle_.get());
for (TableModel::Groups::const_iterator it = groups.begin();
it != groups.end(); ++it) {
NSDictionary* group = [NSDictionary dictionaryWithObjectsAndKeys:
base::SysWideToNSString(it->title), groupTitle_.get(),
[NSNumber numberWithBool:YES], kIsGroupRow,
nil];
[self addObject:group];
}
}
[self modelDidAddItemsInRange:NSMakeRange(0, model_->RowCount())];
}
- (NSUInteger)offsetForGroupID:(int)groupID startingOffset:(NSUInteger)offset {
const TableModel::Groups& groups = model_->GetGroups();
DCHECK_GT(offset, 0u);
for (NSUInteger i = offset - 1; i < groups.size(); ++i) {
if (groups[i].id == groupID)
return i + 1;
}
NOTREACHED();
return NSNotFound;
}
- (NSUInteger)offsetForGroupID:(int)groupID {
return [self offsetForGroupID:groupID startingOffset:1];
}
- (int)groupIDForControllerRow:(NSUInteger)row {
NSDictionary* values = [[self arrangedObjects] objectAtIndex:row];
return [[values objectForKey:kGroupID] intValue];
}
- (void)setModelRows:(RemoveRowsTableModel::Rows*)modelRows
fromControllerRows:(NSIndexSet*)rows {
if ([rows count] == 0)
return;
if (!model_->HasGroups()) {
for (NSUInteger i = [rows firstIndex];
i != NSNotFound;
i = [rows indexGreaterThanIndex:i]) {
modelRows->insert(i);
}
return;
}
NSUInteger offset = 1;
for (NSUInteger i = [rows firstIndex];
i != NSNotFound;
i = [rows indexGreaterThanIndex:i]) {
int group = [self groupIDForControllerRow:i];
offset = [self offsetForGroupID:group startingOffset:offset];
modelRows->insert(i - offset);
}
}
- (NSIndexSet*)controllerRowsForModelRowsInRange:(NSRange)range {
if (!model_->HasGroups())
return [NSIndexSet indexSetWithIndexesInRange:range];
NSMutableIndexSet* indexes = [NSMutableIndexSet indexSet];
NSUInteger offset = 1;
for (NSUInteger i = range.location; i < NSMaxRange(range); ++i) {
int group = model_->GetGroupID(i);
offset = [self offsetForGroupID:group startingOffset:offset];
[indexes addIndex:i + offset];
}
return indexes;
}
- (void)modelDidAddItemsInRange:(NSRange)range {
NSMutableArray* rows = [NSMutableArray arrayWithCapacity:range.length];
for (NSUInteger i=range.location; i<NSMaxRange(range); ++i)
[rows addObject:[self columnValuesForRow:i]];
[self insertObjects:rows
atArrangedObjectIndexes:[self controllerRowsForModelRowsInRange:range]];
}
- (void)modelDidRemoveItemsInRange:(NSRange)range {
NSMutableIndexSet* indexes =
[NSMutableIndexSet indexSetWithIndexesInRange:range];
if (model_->HasGroups()) {
// When this method is called, the model has already removed items, so
// accessing items in the model from |range.location| on may not be possible
// anymore. Therefore we use the item right before that, if it exists.
NSUInteger offset = 0;
if (range.location > 0) {
int last_group = model_->GetGroupID(range.location - 1);
offset = [self offsetForGroupID:last_group];
}
[indexes shiftIndexesStartingAtIndex:0 by:offset];
for (NSUInteger row = range.location + offset;
row < NSMaxRange(range) + offset;
++row) {
if ([self tableView:nil isGroupRow:row]) {
// Skip over group rows.
[indexes shiftIndexesStartingAtIndex:row by:1];
offset++;
}
}
}
[self removeObjectsAtArrangedObjectIndexes:indexes];
}
- (NSDictionary*)columnValuesForRow:(NSInteger)row {
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
if (model_->HasGroups()) {
[dict setObject:[NSNumber numberWithInt:model_->GetGroupID(row)]
forKey:kGroupID];
}
for (NSString* identifier in columns_.get()) {
int column_id = [[columns_ objectForKey:identifier] intValue];
std::wstring text = model_->GetText(row, column_id);
[dict setObject:base::SysWideToNSString(text) forKey:identifier];
}
return dict;
}
// Overridden from NSArrayController -----------------------------------------
- (BOOL)canRemove {
if (!model_)
return NO;
RemoveRowsTableModel::Rows rows;
[self setModelRows:&rows fromControllerRows:[self selectionIndexes]];
return model_->CanRemoveRows(rows);
}
- (IBAction)remove:(id)sender {
RemoveRowsTableModel::Rows rows;
[self setModelRows:&rows fromControllerRows:[self selectionIndexes]];
model_->RemoveRows(rows);
}
// Table View Delegate --------------------------------------------------------
- (BOOL)tableView:(NSTableView*)tv isGroupRow:(NSInteger)row {
NSDictionary* values = [[self arrangedObjects] objectAtIndex:row];
return [[values objectForKey:kIsGroupRow] boolValue];
}
- (NSIndexSet*)tableView:(NSTableView*)tableView
selectionIndexesForProposedSelection:(NSIndexSet*)proposedIndexes {
NSMutableIndexSet* indexes = [proposedIndexes mutableCopy];
for (NSUInteger i = [proposedIndexes firstIndex];
i != NSNotFound;
i = [proposedIndexes indexGreaterThanIndex:i]) {
if ([self tableView:tableView isGroupRow:i]) {
[indexes removeIndex:i];
NSUInteger row = i + 1;
while (row < [[self arrangedObjects] count] &&
![self tableView:tableView isGroupRow:row])
[indexes addIndex:row++];
}
}
return indexes;
}
// Actions --------------------------------------------------------------------
- (IBAction)removeAll:(id)sender {
model_->RemoveAll();
}
@end
|