Skip to content
Rain Hu's Workspace
Go back

[DXP] 在 spotfire 中創建自定義工具

Rain Hu

簡介


概要


Tools 文本

1. Application

public sealed class MyApplicationTool : CustomTool<AnalysisApplication>

2. Document

public sealed class MyDocumentTool : CustomTool<Document>

3. Page

public sealed class MyPageTool : CustomTool<Page>

4. Visual

public sealed class MyVisualTool : CustomTool<VisualContent>

5. TablePlot

TablePlotColumn

public sealed class MyTablePlotColumnTool : CustomTool<TablePlotColumnContext>

TablePlotCell

public sealed class MyTablePlotCellTool : CustomTool<TablePlotCellContext>

TablePlotCopyCellValue

public sealed class MyTablePlotCopyCellValueTool : CustomTool<TablePlotCopyCellValueContext>

6. Map Chart Coordinates Context

public sealed class MyMapChartCoordinatesTool : CustomTool<MapChartCoordinatesContext>

7. Filter

public sealed class MyFilterTool : CustomTool<FilterBase>

Grouping Tools、註冊

public sealed class CustomToolAddIn : AddIn
{
    protected override void RegisterTools(ToolRegistrar registrar)
    {
        base.RegisterTools(registrar);
 
        CustomMenuGroup menuGroup = new CustomMenuGroup("My menu sub group");
 
        registrar.Register(new MyTool(), menuGroup);
    }
    ...
}
[ApiVersion("10.10.0")]
public static MenuCategory CreateCustom(params string[] path);

範例:

MenuCategory.CreateCustom("First", "Second");
public MyTool()
    : base(menuText, requiredLicense, MenuCategory.CreateCustom("First", "Second")) {}

Custom Tool 建構子

protected CustomTool(string menuText, LicensedFunction requiredLicense, MenuCategory menuCategory, Image image)

ExecuteCore

protected virtual void ExecuteCore(TContext context);
protected virtual IEnumerable<Object> ExecuteAndPromptCore(TContext context);

IsEnabledCore

public sealed class PageTool : CustomTool<Page>
{
    public PageTool()
        : base(Properties.Resources.PageToolTitle) {}

    protected override void ExecuteCore(Page page)
    {
        Document document = page.Context.GetAncestor<Document>();
        int index = document.Pages.IndexOf(page);
        document.Pages.Move(index, document.Pages.Count - 1);
    }

    protected override bool IsEnabledCore(Page page)
    {
        Document document = page.Context.GetAncestor<Document>();
        int index = document.Pages.IndexOf(page);
        return index >= 0 && index < document.Pages.Count - 1;
    }
}
public sealed class PlotTool : CustomTool<VisualContent>
{
    public PlotTool()
        : base(Properties.Resources.PlotToolTitle) {}

    protected override void ExecuteCore(VisualContent context)
    {
        Visualization plot = (Visualization)context;
        DataManager dataManager = context.Context.GetSertvice<DataManager>();

        DataTable dataTable = plot.Data.DataTableReference;
        DataColumn firstColumn = dataManager.Tables[dataTable.Id].Columns[0];

        IndexSet markedRows = dataManager.Markings.DefaultMarkingReference.GetSelection(dataTable).AsIndexSet();
        var cursor = DataValueCursor.Create(firstColumn);
        foreach (var row in dataTable.GetRows(markedRows, cursor))
        {
            Trace.WriteLine(cursor.CurrentDataValue.ValidValue);
        }
    }

    protected override bool IsEnabledCore(VisualContent context)
    {
        Visualization plot = context as Visualization;

        DataManager dataManager = context.Context.GetService<DataManager>();
        DataMarkingSelection marking = dataManager.Markings.DefaultMarkingReference;

        if (plot.Data.DataReference == null)
        {
            return false;
        }
        else
        {
            RowSelection selectedRows = marking.GetSelection(plot.Data.DataTableReference);
            return !selectedRows.IsEmpty;
        }
    }

    protected override bool IsVisibleCore(VisualContent context)
    {
        return context is BarChart || context is ScatterPlot;
    }
}

範例3: 在 Spotfire 表格中右鍵點擊顯示圖像的儲存格,選擇「複製儲存格 > 圖像」,將圖像按比例縮放後添加到剪貼簿中。此範例實現了一個工具,可將全尺寸圖像複製到剪貼簿中。它可以從通用的 Spotfire 圖像複製工具相同的位置訪問: pic4

public sealed class CopyCellValueTool : CustomTool<CopyCellValueContext>
{
    public CopyCellValueTool() : base("Image (fullsize)") {}

    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
            }
        }
    }

    protected override bool IsVisibleCore(CopyCellValueContext context)
    {
        return context.DataType != null &&
            context.ContentType != null &&
            !context.DataType.IsSimple &&
            context.ContentType.StartsWith("image/", true, CultureInfo.InvariantCulture);
    }
 
    protected override bool IsEnabledCore(CopyCellValueContext context)
    {
        return context.DataValue != null && context.DataValue.IsValid;
    }
}

範例4: 將「顯示儲存格資訊」項目添加到儲存格內容選單中。該工具會顯示一個訊息方塊,顯示所選儲存格的欄位名稱和儲存格值。

public sealed class TablePlotCellContextExampleTool : CustomTool<TablePlotCellContext>
{
    public TablePlotCellContextExampleTool() : base(Properties.Resources.TablePlotCellToolTitle) {}
 
    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);
    }
 
    protected override bool IsEnabledCore(TablePlotCellContext context)
    {
        return context.DataValue != null && context.DataValue.IsValid;
    }
}

Share this post on:

Previous
[DXP] Spotfire Developer SDK
Next
[DXP] 在 spotfire 中創建自定義任務