#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.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using dataMagus.Common;
using dataMagus.Common.Logical;
using dataMagus.Common.Physical;
using dataMagus.Common.Physical.Objectoriented;
using dataMagus.Common.Physical.Relational;
using dataMagus.Forms.Handlers;
using dataMagus.Forms.Elements;
#endregion

namespace dataMagus.Forms.Controls
{
	/// <summary>
	/// Control to modify element data with.
	/// </summary>
	public partial class SchemaElementDataControl : UserControl
	{
		#region Constructors / Destructors
		/// <summary>
		/// Default constructor.
		/// </summary>
		public SchemaElementDataControl()
		{
			InitializeComponent();
		}
		#endregion
		
		#region Methodes
		/// <summary>
		/// Select the node found by a given element.
		/// </summary>
		/// <param name="element">Element to be selected.</param>
		public void SelectElement(object element)
		{
			dataPropertyGrid.SelectedObject = element;

			this.extendedButton.Enabled = true;
			if (null == element)
			{
				this.extendedButton.Enabled = false;
			}
		}
		
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		private void ExtendedButtonClick(object sender, EventArgs e)
		{
			try
			{
				if (dataPropertyGrid.SelectedObject is LogicalEntity)
				{
					LogicalEntityForm frm = new LogicalEntityForm();
					frm.Entity = (LogicalEntity)dataPropertyGrid.SelectedObject;
					frm.ShowDialog();
				}
				else if (dataPropertyGrid.SelectedObject is LogicalRelationship)
				{
					LogicalRelationshipForm frm = new LogicalRelationshipForm();
					frm.Relationship = (LogicalRelationship)dataPropertyGrid.SelectedObject;
					frm.ShowDialog();
				}
				else if (dataPropertyGrid.SelectedObject is LogicalAttribute)
				{
					LogicalAttributeForm frm = new LogicalAttributeForm();
					frm.Attribute = (LogicalAttribute)dataPropertyGrid.SelectedObject;
					frm.ShowDialog();
				}
				else if (dataPropertyGrid.SelectedObject is PhysicalRelationalTable)
				{
                    PhysicalRelationalTableForm frm = new PhysicalRelationalTableForm();
                    frm.Table = (PhysicalRelationalTable)dataPropertyGrid.SelectedObject;
                    frm.ShowDialog();
				}
				else if (dataPropertyGrid.SelectedObject is PhysicalRelationalRelationship)
				{
                    PhysicalRelationalRelationshipForm frm = new PhysicalRelationalRelationshipForm();
                    frm.Relationship = (PhysicalRelationalRelationship)dataPropertyGrid.SelectedObject;
                    frm.ShowDialog();
				}
				else if (dataPropertyGrid.SelectedObject is PhysicalRelationalColumn)
				{
					PhysicalRelationalColumnForm frm = new PhysicalRelationalColumnForm();
					frm.Column = (PhysicalRelationalColumn)dataPropertyGrid.SelectedObject;
					frm.ShowDialog();
				}
				else if (dataPropertyGrid.SelectedObject is PhysicalObjectorientedClass)
				{
                    PhysicalObjectorientedClassForm frm = new PhysicalObjectorientedClassForm();
                    frm.Class = (PhysicalObjectorientedClass)dataPropertyGrid.SelectedObject;
                    frm.ShowDialog();
				}
				else if (dataPropertyGrid.SelectedObject is PhysicalObjectorientedAssociation)
				{
                    PhysicalObjectorientedAssociationForm frm = new PhysicalObjectorientedAssociationForm();
                    frm.Association = (PhysicalObjectorientedAssociation)dataPropertyGrid.SelectedObject;
                    frm.ShowDialog();
				}
				else if (dataPropertyGrid.SelectedObject is PhysicalObjectorientedProperty)
				{
					PhysicalObjectorientedPropertyForm frm = new PhysicalObjectorientedPropertyForm();
					frm.Property = (PhysicalObjectorientedProperty)dataPropertyGrid.SelectedObject;
					frm.ShowDialog();
				}
                else if (dataPropertyGrid.SelectedObject is PhysicalRelationalIndex)
                {
                    PhysicalRelationalIndexForm frm = new PhysicalRelationalIndexForm();
                    frm.Index = (PhysicalRelationalIndex)dataPropertyGrid.SelectedObject;
                    frm.ShowDialog();
                }
                else if (dataPropertyGrid.SelectedObject is DataType)
                {
                    DataTypeForm frm = new DataTypeForm();
                    frm.ShowDialog();
                }
                else
                {
                    // no data - just disable
                    this.extendedButton.Enabled = false;
                }

				dataPropertyGrid.Refresh();
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		#endregion
	}
}
