Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
scrollbar

h2. Exercise outline

{color:#000000}The goal of this exercise is to add a Ribbon button for volume models. As a result, it should be possible to configure the input data of volume models in just a couple of clicks.{color}


h2. Create a new gui command

Add a new folder to the plugin project named _Commands_. In this folder, create a new class named _AddInputDataToVolumeModelCommand.cs_ and add the following code:

{code}
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DelftTools.Shell.Gui;
using DeltaShell.Plugin.VolumeModel.Importers;
using DeltaShell.Plugins.NetworkEditor.Import;
using SharpMap.Api;
using SharpMap.Data.Providers;

namespace DeltaShell.Plugin.VolumeModel.Commands
{
    internal class AddInputDataToVolumeModelCommand : IGuiCommand
    {
        /// <summary>
        /// The name of the gui command
        /// </summary>
        public string Name
        {
            get { return "Add input data"; }
        }
 
        /// <summary>
        /// Ensures the gui command is enabled for volume models only
        /// </summary>
        public bool Enabled
        {
            get { return Gui != null && Gui.SelectedModel is Models.VolumeModel; }
        }
 
        /// <summary>
        /// The image of the gui command
        /// </summary>
        public Image Image { get; set; }

        /// <summary>
        /// Whether or not the gui command is checked
        /// </summary>
        /// <remarks>Not relevant in this tutorial</remarks>
        public bool Checked { get; set; }

        /// <summary>
        /// A reference to the Delta Shell gui
        /// </summary>
        public IGui Gui { get; set; }
 
        /// <summary>
        /// The action that should be performed while executing the gui command
        /// </summary>
        public void Execute(params object[] arguments)
        {
            // Obtain the selected volume model
            var volumeModel = (Models.VolumeModel) Gui.SelectedModel;
 
            // Create a WaterML2 time series importer
            var waterMl2TimeSeriesImporter = new WaterML2TimeSeriesImporter();

            // Try to obtain a precipitation file via a file dialog
            var fileDialog = new OpenFileDialog
            {
                Title = "Choose precipitation time series",
                Filter = "WaterML2 files|*.XML",
                Multiselect = false
            };
 
            if (fileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
 
            // Import a WaterML2 file to the precipitation time series of the volume model
            waterMl2TimeSeriesImporter.ImportItem(fileDialog.FileName, volumeModel.Precipitation);
 
            // Create a catchment importer
            var catchmentsImporter = new CatchmentFromGisImporter
            {
                FileBasedFeatureProviders = new List<IFileBasedFeatureProvider>
                {
                    new ShapeFile()
                }
            };

            // Try to obtain a shape file via a file dialog
            fileDialog = new OpenFileDialog
            {
                Title = "Choose basin shape file",
                Filter = "Shape files|*.shp",
                Multiselect = false
            };
 
            if (fileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
 
            // Configure the catchment importer
            var catchmentImporterSettings = catchmentsImporter.FeatureFromGisImporterSettings;
            catchmentImporterSettings.Path = fileDialog.FileName;
            catchmentImporterSettings.PropertiesMapping.First(property => property.PropertyName == "Name").MappingColumn.ColumnName = "GM_NAAM";
 
            // Import a shape file to the basin of the volume model
            catchmentsImporter.ImportItem(null, volumeModel.Basin);
        }
 
        /// <summary>
        /// The action that should be performed in order to undo execute actions of the gui command
        /// </summary>
        /// <remarks>Not relevant in this tutorial</remarks>
        public void Unexecute()
        {
 
        }
    }
}
{code}
{info}

The command is derived from the IGuiCommand interface in order to obtain a reference to the Delta Shell gui (which is automatically set by the Delta Shell framework).

Furthermore, the comments in the code should explain the different parts of the gui command implementation.
{info}

h2. Create a new Ribbon control

Create a new *WPF user control* named _VolumeModelRibbon.xaml_ and add the following xaml code:
{code}
<UserControl x:Class="DeltaShell.Plugin.VolumeModel.VolumeModelRibbon"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:fluent="clr-namespace:Fluent;assembly=Fluent" <!--Create a ribbon control-->
             mc:Ignorable="d" Height="145" Width="632">
    <!--Create a ribbon control-->
    <fluent:Ribbon Name="VolumeModelRibbonControl" x:FieldModifier="private">
        <!--Create a ribbon tab-->
        <fluent:RibbonTabItem Header="Volume model" fluent:KeyTip.Keys="E">
            <!--Create a ribbon group box-->
            <fluent:RibbonGroupBox Header="Input">
                <!--Create a ribbon button-->
                <fluent:Button x:Name="ButtonAddInputDataToVolumeModel"
                               Header="Add input data"
                               ToolTip="Add input data to the selected volume model"
                               Click="ButtonAddInputDataToVolumeModel_OnClick"
                               Size="Middle"
                               SizeDefinition="Middle,Small,Small"/>
            </fluent:RibbonGroupBox>
        </fluent:RibbonTabItem>
    </fluent:Ribbon>
</UserControl>
{code}

Additionally, add the following interaction logic in _VolumeRibbon.xaml.cs_ (right click the class \| View Code):

{code}
using System.Collections.Generic;
using System.Windows;
using DelftTools.Controls;
using DelftTools.Shell.Gui;
using DelftTools.Shell.Gui.Forms;
using DeltaShell.Plugin.VolumeModel.Commands;

namespace DeltaShell.Plugin.VolumeModel
{
    /// <summary>
    /// Interaction logic for VolumeModelRibbon.xaml
    /// </summary>
    public partial class VolumeModelRibbon : IRibbonCommandHandler
    {
        private readonly IGuiCommand addInputDataToVolumeModelCommand = new AddInputDataToVolumeModelCommand(); // Instance of the implemented gui command

        /// <summary>
        /// Creates the Ribbon command handler
        /// </summary>
        public VolumeModelRibbon()
        {
            // Initialize the command handler (standard user control logic)
            InitializeComponent();
        }

        /// <summary>
        /// Returns the volume model Ribbon control
        /// </summary>
        public object GetRibbonControl()
        {
            return VolumeModelRibbonControl;
        }

        /// <summary>
        /// Enabling/disabling actions to be performed while validating the Ribbon items (triggered by Delta Shell)
        /// </summary>
        public void ValidateItems()
        {
            ButtonAddDemoDataToVolumeModel.IsEnabled = addDemoDataCommand.Enabled;
        }

        /// <summary>
        /// Whether or not the contextual tab should be visible
        /// </summary>
        public bool IsConextualTabVisible(string tabGroupName, string tabName)
        {
            return false;
        }

        /// <summary>
        /// The (gui) commands of the Ribbon command handler
        /// </summary>
        public IEnumerable<ICommand> Commands
        {
            get { yield return addInputDataToVolumeModelCommand; }
        }

        /// <summary>
        /// Actions to be performed after clicking the AddInputDataToVolumeModel button
        /// </summary>
        private void ButtonAddInputDataToVolumeModel_OnClick(object sender, RoutedEventArgs e)
        {
            addDemoDataCommand.Execute();
        }
    }
}
{code}

h2. Fixme

{color:#ff0000}*\[TODO\]*{color} {color:#ff0000}Fixme{color}


h2. Exercise results

{color:#ff0000}*\[TODO\]*{color} {color:#ff0000}Description of the exercise result{color}


\\  !NewWPFControl .png!\\


7. Run the application, create a volume model and click the newly created Ribbon button: ensure input data is added correctly to the volume model by opening the data views or by running the model \[TODO: Image\]
\\
\\
\\