OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/cocoa/bookmark_groups_controller.h" |
| 6 |
| 7 #include "base/sys_string_conversions.h" |
| 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 9 #import "chrome/browser/cocoa/bookmark_manager_controller.h" |
| 10 #import "chrome/browser/cocoa/bookmark_tree_controller.h" |
| 11 #import "third_party/apple/ImageAndTextCell.h" |
| 12 |
| 13 |
| 14 @interface BookmarkGroupsController () |
| 15 - (void)syncSelection; |
| 16 - (void)reload; |
| 17 @end |
| 18 |
| 19 |
| 20 @implementation BookmarkGroupsController |
| 21 |
| 22 @synthesize groups = groups_, selectedGroup = selectedGroup_; |
| 23 |
| 24 -(void)dealloc { |
| 25 [groups_ release]; |
| 26 [selectedGroup_ release]; |
| 27 [super dealloc]; |
| 28 } |
| 29 |
| 30 // Completely reloads the contents of the table view. |
| 31 - (void)reload { |
| 32 NSMutableArray* groups = [NSMutableArray array]; |
| 33 BookmarkModel* bookmarkModel = [manager_ bookmarkModel]; |
| 34 const BookmarkNode* node = bookmarkModel->GetBookmarkBarNode(); |
| 35 [groups addObject:[manager_ itemFromNode:node]]; |
| 36 node = bookmarkModel->other_node(); |
| 37 for (int i = 0; i < node->GetChildCount(); i++) { |
| 38 const BookmarkNode* child = node->GetChild(i); |
| 39 if (child->is_folder()) { |
| 40 [groups addObject:[manager_ itemFromNode:child]]; |
| 41 } |
| 42 } |
| 43 //TODO(snej): Append Recents and Search groups |
| 44 |
| 45 if (![groups isEqual:[self groups]]) { |
| 46 [self setGroups:groups]; |
| 47 [groupsTable_ reloadData]; |
| 48 [self syncSelection]; |
| 49 } |
| 50 } |
| 51 |
| 52 // Responds to changes in the bookmark data model. |
| 53 - (void)nodeChanged:(const BookmarkNode*)node |
| 54 childrenChanged:(BOOL)childrenChanged { |
| 55 if (node == [manager_ bookmarkModel]->other_node()) { |
| 56 [self reload]; |
| 57 } |
| 58 } |
| 59 |
| 60 // Returns the selected BookmarkNode. |
| 61 - (const BookmarkNode*)selectedNode { |
| 62 NSInteger row = [groupsTable_ selectedRow]; |
| 63 if (row < 0) |
| 64 return NULL; |
| 65 id item = [groups_ objectAtIndex:row]; |
| 66 return [manager_ nodeFromItem:item]; |
| 67 } |
| 68 |
| 69 - (id)selectedGroup { |
| 70 return selectedGroup_; |
| 71 } |
| 72 |
| 73 - (void)setSelectedGroup:(id)group { |
| 74 [selectedGroup_ autorelease]; |
| 75 selectedGroup_ = [group retain]; |
| 76 |
| 77 NSInteger row = group ? [groups_ indexOfObject:group] : NSNotFound; |
| 78 if (row != NSNotFound) |
| 79 [groupsTable_ selectRow:row byExtendingSelection:NO]; |
| 80 else |
| 81 [groupsTable_ deselectAll:self]; |
| 82 } |
| 83 |
| 84 - (NSTableView*)groupsTable { |
| 85 return groupsTable_; |
| 86 } |
| 87 |
| 88 |
| 89 #pragma mark - |
| 90 #pragma mark COMMANDS: |
| 91 |
| 92 |
| 93 // Updates the |selectedGroup| property based on the table view's selection. |
| 94 - (void)syncSelection { |
| 95 id selGroup = nil; |
| 96 NSInteger row = [groupsTable_ selectedRow]; |
| 97 if (row >= 0) { |
| 98 selGroup = [groups_ objectAtIndex:row]; |
| 99 if (![selGroup isKindOfClass:[NSValue class]]) |
| 100 selGroup = nil; |
| 101 } |
| 102 if (selGroup != [self selectedGroup]) |
| 103 [self setSelectedGroup:selGroup]; |
| 104 } |
| 105 |
| 106 // Called when a row is clicked; updates the |selectedGroup| property. |
| 107 - (IBAction)tableClicked:(id)sender { |
| 108 [self syncSelection]; |
| 109 } |
| 110 |
| 111 // The Delete command; also invoked by the delete key. |
| 112 - (IBAction)delete:(id)sender { |
| 113 const BookmarkNode* sel = [self selectedNode]; |
| 114 if (!sel) { |
| 115 NSBeep(); |
| 116 return; |
| 117 } |
| 118 const BookmarkNode* parent = sel->GetParent(); |
| 119 [manager_ bookmarkModel]->Remove(parent, parent->IndexOfChild(sel)); |
| 120 } |
| 121 |
| 122 |
| 123 #pragma mark - |
| 124 #pragma mark LIST VIEW: |
| 125 |
| 126 |
| 127 // Returns the number of rows in the table (NSTableView data source). |
| 128 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { |
| 129 return [groups_ count]; |
| 130 } |
| 131 |
| 132 // Returns the contents of a table cell (NSTableView data source). |
| 133 - (id) tableView:(NSTableView*)tableView |
| 134 objectValueForTableColumn:(NSTableColumn*)tableColumn |
| 135 row:(NSInteger)row { |
| 136 id item = [groups_ objectAtIndex:row]; |
| 137 return base::SysWideToNSString([manager_ nodeFromItem:item]->GetTitle()); |
| 138 } |
| 139 |
| 140 // Sets a table cell's icon before it's drawn (NSTableView delegate). |
| 141 - (void)tableView:(NSTableView*)tableView |
| 142 willDisplayCell:(id)cell |
| 143 forTableColumn:(NSTableColumn*)tableColumn |
| 144 row:(NSInteger)row { |
| 145 id item = [groups_ objectAtIndex:row]; |
| 146 [(ImageAndTextCell*)cell setImage:[manager_ iconForItem:item]]; |
| 147 } |
| 148 |
| 149 @end |
| 150 |
| 151 |
| 152 @implementation BookmarksTableView |
| 153 |
| 154 - (IBAction)delete:(id)sender { |
| 155 [(BookmarkGroupsController*)[self delegate] delete:sender]; |
| 156 } |
| 157 |
| 158 - (void)keyDown:(NSEvent*)event { |
| 159 if ([event keyCode] == 51) // Delete key |
| 160 [self delete:self]; |
| 161 else |
| 162 [super keyDown:event]; |
| 163 } |
| 164 |
| 165 @end |
OLD | NEW |