fix build
[mono.git] / mcs / class / System.Design / System.ComponentModel.Design.Serialization / ComponentCodeDomSerializer.cs
1 //
2 // System.ComponentModel.Design.Serialization.ComponentCodeDomSerializer
3 //
4 // Authors:
5 //        Ivan N. Zlatev (contact i-nZ.net)
6 //
7 // (C) 2007 Ivan N. Zlatev
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
36
37 using System.CodeDom;
38
39 namespace System.ComponentModel.Design.Serialization
40 {
41         // A serializer for the IComponent Type, supplied by the CodeDomSerializationProvider,
42         // added as a provider by the RootComponentCodeDomSerializer
43         //
44         internal class ComponentCodeDomSerializer : CodeDomSerializer
45         {
46
47                 public ComponentCodeDomSerializer ()
48                 {
49                 }
50
51                 public override object Serialize (IDesignerSerializationManager manager, object value)
52                 {
53                         if (value == null)
54                                 throw new ArgumentNullException ("value");
55                         if (manager == null)
56                                 throw new ArgumentNullException ("manager");
57
58                         RootContext rootContext = manager.Context[typeof (RootContext)] as RootContext;
59                         if (rootContext != null && rootContext.Value == value)
60                                 return rootContext.Expression;
61
62                         CodeStatementCollection statements = new CodeStatementCollection ();
63
64                         if (((IComponent)value).Site == null) {
65                                 ReportError (manager, "Component of type '" + value.GetType().Name + "' not sited");
66                                 return statements;
67                         }
68
69                         // the trick with the nested components is that GetName will return the full name
70                         // e.g splitter1.Panel1 and thus the code below will create a reference to that.
71                         // 
72                         string name = manager.GetName (value);
73
74                         CodeExpression componentRef = null;
75                         if (rootContext != null)
76                                 componentRef = new CodeFieldReferenceExpression (rootContext.Expression , name);
77                         else
78                                 componentRef = new CodeFieldReferenceExpression (new CodeThisReferenceExpression () , name);
79
80                         base.SetExpression (manager, value, componentRef);
81
82                         ExpressionContext context = manager.Context[typeof (ExpressionContext)] as ExpressionContext;
83                         // Perform some heuristics here. 
84                         // 
85                         // If there is an ExpressionContext of PropertyReference where PresetValue == this
86                         // partial serialization doesn't make sense, so perform full. E.g in the case of:
87                         // 
88                         // PropertyCodeDomSerializer.SerializeContentProperty and splitContainer1.*Panel1* 
89                         //
90                         if (context == null || context.PresetValue != value ||
91                             (context.PresetValue == value && (context.Expression is CodeFieldReferenceExpression ||
92                                                               context.Expression is CodePropertyReferenceExpression))) {
93                                 bool isComplete = true;
94                                 statements.Add (new CodeCommentStatement (String.Empty));
95                                 statements.Add (new CodeCommentStatement (name));
96                                 statements.Add (new CodeCommentStatement (String.Empty));
97         
98                                 // Do not serialize a creation expression for Nested components
99                                 //
100                                 if (! (((IComponent)value).Site is INestedSite)) {
101                                         CodeStatement assignment = new CodeAssignStatement (componentRef, 
102                                                                         base.SerializeCreationExpression (manager, value, 
103                                                                                                           out isComplete));
104                                         assignment.UserData["statement-order"] = "initializer";
105                                         statements.Add (assignment);
106                                 }
107         
108                                 base.SerializeProperties (manager, statements, value, new Attribute[0]);
109                                 base.SerializeEvents (manager, statements, value);
110                         }
111
112                         return statements;
113                 }
114         }
115 }
116 #endif