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