using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Inet.Viewer.Data;
namespace Inet.Viewer.WinForms.Prompt
{
///
/// panel for a prompt field which allows for multiple prompt values to be chosen and
/// added to a list
///
public partial class MultiPromptField : PromptControl
{
internal PromptControl pnlSingle;
internal PromptControl pnlRange;
private IButtonControl oldAcceptButton;
///
/// simple constructor with the necessary dependencies
///
/// prompt this panel is based on
/// the single prompt panel part (can be null if no single values are allowed)
/// the range prompt panel part (can be null if no range values are allowed)
public MultiPromptField(Data.PromptData p, PromptControl singleControl, PromptControl rangeControl)
{
InitializeComponent();
this.PromptData = p;
this.pnlSingle = singleControl;
this.pnlRange = rangeControl;
if (singleControl != null)
{
btnAddSingle.Enabled = singleControl.ValidatePrompt();
singleControl.ValueChanged += (s, e) => { btnAddSingle.Enabled = singleControl.ValidatePrompt(); };
panelSingle.Controls.Add(singleControl);
groupBoxSingle.Size = new Size(panelSingle.Size.Width, singleControl.Height + btnAddSingle.Height + panelSingle.Location.Y + 4);
}
else
{
groupBoxSingle.Visible = false;
}
if (rangeControl != null)
{
btnAddRange.Enabled = rangeControl.ValidatePrompt();
rangeControl.ValueChanged += (s, e) => { btnAddRange.Enabled = rangeControl.ValidatePrompt(); };
panelRange.Controls.Add(rangeControl);
groupBoxRange.Size = new Size(panelRange.Size.Width, rangeControl.Height + btnAddRange.Height + panelRange.Location.Y + 4);
}
else
{
groupBoxRange.Visible = false;
}
this.btnDeleteEntry.Enabled = false;
object v = p.Values;
if (v is MultiPromptValue)
{
MultiPromptValue chosenValue = v as MultiPromptValue;
ArrayList values = chosenValue.Values;
this.listBox.Items.AddRange(values.ToArray());
}
}
///
/// called to add a single element to the list
///
/// button which led to the event
/// eventargs of the action
private void button1_Click(object sender, EventArgs e)
{
if (pnlSingle.ValidatePrompt())
{
PromptValue value = pnlSingle.Value;
if (value != null)
{
this.listBox.Items.Add(value);
}
}
}
///
/// called to add a range element to the list
///
/// button which led to the event
/// eventargs of the action
private void button2_Click(object sender, EventArgs e)
{
if (pnlRange.ValidatePrompt())
{
PromptValue value = pnlRange.Value;
if (value != null)
{
this.listBox.Items.Add(value);
}
}
}
///
/// called to remove an element from the list
///
/// button which led to the event
/// eventargs of the action
private void button1_Click_1(object sender, EventArgs e)
{
if (this.listBox.SelectedIndex >= 0)
{
this.listBox.Items.RemoveAt(this.listBox.SelectedIndex);
if (this.listBox.Items.Count == 0)
{
this.btnDeleteEntry.Enabled = this.listBox.SelectedIndex >= 0;
}
}
}
///
/// the MultiPromptValue chosen for this prompt field panel
///
internal override PromptValue Value
{
get
{
MultiPromptValue result = new MultiPromptValue(this.PromptData.Type);
result.Values.AddRange(this.listBox.Items);
return result;
}
}
///
/// when the index of the chosen element in the list of chosen elements is changed
///
/// control responsible for the action
/// eventargs of the action
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
this.btnDeleteEntry.Enabled = this.listBox.SelectedIndex >= 0;
}
internal override bool ValidatePrompt()
{
return true;
}
private void panelSingle_Enter(object sender, EventArgs e)
{
Form form = FindForm();
if (form != null) {
oldAcceptButton = form.AcceptButton;
form.AcceptButton = btnAddSingle;
}
}
private void panelRange_Enter(object sender, EventArgs e)
{
Form form = FindForm();
if (form != null)
{
oldAcceptButton = form.AcceptButton;
form.AcceptButton = btnAddRange;
}
}
private void panel_Leave(object sender, EventArgs e)
{
Form form = FindForm();
if (form != null)
{
form.AcceptButton = oldAcceptButton;
}
}
}
}