#region Copyright and GPL
/*************************************************************************
 * dataMagus - The open source database learning software
 * Copyright (C) 2007 by all authors
 * Authors: Monika Schwitter (monsy@gmx.ch)
 *          Jürg Zgraggen (j.zgraggen@ejaz.net)
 *          HSLU (Hochschule Luzern - Technik+Architektur)
 * 
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
#endregion
#region Using
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using dataMagus.Common.Physical.Objectoriented;
using dataMagus.Common.PlugIn.Generator;
using dataMagus.Common.Physical;
#endregion

namespace dataMagus.Forms.Elements
{
	/// <summary>
	/// Extended data form for managing physical objectoriented associations.
	/// </summary>
	public partial class PhysicalObjectorientedAssociationForm : Form
	{
        #region Members
        /// <summary>
		/// Physical objectoriented assocation data the form is based on.
		/// </summary>
        private PhysicalObjectorientedAssociation _association = null;
        #endregion

        #region Constructors / Destructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public PhysicalObjectorientedAssociationForm()
		{
			InitializeComponent();
        }
        #endregion

        #region Properties
        /// <summary>
		/// Physical objectoriented assocation data the form is based on.
		/// </summary>
        public PhysicalObjectorientedAssociation Association
        {
            get { return this._association; }
            set { this._association = value; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
        private void OkButtonClick(object sender, EventArgs e)
		{
        	try
			{
	            if (!this.FormIsValidate())
	            {
	                return;
	            }
	
	            this._association.Name = this.nameTextBox.Text.Trim();
	            this._association.Rolename1 = this.rolename1TextBox.Text.Trim();
	            this._association.Rolename2 = this.rolename2TextBox.Text.Trim();
	
	            this.DialogResult = DialogResult.OK;
	            this.Close();
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		
        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
		private void CancelButtonClick(object sender, EventArgs e)
		{
			try
			{
	            this.DialogResult = DialogResult.Cancel;
	            this.Close();
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		
        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
		private void PhysicalObjectorientedAssociationFormLoad(object sender, EventArgs e)
        {
			try
			{
	            this.CleanData();
	            this.LoadData();
	            this.nameTextBox.Focus();
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}

		/// <summary>
		/// Clean the data on the form for primitive state.
		/// </summary>
        private void CleanData()
        {
            // clean
            this.nameTextBox.Clear();
            this.rolename1TextBox.Clear();
            this.rolename2TextBox.Clear();

            // fill generators
            List<IGenerator> generators = GeneratorManager.Instance.FindInterfaceType(typeof(IObjectorientedGenerator));
            this.generatorCombo.DisplayMember = "Name";
            this.generatorCombo.DataSource = generators;
        }

        /// <summary>
		/// Load the data into the form.
		/// </summary>
        private void LoadData()
        {
            if (null != this._association)
            {
                this.nameTextBox.Text = this._association.Name;
                this.rolename1TextBox.Text = this._association.Rolename1;
                this.rolename2TextBox.Text = this._association.Rolename2;
                this.class1TextBox.Text = this._association.Class1.ToString();
                this.class2TextBox.Text = this._association.Class2.ToString();
            }
        }

		/// <summary>
		/// Validate the data on the form and set the specified error provider for the
		/// invalide control.
		/// </summary>
		/// <returns>
		/// true indicates that the form is valid, false indicates that the form
		/// contains invalid data which sould not be used until corrected.
		/// </returns>
        private bool FormIsValidate()
        {
            bool isValid = true;
            this.errorProvider.Clear();

            if (0 == this.nameTextBox.Text.Trim().Length)
            {   //TODO: ins Resource-File
                this.errorProvider.SetError(this.nameTextBox, "Please fill in the name.");
                isValid = false;
            }

            if (0 == this.rolename1TextBox.Text.Trim().Length)
            {   //TODO: ins Resource-File
                this.errorProvider.SetError(this.nameTextBox, "Please fill in the rolename1.");
                isValid = false;
            }

            if (0 == this.rolename2TextBox.Text.Trim().Length)
            {   //TODO: ins Resource-File
                this.errorProvider.SetError(this.nameTextBox, "Please fill in the rolename2.");
                isValid = false;
            }

            return isValid;
        }

        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
        private void PhysicalObjectorientedAssociationForm_FormClosing(object sender, FormClosingEventArgs e)
        {
        	try
			{
	            List<IPhysicalSchema> currentSchemas = PhysicalSchemaManager.Instance.FindType(typeof(PhysicalObjectorientedSchema));
	            IPhysicalSchema clonedSchema = (IPhysicalSchema)currentSchemas[0].Clone();
	
	            List<IGenerator> generators = GeneratorManager.Instance.FindInterfaceType(typeof(IObjectorientedGenerator));
	            foreach (IGenerator generator in generators)
	            {
	                generator.CleanExtendedData(clonedSchema);
	            }
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
        }

        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
        private void generatorCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
        	try
			{
	            this.generatorPanel.Controls.Clear();
	            this.generatorPanel.Controls.Clear();
	            if (-1 != this.generatorCombo.SelectedIndex)
	            {
	                IObjectorientedGenerator generator = (IObjectorientedGenerator)this.generatorCombo.SelectedItem;
	
	                PhysicalObjectorientedAssociation cloned = (PhysicalObjectorientedAssociation)this._association.Clone();
	                // update clone with actual data from the form
	                cloned.Name = this.nameTextBox.Text;
	                cloned.Rolename1 = this.rolename1TextBox.Text;
	                cloned.Rolename2 = this.rolename2TextBox.Text;
	
	                GeneratorControl ctrl = generator.GetExtendedAssociationDataControl(cloned);
	                if (null != ctrl)
	                {
	                    ctrl.Dock = DockStyle.Fill;
	                    this.generatorPanel.Controls.Add(ctrl);
	                }
	            }
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
        }

        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
        	try
        	{
	            if (this.tabControl.SelectedTab == this.targetTab)
	            {
	                this.generatorCombo_SelectedIndexChanged(sender, e);
	            }
        	}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
        }
        #endregion
    }
}
