ChangeLog: Updated ChangeLog.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BaseDataList.cs
index becfacc6bb253cbeef7c83be204988e6a2bf96a7..877a8de841abee74ca86d8e2211e881bd654be52 100755 (executable)
-/**\r
- * Namespace: System.Web.UI.WebControls\r
- * Class:     BaseDataList\r
- *\r
- * Author:  Gaurav Vaish\r
- * Maintainer: gvaish@iitk.ac.in\r
- * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>\r
- * Implementation: yes\r
- * Status:  20%\r
- *\r
- * (C) Gaurav Vaish (2001)\r
- */\r
+//
+// System.Web.UI.WebControls.BaseDataList.cs
+//
+// Authors:
+//   Gaurav Vaish (gvaish@iitk.ac.in)
+//   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+//
+// (C) Gaurav Vaish (2001)
+// (C) 2003 Andreas Nahr
+//\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 \r
 using System;\r
+using System.ComponentModel;
+using System.ComponentModel.Design;\r
 using System.Collections;\r
 using System.Web;\r
-using System.Web.UI;\r
+using System.Web.UI;
+using System.Web.Util;\r
 \r
 namespace System.Web.UI.WebControls\r
 {\r
+       [DefaultEvent("SelectedIndexChanged")]\r
+       [DefaultProperty("DataSource")]\r
+       [Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]\r
        public abstract class BaseDataList: WebControl\r
        {\r
+               private  static readonly object SelectedIndexChangedEvent = new object();\r
+               internal static string          ItemCountViewStateKey     = "_!ItemCount";\r
 \r
-               private int cellPadding = -1;\r
-               private int cellSpacing = 0;\r
-               private object dataSource = null;\r
-               private string dataKeyField = String.Empty;\r
-               private DataKeyCollection dataKeys;             // TODO: From where do get the values into it?\r
-               private string dataMember;\r
-               private GridLines gridLines;\r
-               private HorizontalAlign hAlign;\r
+               private DataKeyCollection dataKeys;\r
+               private object            dataSource;\r
 \r
-               public BaseDataList()\r
+               public BaseDataList() : base()\r
                {\r
-                       // TODO Something\r
-                       dataKeys = new DataKeyCollection(new ArrayList());\r
-                       dataMember = String.Empty;\r
-                       gridLines = GridLines.Both;\r
-                       hAlign = HorizontalAlign.NotSet;\r
                }\r
 \r
                public static bool IsBindableType(Type type)\r
                {\r
-                       //TODO: To see what has to be here\r
-                       if(type.IsPrimitive)\r
+                       if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))\r
+                               return true;\r
+                       return false;\r
+               }\r
+\r
+               public override ControlCollection Controls\r
+               {\r
+                       get\r
                        {\r
-                               //Type.GetTypeFromHandle(new RuntimeTypeHandle());\r
+                               EnsureChildControls();\r
+                               return base.Controls;\r
                        }\r
-                       return false; //for the time being, to be able to make it compile\r
                }\r
 \r
+               public override void DataBind()\r
+               {
+                       #if NET_2_0
+                       RequiresDataBinding = false;
+                       #endif\r
+                       OnDataBinding(EventArgs.Empty);\r
+               }\r
+\r
+               [WebCategory("Action")]\r
+               [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]\r
+               public event EventHandler SelectedIndexChanged\r
+               {\r
+                       add\r
+                       {\r
+                               Events.AddHandler(SelectedIndexChangedEvent, value);\r
+                       }\r
+                       remove\r
+                       {\r
+                               Events.RemoveHandler(SelectedIndexChangedEvent, value);\r
+                       }\r
+               }\r
+\r
+               [Bindable(true)]\r
+               [DefaultValue(-1)]\r
+               [WebCategory("Layout")]\r
+               [WebSysDescription("BaseDataList_CellPadding")]\r
                public virtual int CellPadding\r
                {\r
                        get\r
                        {\r
-                               return cellPadding;\r
+                               if(!ControlStyleCreated)\r
+                                       return -1;\r
+                               return ((TableStyle)ControlStyle).CellPadding;\r
                        }\r
                        set\r
                        {\r
-                               cellPadding = value;\r
+                               ((TableStyle)ControlStyle).CellPadding = value;\r
                        }\r
                }\r
 \r
+               [Bindable(true)]\r
+               [DefaultValue(-1)]\r
+               [WebCategory("Layout")]\r
+               [WebSysDescription("BaseDataList_CellSpacing")]\r
                public virtual int CellSpacing\r
                {\r
                        get\r
                        {\r
-                               return cellSpacing;\r
+                               if(!ControlStyleCreated)\r
+                                       return -1;\r
+                               return ((TableStyle)ControlStyle).CellSpacing;\r
                        }\r
                        set\r
                        {\r
-                               cellSpacing = value;\r
+                               ((TableStyle)ControlStyle).CellSpacing = value;\r
                        }\r
                }\r
 \r
+               [DefaultValue("")]\r
+               [WebCategory("Data")]\r
+               [WebSysDescription("BaseDataList_DataKeyField")]\r
                public virtual string DataKeyField\r
                {\r
                        get\r
                        {\r
-                               return dataKeyField;\r
+                               object o = ViewState["DataKeyField"];\r
+                               if(o!=null)\r
+                                       return (string)o;\r
+                               return String.Empty;\r
                        }\r
                        set\r
                        {\r
-                               dataKeyField = value;\r
+                               ViewState["DataKeyField"] = value;\r
                        }\r
                }\r
 \r
+               [Browsable(true)]\r
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
+               [WebSysDescription("BaseDataList_DataKeys")]\r
                public DataKeyCollection DataKeys\r
                {\r
                        get\r
                        {\r
+                               if( dataKeys==null )\r
+                                       dataKeys = new DataKeyCollection(DataKeysArray);\r
                                return dataKeys;\r
+\r
                        }\r
                }\r
 \r
+               [DefaultValue("")]\r
+               [WebCategory("Data")]\r
+               [WebSysDescription("BaseDataList_DataMember")]\r
                public string DataMember\r
                {\r
                        get\r
                        {\r
-                               return dataMember;\r
+                               object o = ViewState["DataMember"];\r
+                               if(o!=null)\r
+                                       return (string)o;\r
+                               return String.Empty;\r
                        }\r
                        set\r
                        {\r
-                               dataMember = value;\r
+                               ViewState["DataMember"] = value;\r
                        }\r
                }\r
 \r
-               public virtual object DataSource\r
+               [Bindable(true)]\r
+               [DefaultValue(null)]\r
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\r
+               [WebCategory("Data")]\r
+               [WebSysDescription("BaseDataList_DataSource")]\r
+               public virtual object DataSource {
+                       get {
+                               return dataSource;
+                       }
+                       set {
+                               if (value == null || value is IListSource || value is IEnumerable) {
+                                       dataSource = value;
+                               } else {
+                                       throw new ArgumentException (HttpRuntime.FormatResourceString (
+                                                               "Invalid_DataSource_Type", ID));
+                               }
+                       }
+               }
+
+               [Bindable(true)]\r
+               [DefaultValue(GridLines.Both)]\r
+               [WebCategory("Appearance")]\r
+               [WebSysDescription("BaseDataList_GridLines")]\r
+               public virtual GridLines GridLines\r
                {\r
                        get\r
                        {\r
-                               return dataSource;\r
+                               if(ControlStyleCreated)\r
+                                       return ((TableStyle)ControlStyle).GridLines;\r
+                               return GridLines.Both;\r
                        }\r
                        set\r
                        {\r
-                               dataSource = value;\r
+                               ((TableStyle)ControlStyle).GridLines = value;\r
                        }\r
-               }\r
+               }
+
+               // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
+               // but is defined incorrectly in the MS framework\r
 \r
-               public virtual GridLines GridLines\r
+               [Bindable(true)]\r
+               [DefaultValue(HorizontalAlign.NotSet)]\r
+               [Category("Layout")]\r
+               [WebSysDescription("BaseDataList_HorizontalAlign")]\r
+               public virtual HorizontalAlign HorizontalAlign\r
                {\r
                        get\r
                        {\r
-                               return gridLines;\r
+                               if(ControlStyleCreated)\r
+                                       return ((TableStyle)ControlStyle).HorizontalAlign;\r
+                               return HorizontalAlign.NotSet;\r
                        }\r
                        set\r
                        {\r
-                               gridLines = value;\r
+                               ((TableStyle)ControlStyle).HorizontalAlign = value;\r
                        }\r
                }\r
 \r
-               public virtual HorizontalAlign HorizontalAlign\r
+               protected ArrayList DataKeysArray\r
                {\r
                        get\r
                        {\r
-                               return hAlign;\r
-                       }\r
-                       set\r
-                       {\r
-                               hAlign = value;\r
+                               object o = ViewState["DataKeys"];\r
+                               if(o == null)\r
+                               {\r
+                                       o = new ArrayList();\r
+                                       ViewState["DataKeys"] = o;\r
+                               }\r
+                               return (ArrayList)o;\r
                        }\r
                }\r
 \r
-               public override void DataBind()\r
-               {\r
-                       // TODO: have to write the implementation\r
-                       // I am not sure of whether it will be of any use here since\r
-                       // I am an abstract class, and have no identity of myself.\r
-                       //dataBindEventArgs = EventArgs.Empty;\r
-                       OnDataBinding(EventArgs.Empty);\r
-                       throw new NotImplementedException();\r
-               }\r
-\r
-               //TODO: Check - where are the following abstract methods?\r
-               /*\r
-                * CreateControlHierarchy(bool)\r
-                * PrepareControlHierarchy()\r
-               */\r
-\r
                protected override void AddParsedSubObject(object o)\r
                {\r
-                       // Preventing literal controls from being added as children: Do nothing here.\r
+                       // Preventing literal controls from being added as children.\r
                }\r
 \r
                protected override void CreateChildControls()\r
                {\r
                        Controls.Clear();\r
-                       if(ViewState["_!ItemCount"]!=null)\r
+                       if(ViewState[ItemCountViewStateKey]!=null)\r
                        {\r
-                               CreateControlHierarchy(true);\r
+                               CreateControlHierarchy(false);\r
                                ClearChildViewState();\r
                        }\r
                }\r
 \r
-               protected abstract void CreateControlHierarchy(bool useDataSource);\r
+               protected override void OnDataBinding(EventArgs e)\r
+               {\r
+                       base.OnDataBinding(e);\r
+                       Controls.Clear();\r
+                       ClearChildViewState();\r
+                       CreateControlHierarchy(true);\r
+                       ChildControlsCreated = true;\r
+                       TrackViewState();\r
+               }\r
+\r
+               protected virtual void OnSelectedIndexChanged(EventArgs e)\r
+               {\r
+                       if(Events != null)\r
+                       {\r
+                               EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);\r
+                               if(eh!=null)\r
+                                       eh(this, e);\r
+                       }\r
+               }\r
+\r
+               protected override void Render(HtmlTextWriter writer)\r
+               {\r
+                       PrepareControlHierarchy();\r
+                       RenderContents(writer);\r
+               }\r
+\r
+               protected abstract void PrepareControlHierarchy();\r
+               protected abstract void CreateControlHierarchy(bool useDataSource);
+               
+               #if NET_2_0
+
+                       
+                       // should be `internal protected' (why, oh WHY did they do that !?!)
+                       protected override void OnInit (EventArgs e)
+                       {
+                               base.OnInit(e);
+                               inited = true;
+                               if (!Page.IsPostBack)
+                                       RequiresDataBinding = true;
+                       }
+                       
+                       // should be `internal protected' (why, oh WHY did they do that !?!)
+                       protected override void OnLoad (EventArgs e)
+                       {
+                               IDataSource ds = GetDataSourceObject () as IDataSource;
+                               if (ds != null && DataSourceID != "")
+                                       ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
+                               
+                               base.OnLoad(e);
+                       }
+                       
+                       // should be `internal protected' (why, oh WHY did they do that !?!)
+                       protected override void OnPreRender (EventArgs e)
+                       {
+                               EnsureDataBound ();
+                               base.OnPreRender (e);
+                       }
+                               
+                       protected void EnsureDataBound ()
+                       {
+                               if (RequiresDataBinding && DataSourceID != "")
+                                       DataBind ();
+                       }
+                       
+                       protected virtual object GetDataSourceObject ()
+                       {
+                               if (DataSourceID != "")
+                                       return (IDataSource) NamingContainer.FindControl (DataSourceID);
+                               
+                               return DataSource;
+                       }
+                       
+                       protected virtual IEnumerable GetResolvedDataSource ()
+                       {
+                               if (DataSource != null && DataSourceID != "")
+                                       throw new HttpException ();
+                               
+                               IDataSource ds = this.GetDataSourceObject () as IDataSource;
+                               if (ds != null && DataSourceID != "")
+                                       return ds.GetView (DataMember).Select ();
+                               else if (DataSource != null)
+                                       return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
+                               else
+                                       return null; 
+                       }
+                       
+                       protected void OnDataSourceChanged (object sender, EventArgs e)
+                       {
+                               RequiresDataBinding = true;
+                       }
+                       
+                       public virtual string DataSourceID {
+                               get {
+                                       object o = ViewState ["DataSourceID"];
+                                       if (o != null)
+                                               return (string)o;
+                                       
+                                       return String.Empty;
+                               }
+                               set {
+                                       if (inited)
+                                               RequiresDataBinding = true;
+                                       
+                                       ViewState ["DataSourceID"] = value;
+                               }
+                       }
+                       
+                       bool requiresDataBinding;
+                       protected bool RequiresDataBinding {
+                               get { return requiresDataBinding; }
+                               set { requiresDataBinding = value; }
+                       }
+                       
+                       protected bool inited;
+                               
+               #else
+                       internal IEnumerable GetResolvedDataSource ()
+                       {
+                               if (DataSource != null)
+                                       return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
+                               else
+                                       return null; 
+                       }
+               #endif\r
        }\r
 }\r