Friday, December 28, 2007

Adding a Menu to a DSL (vs.net 2008 SDK + VSCT file)

It's just strange that something so simple is not integrated as part of the basic features of the DSL tools themselves. Furthermore, the only DSL book I have it shows old code (refers to the old ctc file), the document in the vs.net 2008 SDK shows the same problem, etc.

The "closest" example I've found is this one: El Bruno (Spanish) but unfortunately, this is also based on CTC file and not using the new VSCT example. I must admit that with the new VSCT file it's easier than the previously CTC file.

All the BLOG's I found showed vs.net 2005 examples all using the old ctc file.

I first tried to use Clarius SFT (Software Factories Toolkit) which probably would have been a better options but I could not install it ( VS.NET 2008 RTM , can't install old GAX extensions…so SFT would also fail to install). Victor Garcia Aprea had exactly the same problem as looks like I share his point of view about how obscure adding a menu could be.

This msdn page: VSCT Samples in C# showed me the missing link: How to define the command Guid's and ID's.

Step#1: Modify Commands.vsct

  • Project = DSLPackage
  • Open Commands.vsct
  • Add Symbols ( Guid + ID)
    • Please remmember that the commands ID and GUID need to be unique not only in your project but in your whole vs.net environment. I’ve created by accident 2 projects which shared the same ID and I was getting the wrong text.
  • Add Commands
Example modified VSCT File

   18 <Commands package="guidPkg">
   19 
   20     <Buttons>
   21       <Button guid="cmdImportDbSchemaGUID" id="cmdImportDbSchemaID" priority="0x0902"  type="Button">
   22         <Parent guid="guidCmdSet" id="grpidContextMain"/>
   23         <Strings>
   24           <CanonicalName>cmdImportDbSchemaCanonicalName>
   25           <ButtonText>Import from Database SchemaButtonText>
   26           <ToolTipText>Use this option to create the domain from an existing Database SchemaToolTipText>
   27         Strings>
   28       Button>
   29 
   30       <Button guid="cmdImportClassesGUID" id="cmdImportClassesID" priority="0x0902"  type="Button">
   31         <Parent guid="guidCmdSet" id="grpidContextMain"/>
   32         <Strings>
   33           <CanonicalName>cmdImportClassesCanonicalName>
   34           <ButtonText>Import from Existing ClassesButtonText>
   35         Strings>
   36       Button>
   37     Buttons>
   38   Commands>
   39 
   40   <Symbols>
   41     <GuidSymbol name="cmdImportDbSchemaGUID" value="{EC120F9A-9E7F-469d-8D61-F4E2A97E5725}">
   42       <IDSymbol name="cmdImportDbSchemaID" value="0x810">IDSymbol>
   43     GuidSymbol>
   44 
   45     <GuidSymbol name="cmdImportClassesGUID" value="{EC120F9A-9E7F-469d-8D61-F4E2A97E5726}">
   46       <IDSymbol name="cmdImportClassesID" value="0x811">IDSymbol>
   47     GuidSymbol>
   48   Symbols>
   49 CommandTable>

Step#2: Modify Package version
  • Locate the Package.tt , file , edit
  • Increment the version number (2nd parameter) as shown below

Step#3: Search for "your" CommandSet class
  • Search in your DSL Solution (DslPackage project) for the text “DslShell::CommandSet”
  • You should notice that’s found 1 time in the CommandSet.cs
  • If you now find the references for that found class (in this case DomainDesignCommandSetBase ) but in your project will be named differently. You should find that it’s derived + made partial to allow customization
Step#4: Customize your specific CommandSet
  • (optional) Create a folder to hold your own customized classes
  • Create a partial class (as shown below)
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.ComponentModel.Design;
    6 using Microsoft.VisualStudio.Modeling.Shell;
    7 
    8 namespace GenWise.VsNet.DomainDesign.DslPackage
    9 {
   10     internal partial class DomainDesignCommandSet : DomainDesignCommandSetBase
   11     {
   12         // Note: Here you need to repeat the code from the VSCT .
   13         //       I could not find an easy way of re-using the Symbols from the vsct
   14         public Guid dbImportSchemaGuid = new Guid("EC120F9A-9E7F-469d-8D61-F4E2A97E5725");
   15         public const int dbImportSchemaID = 0x810;
   16 
   17         public Guid dbImportClassesGuid = new Guid("EC120F9A-9E7F-469d-8D61-F4E2A97E5726");
   18         public const int dbImportClassesID = 0x811;
   19 
   20 
   21         protected override IList GetMenuCommands()
   22         {
   23             IList commands = base.GetMenuCommands();
   24 
   25             // You need to create a new command and add it to
   26             // the commands collection.
   27             DynamicStatusMenuCommand cmdImportDbSchema =
   28                     new DynamicStatusMenuCommand(
   29                          new EventHandler(OnPopUpMenuDisplayAction),
   30                          new EventHandler(OnPopUpMenuClick),
   31                          new CommandID(dbImportSchemaGuid, dbImportSchemaID));
   32             commands.Add(cmdImportDbSchema);
   33 
   34             // You need to create a new command and add it to
   35             // the commands collection.
   36             DynamicStatusMenuCommand cmdImportClasses =
   37                     new DynamicStatusMenuCommand(
   38                          new EventHandler(OnPopUpMenuDisplayAction),
   39                          new EventHandler(OnPopUpMenuClick),
   40                          new CommandID(dbImportClassesGuid, dbImportClassesID));
   41 
   42             commands.Add(cmdImportClasses);
   43 
   44             return commands;
   45         }
   46 
   47

   56         internal void OnPopUpMenuDisplayAction(object sender, EventArgs e)
   57         {
   58             MenuCommand command = sender as MenuCommand;
   59 
   60             foreach (object selectedObject in this.CurrentSelection)
   61             {
   62                 if (selectedObject is ClassDiagram)
   63                 {
   64                     // The popmenu command is always visible
   65                     command.Visible = true;
   66                     command.Enabled = true;
   67                     return;
   68                 }
   69             }
   70 
   71             // The popmenu command is always visible
   72             command.Visible = false;
   73             command.Enabled = false;
   74         }
   75 
   76         internal void OnPopUpMenuClick(object sender, EventArgs e)
   77         {
   78             MenuCommand command = sender as MenuCommand;
   79 
   80             StringBuilder sb = new StringBuilder();
   81             foreach (object selectedObject in this.CurrentSelection)
   82             {
   83                 sb.AppendLine("Selected Shape: " + selectedObject.ToString());
   84 
   85                 // the Current selection will hold your "shape" class, so if you are interested
   86                 // in getting the Model Class you need to use the ModelElement Property as shown below.
   87                 if (selectedObject is ClassShape)
   88                 {
   89                     ModelClass modelClass = (ModelClass)(selectedObject as ClassShape).ModelElement;
   90                     sb.AppendLine("*** Related Domain Class: " + modelClass.ToString());
   91                 }
   92 
   93                 if (selectedObject is ClassDiagram)
   94                 {
   95                 }
   96             }
   97 
   98             System.Windows.Forms.MessageBox.Show(sb.ToString());
   99         }
  100 
  101     }
  102 
  103 
  104 }

Step #5 : Transform Templates + Test It

  • Ok, finally, you can now transform templates + run + test it.
  • You should see the following in your diagram:



How to access the Related Domain Object (given a shape)?

  // the Current selection will hold your "shape" class, so if you are interested
  // in getting the Model Class you need to use the ModelElement Property as shown below.
   87                 if (selectedObject is ClassShape)
   88                 {
   89                     ModelClass modelClass = (ModelClass)(selectedObject as ClassShape).ModelElement;
   90                     sb.AppendLine("*** Related Domain Class: " + modelClass.ToString());
   91                 }


How to control visibility of the Command?
Look at lines 56 - 74 from the previous code, it clearly shows how you can based on a condition, enable or disable the command.

16 comments:

Anonymous said...

HI, do you know how to add an icon to that menu?

Thanks and nice post!
Hernan.

Anonymous said...

Found it here: http://blogs.msdn.com/aaronmar/archive/2007/04/02/ctc-is-dead-long-live-vsct-part-1.aspx

Thanks,
Hernan

Anonymous said...

I published a DSL Tools based designer on codeplex (http:www.codeplex.com/vsctdesigner) which could help you ?
Alain

dghnfgj said...

Today is the gold für wow second day of 2009 ,world of warcraft gold it also a mesos special for me .cheap wow gold Because i have cheap maplestory mesos a chance to go to an english speech of LiYang and crazy to learn english follow him . He is a firendly,kindly person who make me feel good.wow gold kaufen What‘s more ,maple story mesos he very confidence .And he make me sure what he can do i also can do,as long as i make a determination and force myself to do it every second,Crazy just like him .wow geld I learn one setence from his book ever :maple story items If you want to succeed always force yourself to do more .I can't agree more with him .wow gold farmen To be honest ,when i was a littel girl i already fall in love with english.But what a pity i am poor in english ,Maple Story Account and it make me feel frustrating .So i want to give up many times ,but i can't as i still love it .So i tell mysefl :if you think you can you can wow leveling,and all your maple story power leveling hard work will pay off.wow power leveling Today i am very happy i can listien this wonderful speech of LiYang. I reap a great benifits from him .maple story money As he say :i am the best ,and every one can do it . world of warcraft power leveling Yes,i belive i can do it if i crazy as he buy archlord gold.Now i want to say :i will never give up.

Anonymous said...

After separate for one year, I can not leave you, leave Entropiauniverse ped . Now I buy Entropia Universe Gold again, I do not want to leave you, but at that time, I have no idea. Buy Entropia Universe Money
is the thing I want to do for long time. I know that cheap Entropiauniverse ped is your life. So I will try my best and do not let you pass away. In my mind, I think I Buy Entropia Universe Gold is the fate.

Without hesitate, I bought eve isk , in the game I can find myself. I feel lonely, but I do not want to talk with anyone, so I buy isk . At present, think the happy day I spend in knight, I am eager to enter it, and buy eve online isk . Own eve online isk , it means that you own the life of happiness. So I will not leave cheap eve isk . It is the origin of the happiness.

Anonymous said...

Do you want to know the magic of online games, and here you can get more angels gold. Do you want to have a try? Come on and angels online gold can make you happy. You can change a lot buy angels gold for play games. And you can use the cheap angels online gold do what you want to do in the online game.
Do you want to know the magic of online games, and here you can get more Rose zuly. Do you want to have a try? Come on and rose zulie can make you happy.You can change a lot rose online zuly for play games. Playing online games can make much rose online zulie. And you can Arua ROSE zuly do what you want to do in the online game.

Anonymous said...

I want give they happy time, twelve sky Gold, I work here with 12sky gold. Though, I can not make much money, twelvesky Gold, But I want to Exercise myself, 12Sky Silver Coins, I get up early 12 sky goldI always believe the angel is being and 12sky2 Gold . In fact, we all like listening to the songs of milk tea twelve sky2 Gold .
Generally speaking, I think is her voice very comfortable and mood of the story twelvesky2 Gold , We feel life bit by bit, looking forward the love belonging of the life feelings buy 12sky2 Gold , you will love milk tea like me, let us love her together and play cheap twelve sky2 Gold .

Anonymous said...

Do you know dofus kamas? I like it.
My brother often go to the internet bar to buy kamas and play it.
After school, He likes playing games using these cheap kamas with his friend.
I do not like to play it. Because I think that it not only costs much money but also spend much time. One day, he give me many dofus gold and play the game with me.
I came to the bar following him and found buy dofus kamas was so cheap. After that, I also go to play game with him.
Do you know Archlord gold? I like it.
My brother often go to the internet bar to buy Archlord money and play it.
After school, He likes playing games using these archlord online Gold with his friend.
I do not like to play it. Because I think that it not only costs much money but also spend much time. One day, he give me many cheap Archlord gold and play the game with me.
I came to the bar following him and found buy Archlord gold was so cheap. After that, I also go to play game with him.

Anonymous said...

Have you heared about 9Dragons which you need use 9Dragons gold to play, and you can also borrow 9 Dragons gold from other players? But you can buy 9 Dragons gold, or you will lose the choice if you do not have cheap 9Dragons gold. If you get 9Dragons money, you can continue this game.
Have you heared about a game which you need use Anarchy credits to play, and you can also borrow Anarchy Online credits from other players? But you can buy AO credits, or you will lose the choice if you do not have Anarchy online gold. If you get Anarchy gold, you can continue this game.

feilin said...

I like holic gold very much. Since I entered into this game, I learnt skills to earn holic money. Thanks to holic online gold let me know a lot of friends. It is my habit to buy holic online money, and I get some cheap holic gold from my friends and Internet.
I like to play last chaos, because I like its name, also I like last chaos gold. My friend told me that she would buy lastchaos gold for me, and I was so happy. I do not like to go shopping, because it always spends a lot of money, but I never hesitate to buy last chaos gold. You can buy cheap lastchaos gold; it is so easy and convenient. I believe the future of lastchaos money is bright.

Filipe Romano said...

This example works perfectly, however managing all these menu entries is not that simple in the current version of the DSL Tools. Adding or removing entries is quite a tedious task :)

I implemented a method to generate the artifacts needed for a menu entry based on a simple "model".

Check it out here:

http://lessisthenewmore.wordpress.com/

Jovan said...

very useful!!!

Alex Ilyin said...

Nice post

qishaya said...

abercrombiefitch.uk.com propose Concise design model, whether it is Abercrombie fitch or a shirt, jacket is a good array oh.Ultra-Zan’s pants manner. buy Abercrombie Make up your body a little less than the curve.Abercrombie sale Easily with any clothing, different shoes and boots with different styles. Both kind, genial comfort, you can also cool very special Abercrombie .Designed to highlight hurtful the chest, waist and hip, quite close, sexy extraordinary.Companies consider the use of at least 4 time in-gravity Japanese market. Ginza stockroom Abercrombie & FitchAbercrombie Sweaters may be the most costly ever built one of the flagship storeroom, flagship stockpile in cheap Abercrombie compared to superior construction expenses for 1800-2000 million. The circle spokesman said in Tokyo supplies opened for the Abercrombie & Fitch's worldwide growth policy in provisos of a very important measure. Ginza supply is the circle's first mass in Asia, the business campaign to open next year in Fukuoka, Japan, out of a moment Asian restaurant. However, abercrombie stores the spokeswoman did not disclose more shop list.present high level and high assess clothing for youngsters. The food extended of Abercrombie and Fitch embrace not only casual wears,breitling watches, shirts and dresses for the youngsters but it also includes luxury matter such as perfumes and discount Abercrombie accessories. The new limit of yield from Abercrombie and Fitch has superb designs for kids and teenagers. The strain is also recognized for its advertisements,model watches, where you have physically attractivpoorly clad men and women, who begin vigorous, abercrombie outlet enthusiastic, smart and outgoing.Abercrombie 2010

songsong_like said...

Young and creative style.
abercrombie and fitch
abercrombie & fitch
You can have a look at it.
Abercrombie and fitch outlet
ED Hardy clothing bring you a super surprise!
ed hardy wholesale clothing
If you really want it.
nike outlet

Anonymous said...

Brilliant! Thank you so much!