~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do/src/Do.Core/FirstSearchController.cs

  • Committer: Hardeep S
  • Date: 2009-06-23 05:57:47 UTC
  • Revision ID: ootz0rz@gmail.com-20090623055747-3srobsuq3q8wbn81
initial adding of Do core stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// FirstSearchController.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.Linq;
 
22
using System.Collections.Generic;
 
23
 
 
24
using Do.UI;
 
25
using Do.Universe;
 
26
using Do.Platform;
 
27
 
 
28
namespace Do.Core
 
29
{
 
30
        
 
31
        public class FirstSearchController : SimpleSearchController
 
32
        {
 
33
                public FirstSearchController() : base ()
 
34
                {
 
35
                }
 
36
                
 
37
                public override bool ToggleSecondaryCursor (int cursorLocation)
 
38
                {
 
39
                        bool update = (SecondaryCursors.Length == 0);
 
40
                        bool result = base.ToggleSecondaryCursor (cursorLocation);
 
41
                        
 
42
                        if (update && result)
 
43
                                UpdateResults ();
 
44
                        return result;
 
45
                }
 
46
                
 
47
                public override void Reset ()
 
48
                {
 
49
                        base.Reset ();
 
50
                        textMode = false;
 
51
                }
 
52
 
 
53
                public override IEnumerable<Type> SearchTypes {
 
54
                        get {
 
55
                                if (TextMode) {
 
56
                                        yield return typeof (ITextItem);
 
57
                                } else if (context.SecondaryCursors.Any ()) {
 
58
                                        // This is pretty bad.
 
59
                                        yield return Results [SecondaryCursors [0]].GetType ();
 
60
                                } else {
 
61
                                        foreach (Type t in defaultFilter) yield return t;
 
62
                                }
 
63
                        }
 
64
                }
 
65
 
 
66
                public override bool TextMode {
 
67
                        get { 
 
68
                                return textMode || ImplicitTextMode; 
 
69
                        }
 
70
                        set { 
 
71
                                if (context.ParentContext != null) return;
 
72
                                textMode = value; 
 
73
                                textModeFinalize = false;
 
74
                                if (Query.Length > 0)
 
75
                                        BuildNewContextFromQuery ();
 
76
                        }
 
77
                }
 
78
                
 
79
                protected override void UpdateResults ()
 
80
                {
 
81
                        context.Results = InitialResults ();
 
82
                        bool searchChanged = context.LastContext == null || context.LastContext.Selection != context.Selection;
 
83
                        base.OnSearchFinished (searchChanged, true, Selection, Query);
 
84
                }
 
85
                
 
86
                public override void SetString (string str)
 
87
                {
 
88
                        context.Query = str;
 
89
                        BuildNewContextFromQuery ();
 
90
                }
 
91
 
 
92
                private void BuildNewContextFromQuery ()
 
93
                {
 
94
                        string query = Query;
 
95
                        
 
96
                        context.Destroy ();
 
97
                        context = new SimpleSearchContext ();
 
98
                        
 
99
                        foreach (char c in query.ToCharArray ()) {
 
100
                                context.LastContext = context.Clone () as SimpleSearchContext;
 
101
                                context.Query += c;
 
102
                                context.Results = InitialResults ();
 
103
                        }
 
104
                        base.OnSearchFinished (true, true, Selection, Query);
 
105
                }
 
106
        }
 
107
}