bookmark_editor_view.h revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_EDITOR_VIEW_H_ 6#define CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_EDITOR_VIEW_H_ 7 8#include <vector> 9 10#include "base/compiler_specific.h" 11#include "base/gtest_prod_util.h" 12#include "base/strings/string16.h" 13#include "chrome/browser/bookmarks/bookmark_expanded_state_tracker.h" 14#include "chrome/browser/bookmarks/bookmark_model_observer.h" 15#include "chrome/browser/ui/bookmarks/bookmark_editor.h" 16#include "ui/base/models/simple_menu_model.h" 17#include "ui/base/models/tree_node_model.h" 18#include "ui/views/context_menu_controller.h" 19#include "ui/views/controls/button/button.h" 20#include "ui/views/controls/textfield/textfield.h" 21#include "ui/views/controls/textfield/textfield_controller.h" 22#include "ui/views/controls/tree/tree_view_controller.h" 23#include "ui/views/window/dialog_delegate.h" 24 25namespace views { 26class Label; 27class LabelButton; 28class MenuRunner; 29class TreeView; 30} 31 32class BookmarkEditorViewTest; 33class GURL; 34class Menu; 35class Profile; 36 37// View that allows the user to edit a bookmark/starred URL. The user can 38// change the URL, title and where the bookmark appears as well as adding 39// new folders and changing the name of other folders. The editor is used for 40// both editing a url bookmark, as well as editing a folder bookmark when 41// created from 'Bookmark all tabs'. 42// 43// Edits are applied to the BookmarkModel when the user presses 'OK'. 44// 45// To use BookmarkEditorView invoke the static show method. 46 47class BookmarkEditorView : public BookmarkEditor, 48 public views::ButtonListener, 49 public views::TreeViewController, 50 public views::DialogDelegateView, 51 public views::TextfieldController, 52 public views::ContextMenuController, 53 public ui::SimpleMenuModel::Delegate, 54 public BookmarkModelObserver { 55 public: 56 // Type of node in the tree. Public purely for testing. 57 typedef ui::TreeNodeWithValue<int64> EditorNode; 58 59 // Model for the TreeView. Trivial subclass that doesn't allow titles with 60 // empty strings. Public purely for testing. 61 class EditorTreeModel : public ui::TreeNodeModel<EditorNode> { 62 public: 63 explicit EditorTreeModel(EditorNode* root) 64 : ui::TreeNodeModel<EditorNode>(root) {} 65 66 virtual void SetTitle(ui::TreeModelNode* node, 67 const string16& title) OVERRIDE; 68 69 private: 70 DISALLOW_COPY_AND_ASSIGN(EditorTreeModel); 71 }; 72 73 BookmarkEditorView(Profile* profile, 74 const BookmarkNode* parent, 75 const EditDetails& details, 76 BookmarkEditor::Configuration configuration); 77 78 virtual ~BookmarkEditorView(); 79 80 // views::DialogDelegateView: 81 virtual string16 GetDialogButtonLabel(ui::DialogButton button) const OVERRIDE; 82 virtual bool IsDialogButtonEnabled(ui::DialogButton button) const OVERRIDE; 83 virtual views::View* CreateExtraView() OVERRIDE; 84 virtual ui::ModalType GetModalType() const OVERRIDE; 85 virtual bool CanResize() const OVERRIDE; 86 virtual string16 GetWindowTitle() const OVERRIDE; 87 virtual bool Accept() OVERRIDE; 88 89 // views::View: 90 virtual gfx::Size GetPreferredSize() OVERRIDE; 91 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 92 93 // views::TreeViewController: 94 virtual void OnTreeViewSelectionChanged(views::TreeView* tree_view) OVERRIDE; 95 virtual bool CanEdit(views::TreeView* tree_view, 96 ui::TreeModelNode* node) OVERRIDE; 97 98 // views::TextfieldController: 99 virtual void ContentsChanged(views::Textfield* sender, 100 const string16& new_contents) OVERRIDE; 101 virtual bool HandleKeyEvent(views::Textfield* sender, 102 const ui::KeyEvent& key_event) OVERRIDE; 103 104 // views::ButtonListener: 105 virtual void ButtonPressed(views::Button* sender, 106 const ui::Event& event) OVERRIDE; 107 108 // ui::SimpleMenuModel::Delegate: 109 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 110 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 111 virtual bool GetAcceleratorForCommandId( 112 int command_id, 113 ui::Accelerator* accelerator) OVERRIDE; 114 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 115 116 // Creates a Window and adds the BookmarkEditorView to it. When the window is 117 // closed the BookmarkEditorView is deleted. 118 void Show(gfx::NativeWindow parent); 119 120 // views::ContextMenuController: 121 virtual void ShowContextMenuForView(views::View* source, 122 const gfx::Point& point, 123 ui::MenuSourceType source_type) OVERRIDE; 124 125 private: 126 friend class BookmarkEditorViewTest; 127 128 // Creates the necessary sub-views, configures them, adds them to the layout, 129 // and requests the entries to display from the database. 130 void Init(); 131 132 // BookmarkModel observer methods. Any structural change results in 133 // resetting the tree model. 134 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE {} 135 virtual void BookmarkNodeMoved(BookmarkModel* model, 136 const BookmarkNode* old_parent, 137 int old_index, 138 const BookmarkNode* new_parent, 139 int new_index) OVERRIDE; 140 virtual void BookmarkNodeAdded(BookmarkModel* model, 141 const BookmarkNode* parent, 142 int index) OVERRIDE; 143 virtual void BookmarkNodeRemoved(BookmarkModel* model, 144 const BookmarkNode* parent, 145 int index, 146 const BookmarkNode* node) OVERRIDE; 147 virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE; 148 virtual void BookmarkNodeChanged(BookmarkModel* model, 149 const BookmarkNode* node) OVERRIDE {} 150 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, 151 const BookmarkNode* node) OVERRIDE; 152 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, 153 const BookmarkNode* node) OVERRIDE {} 154 155 // Resets the model of the tree and updates the various buttons appropriately. 156 void Reset(); 157 158 // Expands all the nodes in the tree and selects the parent node of the 159 // url we're editing or the most recent parent if the url being editted isn't 160 // starred. 161 void ExpandAndSelect(); 162 163 // Creates a returns the new root node. This invokes CreateNodes to do 164 // the real work. 165 EditorNode* CreateRootNode(); 166 167 // Adds and creates a child node in b_node for all children of bb_node that 168 // are folders. 169 void CreateNodes(const BookmarkNode* bb_node, EditorNode* b_node); 170 171 // Returns the node with the specified id, or NULL if one can't be found. 172 EditorNode* FindNodeWithID(BookmarkEditorView::EditorNode* node, int64 id); 173 174 // Invokes ApplyEdits with the selected node. 175 void ApplyEdits(); 176 177 // Applies the edits done by the user. |parent| gives the parent of the URL 178 // being edited. 179 void ApplyEdits(EditorNode* parent); 180 181 // Recursively adds newly created folders and sets the title of nodes to 182 // match the user edited title. 183 // 184 // bb_node gives the BookmarkNode the edits are to be applied to, with b_node 185 // the source of the edits. 186 // 187 // If b_node == parent_b_node, parent_bb_node is set to bb_node. This is 188 // used to determine the new BookmarkNode parent based on the EditorNode 189 // parent. 190 void ApplyNameChangesAndCreateNewFolders( 191 const BookmarkNode* bb_node, 192 BookmarkEditorView::EditorNode* b_node, 193 BookmarkEditorView::EditorNode* parent_b_node, 194 const BookmarkNode** parent_bb_node); 195 196 // Returns the current url the user has input. 197 GURL GetInputURL() const; 198 199 // Invoked when the url or title has possibly changed. Updates the background 200 // of Textfields and ok button appropriately. 201 void UserInputChanged(); 202 203 // Creates a new folder as a child of the selected node. If no node is 204 // selected, the new folder is added as a child of the bookmark node. Starts 205 // editing on the new gorup as well. 206 void NewFolder(); 207 208 // Creates a new EditorNode as the last child of parent. The new node is 209 // added to the model and returned. This does NOT start editing. This is used 210 // internally by NewFolder and broken into a separate method for testing. 211 EditorNode* AddNewFolder(EditorNode* parent); 212 213 // If |editor_node| is expanded it's added to |expanded_nodes| and this is 214 // recursively invoked for all the children. 215 void UpdateExpandedNodes(EditorNode* editor_node, 216 BookmarkExpandedStateTracker::Nodes* expanded_nodes); 217 218 ui::SimpleMenuModel* GetMenuModel(); 219 220 // Profile the entry is from. 221 Profile* profile_; 222 223 // Model driving the TreeView. 224 scoped_ptr<EditorTreeModel> tree_model_; 225 226 // Displays star folder. 227 views::TreeView* tree_view_; 228 229 // Used to create a new folder. 230 scoped_ptr<views::LabelButton> new_folder_button_; 231 232 // The label for the url text field. 233 views::Label* url_label_; 234 235 // The text field used for editing the URL. 236 views::Textfield* url_tf_; 237 238 // The label for the title text field. 239 views::Label* title_label_; 240 241 // The text field used for editing the title. 242 views::Textfield* title_tf_; 243 244 // Initial parent to select. Is only used if |details_.existing_node| is 245 // NULL. 246 const BookmarkNode* parent_; 247 248 const EditDetails details_; 249 250 // The context menu. 251 scoped_ptr<ui::SimpleMenuModel> context_menu_model_; 252 scoped_ptr<views::MenuRunner> context_menu_runner_; 253 254 // Mode used to create nodes from. 255 BookmarkModel* bb_model_; 256 257 // If true, we're running the menu for the bookmark bar or other bookmarks 258 // nodes. 259 bool running_menu_for_root_; 260 261 // Is the tree shown? 262 bool show_tree_; 263 264 // List of deleted bookmark folders. 265 std::vector<int64> deletes_; 266 267 DISALLOW_COPY_AND_ASSIGN(BookmarkEditorView); 268}; 269 270#endif // CHROME_BROWSER_UI_VIEWS_BOOKMARKS_BOOKMARK_EDITOR_VIEW_H_ 271