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

« back to all changes in this revision

Viewing changes to Do/src/Do.Core/SearchControllers/SimpleSearchController.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
// SearchController.cs
 
2
// 
 
3
// GNOME Do is the legal property of its developers. Please refer to the
 
4
// COPYRIGHT file distributed with this source distribution.
 
5
//
 
6
// This program is free software: you can redistribute it and/or modify
 
7
// it under the terms of the GNU General Public License as published by
 
8
// the Free Software Foundation, either version 3 of the License, or
 
9
// (at your option) any later version.
 
10
//
 
11
// This program is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
// GNU General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public License
 
17
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
//
 
19
 
 
20
using System;
 
21
using System.Collections.Generic;
 
22
 
 
23
using Do.Addins;
 
24
using Do.Universe;
 
25
using Do.UI;
 
26
 
 
27
namespace Do.Core
 
28
{
 
29
        public abstract class SimpleSearchController : ISearchController
 
30
        {       
 
31
                protected bool textMode = false;
 
32
                
 
33
                protected SimpleSearchContext context;
 
34
                protected Type[] searchFilter;
 
35
                protected Type[] defaultFilter = Type.EmptyTypes;
 
36
                protected const int Timeout = 300;
 
37
                
 
38
                public IUIContext UIContext {
 
39
                        get {
 
40
                                return context.GetIUIContext (TextMode);
 
41
                        }
 
42
                }
 
43
                
 
44
                public IObject[] Results {
 
45
                        get {
 
46
                                return context.Results;
 
47
                        }
 
48
                        set {
 
49
                                context.Results = value;
 
50
                                OnSelectionChanged ();
 
51
                        }
 
52
                }
 
53
 
 
54
                public IObject[] FullSelection {
 
55
                        get {
 
56
                                return context.FullSelection;
 
57
                        }
 
58
                }
 
59
 
 
60
                public virtual IObject Selection {
 
61
                        get {
 
62
                                return context.Selection;
 
63
                        }
 
64
                }
 
65
 
 
66
                public int Cursor {
 
67
                        get {
 
68
                                return context.Cursor;
 
69
                        }
 
70
                        set {
 
71
                                IObject tmp = Selection;
 
72
                                int ctmp = context.Cursor;
 
73
                                context.Cursor = value;
 
74
                                if (tmp != Selection || context.Cursor != ctmp) {
 
75
                                        try {
 
76
                                                OnSelectionChanged ();
 
77
                                        } catch {}
 
78
                                }
 
79
                        }
 
80
                }
 
81
 
 
82
                public int[] SecondaryCursors { 
 
83
                        get { 
 
84
                                return context.SecondaryCursorsToIntArray (); 
 
85
                        }
 
86
                }
 
87
 
 
88
                public virtual bool TextMode {
 
89
                        get {
 
90
                                return textMode;
 
91
                        }
 
92
                        set { }
 
93
                }
 
94
 
 
95
                public bool DefaultFilter {
 
96
                        get {
 
97
                                return (SearchTypes == defaultFilter);
 
98
                        }
 
99
                }
 
100
 
 
101
                public abstract Type[] SearchTypes {
 
102
                        get;
 
103
                }
 
104
                
 
105
                public string Query {
 
106
                        get {
 
107
                                return context.Query;
 
108
                        }
 
109
                }
 
110
 
 
111
                protected SimpleSearchController ()
 
112
                {
 
113
                        context = new SimpleSearchContext ();
 
114
                        searchFilter = defaultFilter;
 
115
                }
 
116
                
 
117
                public void AddChar (char character)
 
118
                {
 
119
                        context.LastContext = (SimpleSearchContext) context.Clone ();
 
120
                        context.Query += character;
 
121
                        
 
122
                        OnQueryChanged ();
 
123
                        UpdateResults ();
 
124
                        
 
125
                }
 
126
                
 
127
                protected abstract void UpdateResults ();
 
128
                
 
129
                protected virtual List<IObject> InitialResults ()
 
130
                {
 
131
                        //We continue off our previous results if possible
 
132
                        if (context.LastContext != null && 
 
133
                            context.LastContext.Results.Length != 0) {
 
134
                                return new List<IObject> (Do.UniverseManager.
 
135
                                                          Search (context.Query, SearchTypes, 
 
136
                                                                  context.LastContext.Results));
 
137
                        } else if (context.ParentContext != null) {
 
138
                                //If we have a parent context we NEVER do a full search.  Just return
 
139
                                //the results as they are.
 
140
                                return new List<IObject> (context.Results);
 
141
                        } else { 
 
142
                                //else we do things the slow way
 
143
                                return new List<IObject> (Do.UniverseManager.
 
144
                                                          Search (context.Query, SearchTypes));
 
145
                        }
 
146
                }
 
147
                
 
148
                public virtual void DeleteChar ()
 
149
                {
 
150
                        if (context.LastContext == null) {
 
151
                                return;
 
152
                        }
 
153
                        
 
154
                        IObject tmp = context.Selection;
 
155
                        context = context.LastContext;
 
156
                        
 
157
                        
 
158
                        if (tmp != context.Selection)
 
159
                                SelectionChanged ();
 
160
                        OnQueryChanged ();
 
161
                }
 
162
 
 
163
                public virtual bool ToggleSecondaryCursor (int cursorLocation)
 
164
                {
 
165
                        if (Results.Length - 1 < cursorLocation) return false;
 
166
                        
 
167
                        List<IObject> secondary;
 
168
                        secondary = new List<IObject> (context.SecondaryCursors);
 
169
                        
 
170
                        IObject newObject = Results[cursorLocation];
 
171
                        
 
172
                        if (secondary.Contains (newObject))
 
173
                                secondary.Remove (newObject);
 
174
                        else if (newObject is IItem)
 
175
                                secondary.Add (newObject);
 
176
                        else
 
177
                                return false;
 
178
                        
 
179
                        context.SecondaryCursors = secondary.ToArray ();
 
180
                        
 
181
                        return true;
 
182
                }
 
183
                
 
184
                public bool ItemChildSearch ()
 
185
                {
 
186
                        if (context.Selection is IAction)
 
187
                                return false;
 
188
                        
 
189
                        IItem item = context.Selection as IItem;
 
190
                        List<IObject> children = new List<IObject> ();
 
191
 
 
192
                        foreach (DoItemSource source in PluginManager.GetItemSources ()) {
 
193
                                foreach (IObject child in source.ChildrenOfItem (item))
 
194
                                        children.Add (child);
 
195
                        }
 
196
                        
 
197
                        if (children.Count == 0)
 
198
                                return false;
 
199
                        
 
200
                        SimpleSearchContext newContext = new SimpleSearchContext ();
 
201
                        newContext.ParentContext = context;
 
202
                        context = newContext;
 
203
                        
 
204
                        context.Results = Do.UniverseManager.Search (Query, defaultFilter, children);
 
205
                        OnSelectionChanged ();
 
206
                        return true;
 
207
                }
 
208
                
 
209
                public bool ItemParentSearch ()
 
210
                {
 
211
                        if (context.ParentContext == null) return false;
 
212
                        
 
213
                        SimpleSearchContext parent = context.ParentContext;
 
214
                        context.Destroy (true);
 
215
                        context = parent;
 
216
                        OnSelectionChanged ();
 
217
                        return true;
 
218
                }
 
219
                
 
220
                public virtual void Reset ()
 
221
                {
 
222
                        searchFilter = defaultFilter;
 
223
                        context.Destroy ();
 
224
                        context = new SimpleSearchContext ();
 
225
                }
 
226
                
 
227
                protected void OnSelectionChanged ()
 
228
                {
 
229
                        SelectionChanged ();
 
230
                }
 
231
                
 
232
                protected void OnSearchStarted (bool upstream_search)
 
233
                {
 
234
                        SearchStarted (upstream_search);
 
235
                }       
 
236
                
 
237
                protected void OnSearchFinished (bool selection_changed)
 
238
                {
 
239
                        SearchFinished (selection_changed);
 
240
                }
 
241
                
 
242
                protected void OnQueryChanged ()
 
243
                {
 
244
                        if (QueryChanged != null)
 
245
                                QueryChanged ();
 
246
                }
 
247
                
 
248
                public event NullEventHandler SelectionChanged;
 
249
                public event NullEventHandler QueryChanged;
 
250
                public event SearchStartedEventHandler SearchStarted;
 
251
                public event SearchFinishedEventHandler SearchFinished;
 
252
        }
 
253
}