using System;
using System.Web.UI;
using System.Web.UI.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using Csla.Web;
namespace Csla.Web.Design
{
  /// 
  /// Implements designer support for CslaDataSource.
  /// 
  public class CslaDataSourceDesigner : DataSourceDesigner
  {
    private DataSourceControl _control = null;
    private CslaDesignerDataSourceView _view = null;
    /// 
    /// Initialize the designer component.
    /// 
    /// The CslaDataSource control to 
    /// be designed.
    public override void Initialize(IComponent component)
    {
      base.Initialize(component);
      _control = (DataSourceControl)component;
    }
    internal System.ComponentModel.ISite Site
    {
      get
      {
        return _control.Site;
      }
    }
    /// 
    /// Returns the default view for this designer.
    /// 
    /// Ignored
    /// 
    /// 
    /// This designer supports only a "Default" view.
    /// 
    public override DesignerDataSourceView GetView(string viewName)
    {
      if (_view == null)
      {
        _view = new CslaDesignerDataSourceView(this, "Default");
      }
      return _view;
    }
    /// 
    /// Return a list of available views.
    /// 
    /// 
    /// This designer supports only a "Default" view.
    /// 
    public override string[] GetViewNames()
    {
      return new string[] { "Default" };
    }
    /// 
    /// Refreshes the schema for the data.
    /// 
    /// 
    /// 
    public override void RefreshSchema(bool preferSilent)
    {
      this.OnSchemaRefreshed(EventArgs.Empty);
    }
    /// 
    /// Get a value indicating whether the control can
    /// refresh its schema.
    /// 
    public override bool CanRefreshSchema
    {
      get
      {
        return true;
      }
    }
    /// 
    /// Invoke the design time configuration
    /// support provided by the control.
    /// 
    public override void Configure()
    {
      InvokeTransactedChange(_control, ConfigureCallback, null, "ConfigureDataSource");
    }
    private bool ConfigureCallback(object context)
    {
      bool result = false;
      string oldTypeName;
      if (string.IsNullOrEmpty(((CslaDataSource)DataSourceControl).TypeAssemblyName))
        oldTypeName = ((CslaDataSource)DataSourceControl).TypeName;
      else
        oldTypeName = string.Format("{0}, {1}", 
          ((CslaDataSource)DataSourceControl).TypeName, ((CslaDataSource)DataSourceControl).TypeAssemblyName);
      IUIService uiService = (IUIService)_control.Site.GetService(typeof(IUIService));
      CslaDataSourceConfiguration cfg = new CslaDataSourceConfiguration(_control, oldTypeName);
      if (uiService.ShowDialog(cfg) == System.Windows.Forms.DialogResult.OK)
      {
        SuppressDataSourceEvents();
        try
        {
          ((CslaDataSource)DataSourceControl).TypeAssemblyName = string.Empty;
          ((CslaDataSource)DataSourceControl).TypeName = cfg.TypeName;
          OnDataSourceChanged(EventArgs.Empty);
          result = true;
        }
        finally
        {
          ResumeDataSourceEvents();
        }
      }
      cfg.Dispose();
      return result;
    }
    /// 
    /// Get a value indicating whether this control
    /// supports design time configuration.
    /// 
    public override bool CanConfigure
    {
      get
      {
        return true;
      }
    }
    /// 
    /// Get a value indicating whether the control can
    /// be resized.
    /// 
    public override bool AllowResize
    {
      get
      {
        return false;
      }
    }
    /// 
    /// Get a reference to the CslaDataSource control being
    /// designed.
    /// 
    internal CslaDataSource DataSourceControl
    {
      get
      {
        return (CslaDataSource)_control;
      }
    }
  }
}