using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Inet.Viewer.Data;
using Inet.Viewer.Resources;
namespace Inet.Viewer.WinForms.Prompt
{
///
/// responsible for panels for prompts with single values and with default values
///
public partial class SinglePromptFieldWithDefaultValues : PromptControl
{
private string oldTextIfNoValueNotChecked;
///
/// constructor with the given dependencies
///
/// prompt to created this panel for
public SinglePromptFieldWithDefaultValues(Data.PromptData promptData)
{
InitializeComponent();
this.components = new System.ComponentModel.Container();
this.ErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
this.cmbDefaultValues.TextChanged += new System.EventHandler(this.HandleChanged);
this.PromptData = promptData;
List defaultValues = new List();
ArrayList values = promptData.DefaultValues;
PromptValue alreadySelectedValue = promptData.Values;
string alreadySelectedValueRepr = alreadySelectedValue == null ? null : alreadySelectedValue.StringRepresentation;
PromptValue valueToSelect = null;
foreach(PromptValue v in values)
{
if (!(v is RangePromptValue))
{
defaultValues.Add(v);
if (alreadySelectedValueRepr == v.StringRepresentation)
{
valueToSelect = v;
}
}
}
this.cmbDefaultValues.DataSource = defaultValues;
this.cmbDefaultValues.DisplayMember = "DisplayString";
this.cmbDefaultValues.ValueMember = "Value";
AutoSizeComboBox(cmbDefaultValues);
if (valueToSelect != null)
{
this.cmbDefaultValues.SelectedItem = valueToSelect;
}
else if (alreadySelectedValue != null && !promptData.MultipleAllowed)
{
this.cmbDefaultValues.SelectedItem = null;
if (alreadySelectedValue.Value == null)
{
this.chkNoValue.Checked = true;
}
else
{
this.cmbDefaultValues.Text = alreadySelectedValue.ValueString;
}
}
if (!this.PromptData.Changeable)
{
this.cmbDefaultValues.DropDownStyle = ComboBoxStyle.DropDownList;
this.chkNoValue.Visible = false;
}
else if (this.PromptData.MultipleAllowed || promptData.MinValue != null)
{
this.chkNoValue.Visible = false;
}
ControlForErrors = chkNoValue.Visible ? (Control)this.chkNoValue : (Control)this.cmbDefaultValues;
}
///
/// Called when the "no value" checkbox is checked: sets the value to null
///
/// button being checked
/// args of the event
internal void chkNoValue_CheckedChanged(object sender, EventArgs e)
{
if (this.chkNoValue.Checked)
{
oldTextIfNoValueNotChecked = this.cmbDefaultValues.Text;
this.cmbDefaultValues.Text = '<' + strings.Prompt_None_Long + '>';
this.cmbDefaultValues.Enabled = false;
}
else
{
if (oldTextIfNoValueNotChecked == null)
{
oldTextIfNoValueNotChecked = "";
}
this.cmbDefaultValues.Text = oldTextIfNoValueNotChecked;
this.cmbDefaultValues.Enabled = true;
}
ValidatePrompt();
OnValueChanged();
}
///
///
///
internal override Data.PromptValue Value
{
get {
if (this.chkNoValue.Checked)
{
return new Data.SinglePromptValue(null, "", this.PromptData.Type);
}
object obj = this.cmbDefaultValues.SelectedItem;
string valueString = null;
if (obj is PromptValue)
{
return obj as PromptValue;
}
else if (obj != null)
{
valueString = obj.ToString();
}
else
{
valueString = this.cmbDefaultValues.Text;
}
return new Data.SinglePromptValue(
this.PromptData.type,
valueString);
}
}
}
}