5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
using System.Collections.ObjectModel;
31
using System.Collections.Generic;
36
public struct NodePosition
38
internal TreePosition ParentPos;
42
public class TreeNavigator
44
ITreeStoreBackend backend;
47
internal TreeNavigator (ITreeStoreBackend backend, TreePosition pos)
49
this.backend = backend;
53
public TreePosition CurrentPosition {
57
public TreeNavigator Clone ()
59
return new TreeNavigator (backend, pos);
62
bool CommitPos (TreePosition newPosition)
64
if (newPosition != null) {
72
public bool MoveToFirst ()
74
return CommitPos (backend.GetChild (null, 0));
77
public bool MoveNext ()
79
return CommitPos (backend.GetNext (pos));
82
public bool MovePrevious ()
84
return CommitPos (backend.GetPrevious (pos));
87
public bool MoveToChild ()
89
return CommitPos (backend.GetChild (pos, 0));
92
public bool MoveToParent ()
94
return CommitPos (backend.GetParent (pos));
97
public TreeNavigator InsertBefore ()
99
pos = backend.InsertBefore (pos);
103
public TreeNavigator InsertAfter ()
105
pos = backend.InsertAfter (pos);
109
public TreeNavigator AddChild ()
111
pos = backend.AddChild (pos);
115
public TreeNavigator SetValue<T> (DataField<T> field, T data)
117
backend.SetValue (pos, field.Index, data);
121
public T GetValue<T> (DataField<T> field)
123
return (T) backend.GetValue (pos, field.Index);
126
public void Remove ()
128
backend.Remove (pos);
131
public void RemoveChildren ()
133
TreePosition child = backend.GetChild (pos, 0);
134
while (child != null) {
135
backend.Remove (child);
136
child = backend.GetChild (pos, 0);