Skip to main content
RSS feed Subscribe to feed

 

How to Create a Table Context Menu Entry

When a context menu is opened on a table plot cell, any custom tool registered for one the TablePlotCellContext or CopyCellValueContext are added to the menu.

Overview

Spotfire tables can be extended with custom tools available from the table, column and cell context menus. Refer to Creating a Tool, specifically the Visualization and Additional Table Visualization Contexts sections.

Background Information
  • Creating a Table Context Menu
    In addition to hooking in a context menu tool on a table, table context tools may be hooked in on column as well as cell level.
Prerequisites
  • Spotfire SDK\Examples\Extensions\SpotfireDeveloper.CustomToolsExample

A Cell Value Tool

Right-clicking a Spotfire table cell displaying an image selecting Copy Cell > Image, adds the image as scaled in the table to the clipboard. The CopyCellValueTool.cs file of the SpotfireDeveloper.CustomToolsExample SDK example implements a tool copying the full-size image to the clipboard. It is accessible from the same location as the generic Spotfire image copy tool:

Copy Cell Value context tool

The tool inherits from the CustomTool<CopyCellValueContext> The implementation is straight-forward:

/// <summary>
/// A copy cell value tool which will show on context menu when activated for an image
/// value.
/// </summary>
public sealed class CopyCellValueTool : CustomTool<CopyCellValueContext>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="CopyCellValueTool"/> class.
    /// </summary>
    public CopyCellValueTool() : base("Image (fullsize)")
    {
    }

    /// <summary>
    /// Executes the tool. Copies an image blob to the clipboard.
    /// </summary>
    /// <param name="context">The context of the tool. Is never <c>null</c>.</param>
    /// <remarks>Note that this method is only called if
    /// <paramref name="context"/> is not <c>null</c> and
    /// <see cref="M:Spotfire.Dxp.Application.Tool`1.IsEnabled(`0)"/>
    /// returns <c>true</c>.</remarks>
    protected override void ExecuteCore(CopyCellValueContext context)
    {
        BinaryLargeObject blob = context.DataValue.Value as BinaryLargeObject;
        if (blob != null)
        {
            try
            {
                Image img = Image.FromStream(blob.GetByteStream());
                Clipboard.Clear();
                Clipboard.SetImage(img);
            }
            catch (ArgumentException)
            {
                // Unable to convert to image, ignore
            }
        }
    }

    /// <summary>
    /// CopyCellValueTool is only visible when the <see cref="DataType"/> in context 
    /// is binary, and content type in context begins with "image/".
    /// </summary>
    /// <param name="context">The context of the tool. Can be <c>null</c>.</param>
    /// <returns>
    /// Returns <c>true</c> if type is binary and content type beings with "image/",
    /// <c>false</c> otherwise.
    /// </returns>
    protected override bool IsVisibleCore(CopyCellValueContext context)
    {
        return context.DataType != null &&
            context.ContentType != null &&
            !context.DataType.IsSimple &&
            context.ContentType.StartsWith("image/", true, CultureInfo.InvariantCulture);
    }

    /// <summary>
    /// CopyCellValueTool is only enabled if the DataValue in the context is valid.
    /// When working with a virtual column, a invalid datavalues are recieved if 
    /// the value has not yet been fetched.
    /// </summary>
    /// <param name="context">The context of the tool. Is never <c>null</c>.</param>
    /// <returns>
    /// Returns <c>true</c> if the DataValue is valid, otherwise <c>false</c>.
    /// </returns>
    /// <remarks>Note that this method is only called if
    /// <paramref name="context"/> is not <c>null</c></remarks>
    protected override bool IsEnabledCore(CopyCellValueContext context)
    {
        return context.DataValue != null && context.DataValue.IsValid;
    }
}

A Table Plot Cell Context Tool

The TablePlotCellContextTool.cs file of the SpotfireDeveloper.CustomToolsExample SDK example implements a tool adding the Show cell information entry to the cell context menu. The tool dispalys a message box naming the column of the selected cell and the cell value.

Table Plot Cell context tool

This tool inherits from the CustomTool<TablePlotCellContext>:

/// <summary>
/// A table plot context menu tool tool that display information about
/// the cell upon which the context menu was opened.
/// </summary>
/// 
/// <remarks>This tool is only available for 
/// <see cref="TablePlot">Table Plots</see>.
/// </remarks>
public sealed class TablePlotCellContextExampleTool : CustomTool<TablePlotCellContext>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="TablePlotCellContextExampleTool"/> class.
    /// </summary>
    public TablePlotCellContextExampleTool() : base(Properties.Resources.TablePlotCellToolTitle)
    {
    }

    /// <summary>
    /// Executes the tool. Shows a messagebox that display information about
    /// the cell upon which the context menu was opened.
    /// </summary>
    /// <param name="tablePlotCellContext">The table plot cell context.</param>
    protected override void ExecuteCore(TablePlotCellContext tablePlotCellContext)
    {
        string dataValue = tablePlotCellContext.DataValue.Value.ToString();

        string info = string.Format(
            System.Threading.Thread.CurrentThread.CurrentCulture,
            Properties.Resources.TablePlotCellToolInfo,
            new object[] { tablePlotCellContext.TableColumn.Name,
                           dataValue });

        System.Windows.Forms.MessageBox.Show(info, Properties.Resources.TablePlotCellToolTitle);
    }

    /// <summary>
    /// TablePlotCellContextTool is only enabled if the DataValue in the context
    /// is valid. When working with a virtual column, a invalid datavalues are
    /// recieved if the value has not yet been fetched.
    /// </summary>
    /// <param name="context">The context of the tool. Is never <c>null</c>.</param>
    /// <returns>
    /// Returns <c>true</c> if the DataValue is valid, otherwise <c>false</c>.
    /// </returns>
    /// <remarks>Note that this method is only called if
    /// <paramref name="context"/> is not <c>null</c></remarks>
    protected override bool IsEnabledCore(TablePlotCellContext context)
    {
        return context.DataValue != null && context.DataValue.IsValid;
    }
}