Navigation:  Application Architecture > Putting it All Together > Designing for Extensibility >

Testing the theory

Previous pageReturn to chapter overviewNext page

At this point it may be helpful to put ourselves in the position of the tool developer and create a skeleton tool to try it out.  Let’s define a PigLatinTool class to translate the contents of the editor into Pig Latin1:

[ExtensionOf(typeof(TextEditorToolExtensionPoint))]

public class PigLatinTool : Tool<ITextEditorToolContext>

{

}

 

Note we’ve marked it as an extension of TextEditorToolExtensionPoint, and inherited the abstract class Tool.  We know from the documentation that comes with TextEditorToolExtensionPoint (which we’ve provided!) that we can expect a context of type ITextEditorToolContext, so this type is passed as the generic parameter to Tool.  The Tool class has a Context property that will return a reference to this type.

Now we must provide an action that will allow the user to invoke the conversion of plain text to Pig Latin.  We’ll use an action attribute for this:

[MenuAction("apply""global-menus/Text Editor/Pig Latinize", "Apply")]

 

[ExtensionOf(typeof(TextEditorToolExtensionPoint))]

public class PigLatinTool : Tool<ITextEditorToolContext>

{

    ...

}

 

Note that we’ve defined a menu action named “apply” that will invoke our Pig Latin conversion.  The path for this action specifies a site of “global-menus”, so we expect this action to appear on the global menu.  It will appear under the heading Text Editor > Pig Latinize.  The third argument in the MenuActionAttribute, "Apply", specifies the method that will be executed when the menu item is selected by the user.

public void Apply()

{

   this.Context.Text = PigLatinize(this.Context.Text);

}

 

Assuming the existence of a helper method named PigLatinize that takes a string as input and returns a string as output, we simply get the text from the context, modify it, and send it back to the context.  We've verified, from the point of view of the developer of PigLatinTool, that the tool can accomplish its intended task.


1 For readers who may be unfamiliar with the concept of Pig Latin, it is basically a children's game where English words are obfuscated according to a certain set of rules.  Feel free to replace the call to PigLatinize(...) with something else, such as a simple cipher encryption algorithm.