~do-core/do/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// CommandManager.cs created with MonoDevelop
// User: dave at 9:58 AMĀ 8/19/2007
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using System.Collections;
using System.Collections.Generic;

namespace Do.Core
{

	public class CommandManager : GCObjectManager
	{
			
		private Hashtable commandLists;
		
		public CommandManager()
		{
			commandLists = new Hashtable ();
		}
		
		public void AddCommand (Command command)
		{
			foreach (Type type in command.SupportedTypes) {
				List<Command> commands;
				if (!commandLists.ContainsKey (type)) {
					commandLists[type] = new List<Command> ();
				}
				commands = commandLists[type] as List<Command>;
				if (!commands.Contains (command)) {
					commands.Add (command);
				}
			}
		}
		
		public Command[] CommandsForItem (Item item, string match) {
			SearchContext context;
			
			context = new SearchContext ();
			context.Item = item;
			context.CommandSearchString = match;
			Search (context);
			return context.Results as Command[];
		}
		
		private Command[] _CommandsForItem (Item item) {
			List<Command> commands;
			
			commands = new List<Command> ();
			Type[] interfaces = item.IItem.GetType ().GetInterfaces ();
			foreach (Type type in interfaces) {
				if (commandLists.ContainsKey (type)) {
					foreach (Command command in commandLists[type] as IEnumerable<Command>) {
						if (command.SupportsItem (item.IItem)) {
							commands.Add (command);
						}
					}
				}
			}
			return commands.ToArray ();
		}
		
		protected override ContextRelation GetContextRelation (SearchContext a, SearchContext b)
		{
			
			if (a.Item == b.Item && a.CommandSearchString == b.CommandSearchString)
				return ContextRelation.Repeat;
			else if (a.Item == b.Item && a.CommandSearchString.StartsWith (b.CommandSearchString))
				return ContextRelation.Continuation;
			else
				return ContextRelation.Fresh;
		}
		
		protected override void PerformSearch (SearchContext context)
		{
			int numScoreNonZero;
			Command[] commands;
			
			// Use intermediate search results if available.
			if (context.Results == null) {
				commands = _CommandsForItem (context.Item);
			} else {
				commands = context.Results as Command[];
			}
			
			// Score the commands based on the search string and sort them.
			foreach (Command command in commands) {
				command.Score = command.ScoreForAbbreviation (context.CommandSearchString);
			}
			Array.Sort<GCObject> (commands, new GCObjectScoreComparer ());
			
			// Chop the array where the scores become zero
			for (numScoreNonZero = 0; numScoreNonZero < commands.Length; ++numScoreNonZero) {
				if (commands[numScoreNonZero].Score == 0) break;
			}
			Array.Resize<Command> (ref commands, numScoreNonZero);
			
			context.Results = commands;
		}


	}
}