Navigation:  I Want To... > Basics >

Create a desktop tool

Previous pageReturn to chapter overviewNext page

Use Case

You want the tool to perform an operation when you click a toolbar button, or select a menu item
You want the tool to be associated with the desktop

Relevant Architecture

Tools
Extending functionality with Tools

Relevant Types

DesktopToolExtensionPoint
IDesktopToolContext
Tool<T>

Sample Code

using System;

using ClearCanvas.Common;

using ClearCanvas.Common.Utilities;

using ClearCanvas.Desktop;

using ClearCanvas.Desktop.Actions;

using ClearCanvas.Desktop.Tools;

 

// ... (other using namespace statements here)

 

namespace MyPlugin.Basics

{

   [MenuAction("apply""global-menus/MenuTools/MenuStandard/MenuMyDesktopTool""Apply")]

   [ButtonAction("apply""global-toolbars/ToolbarStandard/ToolbarMyDesktopTool""Apply")]

   [Tooltip("apply""TooltipMyDesktopTool")]

   [IconSet("apply", IconScheme.Colour, "Icons.MyToolSmall.png""Icons.MyToolMedium.png""Icons.MyToolLarge.png")]

   [EnabledStateObserver("apply""Enabled""EnabledChanged")]

   // ... (other action attributes here)

   [ExtensionOf(typeof (DesktopToolExtensionPoint))]

   public class MyDesktopTool : Tool<IDesktopToolContext>

   {

      private bool _enabled;

      private event EventHandler _enabledChanged;

 

      /// <summary>

      /// Default constructor.  A no-args constructor is required by the

      /// framework.  Do not remove.

      /// </summary>

      public MyDesktopTool()

      {

         _enabled = true;

      }

 

      /// <summary>

      /// Called by the framework to initialize this tool.

      /// </summary>

      public override void Initialize()

      {

         base.Initialize();

         // TODO: add any significant initialization code here rather than in the constructor

      }

 

      /// <summary>

      /// Called to determine whether this tool is enabled/disabled in the UI.

      /// </summary>

      public bool Enabled

      {

         get { return _enabled; }

         protected set

         {

            if (_enabled != value)

            {

               _enabled = value;

               EventsHelper.Fire(_enabledChanged, this, EventArgs.Empty);

            }

         }

      }

 

      /// <summary>

      /// Notifies that the Enabled state of this tool has changed.

      /// </summary>

      public event EventHandler EnabledChanged

      {

         add { _enabledChanged += value; }

         remove { _enabledChanged -= value; }

      }

 

      /// <summary>

      /// Called by the framework when the user clicks the "apply" menu item or toolbar button.

      /// </summary>

      public void Apply()

      {

         // Add code here to implement the functionality of the tool

      }

   }

}

Remarks

A desktop tool is the simplest type of tool you can create.  Typically, a desktop tool will always be visible on the toolbar and/or menu.  In the example above, when the toolbar button is clicked or menu item selected, the Apply() method is executed.  Note that the handling method doesn't have to be called Apply().  You specify the handling method name in the third parameter of the class level MenuAction  or ButtonAction attribute.  Similarly, the EnabledStateObserver attribute allows you to specify the name of the boolean property that indicates whether the tool is enabled (in this case, "Enabled"), and the name of the event that notifies the Framework that the property has changed (in this case, "EnabledChanged").

Using the Wizard

The ClearCanvas SDK provides some useful wizards to make common tasks a little easier.  One such task is creating a desktop tool.  To use the wizard to create a desktop tool:

1.Create a new ClearCanvas plugin, or select an existing plugin in which to host the tool.
2.Add references to ClearCanvas.Desktop.dll and ClearCanvas.ImageViewer.dll.
3.Bring up the Add New Item dialog (right click on the project, then select Add followed by New Item).
4.Under My Templates, select ClearCanvas Tool.  Name the Tool appropriately and click OK.

5.On the ClearCanvas Tool Wizard that appears, select General Tool under Template and click OK.  This will generate the appropriate boilerplate code.

6.Fill in the Apply() method.  This method is called when the tool is invoked through the UI, and this is where the tool does its work.
7.Follow the TODO instructions in the file to customize your tool.  You can customize such things as:
whether the tool appears in the menu, on a toolbar, or both
which menu/toolbar the tool appears in
the icon and tooltip message
the enablement of the tool in the menu/toolbar
8.Compile the project and copy the resulting .dll into the /plugins directory of your ClearCanvas Workstation installation.  Your tool will be integrated into the application, and by default will show up in the menu under Tools > My Tools > MyTool, and on the toolbar as a hammer icon.

Important Note: By default, the Framework will try to use the string resource (in the assembly's SR.resx file) to resolve the names of the individual components of menu and toolbar paths.  If a string resource is not available, then the literal will be used.  So for example, if the menu path component is MyTool, the framework will look for a string resource called MyTool and use whatever value has been assigned to MyTool as the actual menu item label.  If the MyTool string resource doesn't exist, the menu item will be literally labelled "MyTool".

Examples in Code Base

ClearCanvas.Desktop.Help.HelpTool
ClearCanvas.Desktop.Configuration.Tools.OptionsTool