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 "ui/views/examples/tree_view_example.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/views/controls/button/label_button.h"
9 #include "ui/views/controls/menu/menu_model_adapter.h"
10 #include "ui/views/controls/menu/menu_runner.h"
11 #include "ui/views/controls/tree/tree_view.h"
12 #include "ui/views/layout/grid_layout.h"
14 using base::ASCIIToUTF16
;
19 TreeViewExample::TreeViewExample()
20 : ExampleBase("Tree View"),
22 model_(new NodeType(ASCIIToUTF16("root"), 1)) {
25 TreeViewExample::~TreeViewExample() {
26 // Delete the view before the model.
31 void TreeViewExample::CreateExampleView(View
* container
) {
32 // Add some sample data.
33 NodeType
* colors_node
= new NodeType(ASCIIToUTF16("colors"), 1);
34 model_
.GetRoot()->Add(colors_node
, 0);
35 colors_node
->Add(new NodeType(ASCIIToUTF16("red"), 1), 0);
36 colors_node
->Add(new NodeType(ASCIIToUTF16("green"), 1), 1);
37 colors_node
->Add(new NodeType(ASCIIToUTF16("blue"), 1), 2);
39 NodeType
* sheep_node
= new NodeType(ASCIIToUTF16("sheep"), 1);
40 model_
.GetRoot()->Add(sheep_node
, 0);
41 sheep_node
->Add(new NodeType(ASCIIToUTF16("Sheep 1"), 1), 0);
42 sheep_node
->Add(new NodeType(ASCIIToUTF16("Sheep 2"), 1), 1);
44 tree_view_
= new TreeView();
45 tree_view_
->set_context_menu_controller(this);
46 tree_view_
->SetRootShown(false);
47 tree_view_
->SetModel(&model_
);
48 tree_view_
->SetController(this);
49 add_
= new LabelButton(this, ASCIIToUTF16("Add"));
50 add_
->SetFocusable(true);
51 remove_
= new LabelButton(this, ASCIIToUTF16("Remove"));
52 remove_
->SetFocusable(true);
53 change_title_
= new LabelButton(this, ASCIIToUTF16("Change Title"));
54 change_title_
->SetFocusable(true);
56 GridLayout
* layout
= new GridLayout(container
);
57 container
->SetLayoutManager(layout
);
59 const int tree_view_column
= 0;
60 ColumnSet
* column_set
= layout
->AddColumnSet(tree_view_column
);
61 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
,
62 1.0f
, GridLayout::USE_PREF
, 0, 0);
63 layout
->StartRow(1 /* expand */, tree_view_column
);
64 layout
->AddView(tree_view_
->CreateParentIfNecessary());
66 // Add control buttons horizontally.
67 const int button_column
= 1;
68 column_set
= layout
->AddColumnSet(button_column
);
69 for (int i
= 0; i
< 3; i
++) {
70 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
,
71 1.0f
, GridLayout::USE_PREF
, 0, 0);
74 layout
->StartRow(0 /* no expand */, button_column
);
75 layout
->AddView(add_
);
76 layout
->AddView(remove_
);
77 layout
->AddView(change_title_
);
80 void TreeViewExample::AddNewNode() {
81 NodeType
* selected_node
=
82 static_cast<NodeType
*>(tree_view_
->GetSelectedNode());
84 selected_node
= model_
.GetRoot();
85 NodeType
* new_node
= new NodeType(selected_node
->GetTitle(), 1);
86 model_
.Add(selected_node
, new_node
, selected_node
->child_count());
87 tree_view_
->SetSelectedNode(new_node
);
90 bool TreeViewExample::IsCommandIdEnabled(int command_id
) {
91 return command_id
!= ID_REMOVE
||
92 tree_view_
->GetSelectedNode() != model_
.GetRoot();
95 void TreeViewExample::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
96 NodeType
* selected_node
=
97 static_cast<NodeType
*>(tree_view_
->GetSelectedNode());
100 } else if (sender
== remove_
) {
101 DCHECK(selected_node
);
102 DCHECK_NE(model_
.GetRoot(), selected_node
);
103 model_
.Remove(selected_node
->parent(), selected_node
);
104 } else if (sender
== change_title_
) {
105 DCHECK(selected_node
);
106 model_
.SetTitle(selected_node
,
107 selected_node
->GetTitle() + ASCIIToUTF16("new"));
111 void TreeViewExample::OnTreeViewSelectionChanged(TreeView
* tree_view
) {
112 ui::TreeModelNode
* node
= tree_view_
->GetSelectedNode();
114 change_title_
->SetEnabled(true);
115 remove_
->SetEnabled(node
!= model_
.GetRoot());
117 change_title_
->SetEnabled(false);
118 remove_
->SetEnabled(false);
122 bool TreeViewExample::CanEdit(TreeView
* tree_view
,
123 ui::TreeModelNode
* node
) {
127 void TreeViewExample::ShowContextMenuForView(View
* source
,
128 const gfx::Point
& point
,
129 ui::MenuSourceType source_type
) {
130 ui::SimpleMenuModel
context_menu_model(this);
131 context_menu_model
.AddItem(ID_EDIT
, ASCIIToUTF16("Edit"));
132 context_menu_model
.AddItem(ID_REMOVE
, ASCIIToUTF16("Remove"));
133 context_menu_model
.AddItem(ID_ADD
, ASCIIToUTF16("Add"));
134 context_menu_runner_
.reset(new MenuRunner(&context_menu_model
, 0));
135 if (context_menu_runner_
->RunMenuAt(source
->GetWidget(),
137 gfx::Rect(point
, gfx::Size()),
140 MenuRunner::MENU_DELETED
) {
145 bool TreeViewExample::IsCommandIdChecked(int command_id
) const {
149 bool TreeViewExample::IsCommandIdEnabled(int command_id
) const {
150 return const_cast<TreeViewExample
*>(this)->IsCommandIdEnabled(command_id
);
153 bool TreeViewExample::GetAcceleratorForCommandId(
155 ui::Accelerator
* accelerator
) {
159 void TreeViewExample::ExecuteCommand(int command_id
, int event_flags
) {
160 NodeType
* selected_node
=
161 static_cast<NodeType
*>(tree_view_
->GetSelectedNode());
162 switch (command_id
) {
164 tree_view_
->StartEditing(selected_node
);
167 model_
.Remove(selected_node
->parent(), selected_node
);
177 } // namespace examples