Sunday, May 6, 2007

Some new template developments for GenWise.


We are working hard on our GenWise tool. See http://www.GenWise.com
Lately we have been adding some templates outside of the GenWise environment, just to see how that would work. GenWise templates are compiled .Net assemblies that can be plugged into the GenWise Code generation environment. The templates just have to implement some standard interfaces to work in the GenWise IDE. Here is a small example of a Code template that lets the programmer select a color code in the source code editor via a nice interface:

using System;
using GenWise.Framework.Generic;
using GenWise.Framework.Prj;
using GenWise.Framework.Templates;

namespace ColorCodeTemplate
{
/// Adds Color Code Template
[Serializable]
[Template("ColorCodeTemplate", "ColorCodeTemplate", "ColorCodeTemplate",
SymbolObjectType = typeof(ColorCodeTemplateSymbols),
SingleInstance = false
)
]
// The template For the ColorcodeSelection.
// Inherists from CodeTemplate that contains all basic functionality to make the template work.
public class ColorCodeTemplate : CodeTemplate
{
#region --- Constructors ---
/// Create a ColorCode Template specifying it's inital Parent Template
///
public ColorCodeTemplate(BaseTemplate pParentTemplate)
: base(pParentTemplate)
{
}
/// Create a Color Code Template specifying it's inital codeblock
public ColorCodeTemplate(BaseTemplate pParentTemplate, CodeBlock pCodeBlock)
: base(pParentTemplate, pCodeBlock)
{
}

#endregion

/// Creates the Symbol Object, these are the template prompts. These prompts can be maintained in a
/// XML file. An automatic interface can be used, or you can specific your own interface.
protected override ISymbolObject CreateSymbolObject()
{
return new ColorCodeTemplateSymbols(Symbols, this);
}

/// Overide the default designer. I have my own
public override System.Windows.Forms.UserControl GetDesigner()
{
ColorPicker CPick = new ColorPicker(this, Options.SelectedColor.GetValueOrDefault());
CPick.ShowDialog();
return null;
}
/// Template Options
public ColorCodeTemplateSymbols Options
{
get { return (SymbolsObject as ColorCodeTemplateSymbols); }
}
/// Validates that the Symbols are correctly configured. No required, but if there are own validations add them here.
public override ValidationResult Validate(bool pRecursive, bool pFullValidation)
{
ValidationResult _result = base.Validate(pRecursive, pFullValidation);

if (!_result.IsValid) return _result; // Do not continue checking stuff if the base already fails.

if (Item.NeedsRegeneration == GenerationUpdateStatus.NotNeeded)
{
if (AttachedCodeBlock.File.FileExtension != "cs")
_result.AddError(this, "This Template writes c# code thus it needs to be used inside a C# CodeBlock");
}

if (Options.SelectedColor == null)
_result.AddError(this, "There is no Color selected.");

return _result;
}

/// What needs to be generated comes here.
protected override void Generate()
{
if (!IsValid) return;

// When item is invalid, no code generation
if (!Item.IsValid) return;
and add the namespace
// The class builder in this case is the parent where we are adding this code template
//
if (ParentTemplate is IClassTemplate)
{
IClassTemplate _classTemplate = ParentTemplate as IClassTemplate;
_classTemplate.ClassBuilder.NameSpaces.Add(new NameSpace("System.Drawing", "System.Drawing"));
}

base.Generate();

// Write the Template code
CodeBlock.OpenRegion(ToString()); // Open a c# region in the source editor
string _fieldName = "_" + Name + "Color"; // the generated field name

CodeBlock.Add("Color {0} = Color.FromArgb({1},{2},{3});", _fieldName, Options.SelectedColor.Value.R.ToString(), Options.SelectedColor.Value.G.ToString(), Options.SelectedColor.Value.B.ToString());

CodeBlock.AddUser("AfterColorAssigned", "After the color is assigned");
CodeBlock.CloseRegion(); // close the openend editor region.

}

}
}

No comments: