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