| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_BASE_MODELS_TREE_NODE_MODEL_H_ | 5 #ifndef UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
| 6 #define UI_BASE_MODELS_TREE_NODE_MODEL_H_ | 6 #define UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 template <class NodeType> | 64 template <class NodeType> |
| 65 class TreeNode : public TreeModelNode { | 65 class TreeNode : public TreeModelNode { |
| 66 public: | 66 public: |
| 67 TreeNode() : parent_(NULL) {} | 67 TreeNode() : parent_(NULL) {} |
| 68 | 68 |
| 69 explicit TreeNode(const string16& title) | 69 explicit TreeNode(const string16& title) |
| 70 : title_(title), parent_(NULL) {} | 70 : title_(title), parent_(NULL) {} |
| 71 | 71 |
| 72 virtual ~TreeNode() {} | 72 virtual ~TreeNode() {} |
| 73 | 73 |
| 74 // Adds |node| as a child of this one, at |index|. | 74 // Adds |node| as a child of this node, at |index|. |
| 75 virtual void Add(NodeType* node, int index) { | 75 virtual void Add(NodeType* node, int index) { |
| 76 DCHECK(node); | 76 DCHECK(node); |
| 77 DCHECK_GE(index, 0); | 77 DCHECK_GE(index, 0); |
| 78 DCHECK_LE(index, child_count()); | 78 DCHECK_LE(index, child_count()); |
| 79 // If the node has a parent, remove it from its parent. | 79 // If |node| has a parent, remove it from its parent. |
| 80 NodeType* parent = node->parent_; | 80 NodeType* parent = node->parent_; |
| 81 if (parent) | 81 if (parent) |
| 82 parent->Remove(node); | 82 parent->Remove(node); |
| 83 node->parent_ = static_cast<NodeType*>(this); | 83 node->parent_ = static_cast<NodeType*>(this); |
| 84 children_->insert(children_->begin() + index, node); | 84 children_->insert(children_->begin() + index, node); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Removes |node| from this node and returns it. It's up to the caller to | 87 // Removes |node| from this node and returns it. It's up to the caller to |
| 88 // delete it. | 88 // delete it. |
| 89 virtual NodeType* Remove(NodeType* node) { | 89 virtual NodeType* Remove(NodeType* node) { |
| 90 typename std::vector<NodeType*>::iterator i = | 90 typename std::vector<NodeType*>::iterator i = |
| 91 std::find(children_->begin(), children_->end(), node); | 91 std::find(children_->begin(), children_->end(), node); |
| 92 DCHECK(i != children_.end()); | 92 DCHECK(i != children_->end()); |
| 93 node->parent_ = NULL; | 93 node->parent_ = NULL; |
| 94 children_->erase(i); | 94 children_->erase(i); |
| 95 return node; | 95 return node; |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Removes all the children from this node. This does NOT delete the nodes. | 98 // Removes all the children from this node. This does NOT delete the nodes. |
| 99 void RemoveAll() { | 99 void RemoveAll() { |
| 100 for (size_t i = 0; i < children_->size(); ++i) | 100 for (size_t i = 0; i < children_->size(); ++i) |
| 101 children_[i]->parent_ = NULL; | 101 children_[i]->parent_ = NULL; |
| 102 children_->clear(); | 102 children_->clear(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 // Returns the parent node, or NULL if this is the root node. | 105 // Returns the parent node, or NULL if this is the root node. |
| 106 const NodeType* parent() const { return parent_; } | 106 const NodeType* parent() const { return parent_; } |
| 107 NodeType* parent() { return parent_; } | 107 NodeType* parent() { return parent_; } |
| 108 | 108 |
| 109 // Returns true if this is the root node. | 109 // Returns true if this is the root node. |
| 110 bool is_root() const { return parent_ == NULL; } | 110 bool is_root() const { return parent_ == NULL; } |
| 111 | 111 |
| 112 // Returns the number of children. | 112 // Returns the number of children. |
| 113 int child_count() const { return static_cast<int>(children_->size()); } | 113 int child_count() const { return static_cast<int>(children_->size()); } |
| 114 | 114 |
| 115 // Returns true if this node has no children. | 115 // Returns true if this node has no children. |
| 116 bool empty() const { return children_.empty(); } | 116 bool empty() const { return children_->empty(); } |
| 117 | 117 |
| 118 // Returns the number of all nodes in the subtree rooted at this node, | 118 // Returns the number of all nodes in the subtree rooted at this node, |
| 119 // including this node. | 119 // including this node. |
| 120 int GetTotalNodeCount() const { | 120 int GetTotalNodeCount() const { |
| 121 int count = 1; // Start with one to include the node itself. | 121 int count = 1; // Start with one to include the node itself. |
| 122 for (size_t i = 0; i < children_->size(); ++i) | 122 for (size_t i = 0; i < children_->size(); ++i) |
| 123 count += children_[i]->GetTotalNodeCount(); | 123 count += children_[i]->GetTotalNodeCount(); |
| 124 return count; | 124 return count; |
| 125 } | 125 } |
| 126 | 126 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 | 289 |
| 290 // The root. | 290 // The root. |
| 291 scoped_ptr<NodeType> root_; | 291 scoped_ptr<NodeType> root_; |
| 292 | 292 |
| 293 DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); | 293 DISALLOW_COPY_AND_ASSIGN(TreeNodeModel); |
| 294 }; | 294 }; |
| 295 | 295 |
| 296 } // namespace ui | 296 } // namespace ui |
| 297 | 297 |
| 298 #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ | 298 #endif // UI_BASE_MODELS_TREE_NODE_MODEL_H_ |
| OLD | NEW |