[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 5 | #ifndef UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
| 6 | #define UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | 775cfd76 | 2008-12-09 17:12:12 | [diff] [blame] | 9 | #include <algorithm> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
| 12 | #include "base/basictypes.h" |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 15 | #include "base/observer_list.h" |
[email protected] | 1fec840 | 2009-03-13 19:11:59 | [diff] [blame] | 16 | #include "base/scoped_ptr.h" |
[email protected] | 80720414 | 2009-05-05 03:31:44 | [diff] [blame] | 17 | #include "base/scoped_vector.h" |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 18 | #include "base/string16.h" |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 19 | #include "ui/base/models/tree_model.h" |
| 20 | |
| 21 | namespace ui { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | |
| 23 | // TreeNodeModel and TreeNodes provide an implementation of TreeModel around |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 24 | // TreeNodes. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | // |
| 26 | // TreeNodes own their children, so that deleting a node deletes all |
| 27 | // descendants. |
| 28 | // |
| 29 | // TreeNodes do NOT maintain a pointer back to the model. As such, if you |
| 30 | // are using TreeNodes with a TreeNodeModel you will need to notify the observer |
| 31 | // yourself any time you make any change directly to the TreeNodes. For example, |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 32 | // if you directly invoke set_title on a node it does not notify the observer, |
| 33 | // you will need to do it yourself. This includes the following methods: Add, |
| 34 | // Remove and set_title. TreeNodeModel provides cover methods that mutate the |
| 35 | // TreeNodes and notify the observer. If you are using TreeNodes with a |
| 36 | // TreeNodeModel use the cover methods to save yourself the headache. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | // |
| 38 | // The following example creates a TreeNode with two children and then |
| 39 | // creates a TreeNodeModel from it: |
| 40 | // |
[email protected] | 964b677 | 2010-08-25 16:50:48 | [diff] [blame] | 41 | // TreeNodeWithValue<int> root = |
| 42 | // new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0); |
| 43 | // root.add(new TreeNodeWithValue<int>(ASCIIToUTF16("child 1"), 1)); |
| 44 | // root.add(new TreeNodeWithValue<int>(ASCIIToUTF16("child 2"), 1)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | // TreeNodeModel<TreeNodeWithValue<int>>* model = |
| 46 | // new TreeNodeModel<TreeNodeWithValue<int>>(root); |
| 47 | // |
| 48 | // Two variants of TreeNode are provided here: |
| 49 | // |
| 50 | // . TreeNode itself is intended for subclassing. It has one type parameter |
| 51 | // that corresponds to the type of the node. When subclassing use your class |
| 52 | // name as the type parameter, eg: |
| 53 | // class MyTreeNode : public TreeNode<MyTreeNode> . |
| 54 | // . TreeNodeWithValue is a trivial subclass of TreeNode that has one type |
| 55 | // type parameter: a value type that is associated with the node. |
| 56 | // |
| 57 | // Which you use depends upon the situation. If you want to subclass and add |
| 58 | // methods, then use TreeNode. If you don't need any extra methods and just |
| 59 | // want to associate a value with each node, then use TreeNodeWithValue. |
| 60 | // |
| 61 | // Regardless of which TreeNode you use, if you are using the nodes with a |
| 62 | // TreeView take care to notify the observer when mutating the nodes. |
| 63 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | // TreeNode ------------------------------------------------------------------- |
| 65 | |
| 66 | template <class NodeType> |
| 67 | class TreeNode : public TreeModelNode { |
| 68 | public: |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 69 | TreeNode() : parent_(NULL) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 71 | explicit TreeNode(const string16& title) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | : title_(title), parent_(NULL) {} |
| 73 | |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 74 | virtual ~TreeNode() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 76 | // Adds a TreeNode as a child of this one, at |index|. |
| 77 | virtual void Add(NodeType* node, int index) { |
| 78 | DCHECK(node); |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 79 | DCHECK_LE(0, index); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 80 | DCHECK_GE(child_count(), index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | // If the node has a parent, remove it from its parent. |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 82 | NodeType* parent = node->parent(); |
| 83 | if (parent) |
| 84 | parent->Remove(parent->GetIndexOf(node)); |
| 85 | node->parent_ = static_cast<NodeType*>(this); |
| 86 | children_->insert(children_->begin() + index, node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Removes the node by index. This does NOT delete the specified node, it is |
| 90 | // up to the caller to delete it when done. |
| 91 | virtual NodeType* Remove(int index) { |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 92 | DCHECK(index >= 0 && index < child_count()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | NodeType* node = GetChild(index); |
| 94 | node->parent_ = NULL; |
| 95 | children_->erase(index + children_->begin()); |
| 96 | return node; |
| 97 | } |
| 98 | |
[email protected] | 58b359d | 2009-02-27 22:05:08 | [diff] [blame] | 99 | // Removes all the children from this node. This does NOT delete the nodes. |
| 100 | void RemoveAll() { |
| 101 | for (size_t i = 0; i < children_->size(); ++i) |
| 102 | children_[i]->parent_ = NULL; |
| 103 | children_->clear(); |
| 104 | } |
| 105 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | // Returns the number of children. |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 107 | int child_count() const { return static_cast<int>(children_->size()); } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | |
[email protected] | 9ff22ee4 | 2009-10-25 06:03:03 | [diff] [blame] | 109 | // Returns the number of all nodes in teh subtree rooted at this node, |
| 110 | // including this node. |
| 111 | int GetTotalNodeCount() const { |
| 112 | int count = 1; // Start with one to include the node itself. |
| 113 | for (size_t i = 0; i < children_->size(); ++i) { |
[email protected] | dce5162 | 2009-11-06 04:58:48 | [diff] [blame] | 114 | const TreeNode<NodeType>* child = children_[i]; |
[email protected] | 9ff22ee4 | 2009-10-25 06:03:03 | [diff] [blame] | 115 | count += child->GetTotalNodeCount(); |
| 116 | } |
| 117 | return count; |
| 118 | } |
| 119 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 120 | // Returns the node at |index|. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | NodeType* GetChild(int index) { |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 122 | DCHECK(index >= 0 && index < child_count()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | return children_[index]; |
| 124 | } |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 125 | const NodeType* GetChild(int index) const { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 126 | DCHECK_LE(0, index); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 127 | DCHECK_GT(child_count(), index); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 128 | return children_[index]; |
| 129 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | |
[email protected] | 2d48ee84 | 2011-03-08 23:27:29 | [diff] [blame] | 131 | // Returns the parent of this object, or NULL if it's the root. |
| 132 | const NodeType* parent() const { return parent_; } |
| 133 | NodeType* parent() { return parent_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | |
[email protected] | 368f3a7 | 2011-03-08 17:17:48 | [diff] [blame] | 135 | // Returns the index of |node|, or -1 if |node| is not a child of this. |
| 136 | int GetIndexOf(const NodeType* node) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | DCHECK(node); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 138 | typename std::vector<NodeType*>::const_iterator i = |
[email protected] | 775cfd76 | 2008-12-09 17:12:12 | [diff] [blame] | 139 | std::find(children_->begin(), children_->end(), node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | if (i != children_->end()) |
| 141 | return static_cast<int>(i - children_->begin()); |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | // Sets the title of the node. |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 146 | void set_title(const string16& title) { title_ = title; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 148 | // TreeModelNode: |
| 149 | virtual const string16& GetTitle() const OVERRIDE { return title_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
| 151 | // Returns true if this is the root. |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 152 | bool IsRoot() const { return parent_ == NULL; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | |
| 154 | // Returns true if this == ancestor, or one of this nodes parents is |
| 155 | // ancestor. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 156 | bool HasAncestor(const NodeType* ancestor) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | if (ancestor == this) |
| 158 | return true; |
| 159 | if (!ancestor) |
| 160 | return false; |
| 161 | return parent_ ? parent_->HasAncestor(ancestor) : false; |
| 162 | } |
| 163 | |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 164 | protected: |
| 165 | std::vector<NodeType*>& children() { return children_.get(); } |
| 166 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | private: |
| 168 | // Title displayed in the tree. |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 169 | string16 title_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 171 | // This node's parent. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | NodeType* parent_; |
| 173 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 174 | // This node's children. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 175 | ScopedVector<NodeType> children_; |
| 176 | |
[email protected] | 405ed12 | 2008-11-14 17:48:40 | [diff] [blame] | 177 | DISALLOW_COPY_AND_ASSIGN(TreeNode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | // TreeNodeWithValue ---------------------------------------------------------- |
| 181 | |
| 182 | template <class ValueType> |
[email protected] | 405ed12 | 2008-11-14 17:48:40 | [diff] [blame] | 183 | class TreeNodeWithValue : public TreeNode< TreeNodeWithValue<ValueType> > { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | public: |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 185 | TreeNodeWithValue() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | |
[email protected] | a5b58f5 | 2009-11-17 22:15:44 | [diff] [blame] | 187 | explicit TreeNodeWithValue(const ValueType& value) |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 188 | : ParentType(string16()), value(value) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | |
[email protected] | 964b677 | 2010-08-25 16:50:48 | [diff] [blame] | 190 | TreeNodeWithValue(const string16& title, const ValueType& value) |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 191 | : ParentType(title), value(value) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
| 193 | ValueType value; |
| 194 | |
| 195 | private: |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 196 | typedef TreeNode< TreeNodeWithValue<ValueType> > ParentType; |
| 197 | |
[email protected] | 405ed12 | 2008-11-14 17:48:40 | [diff] [blame] | 198 | DISALLOW_COPY_AND_ASSIGN(TreeNodeWithValue); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | // TreeNodeModel -------------------------------------------------------------- |
| 202 | |
| 203 | // TreeModel implementation intended to be used with TreeNodes. |
| 204 | template <class NodeType> |
| 205 | class TreeNodeModel : public TreeModel { |
| 206 | public: |
| 207 | // Creates a TreeNodeModel with the specified root node. The root is owned |
| 208 | // by the TreeNodeModel. |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 209 | explicit TreeNodeModel(NodeType* root) : root_(root) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | virtual ~TreeNodeModel() {} |
| 211 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | NodeType* AsNode(TreeModelNode* model_node) { |
[email protected] | dce5162 | 2009-11-06 04:58:48 | [diff] [blame] | 213 | return static_cast<NodeType*>(model_node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 216 | void Add(NodeType* parent, NodeType* child, int index) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | DCHECK(parent && child); |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 218 | parent->Add(child, index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 219 | NotifyObserverTreeNodesAdded(parent, index, 1); |
| 220 | } |
| 221 | |
| 222 | NodeType* Remove(NodeType* parent, int index) { |
| 223 | DCHECK(parent); |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 224 | NodeType* child = parent->Remove(index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | NotifyObserverTreeNodesRemoved(parent, index, 1); |
[email protected] | 776e749 | 2008-10-23 16:47:41 | [diff] [blame] | 226 | return child; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void NotifyObserverTreeNodesAdded(NodeType* parent, int start, int count) { |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 230 | FOR_EACH_OBSERVER(TreeModelObserver, |
| 231 | observer_list_, |
| 232 | TreeNodesAdded(this, parent, start, count)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void NotifyObserverTreeNodesRemoved(NodeType* parent, int start, int count) { |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 236 | FOR_EACH_OBSERVER(TreeModelObserver, |
| 237 | observer_list_, |
| 238 | TreeNodesRemoved(this, parent, start, count)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 241 | void NotifyObserverTreeNodeChanged(TreeModelNode* node) { |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 242 | FOR_EACH_OBSERVER(TreeModelObserver, |
| 243 | observer_list_, |
| 244 | TreeNodeChanged(this, node)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 247 | // TreeModel: |
| 248 | virtual NodeType* GetRoot() OVERRIDE { |
| 249 | return root_.get(); |
| 250 | } |
| 251 | |
| 252 | virtual int GetChildCount(TreeModelNode* parent) OVERRIDE { |
| 253 | DCHECK(parent); |
| 254 | return AsNode(parent)->child_count(); |
| 255 | } |
| 256 | |
| 257 | virtual NodeType* GetChild(TreeModelNode* parent, int index) OVERRIDE { |
| 258 | DCHECK(parent); |
| 259 | return AsNode(parent)->GetChild(index); |
| 260 | } |
| 261 | |
| 262 | virtual int GetIndexOf(TreeModelNode* parent, TreeModelNode* child) OVERRIDE { |
| 263 | DCHECK(parent); |
| 264 | return AsNode(parent)->GetIndexOf(AsNode(child)); |
| 265 | } |
| 266 | |
| 267 | virtual TreeModelNode* GetParent(TreeModelNode* node) OVERRIDE { |
| 268 | DCHECK(node); |
| 269 | return AsNode(node)->parent(); |
| 270 | } |
| 271 | |
| 272 | virtual void AddObserver(TreeModelObserver* observer) OVERRIDE { |
| 273 | observer_list_.AddObserver(observer); |
| 274 | } |
| 275 | |
| 276 | virtual void RemoveObserver(TreeModelObserver* observer) OVERRIDE { |
| 277 | observer_list_.RemoveObserver(observer); |
| 278 | } |
| 279 | |
| 280 | virtual void SetTitle(TreeModelNode* node, const string16& title) OVERRIDE { |
| 281 | DCHECK(node); |
| 282 | AsNode(node)->set_title(title); |
| 283 | NotifyObserverTreeNodeChanged(node); |
| 284 | } |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 285 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | private: |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 287 | // The observers. |
| 288 | ObserverList<TreeModelObserver> observer_list_; |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 289 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | // The root. |
| 291 | scoped_ptr<NodeType> root_; |
| 292 | |
[email protected] | 405ed12 | 2008-11-14 17:48:40 | [diff] [blame] | 293 | DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | }; |
| 295 | |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 296 | } // namespace ui |
| 297 | |
| 298 | #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ |