[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) |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame^] | 84 | parent->Remove(node); |
[email protected] | a0dd6a3 | 2011-03-18 17:31:37 | [diff] [blame] | 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 | |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame^] | 89 | // Remove |node| from this Node and returns it. It's up to the caller to |
| 90 | // delete it. |
| 91 | virtual NodeType* Remove(NodeType* node) { |
| 92 | typename std::vector<NodeType*>::iterator i = |
| 93 | std::find(children_->begin(), children_->end(), node); |
| 94 | DCHECK(i != children_.end()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | node->parent_ = NULL; |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame^] | 96 | children_->erase(i); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | return node; |
| 98 | } |
| 99 | |
[email protected] | 58b359d | 2009-02-27 22:05:08 | [diff] [blame] | 100 | // Removes all the children from this node. This does NOT delete the nodes. |
| 101 | void RemoveAll() { |
| 102 | for (size_t i = 0; i < children_->size(); ++i) |
| 103 | children_[i]->parent_ = NULL; |
| 104 | children_->clear(); |
| 105 | } |
| 106 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | // Returns the number of children. |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 108 | int child_count() const { return static_cast<int>(children_->size()); } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | |
[email protected] | 9ff22ee4 | 2009-10-25 06:03:03 | [diff] [blame] | 110 | // Returns the number of all nodes in teh subtree rooted at this node, |
| 111 | // including this node. |
| 112 | int GetTotalNodeCount() const { |
| 113 | int count = 1; // Start with one to include the node itself. |
| 114 | for (size_t i = 0; i < children_->size(); ++i) { |
[email protected] | dce5162 | 2009-11-06 04:58:48 | [diff] [blame] | 115 | const TreeNode<NodeType>* child = children_[i]; |
[email protected] | 9ff22ee4 | 2009-10-25 06:03:03 | [diff] [blame] | 116 | count += child->GetTotalNodeCount(); |
| 117 | } |
| 118 | return count; |
| 119 | } |
| 120 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 121 | // Returns the node at |index|. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | NodeType* GetChild(int index) { |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 123 | DCHECK(index >= 0 && index < child_count()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | return children_[index]; |
| 125 | } |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 126 | const NodeType* GetChild(int index) const { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 127 | DCHECK_LE(0, index); |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 128 | DCHECK_GT(child_count(), index); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 129 | return children_[index]; |
| 130 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | |
[email protected] | 2d48ee84 | 2011-03-08 23:27:29 | [diff] [blame] | 132 | // Returns the parent of this object, or NULL if it's the root. |
| 133 | const NodeType* parent() const { return parent_; } |
| 134 | NodeType* parent() { return parent_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | |
[email protected] | 368f3a7 | 2011-03-08 17:17:48 | [diff] [blame] | 136 | // Returns the index of |node|, or -1 if |node| is not a child of this. |
| 137 | int GetIndexOf(const NodeType* node) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | DCHECK(node); |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 139 | typename std::vector<NodeType*>::const_iterator i = |
[email protected] | 775cfd76 | 2008-12-09 17:12:12 | [diff] [blame] | 140 | std::find(children_->begin(), children_->end(), node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | if (i != children_->end()) |
| 142 | return static_cast<int>(i - children_->begin()); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | // Sets the title of the node. |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 147 | void set_title(const string16& title) { title_ = title; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 149 | // TreeModelNode: |
| 150 | virtual const string16& GetTitle() const OVERRIDE { return title_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | |
| 152 | // Returns true if this is the root. |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 153 | bool IsRoot() const { return parent_ == NULL; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | |
| 155 | // Returns true if this == ancestor, or one of this nodes parents is |
| 156 | // ancestor. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 157 | bool HasAncestor(const NodeType* ancestor) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | if (ancestor == this) |
| 159 | return true; |
| 160 | if (!ancestor) |
| 161 | return false; |
| 162 | return parent_ ? parent_->HasAncestor(ancestor) : false; |
| 163 | } |
| 164 | |
[email protected] | e2f86d9 | 2009-02-25 00:22:01 | [diff] [blame] | 165 | protected: |
| 166 | std::vector<NodeType*>& children() { return children_.get(); } |
| 167 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | private: |
| 169 | // Title displayed in the tree. |
[email protected] | e64e901 | 2010-01-11 23:10:55 | [diff] [blame] | 170 | string16 title_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 172 | // This node's parent. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 173 | NodeType* parent_; |
| 174 | |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 175 | // This node's children. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | ScopedVector<NodeType> children_; |
| 177 | |
[email protected] | 405ed12 | 2008-11-14 17:48:40 | [diff] [blame] | 178 | DISALLOW_COPY_AND_ASSIGN(TreeNode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | // TreeNodeWithValue ---------------------------------------------------------- |
| 182 | |
| 183 | template <class ValueType> |
[email protected] | 405ed12 | 2008-11-14 17:48:40 | [diff] [blame] | 184 | class TreeNodeWithValue : public TreeNode< TreeNodeWithValue<ValueType> > { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | public: |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 186 | TreeNodeWithValue() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | |
[email protected] | a5b58f5 | 2009-11-17 22:15:44 | [diff] [blame] | 188 | explicit TreeNodeWithValue(const ValueType& value) |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 189 | : ParentType(string16()), value(value) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | |
[email protected] | 964b677 | 2010-08-25 16:50:48 | [diff] [blame] | 191 | TreeNodeWithValue(const string16& title, const ValueType& value) |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 192 | : ParentType(title), value(value) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | |
| 194 | ValueType value; |
| 195 | |
| 196 | private: |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 197 | typedef TreeNode< TreeNodeWithValue<ValueType> > ParentType; |
| 198 | |
[email protected] | 405ed12 | 2008-11-14 17:48:40 | [diff] [blame] | 199 | DISALLOW_COPY_AND_ASSIGN(TreeNodeWithValue); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | // TreeNodeModel -------------------------------------------------------------- |
| 203 | |
| 204 | // TreeModel implementation intended to be used with TreeNodes. |
| 205 | template <class NodeType> |
| 206 | class TreeNodeModel : public TreeModel { |
| 207 | public: |
| 208 | // Creates a TreeNodeModel with the specified root node. The root is owned |
| 209 | // by the TreeNodeModel. |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 210 | explicit TreeNodeModel(NodeType* root) : root_(root) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | virtual ~TreeNodeModel() {} |
| 212 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | NodeType* AsNode(TreeModelNode* model_node) { |
[email protected] | dce5162 | 2009-11-06 04:58:48 | [diff] [blame] | 214 | return static_cast<NodeType*>(model_node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame^] | 217 | void Add(NodeType* parent, NodeType* node, int index) { |
| 218 | DCHECK(parent && node); |
| 219 | parent->Add(node, index); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | NotifyObserverTreeNodesAdded(parent, index, 1); |
| 221 | } |
| 222 | |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame^] | 223 | NodeType* Remove(NodeType* parent, NodeType* node) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | DCHECK(parent); |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame^] | 225 | int index = parent->GetIndexOf(node); |
| 226 | NodeType* delete_node = parent->Remove(node); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | NotifyObserverTreeNodesRemoved(parent, index, 1); |
[email protected] | 18cc5ff | 2011-03-22 01:05:23 | [diff] [blame^] | 228 | return delete_node; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void NotifyObserverTreeNodesAdded(NodeType* parent, int start, int count) { |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 232 | FOR_EACH_OBSERVER(TreeModelObserver, |
| 233 | observer_list_, |
| 234 | TreeNodesAdded(this, parent, start, count)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void NotifyObserverTreeNodesRemoved(NodeType* parent, int start, int count) { |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 238 | FOR_EACH_OBSERVER(TreeModelObserver, |
| 239 | observer_list_, |
| 240 | TreeNodesRemoved(this, parent, start, count)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 243 | void NotifyObserverTreeNodeChanged(TreeModelNode* node) { |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 244 | FOR_EACH_OBSERVER(TreeModelObserver, |
| 245 | observer_list_, |
| 246 | TreeNodeChanged(this, node)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | } |
| 248 | |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 249 | // TreeModel: |
| 250 | virtual NodeType* GetRoot() OVERRIDE { |
| 251 | return root_.get(); |
| 252 | } |
| 253 | |
| 254 | virtual int GetChildCount(TreeModelNode* parent) OVERRIDE { |
| 255 | DCHECK(parent); |
| 256 | return AsNode(parent)->child_count(); |
| 257 | } |
| 258 | |
| 259 | virtual NodeType* GetChild(TreeModelNode* parent, int index) OVERRIDE { |
| 260 | DCHECK(parent); |
| 261 | return AsNode(parent)->GetChild(index); |
| 262 | } |
| 263 | |
| 264 | virtual int GetIndexOf(TreeModelNode* parent, TreeModelNode* child) OVERRIDE { |
| 265 | DCHECK(parent); |
| 266 | return AsNode(parent)->GetIndexOf(AsNode(child)); |
| 267 | } |
| 268 | |
| 269 | virtual TreeModelNode* GetParent(TreeModelNode* node) OVERRIDE { |
| 270 | DCHECK(node); |
| 271 | return AsNode(node)->parent(); |
| 272 | } |
| 273 | |
| 274 | virtual void AddObserver(TreeModelObserver* observer) OVERRIDE { |
| 275 | observer_list_.AddObserver(observer); |
| 276 | } |
| 277 | |
| 278 | virtual void RemoveObserver(TreeModelObserver* observer) OVERRIDE { |
| 279 | observer_list_.RemoveObserver(observer); |
| 280 | } |
| 281 | |
| 282 | virtual void SetTitle(TreeModelNode* node, const string16& title) OVERRIDE { |
| 283 | DCHECK(node); |
| 284 | AsNode(node)->set_title(title); |
| 285 | NotifyObserverTreeNodeChanged(node); |
| 286 | } |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 287 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | private: |
[email protected] | 0457c6b | 2010-02-10 18:44:48 | [diff] [blame] | 289 | // The observers. |
| 290 | ObserverList<TreeModelObserver> observer_list_; |
[email protected] | 23db9f7 | 2011-03-11 19:43:24 | [diff] [blame] | 291 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | // The root. |
| 293 | scoped_ptr<NodeType> root_; |
| 294 | |
[email protected] | 405ed12 | 2008-11-14 17:48:40 | [diff] [blame] | 295 | DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | }; |
| 297 | |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 298 | } // namespace ui |
| 299 | |
| 300 | #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ |