New test.
[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 { return ViewState.GetString ("CancelImageUrl", ""); }
51                         set {
52                                 ViewState ["CancelImageUrl"] = value;
53                                 OnFieldChanged ();
54                         }
55                 }
56
57                 [LocalizableAttribute (true)]
58                 [WebSysDescription ("")]
59                 [WebCategoryAttribute ("Appearance")]
60                 public virtual string CancelText {
61                         get { return ViewState.GetString ("CancelText", "Cancel"); }
62                         set {
63                                 ViewState ["CancelText"] = value;
64                                 OnFieldChanged ();
65                         }
66                 }
67
68                 [DefaultValueAttribute (true)]
69                 public override bool CausesValidation {
70                         get { return ViewState.GetBool ("CausesValidation", true); }
71                         set {
72                                 ViewState ["CausesValidation"] = value;
73                                 OnFieldChanged ();
74                         }
75                 }
76
77                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
78                 [UrlPropertyAttribute]
79                 [DefaultValueAttribute ("")]
80                 [WebSysDescription ("")]
81                 [WebCategoryAttribute ("Appearance")]
82                 public virtual string DeleteImageUrl {
83                         get { return ViewState.GetString ("DeleteImageUrl", ""); }
84                         set {
85                                 ViewState ["DeleteImageUrl"] = value;
86                                 OnFieldChanged ();
87                         }
88                 }
89
90                 [LocalizableAttribute (true)]
91                 [WebSysDescription ("")]
92                 [WebCategoryAttribute ("Appearance")]
93                 public virtual string DeleteText {
94                         get { return ViewState.GetString ("DeleteText", "Delete"); }
95                         set {
96                                 ViewState ["DeleteText"] = value;
97                                 OnFieldChanged ();
98                         }
99                 }
100
101                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
102                 [UrlPropertyAttribute]
103                 [DefaultValueAttribute ("")]
104                 [WebSysDescription ("")]
105                 [WebCategoryAttribute ("Appearance")]
106                 public virtual string EditImageUrl {
107                         get { return ViewState.GetString ("EditImageUrl", ""); }
108                         set {
109                                 ViewState ["EditImageUrl"] = value;
110                                 OnFieldChanged ();
111                         }
112                 }
113
114                 [LocalizableAttribute (true)]
115                 [WebSysDescription ("")]
116                 [WebCategoryAttribute ("Appearance")]
117                 public virtual string EditText {
118                         get { return ViewState.GetString ("EditText", "Edit"); }
119                         set {
120                                 ViewState ["EditText"] = value;
121                                 OnFieldChanged ();
122                         }
123                 }
124
125                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
126                 [UrlPropertyAttribute]
127                 [DefaultValueAttribute ("")]
128                 [WebSysDescription ("")]
129                 [WebCategoryAttribute ("Appearance")]
130                 public virtual string InsertImageUrl {
131                         get { return ViewState.GetString ("InsertImageUrl", ""); }
132                         set {
133                                 ViewState ["InsertImageUrl"] = value;
134                                 OnFieldChanged ();
135                         }
136                 }
137
138                 [LocalizableAttribute (true)]
139                 [WebSysDescription ("")]
140                 [WebCategoryAttribute ("Appearance")]
141                 public virtual string InsertText {
142                         get { return ViewState.GetString ("InsertText", "Insert"); }
143                         set {
144                                 ViewState ["InsertText"] = value;
145                                 OnFieldChanged ();
146                         }
147                 }
148
149                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
150                 [UrlPropertyAttribute]
151                 [DefaultValueAttribute ("")]
152                 [WebSysDescription ("")]
153                 [WebCategoryAttribute ("Appearance")]
154                 public virtual string NewImageUrl {
155                         get { return ViewState.GetString ("NewImageUrl", ""); }
156                         set {
157                                 ViewState ["NewImageUrl"] = value;
158                                 OnFieldChanged ();
159                         }
160                 }
161
162                 [LocalizableAttribute (true)]
163                 [WebSysDescription ("")]
164                 [WebCategoryAttribute ("Appearance")]
165                 public virtual string NewText {
166                         get { return ViewState.GetString ("NewText", "New"); }
167                         set {
168                                 ViewState ["NewText"] = value;
169                                 OnFieldChanged ();
170                         }
171                 }
172
173                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
174                 [UrlPropertyAttribute]
175                 [DefaultValueAttribute ("")]
176                 [WebSysDescription ("")]
177                 [WebCategoryAttribute ("Appearance")]
178                 public virtual string SelectImageUrl {
179                         get { return ViewState.GetString ("SelectImageUrl", ""); }
180                         set {
181                                 ViewState ["SelectImageUrl"] = value;
182                                 OnFieldChanged ();
183                         }
184                 }
185
186                 [LocalizableAttribute (true)]
187                 [WebSysDescription ("")]
188                 [WebCategoryAttribute ("Appearance")]
189                 public virtual string SelectText {
190                         get { return ViewState.GetString ("SelectText", "Select"); }
191                         set {
192                                 ViewState ["SelectText"] = value;
193                                 OnFieldChanged ();
194                         }
195                 }
196                 
197                 [DefaultValueAttribute (true)]
198                 [WebSysDescription ("")]
199                 [WebCategoryAttribute ("Behavior")]
200                 public virtual bool ShowCancelButton {
201                         get { return ViewState.GetBool ("ShowCancelButton", true); }
202                         set {
203                                 ViewState ["ShowCancelButton"] = value;
204                                 OnFieldChanged ();
205                         }
206                 }
207                 
208                 [DefaultValueAttribute (false)]
209                 [WebSysDescription ("")]
210                 [WebCategoryAttribute ("Behavior")]
211                 public virtual bool ShowDeleteButton {
212                         get { return ViewState.GetBool ("ShowDeleteButton", false); }
213                         set {
214                                 ViewState ["ShowDeleteButton"] = value;
215                                 OnFieldChanged ();
216                         }
217                 }
218                 
219                 [DefaultValueAttribute (false)]
220                 [WebSysDescription ("")]
221                 [WebCategoryAttribute ("Behavior")]
222                 public virtual bool ShowEditButton {
223                         get { return ViewState.GetBool ("ShowEditButton", false); }
224                         set {
225                                 ViewState ["ShowEditButton"] = value;
226                                 OnFieldChanged ();
227                         }
228                 }
229                 
230                 [DefaultValueAttribute (false)]
231                 [WebSysDescription ("")]
232                 [WebCategoryAttribute ("Behavior")]
233                 public virtual bool ShowSelectButton {
234                         get { return ViewState.GetBool ("ShowSelectButton", false); }
235                         set {
236                                 ViewState ["ShowSelectButton"] = value;
237                                 OnFieldChanged ();
238                         }
239                 }
240                 
241                 [DefaultValueAttribute (false)]
242                 [WebSysDescription ("")]
243                 [WebCategoryAttribute ("Behavior")]
244                 public virtual bool ShowInsertButton {
245                         get { return ViewState.GetBool ("ShowInsertButton", false); }
246                         set {
247                                 ViewState ["ShowInsertButton"] = value;
248                                 OnFieldChanged ();
249                         }
250                 }
251
252                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
253                 [UrlPropertyAttribute]
254                 [DefaultValueAttribute ("")]
255                 [WebSysDescription ("")]
256                 [WebCategoryAttribute ("Appearance")]
257                 public virtual string UpdateImageUrl {
258                         get { return ViewState.GetString ("UpdateImageUrl", ""); }
259                         set {
260                                 ViewState ["UpdateImageUrl"] = value;
261                                 OnFieldChanged ();
262                         }
263                 }
264
265                 [LocalizableAttribute (true)]
266                 [WebSysDescription ("")]
267                 [WebCategoryAttribute ("Appearance")]
268                 public virtual string UpdateText {
269                         get { return ViewState.GetString ("UpdateText", "Update"); }
270                         set {
271                                 ViewState ["UpdateText"] = value;
272                                 OnFieldChanged ();
273                         }
274                 }
275                 
276                 public override void InitializeCell (DataControlFieldCell cell,
277                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
278                 {
279                         string index = rowIndex.ToString ();
280                         
281                         if (cellType == DataControlCellType.DataCell)
282                         {
283                                 if ((rowState & DataControlRowState.Edit) != 0) {
284                                         cell.Controls.Add (CreateButton (UpdateText, UpdateImageUrl, DataControlCommands.UpdateCommandName, index));
285                                         if (ShowCancelButton) {
286                                                 AddSeparator (cell);
287                                                 cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));
288                                         }
289                                 } else if ((rowState & DataControlRowState.Insert) != 0) {
290                                         cell.Controls.Add (CreateButton (InsertText, InsertImageUrl, DataControlCommands.InsertCommandName, index));
291                                         if (ShowCancelButton) {
292                                                 AddSeparator (cell);
293                                                 cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));
294                                         }
295                                 } else {
296                                         if (ShowEditButton) {
297                                                 AddSeparator (cell);
298                                                 cell.Controls.Add (CreateButton (EditText, EditImageUrl, DataControlCommands.EditCommandName, index));
299                                         }
300                                         if (ShowDeleteButton) {
301                                                 AddSeparator (cell);
302                                                 cell.Controls.Add (CreateButton (DeleteText, DeleteImageUrl, DataControlCommands.DeleteCommandName, index));
303                                         }
304                                         if (ShowInsertButton) {
305                                                 AddSeparator (cell);
306                                                 cell.Controls.Add (CreateButton (NewText, NewImageUrl, DataControlCommands.NewCommandName, index));
307                                         }
308                                         if (ShowSelectButton) {
309                                                 AddSeparator (cell);
310                                                 cell.Controls.Add (CreateButton (SelectText, SelectImageUrl, DataControlCommands.SelectCommandName, index));
311                                         }
312                                 }
313                         } else
314                                 base.InitializeCell (cell, cellType, rowState, rowIndex);
315                 }
316                 
317                 DataControlButton CreateButton (string text, string image, string command, string arg)
318                 {
319                         DataControlButton c = new DataControlButton (Control, text, image, command, arg, false);
320                         c.CausesValidation = CausesValidation;
321                         return c;
322                 }
323                 
324                 void AddSeparator (DataControlFieldCell cell)
325                 {
326                         if (cell.Controls.Count > 0) {
327                                 Literal lit = new Literal ();
328                                 lit.Text = " ";
329                                 cell.Controls.Add (lit);
330                         }
331                 }
332                 
333                 protected override DataControlField CreateField ()
334                 {
335                         return new CommandField ();
336                 }
337                 
338                 protected override void CopyProperties (DataControlField newField)
339                 {
340                         base.CopyProperties (newField);
341                         CommandField field = (CommandField) newField;
342                         field.CancelImageUrl = CancelImageUrl;
343                         field.CancelText = CancelText;
344                         field.DeleteImageUrl = DeleteImageUrl;
345                         field.DeleteText = DeleteText;
346                         field.EditImageUrl = EditImageUrl;
347                         field.EditText = EditText;
348                         field.InsertImageUrl = InsertImageUrl;
349                         field.InsertText = InsertText;
350                         field.NewImageUrl = NewImageUrl;
351                         field.NewText = NewText;
352                         field.SelectImageUrl = SelectImageUrl;
353                         field.SelectText = SelectText;
354                         field.ShowCancelButton = ShowCancelButton;
355                         field.ShowDeleteButton = ShowDeleteButton;
356                         field.ShowEditButton = ShowEditButton;
357                         field.ShowSelectButton = ShowSelectButton;
358                         field.ShowInsertButton = ShowInsertButton;
359                         field.UpdateImageUrl = UpdateImageUrl;
360                         field.UpdateText = UpdateText;
361                 }
362                 
363                 public override void ValidateSupportsCallback ()
364                 {\r
365                         if (ShowSelectButton)\r
366                                 throw new NotSupportedException ("ShowSelectButton = true");
367                 }
368         }
369 }
370 #endif