In this post:
1) General overview of the way the toolbox items work in DSL tools for VS2010
2) Toolbox related Changes from VS2008 to VS2010
3) Customization of static toolbox items - how to grouped model elements into single toolbox item
In VS 2010 we change the way we manage toolbox items from being set explicitly during package initialization to being partially set(*) before the package has been created and initialized. The toolbox items that are set this way called static toolbox items (vs. dynamic toolbox items in VS2008).
* By partially I mean only the text, tooltip and icon are set (there are few more but they are there for technical reasons) – no action or any DSL-specific information is associated with the toolbox item –practically those toolbox items are stubs until their full initialization.
Q: Why did moved to static toolbox items?
A: By using this approach we were able to speed up the overall VS loading time because no package will be created (and initialized) prematurely just because it has some toolbox items.
The full initialization will occur when the specific designer will show up and someone will hover the toolbox item and at that point the partial toolbox items will be replaced with ModelingToolboxItem that will be created according to the following sequence diagram:
Blue line – VS will use package.GetToolboxItemData(..) to try and get the toolbox item data for each of the toolbox items ids you’ve hovered to replace it with the stub toolbox item and cache it in VS toolbox cache.
Red Line – Toolbox helper will create an instance of ModelingToolboxItem according to the toolbox item id.
Q: How would I add custom static toolbox items that are not mapped to a shape or logically group several model elements into single toolbox item?
A: The recommended approach is to add class attribute on partial package class (in DslPackage) and extending the ToolboxHelper class (in Dsl) and supporting the new toolbox item id.
Example for language name “MyLanguage”:
Under DslProject project:
1: // See the generated Package.cs for examples
2: [ProvideStaticToolboxItem(…, “MyNamesSpace.MyNewToolboxItem”,….)]
3: partial class MyLangaugePackage : MylLanguagePackageBase
4: {
5: }
More info: ProvideStaticToolboxItemAttribute
Under Dsl project:
1: partial class MyLanguageToolboxHelper : MyLanguagelToolboxHelperBase
2: {
3: public override ModelingToolboxItem GetToolboxItem(string itemId, Store store)
4: {
5: if (store == null)
6: {
7: return null;
8: }
9: if (itemId != “MyNamesSpace.MyNewToolboxItem”)
10: {
11: return base.GetToolboxItem(itemId, store);
12: }
13:
14:
15: // return the custom toolbox item instance
16: // see the generated ToolboxHelper.cs code for examples
17: return
18: new ModelingToolboxItem(“MyNamesSpace.MyNewToolboxItem”,
19: 1,
20: …,
21: …,
22: …,
23: …,
24: …,
25: …,
26: CreateMyCustomPrototype(store),
27: …
28: });
29: }
30:
31: private static ElementGroupPrototype CreateMyCustomPrototype (Store store)
32: {
33: ElementGroup elementGroup = new ElementGroup(store.DefaultPartition);
34:
35: // add all the model elements that create a logical group
36: // make sure to set the properties that will be used as defaults
37: elementGroup.AddGraph(/*Add model element 1 instance*/, true);
38: elementGroup.AddGraph(/*Add model element 2 instance */, true);
39: elementGroup.AddGraph(/*Add model element 3 instance */, true);
40: return elementGroup.CreatePrototype();
41: }
42: }
More info: ModelingToolboxItem
Published Tuesday, January 19, 2010 3:26 PM by Eyal
תגים:C#, SDK, DSL, Best Practices, DEV, Visual Studio 2010, .NET 4.0, Visual Studio 2008, VS2008, VS10, VS2010
Comments
# re: Toolbox items in DSL tools@ Wednesday, July 04, 2012 8:26 PM
-1'
by 1
# re: Toolbox items in DSL tools@ Tuesday, December 11, 2012 5:29 AM
That takes us up to the next level. Great psoting.
by Keys
沒有留言:
張貼留言