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