Chromium Code Reviews| 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 (PrivateMethods) |
|
dmac
2011/01/06 06:04:05
no need anymore to have these as (PrivateMethods).
Bernhard Bauer
2011/01/08 15:41:02
Ah! Done.
| |
| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 NSDictionary* group = [NSDictionary dictionaryWithObjectsAndKeys: | 85 NSDictionary* group = [NSDictionary dictionaryWithObjectsAndKeys: |
| 86 base::SysWideToNSString(it->title), groupTitle_.get(), | 86 base::SysWideToNSString(it->title), groupTitle_.get(), |
| 87 [NSNumber numberWithBool:YES], kIsGroupRow, | 87 [NSNumber numberWithBool:YES], kIsGroupRow, |
| 88 nil]; | 88 nil]; |
| 89 [self addObject:group]; | 89 [self addObject:group]; |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 [self modelDidAddItemsInRange:NSMakeRange(0, model_->RowCount())]; | 92 [self modelDidAddItemsInRange:NSMakeRange(0, model_->RowCount())]; |
| 93 } | 93 } |
| 94 | 94 |
| 95 - (NSUInteger)offsetForGroupID:(int)groupID startingOffset:(NSUInteger)offset { | 95 - (NSUInteger)offsetForGroupID:(int)groupID startingOffset:(NSUInteger)offset { |
|
dmac
2011/01/06 06:04:05
any good reason to make this 1 based as opposed to
Bernhard Bauer
2011/01/08 15:41:02
If the TableModel has groups, we add one additiona
| |
| 96 const TableModel::Groups& groups = model_->GetGroups(); | 96 const TableModel::Groups& groups = model_->GetGroups(); |
| 97 DCHECK_GT(offset, 0u); | 97 DCHECK_GT(offset, 0u); |
| 98 for (NSUInteger i = offset - 1; i < groups.size(); ++i) { | 98 for (NSUInteger i = offset - 1; i < groups.size(); ++i) { |
| 99 if (groups[i].id == groupID) | 99 if (groups[i].id == groupID) |
| 100 return i + 1; | 100 return i + 1; |
| 101 } | 101 } |
| 102 NOTREACHED(); | 102 NOTREACHED(); |
| 103 return NSNotFound; | 103 return NSNotFound; |
| 104 } | 104 } |
| 105 | 105 |
| (...skipping 37 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 NSMutableArray* rows = [NSMutableArray arrayWithCapacity:range.length]; | 153 NSMutableArray* rows = [NSMutableArray arrayWithCapacity:range.length]; |
|
dmac
2011/01/06 06:04:05
check to see if range.length is 0, and just return
Bernhard Bauer
2011/01/08 15:41:02
OK, done. I don't think anyone actually calls this
| |
| 154 for (NSUInteger i=range.location; i<NSMaxRange(range); ++i) | 154 for (NSUInteger i=range.location; i<NSMaxRange(range); ++i) |
|
dmac
2011/01/06 06:04:05
spaces around < and =
Bernhard Bauer
2011/01/08 15:41:02
Done.
| |
| 155 [rows addObject:[self columnValuesForRow:i]]; | 155 [rows addObject:[self columnValuesForRow:i]]; |
| 156 [self insertObjects:rows | 156 [self insertObjects:rows |
| 157 atArrangedObjectIndexes:[self controllerRowsForModelRowsInRange:range]]; | 157 atArrangedObjectIndexes:[self controllerRowsForModelRowsInRange:range]]; |
|
dmac
2011/01/06 06:04:05
you could probably break this line into two lines
Bernhard Bauer
2011/01/08 15:41:02
Done.
| |
| 158 } | 158 } |
| 159 | 159 |
| 160 - (void)modelDidRemoveItemsInRange:(NSRange)range { | 160 - (void)modelDidRemoveItemsInRange:(NSRange)range { |
| 161 NSMutableIndexSet* indexes = | 161 NSMutableIndexSet* indexes = |
| 162 [NSMutableIndexSet indexSetWithIndexesInRange:range]; | 162 [NSMutableIndexSet indexSetWithIndexesInRange:range]; |
| 163 if (model_->HasGroups()) { | 163 if (model_->HasGroups()) { |
| 164 // When this method is called, the model has already removed items, so | 164 // 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 | 165 // 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. | 166 // anymore. Therefore we use the item right before that, if it exists. |
| 167 NSUInteger offset = 0; | 167 NSUInteger offset = 0; |
| 168 if (range.location > 0) { | 168 if (range.location > 0) { |
|
dmac
2011/01/06 06:04:05
Should you check range for a length of 0? in the c
Bernhard Bauer
2011/01/08 15:41:02
Done.
| |
| 169 int last_group = model_->GetGroupID(range.location - 1); | 169 int last_group = model_->GetGroupID(range.location - 1); |
| 170 offset = [self offsetForGroupID:last_group]; | 170 offset = [self offsetForGroupID:last_group]; |
| 171 } | 171 } |
| 172 [indexes shiftIndexesStartingAtIndex:0 by:offset]; | 172 [indexes shiftIndexesStartingAtIndex:0 by:offset]; |
| 173 for (NSUInteger row = range.location + offset; | 173 for (NSUInteger row = range.location + offset; |
| 174 row < NSMaxRange(range) + offset; | 174 row < NSMaxRange(range) + offset; |
| 175 ++row) { | 175 ++row) { |
| 176 if ([self tableView:nil isGroupRow:row]) { | 176 if ([self tableView:nil isGroupRow:row]) { |
| 177 // Skip over group rows. | 177 // Skip over group rows. |
| 178 [indexes shiftIndexesStartingAtIndex:row by:1]; | 178 [indexes shiftIndexesStartingAtIndex:row by:1]; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 206 [self setModelRows:&rows fromControllerRows:[self selectionIndexes]]; | 206 [self setModelRows:&rows fromControllerRows:[self selectionIndexes]]; |
| 207 return model_->CanRemoveRows(rows); | 207 return model_->CanRemoveRows(rows); |
| 208 } | 208 } |
| 209 | 209 |
| 210 - (IBAction)remove:(id)sender { | 210 - (IBAction)remove:(id)sender { |
| 211 RemoveRowsTableModel::Rows rows; | 211 RemoveRowsTableModel::Rows rows; |
| 212 [self setModelRows:&rows fromControllerRows:[self selectionIndexes]]; | 212 [self setModelRows:&rows fromControllerRows:[self selectionIndexes]]; |
| 213 model_->RemoveRows(rows); | 213 model_->RemoveRows(rows); |
| 214 } | 214 } |
| 215 | 215 |
| 216 // Table View Delegate -------------------------------------------------------- | 216 // Table View Delegate -------------------------------------------------------- |
|
dmac
2011/01/06 06:04:05
instead of these style breaks, perhaps "#pragma ma
Bernhard Bauer
2011/01/08 15:41:02
Done.
| |
| 217 | 217 |
| 218 - (BOOL)tableView:(NSTableView*)tv isGroupRow:(NSInteger)row { | 218 - (BOOL)tableView:(NSTableView*)tv isGroupRow:(NSInteger)row { |
|
dmac
2011/01/06 06:04:05
usually not keen on abbreviations like "tv"
Bernhard Bauer
2011/01/08 15:41:02
Done.
| |
| 219 NSDictionary* values = [[self arrangedObjects] objectAtIndex:row]; | 219 NSDictionary* values = [[self arrangedObjects] objectAtIndex:row]; |
| 220 return [[values objectForKey:kIsGroupRow] boolValue]; | 220 return [[values objectForKey:kIsGroupRow] boolValue]; |
| 221 } | 221 } |
| 222 | 222 |
| 223 - (NSIndexSet*)tableView:(NSTableView*)tableView | 223 - (NSIndexSet*)tableView:(NSTableView*)tableView |
| 224 selectionIndexesForProposedSelection:(NSIndexSet*)proposedIndexes { | 224 selectionIndexesForProposedSelection:(NSIndexSet*)proposedIndexes { |
| 225 NSMutableIndexSet* indexes = [proposedIndexes mutableCopy]; | 225 NSMutableIndexSet* indexes = [proposedIndexes mutableCopy]; |
| 226 for (NSUInteger i = [proposedIndexes firstIndex]; | 226 for (NSUInteger i = [proposedIndexes firstIndex]; |
| 227 i != NSNotFound; | 227 i != NSNotFound; |
| 228 i = [proposedIndexes indexGreaterThanIndex:i]) { | 228 i = [proposedIndexes indexGreaterThanIndex:i]) { |
| 229 if ([self tableView:tableView isGroupRow:i]) { | 229 if ([self tableView:tableView isGroupRow:i]) { |
| 230 [indexes removeIndex:i]; | 230 [indexes removeIndex:i]; |
| 231 NSUInteger row = i + 1; | 231 NSUInteger row = i + 1; |
| 232 while (row < [[self arrangedObjects] count] && | 232 while (row < [[self arrangedObjects] count] && |
| 233 ![self tableView:tableView isGroupRow:row]) | 233 ![self tableView:tableView isGroupRow:row]) |
| 234 [indexes addIndex:row++]; | 234 [indexes addIndex:row++]; |
| 235 } | 235 } |
| 236 } | 236 } |
| 237 return indexes; | 237 return indexes; |
| 238 } | 238 } |
| 239 | 239 |
| 240 // Actions -------------------------------------------------------------------- | 240 // Actions -------------------------------------------------------------------- |
| 241 | 241 |
| 242 - (IBAction)removeAll:(id)sender { | 242 - (IBAction)removeAll:(id)sender { |
| 243 model_->RemoveAll(); | 243 model_->RemoveAll(); |
| 244 } | 244 } |
| 245 | 245 |
| 246 @end | 246 @end |
| 247 | |
| OLD | NEW |