~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.Debugger/MonoDevelop.Debugger/TreePathReference.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// TreePathReference.cs
 
3
//
 
4
// Author: Jeffrey Stedfast <jeff@xamarin.com>
 
5
//
 
6
// Copyright (c) 2013 Xamarin Inc.
 
7
//
 
8
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
9
// of this software and associated documentation files (the "Software"), to deal
 
10
// in the Software without restriction, including without limitation the rights
 
11
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
12
// copies of the Software, and to permit persons to whom the Software is
 
13
// furnished to do so, subject to the following conditions:
 
14
//
 
15
// The above copyright notice and this permission notice shall be included in
 
16
// all copies or substantial portions of the Software.
 
17
//
 
18
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
19
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
20
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
21
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
22
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
23
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
24
// THE SOFTWARE.
 
25
 
 
26
using System;
 
27
 
 
28
using Gtk;
 
29
 
 
30
namespace MonoDevelop.Debugger
 
31
{
 
32
        public sealed class TreePathReference : IDisposable
 
33
        {
 
34
                int[] indices;
 
35
                TreePath path;
 
36
 
 
37
                public TreePathReference (TreeModel model, TreePath path)
 
38
                {
 
39
                        model.RowsReordered += HandleRowsReordered;
 
40
                        model.RowInserted += HandleRowInserted;
 
41
                        model.RowDeleted += HandleRowDeleted;
 
42
 
 
43
                        indices = path.Indices;
 
44
                        this.path = path;
 
45
                        Model = model;
 
46
                }
 
47
 
 
48
                void HandleRowsReordered (object o, RowsReorderedArgs args)
 
49
                {
 
50
                        int length = Model.IterNChildren (args.Iter);
 
51
                        int depth = args.Path.Depth;
 
52
 
 
53
                        if (length < 2 || !args.Path.IsAncestor (Path) || indices.Length <= depth)
 
54
                                return;
 
55
 
 
56
                        for (int i = 0; i < length; i++) {
 
57
                                if (args.NewChildOrder[i] == indices[depth]) {
 
58
                                        indices[depth] = i;
 
59
                                        break;
 
60
                                }
 
61
                        }
 
62
                }
 
63
 
 
64
                void HandleRowInserted (object o, RowInsertedArgs args)
 
65
                {
 
66
                        var inserted = args.Path.Indices;
 
67
                        int i;
 
68
 
 
69
                        for (i = 0; i < inserted.Length - 1 && i < indices.Length - 1; i++) {
 
70
                                if (inserted[i] > indices[i]) {
 
71
                                        // the inserted node is listed below the node we are watching, ignore it
 
72
                                        return;
 
73
                                }
 
74
 
 
75
                                if (inserted[i] < indices[i])
 
76
                                        break;
 
77
                        }
 
78
 
 
79
                        if (inserted[i] <= indices[i]) {
 
80
                                // the node was inserted above the node we are watching, update our position
 
81
                                indices[i]++;
 
82
                                path = null;
 
83
                        }
 
84
                }
 
85
 
 
86
                void HandleRowDeleted (object o, RowDeletedArgs args)
 
87
                {
 
88
                        var deleted = args.Path.Indices;
 
89
                        int i;
 
90
 
 
91
                        for (i = 0; i < deleted.Length && i < indices.Length; i++) {
 
92
                                if (deleted[i] > indices[i]) {
 
93
                                        // the deleted node is listed below the node we are watching, ignore it
 
94
                                        return;
 
95
                                }
 
96
 
 
97
                                if (deleted[i] < indices[i]) {
 
98
                                        // the deleted node is listed above the node we are watching, update our position
 
99
                                        indices[i]--;
 
100
                                        path = null;
 
101
                                        return;
 
102
                                }
 
103
                        }
 
104
 
 
105
                        if (deleted.Length <= indices.Length) {
 
106
                                // the node we are watching (or its parent) has been deleted
 
107
                                Invalidate ();
 
108
                        }
 
109
                }
 
110
 
 
111
                public TreeModel Model {
 
112
                        get; private set;
 
113
                }
 
114
 
 
115
                public TreePath Path {
 
116
                        get {
 
117
                                if (path == null && indices != null)
 
118
                                        path = new TreePath (indices);
 
119
 
 
120
                                return path;
 
121
                        }
 
122
                }
 
123
 
 
124
                public bool IsValid {
 
125
                        get { return Model != null && indices != null; }
 
126
                }
 
127
 
 
128
                void Invalidate ()
 
129
                {
 
130
                        if (Model != null) {
 
131
                                Model.RowsReordered -= HandleRowsReordered;
 
132
                                Model.RowInserted -= HandleRowInserted;
 
133
                                Model.RowDeleted -= HandleRowDeleted;
 
134
                                Model = null;
 
135
                        }
 
136
                        
 
137
                        indices = null;
 
138
                        path = null;
 
139
                }
 
140
 
 
141
                #region IDisposable implementation
 
142
                public void Dispose ()
 
143
                {
 
144
                        Invalidate ();
 
145
                }
 
146
                #endregion
 
147
        }
 
148
}