~ubuntu-branches/ubuntu/precise/gnome-do/precise-proposed

« back to all changes in this revision

Viewing changes to Do/src/Do.Core/SearchControllers/SimpleSearchContext.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-09-14 10:09:40 UTC
  • mto: (0.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20080914100940-kyghudg7py14bu2z
Tags: upstream-0.6.0.0
ImportĀ upstreamĀ versionĀ 0.6.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* SimpleSearchContext.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this
 
5
 * source distribution.
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
using System;
 
22
using System.Collections.Generic;
 
23
 
 
24
using Do.Universe;
 
25
using Do.UI;
 
26
 
 
27
namespace Do.Core
 
28
{
 
29
        public class SimpleSearchContext : ICloneable
 
30
        {
 
31
                string query;
 
32
                int cursor;
 
33
                IObject[] results;
 
34
                
 
35
                public SimpleSearchContext ()
 
36
                {
 
37
                        SecondaryCursors = new IObject[0];
 
38
                        query = string.Empty;
 
39
                        results = new IObject[0];
 
40
                }
 
41
                
 
42
                public SimpleSearchContext LastContext   { get; set; }
 
43
                public SimpleSearchContext ParentContext { get; set; }
 
44
                public IObject[] SecondaryCursors        { get; set; }
 
45
                
 
46
                public string Query
 
47
                {
 
48
                        get { return query ?? query = string.Empty; }
 
49
                        set { query = value; }
 
50
                }
 
51
 
 
52
                public IObject[] Results
 
53
                {
 
54
                        get { return results ?? results = new IObject[0]; }
 
55
                        set {
 
56
                                results = value ?? new IObject[0];
 
57
                                
 
58
                                cursor = 0;
 
59
                                
 
60
                                if (SecondaryCursors.Length == 0) return;
 
61
                                
 
62
                                List<IObject> secondary = new List<IObject> ();
 
63
                                foreach (IObject obj in SecondaryCursors) {
 
64
                                        foreach (IObject robj in Results) {
 
65
                                                if (obj == robj) {
 
66
                                                        secondary.Add (obj);
 
67
                                                }
 
68
                                        }
 
69
                                }
 
70
                                
 
71
                                SecondaryCursors = secondary.ToArray ();
 
72
                        }
 
73
                }
 
74
                
 
75
                public IObject Selection
 
76
                {
 
77
                        get {
 
78
                                try {
 
79
                                        return results[cursor];
 
80
                                } catch {
 
81
                                        return null;
 
82
                                }
 
83
                        }
 
84
                }
 
85
                
 
86
                public IObject[] FullSelection
 
87
                {
 
88
                        get {
 
89
                                List<IObject> outList = new List<IObject> ();
 
90
                                outList.AddRange (SecondaryCursors);
 
91
                                
 
92
                                //Juggle our selection to front to give best possible legacy plugin support.  Ideally this
 
93
                                //wont matter
 
94
                                outList.Remove (Selection);
 
95
                                outList.Insert (0, Selection);
 
96
                                
 
97
                                return outList.ToArray ();
 
98
                        }
 
99
                }
 
100
                
 
101
                public int Cursor
 
102
                {
 
103
                        get { return cursor; }
 
104
                        set { 
 
105
                                if (value > Results.Length - 1)
 
106
                                        cursor = Results.Length - 1;
 
107
                                else if ( value <= 0 )
 
108
                                        cursor = 0;
 
109
                                else
 
110
                                        cursor = value;
 
111
                        }
 
112
                }
 
113
                
 
114
                /// <summary>
 
115
                /// Creates a new IUIContext from the current context.  Contexts however are not away if
 
116
                /// the controller wishes for them to be displayed in Large Text Mode, so this value must
 
117
                /// be passed in.  Parent contexts are always assumed to not use large text mode.
 
118
                /// </summary>
 
119
                /// <param name="textMode">
 
120
                /// A <see cref="System.Boolean"/>
 
121
                /// </param>
 
122
                /// <returns>
 
123
                /// A <see cref="IUIContext"/>
 
124
                /// </returns>
 
125
                public IUIContext GetIUIContext (bool textMode)
 
126
                {
 
127
                        if (ParentContext == null)
 
128
                                return new UIContext (Selection, Results, Cursor, SecondaryCursorsToIntArray (), 
 
129
                                                      Query, textMode, null);
 
130
                        else
 
131
                                return new UIContext (Selection, Results, Cursor, SecondaryCursorsToIntArray (), 
 
132
                                                      Query, textMode, ParentContext.GetIUIContext (false));
 
133
                }
 
134
                
 
135
                /// <summary>
 
136
                /// It is very convinient to convert to an int array.
 
137
                /// </summary>
 
138
                /// <returns>
 
139
                /// A <see cref="System.Int32"/>
 
140
                /// </returns>
 
141
                public int[] SecondaryCursorsToIntArray () 
 
142
                {
 
143
                        if (SecondaryCursors.Length == 0)
 
144
                                return new int[0];
 
145
                        List<int> cursors = new List<int> ();
 
146
                                
 
147
                        foreach (IObject obj in SecondaryCursors) {
 
148
                                for (int i = 0; i < Results.Length; i++) {
 
149
                                        if (Results[i] == obj)
 
150
                                                cursors.Add (i);
 
151
                                }
 
152
                        }
 
153
                        
 
154
                        return cursors.ToArray ();
 
155
                }
 
156
                
 
157
                public object Clone ()
 
158
                {
 
159
                        SimpleSearchContext clone;
 
160
                        
 
161
                        clone = new SimpleSearchContext ();
 
162
                        clone.Query = query;
 
163
                        clone.LastContext = LastContext;
 
164
                        clone.ParentContext = ParentContext;
 
165
                        clone.Cursor = Cursor;
 
166
                        clone.SecondaryCursors = SecondaryCursors; //Cloning these makes no sense
 
167
                        clone.Results = results.Clone () as IObject[];
 
168
                        return clone;
 
169
                }
 
170
                
 
171
                public void Destroy (bool preserveParent)
 
172
                {
 
173
                        if (preserveParent) {
 
174
                                if (LastContext != null)
 
175
                                        LastContext.Destroy ();
 
176
                        } else {
 
177
                                Destroy ();
 
178
                        }
 
179
                                
 
180
                }
 
181
                
 
182
                public void Destroy ()
 
183
                {
 
184
                        if (LastContext != null)
 
185
                                LastContext.Destroy ();
 
186
                        
 
187
                        if (ParentContext != null)
 
188
                                ParentContext.Destroy ();
 
189
                }
 
190
                
 
191
                public override int GetHashCode ()
 
192
                {
 
193
                        return base.GetHashCode ();
 
194
                }
 
195
                
 
196
                public override string ToString ()
 
197
                {
 
198
                        return "SearchContext " + base.ToString () + 
 
199
                        "\n\tQuery: \"" + query + "\"" +
 
200
                        "\n\tHas last context: " + (LastContext != null) +
 
201
                        "\n\tHas parent context: " + (ParentContext != null) +
 
202
                        "\n\tCursor: " + cursor +
 
203
                        "\n\tResults: " + results;
 
204
                }
 
205
        }
 
206
}