#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.Drawing;
using System.Windows.Forms;
using dataMagus.Common;
using dataMagus.Common.Logical;
#endregion

namespace dataMagus.Forms.Elements
{
	/// <summary>
	/// Extended data form for managing logical attributes.
	/// </summary>
	public partial class LogicalAttributeForm : Form
	{
		#region Members
		/// <summary>
		/// Logical attribute data the form is based on.
		/// </summary>
		private LogicalAttribute _attribute = null;
		#endregion

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

		#region Properties
		/// <summary>
		/// Logical attribute data the form is based on.
		/// </summary>
		public LogicalAttribute Attribute
		{
			get { return this._attribute; }
			set { this._attribute = value; }
		}
		#endregion

		#region Methodes
        /// <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;
				}
				
				string name = this.nameTextBox.Text.Trim();
				this._attribute.Name = name;
				
				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>
		/// Clean the data on the form for primitive state.
		/// </summary>
		private void CleanData()
		{
			this.nameTextBox.Text = String.Empty;
		}
		
		/// <summary>
		/// Load the data into the form.
		/// </summary>
		private void LoadData()
		{
			if (null != this._attribute)
			{
				this.nameTextBox.Text = this._attribute.Name;
			}
		}
		
		/// <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;
			if (0 == this.nameTextBox.Text.Trim().Length)
			{
				this.errorProvider.SetError(this.nameTextBox, "Please fill in the name.");
				isValid = false;
			}
			return isValid;
		}
		
        /// <summary>
        /// Event handler.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">EventArgs</param>
		private void LogicalAttributeFormLoad(object sender, EventArgs e)
		{
			try
			{
				this.CleanData();
				this.LoadData();
				this.nameTextBox.Focus();
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		#endregion
	}
}
