2005-09-01 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CommandField.cs
1 //
2 // System.Web.UI.WebControls.CommandField.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Web.UI;
35 using System.ComponentModel;
36 using System.Security.Permissions;
37
38 namespace System.Web.UI.WebControls {
39
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         public class CommandField : ButtonFieldBase
43         {
44                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
45                 [UrlPropertyAttribute]
46                 [DefaultValueAttribute ("")]
47                 [WebSysDescription ("")]
48                 [WebCategoryAttribute ("Appearance")]
49                 public virtual string CancelImageUrl {
50                         get {
51                                 object ob = ViewState ["CancelImageUrl"];
52                                 if (ob != null) return (string) ob;
53                                 return "";
54                         }
55                         set {
56                                 ViewState ["CancelImageUrl"] = value;
57                                 OnFieldChanged ();
58                         }
59                 }
60
61                 [LocalizableAttribute (true)]
62                 [WebSysDescription ("")]
63                 [WebCategoryAttribute ("Appearance")]
64                 public virtual string CancelText {
65                         get {
66                                 object ob = ViewState ["CancelText"];
67                                 if (ob != null) return (string) ob;
68                                 return "Cancel";
69                         }
70                         set {
71                                 ViewState ["CancelText"] = value;
72                                 OnFieldChanged ();
73                         }
74                 }
75
76             [DefaultValueAttribute (true)]\r
77                 public override bool CausesValidation {
78                         get {
79                                 object ob = ViewState ["CausesValidation"];
80                                 if (ob != null) return (bool) ob;
81                                 return true;
82                         }
83                         set {
84                                 ViewState ["CausesValidation"] = value;
85                                 OnFieldChanged ();
86                         }
87                 }
88
89                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
90                 [UrlPropertyAttribute]
91                 [DefaultValueAttribute ("")]
92                 [WebSysDescription ("")]
93                 [WebCategoryAttribute ("Appearance")]
94                 public virtual string DeleteImageUrl {
95                         get {
96                                 object ob = ViewState ["DeleteImageUrl"];
97                                 if (ob != null) return (string) ob;
98                                 return "";
99                         }
100                         set {
101                                 ViewState ["DeleteImageUrl"] = value;
102                                 OnFieldChanged ();
103                         }
104                 }
105
106                 [LocalizableAttribute (true)]
107                 [WebSysDescription ("")]
108                 [WebCategoryAttribute ("Appearance")]
109                 public virtual string DeleteText {
110                         get {
111                                 object ob = ViewState ["DeleteText"];
112                                 if (ob != null) return (string) ob;
113                                 return "Delete";
114                         }
115                         set {
116                                 ViewState ["DeleteText"] = value;
117                                 OnFieldChanged ();
118                         }
119                 }
120
121                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
122                 [UrlPropertyAttribute]
123                 [DefaultValueAttribute ("")]
124                 [WebSysDescription ("")]
125                 [WebCategoryAttribute ("Appearance")]
126                 public virtual string EditImageUrl {
127                         get {
128                                 object ob = ViewState ["EditImageUrl"];
129                                 if (ob != null) return (string) ob;
130                                 return "";
131                         }
132                         set {
133                                 ViewState ["EditImageUrl"] = value;
134                                 OnFieldChanged ();
135                         }
136                 }
137
138                 [LocalizableAttribute (true)]
139                 [WebSysDescription ("")]
140                 [WebCategoryAttribute ("Appearance")]
141                 public virtual string EditText {
142                         get {
143                                 object ob = ViewState ["EditText"];
144                                 if (ob != null) return (string) ob;
145                                 return "Edit";
146                         }
147                         set {
148                                 ViewState ["EditText"] = value;
149                                 OnFieldChanged ();
150                         }
151                 }
152
153                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
154                 [UrlPropertyAttribute]
155                 [DefaultValueAttribute ("")]
156                 [WebSysDescription ("")]
157                 [WebCategoryAttribute ("Appearance")]
158                 public virtual string InsertImageUrl {
159                         get {
160                                 object ob = ViewState ["InsertImageUrl"];
161                                 if (ob != null) return (string) ob;
162                                 return "";
163                         }
164                         set {
165                                 ViewState ["InsertImageUrl"] = value;
166                                 OnFieldChanged ();
167                         }
168                 }
169
170                 [LocalizableAttribute (true)]
171                 [WebSysDescription ("")]
172                 [WebCategoryAttribute ("Appearance")]
173                 public virtual string InsertText {
174                         get {
175                                 object ob = ViewState ["InsertText"];
176                                 if (ob != null) return (string) ob;
177                                 return "Insert";
178                         }
179                         set {
180                                 ViewState ["InsertText"] = value;
181                                 OnFieldChanged ();
182                         }
183                 }
184
185                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
186                 [UrlPropertyAttribute]
187                 [DefaultValueAttribute ("")]
188                 [WebSysDescription ("")]
189                 [WebCategoryAttribute ("Appearance")]
190                 public virtual string NewImageUrl {
191                         get {
192                                 object ob = ViewState ["NewImageUrl"];
193                                 if (ob != null) return (string) ob;
194                                 return "";
195                         }
196                         set {
197                                 ViewState ["NewImageUrl"] = value;
198                                 OnFieldChanged ();
199                         }
200                 }
201
202                 [LocalizableAttribute (true)]
203                 [WebSysDescription ("")]
204                 [WebCategoryAttribute ("Appearance")]
205                 public virtual string NewText {
206                         get {
207                                 object ob = ViewState ["NewText"];
208                                 if (ob != null) return (string) ob;
209                                 return "New";
210                         }
211                         set {
212                                 ViewState ["NewText"] = value;
213                                 OnFieldChanged ();
214                         }
215                 }
216
217                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
218                 [UrlPropertyAttribute]
219                 [DefaultValueAttribute ("")]
220                 [WebSysDescription ("")]
221                 [WebCategoryAttribute ("Appearance")]
222                 public virtual string SelectImageUrl {
223                         get {
224                                 object ob = ViewState ["SelectImageUrl"];
225                                 if (ob != null) return (string) ob;
226                                 return "";
227                         }
228                         set {
229                                 ViewState ["SelectImageUrl"] = value;
230                                 OnFieldChanged ();
231                         }
232                 }
233
234                 [LocalizableAttribute (true)]
235                 [WebSysDescription ("")]
236                 [WebCategoryAttribute ("Appearance")]
237                 public virtual string SelectText {
238                         get {
239                                 object ob = ViewState ["SelectText"];
240                                 if (ob != null) return (string) ob;
241                                 return "Select";
242                         }
243                         set {
244                                 ViewState ["SelectText"] = value;
245                                 OnFieldChanged ();
246                         }
247                 }
248                 
249                 [DefaultValueAttribute (true)]
250                 [WebSysDescription ("")]
251                 [WebCategoryAttribute ("Behavior")]
252                 public virtual bool ShowCancelButton {
253                         get {
254                                 object ob = ViewState ["ShowCancelButton"];
255                                 if (ob != null) return (bool) ob;
256                                 return true;
257                         }
258                         set {
259                                 ViewState ["ShowCancelButton"] = value;
260                                 OnFieldChanged ();
261                         }
262                 }
263                 
264                 [DefaultValueAttribute (false)]
265                 [WebSysDescription ("")]
266                 [WebCategoryAttribute ("Behavior")]
267                 public virtual bool ShowDeleteButton {
268                         get {
269                                 object ob = ViewState ["ShowDeleteButton"];
270                                 if (ob != null) return (bool) ob;
271                                 return false;
272                         }
273                         set {
274                                 ViewState ["ShowDeleteButton"] = value;
275                                 OnFieldChanged ();
276                         }
277                 }
278                 
279                 [DefaultValueAttribute (false)]
280                 [WebSysDescription ("")]
281                 [WebCategoryAttribute ("Behavior")]
282                 public virtual bool ShowEditButton {
283                         get {
284                                 object ob = ViewState ["ShowEditButton"];
285                                 if (ob != null) return (bool) ob;
286                                 return false;
287                         }
288                         set {
289                                 ViewState ["ShowEditButton"] = value;
290                                 OnFieldChanged ();
291                         }
292                 }
293                 
294                 [DefaultValueAttribute (false)]
295                 [WebSysDescription ("")]
296                 [WebCategoryAttribute ("Behavior")]
297                 public virtual bool ShowSelectButton {
298                         get {
299                                 object ob = ViewState ["ShowSelectButton"];
300                                 if (ob != null) return (bool) ob;
301                                 return false;
302                         }
303                         set {
304                                 ViewState ["ShowSelectButton"] = value;
305                                 OnFieldChanged ();
306                         }
307                 }
308                 
309                 [DefaultValueAttribute (false)]
310                 [WebSysDescription ("")]
311                 [WebCategoryAttribute ("Behavior")]
312                 public virtual bool ShowInsertButton {
313                         get {
314                                 object ob = ViewState ["ShowInsertButton"];
315                                 if (ob != null) return (bool) ob;
316                                 return false;
317                         }
318                         set {
319                                 ViewState ["ShowInsertButton"] = value;
320                                 OnFieldChanged ();
321                         }
322                 }
323
324                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
325                 [UrlPropertyAttribute]
326                 [DefaultValueAttribute ("")]
327                 [WebSysDescription ("")]
328                 [WebCategoryAttribute ("Appearance")]
329                 public virtual string UpdateImageUrl {
330                         get {
331                                 object ob = ViewState ["UpdateImageUrl"];
332                                 if (ob != null) return (string) ob;
333                                 return "";
334                         }
335                         set {
336                                 ViewState ["UpdateImageUrl"] = value;
337                                 OnFieldChanged ();
338                         }
339                 }
340
341                 [LocalizableAttribute (true)]
342                 [WebSysDescription ("")]
343                 [WebCategoryAttribute ("Appearance")]
344                 public virtual string UpdateText {
345                         get {
346                                 object ob = ViewState ["UpdateText"];
347                                 if (ob != null) return (string) ob;
348                                 return "Update";
349                         }
350                         set {
351                                 ViewState ["UpdateText"] = value;
352                                 OnFieldChanged ();
353                         }
354                 }
355                 
356                 public override void InitializeCell (DataControlFieldCell cell,
357                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
358                 {
359                         string index = rowIndex.ToString ();
360                         
361                         if (cellType == DataControlCellType.DataCell)
362                         {
363                                 if ((rowState & DataControlRowState.Edit) != 0) {
364                                         cell.Controls.Add (CreateButton (UpdateText, UpdateImageUrl, DataControlCommands.UpdateCommandName, index));
365                                         if (ShowCancelButton) {
366                                                 AddSeparator (cell);
367                                                 cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));
368                                         }
369                                 } else if ((rowState & DataControlRowState.Insert) != 0) {
370                                         cell.Controls.Add (CreateButton (InsertText, InsertImageUrl, DataControlCommands.InsertCommandName, index));
371                                         if (ShowCancelButton) {
372                                                 AddSeparator (cell);
373                                                 cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));
374                                         }
375                                 } else {
376                                         if (ShowEditButton) {
377                                                 AddSeparator (cell);
378                                                 cell.Controls.Add (CreateButton (EditText, EditImageUrl, DataControlCommands.EditCommandName, index));
379                                         }
380                                         if (ShowDeleteButton) {
381                                                 AddSeparator (cell);
382                                                 cell.Controls.Add (CreateButton (DeleteText, DeleteImageUrl, DataControlCommands.DeleteCommandName, index));
383                                         }
384                                         if (ShowSelectButton) {
385                                                 AddSeparator (cell);
386                                                 cell.Controls.Add (CreateButton (SelectText, SelectImageUrl, DataControlCommands.SelectCommandName, index));
387                                         }
388                                         if (ShowInsertButton) {
389                                                 AddSeparator (cell);
390                                                 cell.Controls.Add (CreateButton (NewText, NewImageUrl, DataControlCommands.NewCommandName, index));
391                                         }
392                                 }
393                         } else
394                                 base.InitializeCell (cell, cellType, rowState, rowIndex);
395                 }
396                 
397                 DataControlButton CreateButton (string text, string image, string command, string arg)
398                 {
399                         DataControlButton c = new DataControlButton (Control, text, image, command, arg, false);
400                         c.CausesValidation = CausesValidation;
401                         return c;
402                 }
403                 
404                 void AddSeparator (DataControlFieldCell cell)
405                 {
406                         if (cell.Controls.Count > 0) {
407                                 Literal lit = new Literal ();
408                                 lit.Text = "&nbsp;";
409                                 cell.Controls.Add (lit);
410                         }
411                 }
412                 
413                 protected override DataControlField CreateField ()
414                 {
415                         return new CommandField ();
416                 }
417                 
418                 protected override void CopyProperties (DataControlField newField)
419                 {
420                         base.CopyProperties (newField);
421                         CommandField field = (CommandField) newField;
422                         field.CancelImageUrl = CancelImageUrl;
423                         field.CancelText = CancelText;
424                         field.DeleteImageUrl = DeleteImageUrl;
425                         field.DeleteText = DeleteText;
426                         field.EditImageUrl = EditImageUrl;
427                         field.EditText = EditText;
428                         field.InsertImageUrl = InsertImageUrl;
429                         field.InsertText = InsertText;
430                         field.NewImageUrl = NewImageUrl;
431                         field.NewText = NewText;
432                         field.SelectImageUrl = SelectImageUrl;
433                         field.SelectText = SelectText;
434                         field.ShowCancelButton = ShowCancelButton;
435                         field.ShowDeleteButton = ShowDeleteButton;
436                         field.ShowEditButton = ShowEditButton;
437                         field.ShowSelectButton = ShowSelectButton;
438                         field.ShowInsertButton = ShowInsertButton;
439                         field.UpdateImageUrl = UpdateImageUrl;
440                         field.UpdateText = UpdateText;
441                 }
442                 
443                 public override void ValidateSupportsCallback ()
444                 {
445                 }
446         }
447 }
448 #endif