<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>mike hatch's blog : Domain Specific Languages</title><link>http://blogs.technet.com/mhatch/archive/tags/Domain+Specific+Languages/default.aspx</link><description>Tags: Domain Specific Languages</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Creating a custom DSL Connector</title><link>http://blogs.technet.com/mhatch/archive/2006/11/22/creating-a-custom-dsl-connector.aspx</link><pubDate>Thu, 23 Nov 2006 10:36:00 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:526528</guid><dc:creator>mhatch</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.technet.com/mhatch/comments/526528.aspx</comments><wfw:commentRss>http://blogs.technet.com/mhatch/commentrss.aspx?PostID=526528</wfw:commentRss><wfw:comment>http://blogs.technet.com/mhatch/rsscomments.aspx?PostID=526528</wfw:comment><description>&lt;P&gt;While developing my architecture designer using the Domain Specfic Language tools in Visual Studio I wanted to create my own connector type with filled circles at the ends of the connector.&amp;nbsp; Many thanks to Bhavin B for his &lt;A class="" href="https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=585331&amp;amp;SiteID=1" mce_href="https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=585331&amp;amp;SiteID=1"&gt;post &lt;/A&gt;on the Microsoft Forums for getting me started.&amp;nbsp; I'm going to recap the steps here for clarity since some of his instructions were not completely clear.&lt;/P&gt;
&lt;P&gt;Connectors in the DSL tools come with default decorators that inclue arrows and diamonds, but not circles.&amp;nbsp; Decorators at the ends of the connector lines are drawn using a 2D GraphicsPath so that the drawing scales well in the designer.&amp;nbsp; The process for customizing the decorators involves creating the custom decorator class, and extending the Connector.InitializeInstanceResources to load the custom decorators in the call to SetDecorators.&amp;nbsp;&amp;nbsp;The two first parameters for SetDecorators controls whether the custom decorator appears at one or both ends of the connector.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Modify the ExampleConnector 'shape' (in the right-hand swim lane) to allow modifications by setting 'Generates Double Derived' = True.&lt;/LI&gt;
&lt;LI&gt;Transform templates so the partial classes are created.&lt;/LI&gt;
&lt;LI&gt;Add a new partial class to your project extending the Connector.InitializeInstanceResources to use your custom decorator as arguments (first arg is one end, second is the other end of the connector).&lt;/LI&gt;&lt;/OL&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;CODE&gt;&lt;FONT color=#0000ff size=2&gt;using&lt;/FONT&gt;&lt;FONT size=2&gt; DslModeling = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;global&lt;/FONT&gt;&lt;FONT size=2&gt;::Microsoft.VisualStudio.Modeling; &lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff size=2&gt;using&lt;/FONT&gt;&lt;FONT size=2&gt; DslDesign = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;global&lt;/FONT&gt;&lt;FONT size=2&gt;::Microsoft.VisualStudio.Modeling.Design; &lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff size=2&gt;using&lt;/FONT&gt;&lt;FONT size=2&gt; DslDiagrams = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;global&lt;/FONT&gt;&lt;FONT size=2&gt;::Microsoft.VisualStudio.Modeling.Diagrams; &lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff size=2&gt;using&lt;/FONT&gt;&lt;FONT size=2&gt; System.Drawing.Drawing2D; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff size=2&gt;namespace&lt;/FONT&gt;&lt;FONT size=2&gt; Company.Language1 &lt;BR&gt;{&lt;BR&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;public partial class&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;ExampleConnector&lt;/FONT&gt;&lt;FONT size=2&gt; : &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;ExampleConnectorBase &lt;/FONT&gt;&lt;FONT size=2&gt;&lt;BR&gt;{&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;protected&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;override&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;FONT size=2&gt; InitializeInstanceResources()&lt;/FONT&gt; &lt;BR&gt;{&lt;BR&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color=#0000ff size=2&gt;base&lt;/FONT&gt;&lt;FONT size=2&gt;.InitializeInstanceResources();&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;this&lt;/FONT&gt;&lt;FONT size=2&gt;.SetDecorators(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;&amp;nbsp;DecoratorFilledCircle&lt;/FONT&gt;&lt;FONT size=2&gt;(), &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;DecoratorFilledCircle&lt;/FONT&gt;&lt;FONT size=2&gt;(), &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;false&lt;/FONT&gt;&lt;FONT size=2&gt;);&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/CODE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;4.&amp;nbsp; Create another new class, this time&amp;nbsp;for the custom decorator.&amp;nbsp; This one doesn't have to be a partial class. Use the same 'using' statements as above. Subclass from LinkDecorator, and override the GetPath method to draw the custom decorator shape you desire.&amp;nbsp; Here I've used path.AddEllipse to draw a circle.&amp;nbsp; I also set the FillDecorator property to true so it will draw a filled circle rather than an open one.&amp;nbsp; I manually moved the ellipse x y parameters since it did not appear centered on the end of the connector.&amp;nbsp; There is a property for the line width of the connector that is set in the VS property inspector on the DslDefinition.Connector class called 'Thickness', but I'm not sure how to access it without modifying the parameters passed to GetPath.&lt;/P&gt;&lt;CODE&gt;&lt;FONT color=#0000ff size=2&gt;namespace&lt;/FONT&gt;&lt;FONT size=2&gt; Company.Language1 &lt;BR&gt;{&lt;BR&gt;
&lt;BLOCKQUOTE&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;public&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;class&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;DecoratorFilledCircle&lt;/FONT&gt;&lt;FONT size=2&gt; : DslDiagrams.&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;LinkDecorator &lt;/FONT&gt;&lt;FONT size=2&gt;&lt;BR&gt;{&lt;BR&gt;
&lt;BLOCKQUOTE&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;protected&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;override&lt;/FONT&gt;&lt;FONT size=2&gt; System.Drawing.Drawing2D.&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;GraphicsPath&lt;/FONT&gt;&lt;FONT size=2&gt; GetPath(Microsoft.VisualStudio.Modeling.Diagrams.&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;RectangleD&lt;/FONT&gt;&lt;FONT size=2&gt; bounds) &lt;BR&gt;{&lt;BR&gt;
&lt;BLOCKQUOTE&gt;&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;GraphicsPath&lt;/FONT&gt;&lt;FONT size=2&gt; path = DecoratorPath; &lt;/FONT&gt;
&lt;P mce_keep="true"&gt;&lt;FONT color=#008000 size=2&gt;//Fill the circle&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;this&lt;/FONT&gt;&lt;FONT size=2&gt;.FillDecorator = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;true&lt;/FONT&gt;&lt;FONT size=2&gt;;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;//Draw an ellipse, moving the top left to compensate for line width&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;//TODO: Figure out how to retrieve the line width programmatically. DslDefinition.Connector.Thickness&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;
&lt;P mce_keep="true"&gt;path.AddEllipse((&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;float&lt;/FONT&gt;&lt;FONT size=2&gt;)(bounds.X + .01), (&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;float&lt;/FONT&gt;&lt;FONT size=2&gt;)(bounds.Y + .01), (&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;float&lt;/FONT&gt;&lt;FONT size=2&gt;)(bounds.Width * .8), (&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;float&lt;/FONT&gt;&lt;FONT size=2&gt;)(bounds.Height * .8));&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; path;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;}&lt;BR&gt;&lt;/BLOCKQUOTE&gt;}&lt;BR&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;}&lt;BR&gt;&lt;/P&gt;&lt;/CODE&gt;&lt;/BLOCKQUOTE&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;5. That about does it.&amp;nbsp; Make sure that your custom connector is mapped to the relationship in your DslDefinition.dsl and you should have custom decorators on your connector&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here is a sample of my custom connector.&amp;nbsp; Picture is kinda small, but you get the idea. You can see the filled circles on each end of the connector.&amp;nbsp; They are much more prevalent in the designer.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;A href="http://blogs.technet.com/photos/mhatch/picture526529.aspx" target=_blank&gt;&lt;IMG src="http://blogs.technet.com/photos/mhatch/images/526529/thumb.aspx" border=0&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=526528" width="1" height="1"&gt;</description><category domain="http://blogs.technet.com/mhatch/archive/tags/Domain+Specific+Languages/default.aspx">Domain Specific Languages</category><category domain="http://blogs.technet.com/mhatch/archive/tags/DSL/default.aspx">DSL</category></item></channel></rss>