1
// CommandManager.cs created with MonoDevelop
2
// User: dave at 9:58 AM 8/19/2007
4
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
8
using System.Collections;
9
using System.Collections.Generic;
14
public class CommandManager : GCObjectManager
17
private Hashtable commandLists;
19
public CommandManager()
21
commandLists = new Hashtable ();
24
public void AddCommand (Command command)
26
foreach (Type type in command.SupportedTypes) {
27
List<Command> commands;
28
if (!commandLists.ContainsKey (type)) {
29
commandLists[type] = new List<Command> ();
31
commands = commandLists[type] as List<Command>;
32
if (!commands.Contains (command)) {
33
commands.Add (command);
38
public Command[] CommandsForItem (Item item, string match) {
39
SearchContext context;
41
context = new SearchContext ();
43
context.CommandSearchString = match;
45
return context.Results as Command[];
48
private Command[] _CommandsForItem (Item item) {
49
List<Command> commands;
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);
62
return commands.ToArray ();
65
protected override ContextRelation GetContextRelation (SearchContext a, SearchContext b)
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;
73
return ContextRelation.Fresh;
76
protected override void PerformSearch (SearchContext context)
81
// Use intermediate search results if available.
82
if (context.Results == null) {
83
commands = _CommandsForItem (context.Item);
85
commands = context.Results as Command[];
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);
92
Array.Sort<GCObject> (commands, new GCObjectScoreComparer ());
94
// Chop the array where the scores become zero
95
for (numScoreNonZero = 0; numScoreNonZero < commands.Length; ++numScoreNonZero) {
96
if (commands[numScoreNonZero].Score == 0) break;
98
Array.Resize<Command> (ref commands, numScoreNonZero);
100
context.Results = commands;