星期三, 8月 24, 2005

[資訊雜記]RFMaintainer 的 statechart (補)

昨天的RFMaintainer 的 statechart並沒有實作部份,今天補上...
你可以看到這些都是寫在 Form 裡面,並沒有跟 UI 切開....
這就是我疑問的地方...
不過對照圖之後,你會發現這樣的寫法很清楚...不會有補東牆挖西牆,東改一塊西改一塊的問題...

#region State Chart Implementation.
private int _StateVariable = 0;
private int _StateVariable_A = 0;
private int _StateVariable_B = 0;
private int _StateVariable_C = 0;

///
/// State 1: Transient
///

private void go_state_1()
{
_StateVariable = 1;
if( txtConnectString.Text == "" )
go_state_2();
else
go_state_3();
}

///
/// State 2: Connection string null
///

private void go_state_2()
{
_StateVariable = 2;

// disable controls.
radioButton1.Enabled = false;
radioButton2.Enabled = false;
dtpFrom.Enabled = false;
dtpTo.Enabled = false;
cboType.Enabled = false;
txtPath.Enabled = false;
btnBrowse.Enabled = false;
btnExport.Enabled = false;
}

///
/// State 3: Connection string not null
///

private void go_state_3()
{
_StateVariable = 3;

// enable controls.
radioButton1.Enabled = true;
radioButton1.Checked = true;
radioButton2.Enabled = true;
cboType.Enabled = true;
txtPath.Enabled = true;
btnBrowse.Enabled = true;

go_state_4();
go_state_7();
go_state_10();
}

///
/// State 4: Region A: Transient
///

private void go_state_4()
{
_StateVariable_A = 4;
if( radioButton2.Checked )
go_state_5();
else if( radioButton1.Checked )
go_state_6();
}

///
/// State 5: Region A: "Period" checked
///

private void go_state_5()
{
_StateVariable_A = 5;
dtpFrom.Enabled = true;
dtpTo.Enabled = true;
}

///
/// State 6: Region A: "All" checked
///

private void go_state_6()
{
_StateVariable_A = 6;
dtpFrom.Enabled = false;
dtpTo.Enabled = false;
}

///
/// State 7: Region B: Transient
///

private void go_state_7()
{
_StateVariable_B = 7;

if( txtPath.Text == "" )
go_state_8();
else
go_state_9();
}

///
/// State 8: Region B: "output file" null
///

private void go_state_8()
{
btnExport.Enabled = false;
}

///
/// State 9: Region B: "output file" not null
///

private void go_state_9()
{
btnExport.Enabled = true;
}

///
/// State 10: Region C: "Export type" is XML
///

private void go_state_10()
{
_StateVariable_C = 10;
cboType.SelectedIndex = 0;
}

///
/// State 11: Export
///

///
private void go_state_11()
{
}
#endregion

#region UI independence routines
///
/// I don't consider this button in State chart.
/// And I am lazy to change....
///

///
private bool testConnection( string connectString )
{
}

private bool export( int type, DateTime dtFrom, DateTime dtTo, string outputFile )
{
}
#endregion

#region Event handler
private void btnExport_Click(object sender, System.EventArgs e)
{
int iType = 0;

if( radioButton1.Checked == true )
iType = 1;
else if( radioButton2.Checked == true )
iType = 2;
if( export( iType, dtpFrom.Value, dtpTo.Value, txtPath.Text ) )
MessageBox.Show( "Finished!" );
else
MessageBox.Show( "Fail!!" );
}

private void btnBrowse_Click(object sender, System.EventArgs e)
{
// Displays a SaveFileDialog so the user can save the Image
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "XML file|*.xml";
saveFileDialog1.Title = "Save to XML File";
saveFileDialog1.ShowDialog();
saveFileDialog1.OverwritePrompt = true;

// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
txtPath.Text = saveFileDialog1.FileName;
}
}

private void btnTest_Click(object sender, System.EventArgs e)
{
testConnection( txtConnectString.Text );
}

private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
{
go_state_6();
}

private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
{
go_state_5();
}

private void txtPath_TextChanged(object sender, System.EventArgs e)
{
go_state_7();
}

private void txtConnectString_TextChanged(object sender, System.EventArgs e)
{
go_state_1();
}
#endregion
}

沒有留言: