This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Design / System.Web.UI.Design.WebControls / BaseDataListDesigner.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 /**
23  * Namespace:   System.Web.UI.Design.WebControls
24  * Class:       BaseDataListDesigner
25  *
26  * Author:      Gaurav Vaish
27  * Maintainer:  mastergaurav AT users DOT sf DOT net
28  *
29  * (C) Gaurav Vaish (2002)
30  */
31
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
36 using System.Reflection;
37 using System.Web.UI.Design;
38 using System.Web.UI;
39 using System.Web.UI.WebControls;
40 using System.Data;
41
42 namespace System.Web.UI.Design.WebControls
43 {
44         public abstract class BaseDataListDesigner : TemplatedControlDesigner,
45                                                      IDataSourceProvider
46         {
47                 private BaseDataList baseDataList;
48                 private DataTable    desTimeDataTable;
49                 private DataTable    dummyDataTable;
50
51                 private static readonly string[] validNames = new string[] {
52                         "AlternatingItemStyle",
53                         "BackColor",
54                         "DataSource",
55                         "DataMember",
56                         "EditItemStyle",
57                         "Font",
58                         "ForeColor",
59                         "HeaderStyle",
60                         "FooterStyle",
61                         "ItemStyle",
62                         "SelectedItemStyle",
63                         "SeparatorStyle"
64                 };
65
66                 public BaseDataListDesigner()
67                 {
68                 }
69
70                 public string DataKeyField
71                 {
72                         get
73                         {
74                                 return baseDataList.DataKeyField;
75                         }
76                         set
77                         {
78                                 baseDataList.DataKeyField = value;
79                         }
80                 }
81
82                 public string DataMember
83                 {
84                         get
85                         {
86                                 return baseDataList.DataMember;
87                         }
88                         set
89                         {
90                                 baseDataList.DataMember = value;
91                         }
92                 }
93
94                 public string DataSource
95                 {
96                         get
97                         {
98                                 DataBinding element = DataBindings["DataSource"];
99                                 if(element != null)
100                                 {
101                                         return element.Expression;
102                                 }
103                                 return String.Empty;
104                         }
105                         set
106                         {
107                                 if(value == null && value.Length == 0)
108                                 {
109                                         DataBindings.Remove("DataSource");
110                                 } else
111                                 {
112                                         DataBinding element = DataBindings["DataSource"];
113                                         if(element == null)
114                                         {
115                                                 element = new DataBinding("DataSource",
116                                                                           typeof(IEnumerable),
117                                                                           value);
118                                         } else
119                                         {
120                                                 element.Expression = value;
121                                         }
122                                         DataBindings.Add(element);
123                                 }
124                                 OnDataSourceChanged();
125                                 OnBindingsCollectionChanged("DataSource");
126                         }
127                 }
128
129                 protected internal virtual void OnDataSourceChanged()
130                 {
131                         desTimeDataTable = null;
132                 }
133
134                 public override bool DesignTimeHtmlRequiresLoadComplete
135                 {
136                         get
137                         {
138                                 return (DataSource.Length > 0);
139                         }
140                 }
141
142                 public override DesignerVerbCollection Verbs
143                 {
144                         get
145                         {
146                                 throw new NotImplementedException();
147                         }
148                 }
149
150                 public object GetSelectedDataSource()
151                 {
152                         object retVal = null;
153                         DataBinding element = DataBindings["DataSource"];
154                         if(element != null)
155                         {
156                                 retVal = DesignTimeData.GetSelectedDataSource(baseDataList,
157                                                                        element.Expression);
158                         }
159                         return retVal;
160                 }
161
162                 public IEnumerable GetResolvedSelectedDataSource()
163                 {
164                         IEnumerable retVal = null;
165                         DataBinding element = DataBindings["DataSource"];
166                         if(element != null)
167                         {
168                                 retVal = DesignTimeData.GetSelectedDataSource(baseDataList,
169                                                                         element.Expression,
170                                                                         DataMember);
171                         }
172                         return retVal;
173                 }
174
175                 public override IEnumerable GetTemplateContainerDataSource(
176                                                                string templateName)
177                 {
178                         return GetResolvedSelectedDataSource();
179                 }
180
181                 public override void Initialize(IComponent component)
182                 {
183                         baseDataList = (BaseDataList)component;
184                         base.Initialize(component);
185                 }
186
187                 public override void OnComponentChanged(object sender,
188                                                         ComponentChangedEventArgs e)
189                 {
190                         if(e.Member != null)
191                         {
192                                 string name = e.Member.Name;
193                                 foreach(string current in validNames)
194                                 {
195                                         if(name == current)
196                                         {
197                                                 OnStylesChanged();
198                                                 break;
199                                         }
200                                 }
201                         }
202                         base.OnComponentChanged(sender, e);
203                 }
204
205                 protected internal void OnStylesChanged()
206                 {
207                         OnTemplateEditingVerbsChanged();
208                 }
209
210                 protected abstract void OnTemplateEditingVerbsChanged();
211
212                 protected override void Dispose(bool disposing)
213                 {
214                         if(disposing)
215                                 baseDataList = null;
216                         base.Dispose(disposing);
217                 }
218
219                 protected IEnumerable GetDesignTimeDataSource(int minimumRows,
220                                                       out bool dummyDataSource)
221                 {
222                         return GetDesignTimeDataSource(GetResolvedSelectedDataSource(),
223                                                        minimumRows,
224                                                        out dummyDataSource);
225                 }
226
227                 protected IEnumerable GetDesignTimeDataSource(IEnumerable selectedDataSource,
228                                                               int minimumRows,
229                                                               out bool dummyDataSource)
230                 {
231                         DataTable toDeploy = desTimeDataTable;
232                         dummyDataSource = false;
233                         if(minimumRows == 0)
234                         {
235                                 if(selectedDataSource != null)
236                                 {
237                                         desTimeDataTable = DesignTimeData.CreateSampleDataTable(
238                                                                           selectedDataSource);
239                                         toDeploy = desTimeDataTable;
240                                 }
241                                 if(toDeploy == null)
242                                 {
243                                         if(dummyDataTable == null)
244                                                 dummyDataTable = DesignTimeData.CreateDummyDataTable();
245                                         toDeploy = dummyDataTable;
246                                         dummyDataSource = true;
247                                 }
248                         }
249                         return DesignTimeData.GetDesignTimeDataSource(toDeploy, minimumRows);
250                 }
251         }
252 }