ユーザーインターフェース(フォーム)を実装する。
以下のフィールドを定義する
必要なプロパティを定義する。
必要に応じてコンストラクタを定義する。
データ入力のためのメソッドを定義する。
InitializeDataFormWrapper() | DataFormWrapper初期化メソッド。Formを初期化する時に呼び出す。DataFormWrapperの各要素に適切なコントロール、フィルター、検証を設定する。 |
Get[Entity]() | エンティティ取得メソッド。DataAccessから必要なエンティティを取得して返す。 |
Save[Entity]() | エンティティ保存メソッド。DataAccessを呼び出すしエンティティを保存する。 |
Update[Entity]() | エンティティ保存メソッド(特に「更新のみ」の場合)。DataAccessを呼び出すしエンティティを保存する。 |
Insert[Entity]() | エンティティ保存メソッド(特に「挿入のみ」の場合)。DataAccessを呼び出すしエンティティを保存する。 |
Validate[Entity]() | エンティティ検証メソッド。現在のエンティティの検証を実行する。 |
イベントに反応するためのメソッドを定義する。
(保存トリガーイベント) |
|
(取得トリガーイベント) | エンティティ取得メソッドを呼び出す。 |
namespace FxTravel.Maintainance.Area.Form
{
public partial class CityEditForm : Form
{
///
/// ログ出力
///
static Logger logger = LoggerFactory.GetLogger("default", MethodBase.GetCurrentMethod().ReflectedType.Name);
///
/// 編集都市
///
City city;
MstCityTableProperties cityProperties;
CityEditFormAdapter cityEditFormAdapter;
public CityEditForm()
{
InitializeComponent();
this.InOutType1.Tag = 1;
this.InOutType2.Tag = 2;
this.InOutType3.Tag = 3;
this.cityProperties = new MstCityTableProperties();
this.cityEditFormAdapter = new CityEditFormAdapter();
this.CreateFormAdapter();
//comboのデータソース
TraTypeSource traSource = new TraTypeSource();
List traTypes = traSource.GetSource();
this.TraCityType.DataSource = traTypes;
this.TraCityType.ValueMember = "Id";
this.TraCityType.DisplayMember = "Name";
}
void CreateInputForm()
{
this.cityEditFormAdapter.CityCode = new TextBoxElement(
this.cityCode,
new StringFilter(this.cityProperties.CityCode),
FieldValidator.GetInstance(this.cityProperties.CityCode)
);
this.cityEditFormAdapter.JpnName = new TextBoxElement(
this.JpnName,
new StringFilter(this.cityProperties.JpnName),
FieldValidator.GetInstance(this.cityProperties.JpnName)
); ;
this.cityEditFormAdapter.JpnShortName = new TextBoxElement(
this.JpnShortName,
new StringFilter(this.cityProperties.JpnShortName),
FieldValidator.GetInstance(this.cityProperties.JpnShortName)
);
this.cityEditFormAdapter.AnkName = new TextBoxElement(
this.AnkName,
new StringFilter(this.cityProperties.AnkName),
FieldValidator.GetInstance(this.cityProperties.AnkName)
);
this.cityEditFormAdapter.EngName = new TextBoxElement(
this.EngName,
new StringFilter(this.cityProperties.EngName),
FieldValidator.GetInstance(this.cityProperties.EngName)
);
this.cityEditFormAdapter.EngShortName = new TextBoxElement(
this.EngShortName,
new StringFilter(this.cityProperties.EngShortName),
FieldValidator.GetInstance(this.cityProperties.EngShortName)
);
this.cityEditFormAdapter.KeyName = new TextBoxElement(
this.KeyName,
new StringFilter(this.cityProperties.KeyName),
FieldValidator.GetInstance(this.cityProperties.KeyName)
);
this.cityEditFormAdapter.CountryCode = new TextBoxElement(
this.CountryCode,
new StringFilter(this.cityProperties.CountryCode),
FieldValidator.GetInstance(this.cityProperties.CountryCode)
);
this.cityEditFormAdapter.StateCode = new TextBoxElement(
this.StateCode,
new StringFilter(this.cityProperties.StateCode),
FieldValidator.GetInstance(this.cityProperties.StateCode)
);
this.cityEditFormAdapter.AreasCode = new TextBoxElement(
this.AreasCode,
new StringFilter(this.cityProperties.AreasCode),
FieldValidator.GetInstance(this.cityProperties.AreasCode)
);
this.cityEditFormAdapter.Gmt = new TextBoxElement(
this.Gmt,
new NumberFilter(this.cityProperties.Gmt),
FieldValidator.GetInstance(this.cityProperties.Gmt)
);
this.cityEditFormAdapter.SummerGmt = new TextBoxElement(
this.SummerGmt,
new NumberFilter(this.cityProperties.SummerGmt),
FieldValidator.GetInstance(this.cityProperties.SummerGmt)
);
this.cityEditFormAdapter.SummerFrom = new TextBoxElement(
this.SummerFrom,
new DateTimeFilter(CFW.DateTimeInputFormat.DATE_LONG),
FieldValidator.GetInstance(this.cityProperties.SummerFrom),
CFW.DateTimeOutputFormat.LONG_DATE
);
this.cityEditFormAdapter.SummerTo = new TextBoxElement(
this.SummerTo,
new DateTimeFilter(CFW.DateTimeInputFormat.DATE_LONG),
FieldValidator.GetInstance(this.cityProperties.SummerTo),
CFW.DateTimeOutputFormat.LONG_DATE
);
this.cityEditFormAdapter.InOutType = new RadioButtonGroupElement(
new RadioButton[] { this.InOutType1, this.InOutType2, this.InOutType3 },
new NumberFilter(this.cityProperties.InOutType),
FieldValidator.GetInstance(this.cityProperties.InOutType)
);
this.cityEditFormAdapter.TraCityType = new DropDownListElement(
this.TraCityType,
new NumberFilter(this.cityProperties.TraCityType),
FieldValidator.GetInstance(this.cityProperties.TraCityType)
);
}
public DialogResult NewCity(Form owner)
{
city = new City();
city.IsModified = false;
this.inputForm.SetDataSource(city);
this.inputForm.RenderView();
return ShowDialog(owner);
}
public DialogResult EditCity(string cityCode, Form owner)
{
LoadCity(cityCode);
city.IsModified = false;
this.inputForm.SetDataSource(city);
this.inputForm.RenderView();
return ShowDialog(owner);
}
void LoadCity(string cityCode)
{
CityAccess access = new CityAccess();
this.city = access.GetCity(cityCode);
}
void SaveCity()
{
CityAccess access = new CityAccess();
access.Save(city);
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (!city.IsModified)
{
MessageBox.Show("何も変更していません。", "確認", MessageBoxButtons.OK);
}
this.cityEditFormAdapter.Bind();
if (!this.inputForm.IsValid)
{
MessageBox.Show("エラーがあります.");
this.DialogResult = DialogResult.None;
return;
}
this.cityEditFormAdapter.UpdateDataSource();
this.city = (City)this.inputForm.GetDataSource();
SaveCity();
this.Close();
}
private void CityEditForm_Load(object sender, EventArgs e)
{
this.button1.Enabled = false;
}
private void city_TextChanged(object sender, EventArgs e)
{
if (this.city != null)
{
this.city.IsModified = true;
this.button1.Enabled = true;
}
}
private void CityEditForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (city != null)
{
if (city.IsModified)
{
DialogResult result = MessageBox.Show("変更されています。閉じていいですか?","確認",MessageBoxButtons.YesNo,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1);
if(result != DialogResult.Yes)
{
e.Cancel = true;
}
}
}
}
}
}