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

« back to all changes in this revision

Viewing changes to Do/src/Do.Core/SearchControllers/FirstSearchController.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
// 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.Collections.Generic;
 
22
 
 
23
using Do.Addins;
 
24
using Do.Universe;
 
25
using Do.UI;
 
26
 
 
27
namespace Do.Core
 
28
{
 
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
                        }
 
45
                        
 
46
                        return result;
 
47
                }
 
48
                
 
49
                public override void Reset ()
 
50
                {
 
51
                        base.Reset ();
 
52
                        textMode = false;
 
53
                }
 
54
 
 
55
                public override Type[] SearchTypes {
 
56
                        get {
 
57
                                if (TextMode) {
 
58
                                        return new Type[] {typeof (ITextItem)};
 
59
                                } else if (context.SecondaryCursors.Length > 0) {
 
60
                                        return new Type[] {(Results[SecondaryCursors[0]] as DoObject).Inner.GetType ()};
 
61
                                } else {
 
62
                                        return defaultFilter;
 
63
                                }
 
64
                        }
 
65
                }
 
66
 
 
67
                public override bool TextMode {
 
68
                        get { 
 
69
                                bool implicit_text_mode = false;
 
70
                                implicit_text_mode = Results.Length == 1 && Results[0] is ITextItem;
 
71
                                return (textMode || implicit_text_mode); 
 
72
                        }
 
73
                        set { 
 
74
                                if (context.ParentContext != null) return;
 
75
                                textMode = value; 
 
76
                                if (Query.Length > 0)
 
77
                                        BuildNewContextFromQuery ();
 
78
                        }
 
79
                }
 
80
                
 
81
                protected override void UpdateResults ()
 
82
                {
 
83
                        List<IObject> results;
 
84
                        if (!TextMode)
 
85
                                results = InitialResults ();
 
86
                        else
 
87
                                results = new List<IObject> ();
 
88
                                
 
89
                        
 
90
                        if (context.ParentContext == null) {
 
91
                                if (DefaultFilter) {
 
92
                                        results.Add (new DoTextItem (Query));
 
93
                                } else {
 
94
                                        foreach (Type t in SearchTypes) {
 
95
                                                if (t == typeof (IItem) || t == typeof (ITextItem)) {
 
96
                                                        results.Add (new DoTextItem (Query));
 
97
                                                }
 
98
                                        }
 
99
                                }
 
100
                        }
 
101
                        
 
102
                        context.Results = results.ToArray ();
 
103
                        //Do.PrintPerf ("FirstControllerResultsAssigned");
 
104
                        
 
105
                        if (context.LastContext == null || context.LastContext.Selection != context.Selection) {
 
106
                                base.OnSelectionChanged ();
 
107
                                base.OnSearchFinished (true);
 
108
                        } else {
 
109
                                base.OnSearchFinished (false);
 
110
                        }
 
111
                }
 
112
                
 
113
                private void BuildNewContextFromQuery ()
 
114
                {
 
115
                        string query = Query;
 
116
                        
 
117
                        context = new SimpleSearchContext ();
 
118
                        List<IObject> results;
 
119
                        foreach (char c in query.ToCharArray ()) {
 
120
                                context.LastContext = context.Clone () as SimpleSearchContext;
 
121
                                context.Query += c;
 
122
                                
 
123
                                results = InitialResults ();
 
124
                                
 
125
                                results.Add (new DoTextItem (Query));
 
126
                                context.Results = results.ToArray ();
 
127
                        }
 
128
                        base.OnSelectionChanged ();
 
129
                }
 
130
 
 
131
        }
 
132
}