view defical-sharp/defical-gui/main.xaml.cs @ 0:ebed2bd0d300

Initial import from svn. History be damned.
author Edho P. Arief <me@myconan.net>
date Fri, 02 Apr 2010 23:11:57 +0700
parents
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace defical_gui
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class main : Window
    {
        public main()
        {
            InitializeComponent();
            initBoxes();
            txtNumN.Focus();
        }
        private delegate void StringDelegate(int errCode,string message);
        private void printMessage(int errCode,string message)
        {
            switch (errCode)
            {
                case 0:
                    {
                        txtResult.Text += message;
                        break;
                    }
                default:
                    {
                        switch (errCode)
                        {
                            case 200:
                                {
                                    message = "Started";
                                    break;
                                }
                            case 201:
                                {
                                    message = "Stopped";
                                    break;
                                }
                            case 400:
                                {
                                    message = "Found";
                                    break;
                                }
                            case 401:
                                {
                                    message = "SEMT Labeing can't be constructed with first label of " + message;
                                    break;
                                }

                        }
                        txtResult.Text += "[" + errCode.ToString() + "] " + DateTime.Now.ToString("[yyyy-MM-dd hh:mm:ss]") + ": " + message+"\n";
                        break;
                    }
            }
        }
        private void initBoxes()
        {
            this.isProcessing = false;
            txtResult.Text =
                txtNumDef.Text =
                txtNumN.Text = "";
            validateInput();
        }
        private void txtResult_TextChanged(object sender, TextChangedEventArgs e)
        {
            switchControls(this.isProcessing);
            if (txtResult.Text == "") { btnSave.IsEnabled = false; } else { btnSave.IsEnabled = true; }
            txtResult.ScrollToEnd();
        }
        private bool validateInput()
        {
            btnStart.IsEnabled = false;
            if (txtNumN.Text == "" || txtNumDef.Text == "") { return false; }
            try
            {
                int numN = Convert.ToInt32(txtNumN.Text);
                int numDef = Convert.ToInt32(txtNumDef.Text);
                if (numN.ToString() == txtNumN.Text
                    && numDef.ToString() == txtNumDef.Text
                    && numN >= 3
                    && numDef >= 0)
                {
                    btnStart.IsEnabled = true;
                    return true;
                }
            }
            catch { return false; }
            return false;
        }
        private void switchControls(bool isWorking)
        {
            grpGraph.IsEnabled =
                grpLabels.IsEnabled =
                btnStart.IsEnabled = !isWorking;
            btnStop.IsEnabled =
                barProgress.IsIndeterminate = isWorking;
        }

        private void txtNumN_TextChanged(object sender, TextChangedEventArgs e)
        {
            validateInput();
        }

        private void txtNumDef_TextChanged(object sender, TextChangedEventArgs e)
        {
            validateInput();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            if (!validateInput()) { return; }
            switchControls(true);
            try
            {
                this.isFindEverything = (bool)chkIsFindAll.IsChecked;
                switch (cmbGraphType.Text)
                {
                    case "Wheel":
                        this.graphType= "wheel";
                        break;
                    case "Fan":
                        this.graphType = "fan";
                        break;
                    case "Double Fan":
                        this.graphType = "doublefan";
                        break;
                    default:
                        throw new NotImplementedException();
                }
                this.numVerDef = Convert.ToInt32(txtNumDef.Text);
                int numExtraVer = 0;
                switch (this.graphType)
                {
                    case "wheel":
                    case "fan":
                        {
                            numExtraVer = 1;
                            break;
                        }
                    case "doublefan":
                        {
                            numExtraVer = 2;
                            break;
                        }
                    default:
                        throw new NotImplementedException();
                }
                this.numVerMain = numExtraVer + Convert.ToInt32(txtNumN.Text);
                mainThread = new System.Threading.Thread(go);
                mainThread.IsBackground = true;
                mainThread.Start();
            }
            catch
            {
                switchControls(false);
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            this.isProcessing = false;
            switchControls(false);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Forms.SaveFileDialog dialogSave = new System.Windows.Forms.SaveFileDialog();
                dialogSave.DefaultExt = "txt";
                dialogSave.Filter = "Text file (*.txt)|*.txt|All files (*.*)|*.*";
                dialogSave.AddExtension = true;
                dialogSave.RestoreDirectory = true;
                dialogSave.Title = "Save result";
                dialogSave.InitialDirectory = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
                dialogSave.FileName = "result-" + cmbGraphType.Text + "-" + txtNumN.Text + "," + txtNumDef.Text;
                if (dialogSave.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    StreamWriter fileWriter = null;
                    if (File.Exists(dialogSave.FileName)) { fileWriter = File.AppendText(dialogSave.FileName); }
                    else { fileWriter = File.CreateText(dialogSave.FileName); }
                    fileWriter.Write(txtResult.Text);
                    fileWriter.Close();
                }
            }
            catch { }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txtResult.Text = "";
        }
        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}