tbf/TBF/UI/Bench/Components/SelectComponentClassDlg.cs

110 lines
2.9 KiB
C#

using Common.Forms;
///
/// Copyright (c) 2013-2015 Sensus Slovensko a.s.
///
using System;
using System.Windows.Forms;
using TBF.Resources;
using TBF.Rig.Generic;
using TBF.UI.Shared;
namespace TBF.UI.Bench.Components
{
public partial class SelectComponentClassDlg : Form
{
/// This is the main result of this dialog
public IComponentFactory SlctdCmpntFactory = null;
public string SelectedScriptName = null; /// Filled with a script file name after a right-click
string classNamePrefix;
int selectedIndex = -1;
/// Constructor
public SelectComponentClassDlg(string prefix = null)
{
InitializeComponent();
/// SharedDlgSearchForListBox configuration
sharedSearchForListBox1.ParentForm = this;
sharedSearchForListBox1.ListBoxEx = availCmpntsLstBox;
classNamePrefix = (prefix != null) ? prefix : string.Empty;
foreach (var componentFactory in Rig.TbfComponents.Factories)
{
if (componentFactory.ClassName.StartsWith(classNamePrefix))
{
availCmpntsLstBox.Items.Add(componentFactory);
}
}
}
/// Load components from Rig.SupportedComponents
private void SelectComponentClassDlg_Load(object sender, EventArgs e)
{
Localize();
if (selectedIndex >= 0 && selectedIndex < Rig.TbfComponents.Factories.Count)
{
availCmpntsLstBox.SelectedIndex = selectedIndex;
}
}
void Localize()
{
Text = Strings.Select_Component;
availableComponentsLabel.Text = Strings.Available_Components_;
okButton.Text = Strings.OkBtnText;
cancelButton.Text = Strings.CancelBtnText;
}
/// Double click - Add the clicked component
private void availCmpntsLstBox_DoubleClick(object sender, EventArgs e)
{
okButton_Click(sender, e);
}
/// OK - Add the selected component
private void okButton_Click(object sender, EventArgs e)
{
if (availCmpntsLstBox.SelectedItem is IComponentFactory)
{
SlctdCmpntFactory = availCmpntsLstBox.SelectedItem as IComponentFactory;
DialogResult = DialogResult.OK;
Close();
return;
}
}
/// Cancel - Do nothing
private void cancelButton_Click(object sender, EventArgs e)
{
SlctdCmpntFactory = null;
DialogResult = DialogResult.Cancel;
Close();
return;
}
private void availCmpntsLstBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int index = availCmpntsLstBox.IndexFromPoint(e.Location);
if (index >= 0 && index < Rig.TbfComponents.Factories.Count)
{
selectedIndex = index;
SlctdCmpntFactory = Rig.TbfComponents.Factories[index];
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
SelectedScriptName = dlg.FileName;
}
DialogResult = DialogResult.OK;
Close();
return;
}
}
}
}
}