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 #include "ui/views/examples/tree_view_example.h" 6 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" 13 14 namespace views { 15 namespace examples { 16 17 TreeViewExample::TreeViewExample() 18 : ExampleBase("Tree View"), 19 tree_view_(NULL), 20 model_(new NodeType(ASCIIToUTF16("root"), 1)) { 21 } 22 23 TreeViewExample::~TreeViewExample() { 24 // Delete the view before the model. 25 delete tree_view_; 26 tree_view_ = NULL; 27 } 28 29 void TreeViewExample::CreateExampleView(View* container) { 30 // Add some sample data. 31 NodeType* colors_node = new NodeType(ASCIIToUTF16("colors"), 1); 32 model_.GetRoot()->Add(colors_node, 0); 33 colors_node->Add(new NodeType(ASCIIToUTF16("red"), 1), 0); 34 colors_node->Add(new NodeType(ASCIIToUTF16("green"), 1), 1); 35 colors_node->Add(new NodeType(ASCIIToUTF16("blue"), 1), 2); 36 37 NodeType* sheep_node = new NodeType(ASCIIToUTF16("sheep"), 1); 38 model_.GetRoot()->Add(sheep_node, 0); 39 sheep_node->Add(new NodeType(ASCIIToUTF16("Sheep 1"), 1), 0); 40 sheep_node->Add(new NodeType(ASCIIToUTF16("Sheep 2"), 1), 1); 41 42 tree_view_ = new TreeView(); 43 tree_view_->set_context_menu_controller(this); 44 tree_view_->SetRootShown(false); 45 tree_view_->SetModel(&model_); 46 tree_view_->SetController(this); 47 add_ = new LabelButton(this, ASCIIToUTF16("Add")); 48 add_->set_focusable(true); 49 remove_ = new LabelButton(this, ASCIIToUTF16("Remove")); 50 remove_->set_focusable(true); 51 change_title_ = new LabelButton(this, ASCIIToUTF16("Change Title")); 52 change_title_->set_focusable(true); 53 54 GridLayout* layout = new GridLayout(container); 55 container->SetLayoutManager(layout); 56 57 const int tree_view_column = 0; 58 ColumnSet* column_set = layout->AddColumnSet(tree_view_column); 59 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 60 1.0f, GridLayout::USE_PREF, 0, 0); 61 layout->StartRow(1 /* expand */, tree_view_column); 62 layout->AddView(tree_view_->CreateParentIfNecessary()); 63 64 // Add control buttons horizontally. 65 const int button_column = 1; 66 column_set = layout->AddColumnSet(button_column); 67 for (int i = 0; i < 3; i++) { 68 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 69 1.0f, GridLayout::USE_PREF, 0, 0); 70 } 71 72 layout->StartRow(0 /* no expand */, button_column); 73 layout->AddView(add_); 74 layout->AddView(remove_); 75 layout->AddView(change_title_); 76 } 77 78 void TreeViewExample::AddNewNode() { 79 NodeType* selected_node = 80 static_cast<NodeType*>(tree_view_->GetSelectedNode()); 81 if (!selected_node) 82 selected_node = model_.GetRoot(); 83 NodeType* new_node = new NodeType(selected_node->GetTitle(), 1); 84 model_.Add(selected_node, new_node, selected_node->child_count()); 85 tree_view_->SetSelectedNode(new_node); 86 } 87 88 bool TreeViewExample::IsCommandIdEnabled(int command_id) { 89 return command_id != ID_REMOVE || 90 tree_view_->GetSelectedNode() != model_.GetRoot(); 91 } 92 93 void TreeViewExample::ButtonPressed(Button* sender, const ui::Event& event) { 94 NodeType* selected_node = 95 static_cast<NodeType*>(tree_view_->GetSelectedNode()); 96 if (sender == add_) { 97 AddNewNode(); 98 } else if (sender == remove_) { 99 DCHECK(selected_node); 100 DCHECK_NE(model_.GetRoot(), selected_node); 101 model_.Remove(selected_node->parent(), selected_node); 102 } else if (sender == change_title_) { 103 DCHECK(selected_node); 104 model_.SetTitle(selected_node, 105 selected_node->GetTitle() + ASCIIToUTF16("new")); 106 } 107 } 108 109 void TreeViewExample::OnTreeViewSelectionChanged(TreeView* tree_view) { 110 ui::TreeModelNode* node = tree_view_->GetSelectedNode(); 111 if (node) { 112 change_title_->SetEnabled(true); 113 remove_->SetEnabled(node != model_.GetRoot()); 114 } else { 115 change_title_->SetEnabled(false); 116 remove_->SetEnabled(false); 117 } 118 } 119 120 bool TreeViewExample::CanEdit(TreeView* tree_view, 121 ui::TreeModelNode* node) { 122 return true; 123 } 124 125 void TreeViewExample::ShowContextMenuForView(View* source, 126 const gfx::Point& point, 127 ui::MenuSourceType source_type) { 128 ui::SimpleMenuModel context_menu_model(this); 129 context_menu_model.AddItem(ID_EDIT, ASCIIToUTF16("Edit")); 130 context_menu_model.AddItem(ID_REMOVE, ASCIIToUTF16("Remove")); 131 context_menu_model.AddItem(ID_ADD, ASCIIToUTF16("Add")); 132 context_menu_runner_.reset(new MenuRunner(&context_menu_model)); 133 if (context_menu_runner_->RunMenuAt(source->GetWidget(), NULL, 134 gfx::Rect(point, gfx::Size()), MenuItemView::TOPLEFT, source_type, 0) == 135 MenuRunner::MENU_DELETED) 136 return; 137 } 138 139 bool TreeViewExample::IsCommandIdChecked(int command_id) const { 140 return false; 141 } 142 143 bool TreeViewExample::IsCommandIdEnabled(int command_id) const { 144 return const_cast<TreeViewExample*>(this)->IsCommandIdEnabled(command_id); 145 } 146 147 bool TreeViewExample::GetAcceleratorForCommandId( 148 int command_id, 149 ui::Accelerator* accelerator) { 150 return false; 151 } 152 153 void TreeViewExample::ExecuteCommand(int command_id, int event_flags) { 154 NodeType* selected_node = 155 static_cast<NodeType*>(tree_view_->GetSelectedNode()); 156 switch (command_id) { 157 case ID_EDIT: 158 tree_view_->StartEditing(selected_node); 159 break; 160 case ID_REMOVE: 161 model_.Remove(selected_node->parent(), selected_node); 162 break; 163 case ID_ADD: 164 AddNewNode(); 165 break; 166 default: 167 NOTREACHED(); 168 } 169 } 170 171 } // namespace examples 172 } // namespace views 173