diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-12 21:41:02 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-12 21:41:02 +0000 |
commit | d31c4a5ea83087a926a7c890bf741c69ce84b094 (patch) | |
tree | 632a1617c02c1e866f3a83a5850dbd2ca51ff3bf /chrome/browser/bookmarks | |
parent | 2fa2c8fa4cc64f80a63ff8e1b50364fe200adaa9 (diff) | |
download | chromium_src-d31c4a5ea83087a926a7c890bf741c69ce84b094.zip chromium_src-d31c4a5ea83087a926a7c890bf741c69ce84b094.tar.gz chromium_src-d31c4a5ea83087a926a7c890bf741c69ce84b094.tar.bz2 |
Lands http://codereview.chromium.org/1600010/show for Thiago:
Refactor EditFolderController into BookmarkFolderEditorController
class.
BUG=None
TEST=Add and/or edit a bookmark folder. It should works as before.
Review URL: http://codereview.chromium.org/1640007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44281 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/bookmarks')
3 files changed, 176 insertions, 149 deletions
diff --git a/chrome/browser/bookmarks/bookmark_context_menu_controller.cc b/chrome/browser/bookmarks/bookmark_context_menu_controller.cc index 2060a51..61e14d1 100644 --- a/chrome/browser/bookmarks/bookmark_context_menu_controller.cc +++ b/chrome/browser/bookmarks/bookmark_context_menu_controller.cc @@ -7,12 +7,12 @@ #include "app/l10n_util.h" #include "base/compiler_specific.h" #include "chrome/browser/bookmarks/bookmark_editor.h" +#include "chrome/browser/bookmarks/bookmark_folder_editor_controller.h" #include "chrome/browser/bookmarks/bookmark_manager.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/bookmarks/bookmark_utils.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" -#include "chrome/browser/input_window_dialog.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/pref_service.h" #include "chrome/browser/profile.h" @@ -35,150 +35,6 @@ bool NodeHasURLs(const BookmarkNode* node) { return false; } -// EditFolderController ------------------------------------------------------- - -// EditFolderController manages the editing and/or creation of a folder. If the -// user presses ok, the name change is committed to the model. -// -// EditFolderController deletes itself when the window is closed. -class EditFolderController : public InputWindowDialog::Delegate, - public BookmarkModelObserver { - public: - virtual ~EditFolderController() { - if (model_) - model_->RemoveObserver(this); - } - - static void Show(Profile* profile, - gfx::NativeWindow wnd, - const BookmarkNode* node, - int index, - bool is_new, - bool show_in_manager) { - // EditFolderController deletes itself when done. - EditFolderController* controller = - new EditFolderController(profile, wnd, node, index, is_new, - show_in_manager); - controller->Show(); - } - - private: - EditFolderController(Profile* profile, - gfx::NativeWindow wnd, - const BookmarkNode* node, - int index, - bool is_new, - bool show_in_manager) - : profile_(profile), - model_(profile->GetBookmarkModel()), - node_(node), - index_(index), - is_new_(is_new), - show_in_manager_(show_in_manager) { - DCHECK(is_new_ || node); - - std::wstring title = is_new_ ? - l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW) : - l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE); - std::wstring label = - l10n_util::GetString(IDS_BOOMARK_BAR_EDIT_FOLDER_LABEL); - std::wstring contents = is_new_ ? - l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME) : - UTF16ToWide(node_->GetTitleAsString16()); - - dialog_ = InputWindowDialog::Create(wnd, title, label, contents, this); - model_->AddObserver(this); - } - - void Show() { - dialog_->Show(); - } - - // InputWindowDialog::Delegate methods. - virtual bool IsValid(const std::wstring& text) { - return !text.empty(); - } - - virtual void InputAccepted(const std::wstring& text) { - if (is_new_) { - ALLOW_UNUSED const BookmarkNode* node = - model_->AddGroup(node_, index_, text); - if (show_in_manager_) - BookmarkManager::SelectInTree(profile_, node); - } else { - model_->SetTitle(node_, text); - } - } - - virtual void InputCanceled() { - } - - // BookmarkModelObserver methods, all invoke ModelChanged and close the - // dialog. - virtual void Loaded(BookmarkModel* model) {} - virtual void BookmarkModelBeingDeleted(BookmarkModel* model) { - model_->RemoveObserver(this); - model_ = NULL; - ModelChanged(); - } - - virtual void BookmarkNodeMoved(BookmarkModel* model, - const BookmarkNode* old_parent, - int old_index, - const BookmarkNode* new_parent, - int new_index) { - ModelChanged(); - } - - virtual void BookmarkNodeAdded(BookmarkModel* model, - const BookmarkNode* parent, - int index) { - ModelChanged(); - } - - virtual void BookmarkNodeRemoved(BookmarkModel* model, - const BookmarkNode* parent, - int index, - const BookmarkNode* node) { - ModelChanged(); - } - - virtual void BookmarkNodeChanged(BookmarkModel* model, - const BookmarkNode* node) { - ModelChanged(); - } - - virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model, - const BookmarkNode* node) {} - - virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, - const BookmarkNode* node) { - ModelChanged(); - } - - void ModelChanged() { - dialog_->Close(); - } - - Profile* profile_; - BookmarkModel* model_; - // If is_new is true, this is the parent to create the new node under. - // Otherwise this is the node to change the title of. - const BookmarkNode* node_; - - // Index to insert the new folder at. - int index_; - - bool is_new_; - - // If is_new_ is true and a new node is created, it is selected in the - // bookmark manager. - bool show_in_manager_; - InputWindowDialog* dialog_; - - DISALLOW_COPY_AND_ASSIGN(EditFolderController); -}; - // SelectOnCreationHandler ---------------------------------------------------- // Used when adding a new bookmark. If a new bookmark is created it is selected @@ -349,8 +205,8 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { BookmarkEditor::EditDetails(selection_[0]), editor_config, NULL); } else { - EditFolderController::Show(profile_, parent_window_, selection_[0], - -1, false, false); + BookmarkFolderEditorController::Show(profile_, parent_window_, + selection_[0], -1, BookmarkFolderEditorController::NONE); } break; @@ -397,8 +253,11 @@ void BookmarkContextMenuController::ExecuteCommand(int id) { int index; const BookmarkNode* parent = bookmark_utils::GetParentForNewNodes(parent_, selection_, &index); - EditFolderController::Show(profile_, parent_window_, parent, index, true, - configuration_ != BOOKMARK_BAR); + uint32 details = BookmarkFolderEditorController::IS_NEW; + if (configuration_ != BOOKMARK_BAR) + details |= BookmarkFolderEditorController::SHOW_IN_MANAGER; + BookmarkFolderEditorController::Show(profile_, parent_window_, parent, + index, details); break; } diff --git a/chrome/browser/bookmarks/bookmark_folder_editor_controller.cc b/chrome/browser/bookmarks/bookmark_folder_editor_controller.cc new file mode 100644 index 0000000..06f3ef4 --- /dev/null +++ b/chrome/browser/bookmarks/bookmark_folder_editor_controller.cc @@ -0,0 +1,91 @@ +// 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. + +#include "chrome/browser/bookmarks/bookmark_folder_editor_controller.h" + +#include "app/l10n_util.h" +#include "chrome/browser/bookmarks/bookmark_manager.h" +#include "chrome/browser/bookmarks/bookmark_model.h" +#include "chrome/browser/profile.h" +#include "grit/generated_resources.h" + +BookmarkFolderEditorController::~BookmarkFolderEditorController() { + if (model_) + model_->RemoveObserver(this); +} + +// static +void BookmarkFolderEditorController::Show(Profile* profile, + gfx::NativeWindow wnd, + const BookmarkNode* node, + int index, + uint32 details) { + // BookmarkFolderEditorController deletes itself when done. + BookmarkFolderEditorController* editor = + new BookmarkFolderEditorController(profile, wnd, node, index, details); + editor->Show(); +} + +BookmarkFolderEditorController::BookmarkFolderEditorController( + Profile* profile, + gfx::NativeWindow wnd, + const BookmarkNode* node, + int index, + uint32 details) + : profile_(profile), + model_(profile->GetBookmarkModel()), + node_(node), + index_(index), + details_(details) { + DCHECK(IsNew() || node); + + std::wstring title = IsNew() ? + l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW) : + l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE); + std::wstring label = + l10n_util::GetString(IDS_BOOMARK_BAR_EDIT_FOLDER_LABEL); + std::wstring contents = IsNew() ? + l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME) : + UTF16ToWide(node_->GetTitleAsString16()); + + dialog_ = InputWindowDialog::Create(wnd, title, label, contents, this); + model_->AddObserver(this); +} + +void BookmarkFolderEditorController::Show() { + dialog_->Show(); +} + +bool BookmarkFolderEditorController::IsValid(const std::wstring& text) { + return !text.empty(); +} + +void BookmarkFolderEditorController::InputAccepted(const std::wstring& text) { + if (IsNew()) { + ALLOW_UNUSED const BookmarkNode* node = + model_->AddGroup(node_, index_, text); + if ((details_ & SHOW_IN_MANAGER) != 0) + BookmarkManager::SelectInTree(profile_, node); + } else { + model_->SetTitle(node_, text); + } +} + +void BookmarkFolderEditorController::InputCanceled() { +} + +void BookmarkFolderEditorController::BookmarkModelChanged() { + dialog_->Close(); +} + +void BookmarkFolderEditorController::BookmarkModelBeingDeleted( + BookmarkModel* model) { + model_->RemoveObserver(this); + model_ = NULL; + BookmarkModelChanged(); +} + +bool BookmarkFolderEditorController::IsNew() { + return (details_ & IS_NEW) != 0; +} diff --git a/chrome/browser/bookmarks/bookmark_folder_editor_controller.h b/chrome/browser/bookmarks/bookmark_folder_editor_controller.h new file mode 100644 index 0000000..e3951a4 --- /dev/null +++ b/chrome/browser/bookmarks/bookmark_folder_editor_controller.h @@ -0,0 +1,77 @@ +// 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. + +#ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_FOLDER_EDITOR_CONTROLLER_H_ +#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_FOLDER_EDITOR_CONTROLLER_H_ + +#include "base/basictypes.h" +#include "chrome/browser/bookmarks/base_bookmark_model_observer.h" +#include "chrome/browser/input_window_dialog.h" +#include "gfx/native_widget_types.h" + +class Profile; + +// BookmarkFolderEditorController manages the editing and/or creation of a +// folder. If the user presses ok, the name change is committed to the model. +// +// BookmarkFolderEditorController deletes itself when the window is closed. +class BookmarkFolderEditorController : public InputWindowDialog::Delegate, + public BaseBookmarkModelObserver { + public: + enum Details { + NONE = 1 << 0, + IS_NEW = 1 << 1, + SHOW_IN_MANAGER = 1 << 2, + }; + + virtual ~BookmarkFolderEditorController(); + + // |details| is a bitmask of Details (see above). + static void Show(Profile* profile, + gfx::NativeWindow wnd, + const BookmarkNode* node, + int index, + uint32 details); + + private: + BookmarkFolderEditorController(Profile* profile, + gfx::NativeWindow wnd, + const BookmarkNode* node, + int index, + uint32 details); + void Show(); + + // Overriden from InputWindowDialog::Delegate: + virtual bool IsValid(const std::wstring& text); + virtual void InputAccepted(const std::wstring& text); + virtual void InputCanceled(); + + // Overridden from BaseBookmarkModelObserver: + virtual void BookmarkModelChanged(); + virtual void BookmarkModelBeingDeleted(BookmarkModel* model); + + // Returns true if we are creating a new bookmark folder, otherwise returns + // false if we are editing the bookmark folder. + bool IsNew(); + + Profile* profile_; + + BookmarkModel* model_; + + // If IsNew() is true, this is the parent to create the new node under. + // Otherwise this is the node to change the title of. + const BookmarkNode* node_; + + // Index to insert the new folder at. + int index_; + + // The bitmask of Details (see the enum above). + const uint32 details_; + + InputWindowDialog* dialog_; + + DISALLOW_COPY_AND_ASSIGN(BookmarkFolderEditorController); +}; + +#endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_FOLDER_EDITOR_CONTROLLER_H_ |