~do-core/do/trunk

« back to all changes in this revision

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

  • Committer: djsiegel at gmail
  • Date: 2007-09-14 17:56:28 UTC
  • Revision ID: djsiegel@gmail.com-20070914175628-687padig4f0feh5f
adding Monodevelop-managed branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// CommandManager.cs created with MonoDevelop
 
2
// User: dave at 9:58 AM 8/19/2007
 
3
//
 
4
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
 
5
//
 
6
 
 
7
using System;
 
8
using System.Collections;
 
9
using System.Collections.Generic;
 
10
 
 
11
namespace Do.Core
 
12
{
 
13
 
 
14
        public class CommandManager : GCObjectManager
 
15
        {
 
16
                        
 
17
                private Hashtable commandLists;
 
18
                
 
19
                public CommandManager()
 
20
                {
 
21
                        commandLists = new Hashtable ();
 
22
                }
 
23
                
 
24
                public void AddCommand (Command command)
 
25
                {
 
26
                        foreach (Type type in command.SupportedTypes) {
 
27
                                List<Command> commands;
 
28
                                if (!commandLists.ContainsKey (type)) {
 
29
                                        commandLists[type] = new List<Command> ();
 
30
                                }
 
31
                                commands = commandLists[type] as List<Command>;
 
32
                                if (!commands.Contains (command)) {
 
33
                                        commands.Add (command);
 
34
                                }
 
35
                        }
 
36
                }
 
37
                
 
38
                public Command[] CommandsForItem (Item item, string match) {
 
39
                        SearchContext context;
 
40
                        
 
41
                        context = new SearchContext ();
 
42
                        context.Item = item;
 
43
                        context.CommandSearchString = match;
 
44
                        Search (context);
 
45
                        return context.Results as Command[];
 
46
                }
 
47
                
 
48
                private Command[] _CommandsForItem (Item item) {
 
49
                        List<Command> commands;
 
50
                        
 
51
                        commands = new List<Command> ();
 
52
                        Type[] interfaces = item.IItem.GetType ().GetInterfaces ();
 
53
                        foreach (Type type in interfaces) {
 
54
                                if (commandLists.ContainsKey (type)) {
 
55
                                        foreach (Command command in commandLists[type] as IEnumerable<Command>) {
 
56
                                                if (command.SupportsItem (item.IItem)) {
 
57
                                                        commands.Add (command);
 
58
                                                }
 
59
                                        }
 
60
                                }
 
61
                        }
 
62
                        return commands.ToArray ();
 
63
                }
 
64
                
 
65
                protected override ContextRelation GetContextRelation (SearchContext a, SearchContext b)
 
66
                {
 
67
                        
 
68
                        if (a.Item == b.Item && a.CommandSearchString == b.CommandSearchString)
 
69
                                return ContextRelation.Repeat;
 
70
                        else if (a.Item == b.Item && a.CommandSearchString.StartsWith (b.CommandSearchString))
 
71
                                return ContextRelation.Continuation;
 
72
                        else
 
73
                                return ContextRelation.Fresh;
 
74
                }
 
75
                
 
76
                protected override void PerformSearch (SearchContext context)
 
77
                {
 
78
                        int numScoreNonZero;
 
79
                        Command[] commands;
 
80
                        
 
81
                        // Use intermediate search results if available.
 
82
                        if (context.Results == null) {
 
83
                                commands = _CommandsForItem (context.Item);
 
84
                        } else {
 
85
                                commands = context.Results as Command[];
 
86
                        }
 
87
                        
 
88
                        // Score the commands based on the search string and sort them.
 
89
                        foreach (Command command in commands) {
 
90
                                command.Score = command.ScoreForAbbreviation (context.CommandSearchString);
 
91
                        }
 
92
                        Array.Sort<GCObject> (commands, new GCObjectScoreComparer ());
 
93
                        
 
94
                        // Chop the array where the scores become zero
 
95
                        for (numScoreNonZero = 0; numScoreNonZero < commands.Length; ++numScoreNonZero) {
 
96
                                if (commands[numScoreNonZero].Score == 0) break;
 
97
                        }
 
98
                        Array.Resize<Command> (ref commands, numScoreNonZero);
 
99
                        
 
100
                        context.Results = commands;
 
101
                }
 
102
 
 
103
 
 
104
        }
 
105
}