OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/table_model_array_controller.h" | 5 #import "chrome/browser/ui/cocoa/table_model_array_controller.h" |
6 | 6 |
7 #include "app/table_model.h" | 7 #include "app/table_model.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/browser/remove_rows_table_model.h" | 10 #include "chrome/browser/remove_rows_table_model.h" |
11 | 11 |
12 @interface TableModelArrayController (PrivateMethods) | 12 @interface TableModelArrayController () |
13 | 13 |
14 - (NSUInteger)offsetForGroupID:(int)groupID; | 14 - (NSUInteger)offsetForGroupID:(int)groupID; |
15 - (NSUInteger)offsetForGroupID:(int)groupID startingOffset:(NSUInteger)offset; | 15 - (NSUInteger)offsetForGroupID:(int)groupID startingOffset:(NSUInteger)offset; |
16 - (NSIndexSet*)controllerRowsForModelRowsInRange:(NSRange)range; | 16 - (NSIndexSet*)controllerRowsForModelRowsInRange:(NSRange)range; |
17 - (void)setModelRows:(RemoveRowsTableModel::Rows*)modelRows | 17 - (void)setModelRows:(RemoveRowsTableModel::Rows*)modelRows |
18 fromControllerRows:(NSIndexSet*)rows; | 18 fromControllerRows:(NSIndexSet*)rows; |
19 - (void)modelDidChange; | 19 - (void)modelDidChange; |
20 - (void)modelDidAddItemsInRange:(NSRange)range; | 20 - (void)modelDidAddItemsInRange:(NSRange)range; |
21 - (void)modelDidRemoveItemsInRange:(NSRange)range; | 21 - (void)modelDidRemoveItemsInRange:(NSRange)range; |
22 - (NSDictionary*)columnValuesForRow:(NSInteger)row; | 22 - (NSDictionary*)columnValuesForRow:(NSInteger)row; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 NSUInteger offset = 1; | 143 NSUInteger offset = 1; |
144 for (NSUInteger i = range.location; i < NSMaxRange(range); ++i) { | 144 for (NSUInteger i = range.location; i < NSMaxRange(range); ++i) { |
145 int group = model_->GetGroupID(i); | 145 int group = model_->GetGroupID(i); |
146 offset = [self offsetForGroupID:group startingOffset:offset]; | 146 offset = [self offsetForGroupID:group startingOffset:offset]; |
147 [indexes addIndex:i + offset]; | 147 [indexes addIndex:i + offset]; |
148 } | 148 } |
149 return indexes; | 149 return indexes; |
150 } | 150 } |
151 | 151 |
152 - (void)modelDidAddItemsInRange:(NSRange)range { | 152 - (void)modelDidAddItemsInRange:(NSRange)range { |
| 153 if (range.length == 0) |
| 154 return; |
153 NSMutableArray* rows = [NSMutableArray arrayWithCapacity:range.length]; | 155 NSMutableArray* rows = [NSMutableArray arrayWithCapacity:range.length]; |
154 for (NSUInteger i=range.location; i<NSMaxRange(range); ++i) | 156 for (NSUInteger i = range.location; i < NSMaxRange(range); ++i) |
155 [rows addObject:[self columnValuesForRow:i]]; | 157 [rows addObject:[self columnValuesForRow:i]]; |
156 [self insertObjects:rows | 158 NSArray* indexes = [self controllerRowsForModelRowsInRange:range]; |
157 atArrangedObjectIndexes:[self controllerRowsForModelRowsInRange:range]]; | 159 [self insertObjects:rows atArrangedObjectIndexes:indexes]; |
158 } | 160 } |
159 | 161 |
160 - (void)modelDidRemoveItemsInRange:(NSRange)range { | 162 - (void)modelDidRemoveItemsInRange:(NSRange)range { |
| 163 if (range.length == 0) |
| 164 return; |
161 NSMutableIndexSet* indexes = | 165 NSMutableIndexSet* indexes = |
162 [NSMutableIndexSet indexSetWithIndexesInRange:range]; | 166 [NSMutableIndexSet indexSetWithIndexesInRange:range]; |
163 if (model_->HasGroups()) { | 167 if (model_->HasGroups()) { |
164 // When this method is called, the model has already removed items, so | 168 // When this method is called, the model has already removed items, so |
165 // accessing items in the model from |range.location| on may not be possible | 169 // accessing items in the model from |range.location| on may not be possible |
166 // anymore. Therefore we use the item right before that, if it exists. | 170 // anymore. Therefore we use the item right before that, if it exists. |
167 NSUInteger offset = 0; | 171 NSUInteger offset = 0; |
168 if (range.location > 0) { | 172 if (range.location > 0) { |
169 int last_group = model_->GetGroupID(range.location - 1); | 173 int last_group = model_->GetGroupID(range.location - 1); |
170 offset = [self offsetForGroupID:last_group]; | 174 offset = [self offsetForGroupID:last_group]; |
(...skipping 19 matching lines...) Expand all Loading... |
190 forKey:kGroupID]; | 194 forKey:kGroupID]; |
191 } | 195 } |
192 for (NSString* identifier in columns_.get()) { | 196 for (NSString* identifier in columns_.get()) { |
193 int column_id = [[columns_ objectForKey:identifier] intValue]; | 197 int column_id = [[columns_ objectForKey:identifier] intValue]; |
194 std::wstring text = model_->GetText(row, column_id); | 198 std::wstring text = model_->GetText(row, column_id); |
195 [dict setObject:base::SysWideToNSString(text) forKey:identifier]; | 199 [dict setObject:base::SysWideToNSString(text) forKey:identifier]; |
196 } | 200 } |
197 return dict; | 201 return dict; |
198 } | 202 } |
199 | 203 |
200 // Overridden from NSArrayController ----------------------------------------- | 204 #pragma mark Overridden from NSArrayController |
201 | 205 |
202 - (BOOL)canRemove { | 206 - (BOOL)canRemove { |
203 if (!model_) | 207 if (!model_) |
204 return NO; | 208 return NO; |
205 RemoveRowsTableModel::Rows rows; | 209 RemoveRowsTableModel::Rows rows; |
206 [self setModelRows:&rows fromControllerRows:[self selectionIndexes]]; | 210 [self setModelRows:&rows fromControllerRows:[self selectionIndexes]]; |
207 return model_->CanRemoveRows(rows); | 211 return model_->CanRemoveRows(rows); |
208 } | 212 } |
209 | 213 |
210 - (IBAction)remove:(id)sender { | 214 - (IBAction)remove:(id)sender { |
211 RemoveRowsTableModel::Rows rows; | 215 RemoveRowsTableModel::Rows rows; |
212 [self setModelRows:&rows fromControllerRows:[self selectionIndexes]]; | 216 [self setModelRows:&rows fromControllerRows:[self selectionIndexes]]; |
213 model_->RemoveRows(rows); | 217 model_->RemoveRows(rows); |
214 } | 218 } |
215 | 219 |
216 // Table View Delegate -------------------------------------------------------- | 220 #pragma mark NSTableView delegate methods |
217 | 221 |
218 - (BOOL)tableView:(NSTableView*)tv isGroupRow:(NSInteger)row { | 222 - (BOOL)tableView:(NSTableView*)tableView isGroupRow:(NSInteger)row { |
219 NSDictionary* values = [[self arrangedObjects] objectAtIndex:row]; | 223 NSDictionary* values = [[self arrangedObjects] objectAtIndex:row]; |
220 return [[values objectForKey:kIsGroupRow] boolValue]; | 224 return [[values objectForKey:kIsGroupRow] boolValue]; |
221 } | 225 } |
222 | 226 |
223 - (NSIndexSet*)tableView:(NSTableView*)tableView | 227 - (NSIndexSet*)tableView:(NSTableView*)tableView |
224 selectionIndexesForProposedSelection:(NSIndexSet*)proposedIndexes { | 228 selectionIndexesForProposedSelection:(NSIndexSet*)proposedIndexes { |
225 NSMutableIndexSet* indexes = [proposedIndexes mutableCopy]; | 229 NSMutableIndexSet* indexes = [proposedIndexes mutableCopy]; |
226 for (NSUInteger i = [proposedIndexes firstIndex]; | 230 for (NSUInteger i = [proposedIndexes firstIndex]; |
227 i != NSNotFound; | 231 i != NSNotFound; |
228 i = [proposedIndexes indexGreaterThanIndex:i]) { | 232 i = [proposedIndexes indexGreaterThanIndex:i]) { |
229 if ([self tableView:tableView isGroupRow:i]) { | 233 if ([self tableView:tableView isGroupRow:i]) { |
230 [indexes removeIndex:i]; | 234 [indexes removeIndex:i]; |
231 NSUInteger row = i + 1; | 235 NSUInteger row = i + 1; |
232 while (row < [[self arrangedObjects] count] && | 236 while (row < [[self arrangedObjects] count] && |
233 ![self tableView:tableView isGroupRow:row]) | 237 ![self tableView:tableView isGroupRow:row]) |
234 [indexes addIndex:row++]; | 238 [indexes addIndex:row++]; |
235 } | 239 } |
236 } | 240 } |
237 return indexes; | 241 return indexes; |
238 } | 242 } |
239 | 243 |
240 // Actions -------------------------------------------------------------------- | 244 #pragma mark Actions |
241 | 245 |
242 - (IBAction)removeAll:(id)sender { | 246 - (IBAction)removeAll:(id)sender { |
243 model_->RemoveAll(); | 247 model_->RemoveAll(); |
244 } | 248 } |
245 | 249 |
246 @end | 250 @end |
| 251 |
OLD | NEW |