1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System.Activities.Presentation.Toolbox
8 using System.Collections.Generic;
10 using System.Globalization;
12 // This is helper class which tries to load and create tool items from flat text file.
13 // The content of a file must contain following, colon separated values
14 // 1.) fully qualified class name, which is flagged with ToolboxItemAttribute
15 // 2.) name of the assembly (with extension) where the tool class is located
16 // 3.) an optional bitmap file (in case when activity is not marked with toolboxitem attribute)
17 // 4.) an optional display name
19 // Positions 3 & 4 can be in mixed order. the loader checks for known graphic extension
21 // The category information is defined by string content placed within square brackets [ ].
22 // All items under category definition fall into that category unless new category is defined.
23 // If no category is defined, default one is created
25 // All lines starting with ';' or '#' char are treaded as comments and skipped
27 sealed class ToolboxItemLoader
29 static readonly string FormatExceptionText =
30 "Tool has to be defined either as 'name, assembly', or 'name, assembly, bitmap'";
31 static readonly string DefaultCategory = "default";
33 private ToolboxItemLoader()
37 public static ToolboxItemLoader GetInstance()
39 return new ToolboxItemLoader();
42 public void LoadToolboxItems(string fileName, ToolboxCategoryItems container, bool resetContainer)
44 if (string.IsNullOrEmpty(fileName))
46 throw FxTrace.Exception.AsError(new ArgumentNullException("fileName"));
49 if (null == container)
51 throw FxTrace.Exception.AsError(new ArgumentNullException("container"));
59 using (StreamReader reader = File.OpenText(fileName))
62 ToolboxCategory category = null;
63 while (null != (entry = reader.ReadLine()))
66 if (entry.Length > 1 && (entry[0] != ';' && entry[0] != '#'))
68 if (entry.StartsWith("[", StringComparison.CurrentCulture) && entry.EndsWith("]", StringComparison.CurrentCulture))
70 string categoryName = entry.Substring(1, entry.Length - 2);
71 category = GetCategoryItem(container, categoryName);
77 category = GetCategoryItem(container, DefaultCategory);
79 string[] toolDefinition = entry.Split(';');
81 string toolName = null;
82 string assembly = null;
83 string displayName = null;
86 if (GetToolAssemblyAndName(toolDefinition, ref toolName, ref assembly))
88 GetBitmap(toolDefinition, ref bitmap);
89 GetDisplayName(toolDefinition, ref displayName);
90 category.Add(new ToolboxItemWrapper(toolName, assembly, bitmap, displayName));
94 throw FxTrace.Exception.AsError(new ArgumentOutOfRangeException(FormatExceptionText));
102 bool GetToolAssemblyAndName(string[] toolDefinition, ref string toolName, ref string assembly)
104 if (toolDefinition.Length >= 2)
106 toolName = toolDefinition[0].Trim();
107 assembly = toolDefinition[1].Trim();
113 bool GetBitmap(string[] toolDefinition, ref string bitmap)
115 for (int i = 2; i < toolDefinition.Length; ++i)
117 string current = toolDefinition[i].Trim();
118 if (current.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) ||
119 current.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
120 current.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) ||
121 current.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
122 current.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase) ||
123 current.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) ||
124 current.EndsWith(".exig", StringComparison.OrdinalIgnoreCase))
133 bool GetDisplayName(string[] toolDefinition, ref string displayName)
135 for (int i = 2; i < toolDefinition.Length; ++i)
137 string current = toolDefinition[i].Trim();
138 if (!current.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase) &&
139 !current.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) &&
140 !current.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) &&
141 !current.EndsWith(".png", StringComparison.OrdinalIgnoreCase) &&
142 !current.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase) &&
143 !current.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) &&
144 !current.EndsWith(".exig", StringComparison.OrdinalIgnoreCase))
146 displayName = current;
153 ToolboxCategory GetCategoryItem(ToolboxCategoryItems container, string categoryName)
155 foreach (ToolboxCategory category in container)
157 if (0 == string.Compare(category.CategoryName, categoryName, true, CultureInfo.CurrentUICulture))
162 ToolboxCategory newCategory = new ToolboxCategory(categoryName);
163 container.Add(newCategory);