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.
5 #include "chrome/browser/ui/views/bookmarks/bookmark_editor_view.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/bookmarks/bookmark_model.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/bookmarks/bookmark_utils.h"
17 #include "chrome/browser/history/history_service.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
20 #include "chrome/browser/ui/views/constrained_window_views.h"
21 #include "chrome/common/net/url_fixer_upper.h"
22 #include "components/user_prefs/user_prefs.h"
23 #include "grit/chromium_strings.h"
24 #include "grit/generated_resources.h"
25 #include "grit/locale_settings.h"
26 #include "ui/base/accessibility/accessible_view_state.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/events/event.h"
29 #include "ui/views/background.h"
30 #include "ui/views/controls/button/label_button.h"
31 #include "ui/views/controls/label.h"
32 #include "ui/views/controls/menu/menu_runner.h"
33 #include "ui/views/controls/textfield/textfield.h"
34 #include "ui/views/controls/tree/tree_view.h"
35 #include "ui/views/focus/focus_manager.h"
36 #include "ui/views/layout/grid_layout.h"
37 #include "ui/views/layout/layout_constants.h"
38 #include "ui/views/widget/widget.h"
39 #include "ui/views/window/dialog_client_view.h"
42 using views::GridLayout
;
46 // Background color of text field when URL is invalid.
47 const SkColor kErrorColor
= SkColorSetRGB(0xFF, 0xBC, 0xBC);
52 void BookmarkEditor::Show(gfx::NativeWindow parent_window
,
54 const EditDetails
& details
,
55 Configuration configuration
) {
57 BookmarkEditorView
* editor
= new BookmarkEditorView(profile
,
58 details
.parent_node
, details
, configuration
);
59 editor
->Show(parent_window
);
62 BookmarkEditorView::BookmarkEditorView(
64 const BookmarkNode
* parent
,
65 const EditDetails
& details
,
66 BookmarkEditor::Configuration configuration
)
75 running_menu_for_root_(false),
76 show_tree_(configuration
== SHOW_TREE
) {
81 BookmarkEditorView::~BookmarkEditorView() {
82 // The tree model is deleted before the view. Reset the model otherwise the
83 // tree will reference a deleted model.
85 tree_view_
->SetModel(NULL
);
86 bb_model_
->RemoveObserver(this);
89 base::string16
BookmarkEditorView::GetDialogButtonLabel(
90 ui::DialogButton button
) const {
91 if (button
== ui::DIALOG_BUTTON_OK
)
92 return l10n_util::GetStringUTF16(IDS_SAVE
);
93 return views::DialogDelegateView::GetDialogButtonLabel(button
);
96 bool BookmarkEditorView::IsDialogButtonEnabled(ui::DialogButton button
) const {
97 if (button
== ui::DIALOG_BUTTON_OK
) {
98 if (!bb_model_
->loaded())
101 if (details_
.GetNodeType() != BookmarkNode::FOLDER
)
102 return GetInputURL().is_valid();
107 views::View
* BookmarkEditorView::CreateExtraView() {
108 return new_folder_button_
.get();
111 ui::ModalType
BookmarkEditorView::GetModalType() const {
112 return ui::MODAL_TYPE_WINDOW
;
115 bool BookmarkEditorView::CanResize() const {
119 base::string16
BookmarkEditorView::GetWindowTitle() const {
120 return l10n_util::GetStringUTF16(details_
.GetWindowTitleId());
123 bool BookmarkEditorView::Accept() {
124 if (!IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
)) {
125 if (details_
.GetNodeType() != BookmarkNode::FOLDER
) {
126 // The url is invalid, focus the url field.
127 url_tf_
->SelectAll(true);
128 url_tf_
->RequestFocus();
132 // Otherwise save changes and close the dialog box.
137 gfx::Size
BookmarkEditorView::GetPreferredSize() {
139 return views::View::GetPreferredSize();
141 return gfx::Size(views::Widget::GetLocalizedContentsSize(
142 IDS_EDITBOOKMARK_DIALOG_WIDTH_CHARS
,
143 IDS_EDITBOOKMARK_DIALOG_HEIGHT_LINES
));
146 void BookmarkEditorView::OnTreeViewSelectionChanged(
147 views::TreeView
* tree_view
) {
150 bool BookmarkEditorView::CanEdit(views::TreeView
* tree_view
,
151 ui::TreeModelNode
* node
) {
152 // Only allow editting of children of the bookmark bar node and other node.
153 EditorNode
* bb_node
= tree_model_
->AsNode(node
);
154 return (bb_node
->parent() && bb_node
->parent()->parent());
157 void BookmarkEditorView::ContentsChanged(views::Textfield
* sender
,
158 const base::string16
& new_contents
) {
162 bool BookmarkEditorView::HandleKeyEvent(views::Textfield
* sender
,
163 const ui::KeyEvent
& key_event
) {
167 void BookmarkEditorView::GetAccessibleState(ui::AccessibleViewState
* state
) {
169 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_TITLE
);
170 state
->role
= ui::AccessibilityTypes::ROLE_DIALOG
;
173 void BookmarkEditorView::ButtonPressed(views::Button
* sender
,
174 const ui::Event
& event
) {
175 DCHECK_EQ(new_folder_button_
.get(), sender
);
179 bool BookmarkEditorView::IsCommandIdChecked(int command_id
) const {
183 bool BookmarkEditorView::IsCommandIdEnabled(int command_id
) const {
184 switch (command_id
) {
187 return !running_menu_for_root_
;
188 case IDS_BOOKMARK_EDITOR_NEW_FOLDER_MENU_ITEM
:
196 bool BookmarkEditorView::GetAcceleratorForCommandId(
198 ui::Accelerator
* accelerator
) {
199 return GetWidget()->GetAccelerator(command_id
, accelerator
);
202 void BookmarkEditorView::ExecuteCommand(int command_id
, int event_flags
) {
203 DCHECK(tree_view_
->GetSelectedNode());
204 if (command_id
== IDS_EDIT
) {
205 tree_view_
->StartEditing(tree_view_
->GetSelectedNode());
206 } else if (command_id
== IDS_DELETE
) {
207 EditorNode
* node
= tree_model_
->AsNode(tree_view_
->GetSelectedNode());
210 if (node
->value
!= 0) {
211 const BookmarkNode
* b_node
= bb_model_
->GetNodeByID(node
->value
);
212 if (!b_node
->empty() &&
213 !chrome::ConfirmDeleteBookmarkNode(b_node
,
214 GetWidget()->GetNativeWindow())) {
215 // The folder is not empty and the user didn't confirm.
218 deletes_
.push_back(node
->value
);
220 tree_model_
->Remove(node
->parent(), node
);
222 DCHECK_EQ(IDS_BOOKMARK_EDITOR_NEW_FOLDER_MENU_ITEM
, command_id
);
227 void BookmarkEditorView::Show(gfx::NativeWindow parent
) {
228 CreateBrowserModalDialogViews(this, parent
);
230 if (show_tree_
&& bb_model_
->loaded())
233 // Select all the text in the name Textfield.
234 title_tf_
->SelectAll(true);
235 // Give focus to the name Textfield.
236 title_tf_
->RequestFocus();
239 void BookmarkEditorView::ShowContextMenuForView(
241 const gfx::Point
& point
,
242 ui::MenuSourceType source_type
) {
243 DCHECK_EQ(tree_view_
, source
);
244 if (!tree_view_
->GetSelectedNode())
246 running_menu_for_root_
=
247 (tree_model_
->GetParent(tree_view_
->GetSelectedNode()) ==
248 tree_model_
->GetRoot());
250 context_menu_runner_
.reset(new views::MenuRunner(GetMenuModel()));
252 if (context_menu_runner_
->RunMenuAt(source
->GetWidget()->GetTopLevelWidget(),
253 NULL
, gfx::Rect(point
, gfx::Size()), views::MenuItemView::TOPRIGHT
,
255 views::MenuRunner::HAS_MNEMONICS
| views::MenuRunner::CONTEXT_MENU
) ==
256 views::MenuRunner::MENU_DELETED
)
260 void BookmarkEditorView::Init() {
261 bb_model_
= BookmarkModelFactory::GetForProfile(profile_
);
263 bb_model_
->AddObserver(this);
265 title_label_
= new views::Label(
266 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_NAME_LABEL
));
268 base::string16 title
;
270 if (details_
.type
== EditDetails::EXISTING_NODE
) {
271 title
= details_
.existing_node
->GetTitle();
272 url
= details_
.existing_node
->url();
273 } else if (details_
.type
== EditDetails::NEW_FOLDER
) {
274 title
= l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME
);
275 } else if (details_
.type
== EditDetails::NEW_URL
) {
277 title
= details_
.title
;
279 title_tf_
= new views::Textfield
;
280 title_tf_
->SetAccessibleName(
281 l10n_util::GetStringUTF16(IDS_BOOKMARK_AX_EDITOR_NAME_LABEL
));
282 title_tf_
->SetText(title
);
283 title_tf_
->set_controller(this);
286 tree_view_
= new views::TreeView
;
287 tree_view_
->SetRootShown(false);
288 tree_view_
->set_context_menu_controller(this);
290 new_folder_button_
.reset(new views::LabelButton(this,
291 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_NEW_FOLDER_BUTTON
)));
292 new_folder_button_
->SetStyle(views::Button::STYLE_BUTTON
);
293 new_folder_button_
->set_owned_by_client();
294 new_folder_button_
->SetEnabled(false);
297 GridLayout
* layout
= GridLayout::CreatePanel(this);
298 SetLayoutManager(layout
);
300 const int labels_column_set_id
= 0;
301 const int single_column_view_set_id
= 1;
302 const int buttons_column_set_id
= 2;
304 views::ColumnSet
* column_set
= layout
->AddColumnSet(labels_column_set_id
);
305 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::CENTER
, 0,
306 GridLayout::USE_PREF
, 0, 0);
307 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
308 column_set
->AddColumn(GridLayout::FILL
, GridLayout::CENTER
, 1,
309 GridLayout::USE_PREF
, 0, 0);
311 column_set
= layout
->AddColumnSet(single_column_view_set_id
);
312 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
313 GridLayout::USE_PREF
, 0, 0);
315 column_set
= layout
->AddColumnSet(buttons_column_set_id
);
316 column_set
->AddColumn(GridLayout::FILL
, GridLayout::LEADING
, 0,
317 GridLayout::USE_PREF
, 0, 0);
318 column_set
->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing
);
319 column_set
->AddColumn(GridLayout::FILL
, GridLayout::LEADING
, 0,
320 GridLayout::USE_PREF
, 0, 0);
321 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
322 column_set
->AddColumn(GridLayout::FILL
, GridLayout::LEADING
, 0,
323 GridLayout::USE_PREF
, 0, 0);
324 column_set
->LinkColumnSizes(0, 2, 4, -1);
326 layout
->StartRow(0, labels_column_set_id
);
327 layout
->AddView(title_label_
);
328 layout
->AddView(title_tf_
);
330 if (details_
.GetNodeType() != BookmarkNode::FOLDER
) {
331 url_label_
= new views::Label(
332 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_URL_LABEL
));
334 url_tf_
= new views::Textfield
;
336 profile_
? user_prefs::UserPrefs::Get(profile_
) : NULL
;
337 url_tf_
->SetText(chrome::FormatBookmarkURLForDisplay(url
, prefs
));
338 url_tf_
->set_controller(this);
339 url_tf_
->SetAccessibleName(
340 l10n_util::GetStringUTF16(IDS_BOOKMARK_AX_EDITOR_URL_LABEL
));
342 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
344 layout
->StartRow(0, labels_column_set_id
);
345 layout
->AddView(url_label_
);
346 layout
->AddView(url_tf_
);
350 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
351 layout
->StartRow(1, single_column_view_set_id
);
352 layout
->AddView(tree_view_
->CreateParentIfNecessary());
355 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
357 if (!show_tree_
|| bb_model_
->loaded())
361 void BookmarkEditorView::BookmarkNodeMoved(BookmarkModel
* model
,
362 const BookmarkNode
* old_parent
,
364 const BookmarkNode
* new_parent
,
369 void BookmarkEditorView::BookmarkNodeAdded(BookmarkModel
* model
,
370 const BookmarkNode
* parent
,
375 void BookmarkEditorView::BookmarkNodeRemoved(BookmarkModel
* model
,
376 const BookmarkNode
* parent
,
378 const BookmarkNode
* node
) {
379 if ((details_
.type
== EditDetails::EXISTING_NODE
&&
380 details_
.existing_node
->HasAncestor(node
)) ||
381 (parent_
&& parent_
->HasAncestor(node
))) {
382 // The node, or its parent was removed. Close the dialog.
383 GetWidget()->Close();
389 void BookmarkEditorView::BookmarkAllNodesRemoved(BookmarkModel
* model
) {
393 void BookmarkEditorView::BookmarkNodeChildrenReordered(
394 BookmarkModel
* model
, const BookmarkNode
* node
) {
398 void BookmarkEditorView::Reset() {
405 new_folder_button_
->SetEnabled(true);
407 // Do this first, otherwise when we invoke SetModel with the real one
408 // tree_view will try to invoke something on the model we just deleted.
409 tree_view_
->SetModel(NULL
);
411 EditorNode
* root_node
= CreateRootNode();
412 tree_model_
.reset(new EditorTreeModel(root_node
));
414 tree_view_
->SetModel(tree_model_
.get());
415 tree_view_
->SetController(this);
417 context_menu_runner_
.reset();
423 GURL
BookmarkEditorView::GetInputURL() const {
424 if (details_
.GetNodeType() == BookmarkNode::FOLDER
)
426 return URLFixerUpper::FixupURL(
427 base::UTF16ToUTF8(url_tf_
->text()), std::string());
430 void BookmarkEditorView::UserInputChanged() {
431 if (details_
.GetNodeType() != BookmarkNode::FOLDER
) {
432 const GURL
url(GetInputURL());
434 url_tf_
->SetBackgroundColor(kErrorColor
);
436 url_tf_
->UseDefaultBackgroundColor();
438 GetDialogClientView()->UpdateDialogButtons();
441 void BookmarkEditorView::NewFolder() {
442 // Create a new entry parented to the selected item, or the bookmark
443 // bar if nothing is selected.
444 EditorNode
* parent
= tree_model_
->AsNode(tree_view_
->GetSelectedNode());
450 tree_view_
->StartEditing(AddNewFolder(parent
));
453 BookmarkEditorView::EditorNode
* BookmarkEditorView::AddNewFolder(
454 EditorNode
* parent
) {
455 EditorNode
* new_node
= new EditorNode(
456 l10n_util::GetStringUTF16(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME
), 0);
457 // |new_node| is now owned by |parent|.
458 tree_model_
->Add(parent
, new_node
, parent
->child_count());
462 void BookmarkEditorView::ExpandAndSelect() {
463 BookmarkExpandedStateTracker::Nodes expanded_nodes
=
464 bb_model_
->expanded_state_tracker()->GetExpandedNodes();
465 for (BookmarkExpandedStateTracker::Nodes::const_iterator
i(
466 expanded_nodes
.begin()); i
!= expanded_nodes
.end(); ++i
) {
467 EditorNode
* editor_node
=
468 FindNodeWithID(tree_model_
->GetRoot(), (*i
)->id());
470 tree_view_
->Expand(editor_node
);
473 const BookmarkNode
* to_select
= parent_
;
474 if (details_
.type
== EditDetails::EXISTING_NODE
)
475 to_select
= details_
.existing_node
->parent();
476 int64 folder_id_to_select
= to_select
->id();
478 FindNodeWithID(tree_model_
->GetRoot(), folder_id_to_select
);
480 b_node
= tree_model_
->GetRoot()->GetChild(0); // Bookmark bar node.
482 tree_view_
->SetSelectedNode(b_node
);
485 BookmarkEditorView::EditorNode
* BookmarkEditorView::CreateRootNode() {
486 EditorNode
* root_node
= new EditorNode(base::string16(), 0);
487 const BookmarkNode
* bb_root_node
= bb_model_
->root_node();
488 CreateNodes(bb_root_node
, root_node
);
489 DCHECK(root_node
->child_count() >= 2 && root_node
->child_count() <= 3);
490 DCHECK_EQ(BookmarkNode::BOOKMARK_BAR
, bb_root_node
->GetChild(0)->type());
491 DCHECK_EQ(BookmarkNode::OTHER_NODE
, bb_root_node
->GetChild(1)->type());
492 if (root_node
->child_count() == 3)
493 DCHECK_EQ(BookmarkNode::MOBILE
, bb_root_node
->GetChild(2)->type());
497 void BookmarkEditorView::CreateNodes(const BookmarkNode
* bb_node
,
498 BookmarkEditorView::EditorNode
* b_node
) {
499 for (int i
= 0; i
< bb_node
->child_count(); ++i
) {
500 const BookmarkNode
* child_bb_node
= bb_node
->GetChild(i
);
501 if (child_bb_node
->IsVisible() && child_bb_node
->is_folder()) {
502 EditorNode
* new_b_node
= new EditorNode(child_bb_node
->GetTitle(),
503 child_bb_node
->id());
504 b_node
->Add(new_b_node
, b_node
->child_count());
505 CreateNodes(child_bb_node
, new_b_node
);
510 BookmarkEditorView::EditorNode
* BookmarkEditorView::FindNodeWithID(
511 BookmarkEditorView::EditorNode
* node
,
513 if (node
->value
== id
)
515 for (int i
= 0; i
< node
->child_count(); ++i
) {
516 EditorNode
* result
= FindNodeWithID(node
->GetChild(i
), id
);
523 void BookmarkEditorView::ApplyEdits() {
524 DCHECK(bb_model_
->loaded());
527 tree_view_
->CommitEdit();
529 EditorNode
* parent
= show_tree_
?
530 tree_model_
->AsNode(tree_view_
->GetSelectedNode()) : NULL
;
531 if (show_tree_
&& !parent
) {
538 void BookmarkEditorView::ApplyEdits(EditorNode
* parent
) {
539 DCHECK(!show_tree_
|| parent
);
541 // We're going to apply edits to the bookmark bar model, which will call us
542 // back. Normally when a structural edit occurs we reset the tree model.
543 // We don't want to do that here, so we remove ourselves as an observer.
544 bb_model_
->RemoveObserver(this);
546 GURL
new_url(GetInputURL());
547 base::string16
new_title(title_tf_
->text());
550 BookmarkEditor::ApplyEditsWithNoFolderChange(
551 bb_model_
, parent_
, details_
, new_title
, new_url
);
555 // Create the new folders and update the titles.
556 const BookmarkNode
* new_parent
= NULL
;
557 ApplyNameChangesAndCreateNewFolders(
558 bb_model_
->root_node(), tree_model_
->GetRoot(), parent
, &new_parent
);
560 BookmarkEditor::ApplyEditsWithPossibleFolderChange(
561 bb_model_
, new_parent
, details_
, new_title
, new_url
);
563 BookmarkExpandedStateTracker::Nodes expanded_nodes
;
564 UpdateExpandedNodes(tree_model_
->GetRoot(), &expanded_nodes
);
565 bb_model_
->expanded_state_tracker()->SetExpandedNodes(expanded_nodes
);
567 // Remove the folders that were removed. This has to be done after all the
568 // other changes have been committed.
569 bookmark_utils::DeleteBookmarkFolders(bb_model_
, deletes_
);
572 void BookmarkEditorView::ApplyNameChangesAndCreateNewFolders(
573 const BookmarkNode
* bb_node
,
574 BookmarkEditorView::EditorNode
* b_node
,
575 BookmarkEditorView::EditorNode
* parent_b_node
,
576 const BookmarkNode
** parent_bb_node
) {
577 if (parent_b_node
== b_node
)
578 *parent_bb_node
= bb_node
;
579 for (int i
= 0; i
< b_node
->child_count(); ++i
) {
580 EditorNode
* child_b_node
= b_node
->GetChild(i
);
581 const BookmarkNode
* child_bb_node
= NULL
;
582 if (child_b_node
->value
== 0) {
584 child_bb_node
= bb_model_
->AddFolder(bb_node
,
585 bb_node
->child_count(), child_b_node
->GetTitle());
586 child_b_node
->value
= child_bb_node
->id();
588 // Existing node, reset the title (BookmarkModel ignores changes if the
589 // title is the same).
590 for (int j
= 0; j
< bb_node
->child_count(); ++j
) {
591 const BookmarkNode
* node
= bb_node
->GetChild(j
);
592 if (node
->is_folder() && node
->id() == child_b_node
->value
) {
593 child_bb_node
= node
;
597 DCHECK(child_bb_node
);
598 bb_model_
->SetTitle(child_bb_node
, child_b_node
->GetTitle());
600 ApplyNameChangesAndCreateNewFolders(child_bb_node
, child_b_node
,
601 parent_b_node
, parent_bb_node
);
605 void BookmarkEditorView::UpdateExpandedNodes(
606 EditorNode
* editor_node
,
607 BookmarkExpandedStateTracker::Nodes
* expanded_nodes
) {
608 if (!tree_view_
->IsExpanded(editor_node
))
611 if (editor_node
->value
!= 0) // The root is 0
612 expanded_nodes
->insert(bb_model_
->GetNodeByID(editor_node
->value
));
613 for (int i
= 0; i
< editor_node
->child_count(); ++i
)
614 UpdateExpandedNodes(editor_node
->GetChild(i
), expanded_nodes
);
617 ui::SimpleMenuModel
* BookmarkEditorView::GetMenuModel() {
618 if (!context_menu_model_
.get()) {
619 context_menu_model_
.reset(new ui::SimpleMenuModel(this));
620 context_menu_model_
->AddItemWithStringId(IDS_EDIT
, IDS_EDIT
);
621 context_menu_model_
->AddItemWithStringId(IDS_DELETE
, IDS_DELETE
);
622 context_menu_model_
->AddItemWithStringId(
623 IDS_BOOKMARK_EDITOR_NEW_FOLDER_MENU_ITEM
,
624 IDS_BOOKMARK_EDITOR_NEW_FOLDER_MENU_ITEM
);
626 return context_menu_model_
.get();
629 void BookmarkEditorView::EditorTreeModel::SetTitle(
630 ui::TreeModelNode
* node
,
631 const base::string16
& title
) {
633 ui::TreeNodeModel
<EditorNode
>::SetTitle(node
, title
);