[NUI] Add Tizen.NUI.XamlBuild module
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / public / EXamlBuild / EXaml / EXamlAddEvent.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using Mono.Cecil;
18 using System;
19 using System.Collections.Generic;
20 using System.IO;
21 using System.Reflection;
22 using System.Text;
23 using Tizen.NUI.Binding;
24 using Tizen.NUI.EXaml.Build.Tasks;
25 using Tizen.NUI.Xaml.Build.Tasks;
26
27 namespace Tizen.NUI.EXaml
28 {
29     //use ##
30     internal class EXamlAddEvent : EXamlOperation
31     {
32         internal override string Write()
33         {
34             if (false == Instance.IsValid)
35             {
36                 return "";
37             }
38
39             string ret = "";
40
41             ret += String.Format("({0} ({1} {2} {3} {4}))\n",
42                                 eXamlContext.GetValueString((int)EXamlOperationType.AddEvent),
43                                 eXamlContext.GetValueString(Instance.Index),
44                                 eXamlContext.GetValueString(Element.Index),
45                                 eXamlContext.GetValueString(eXamlContext.definedEvents.GetIndex(eventDef.DeclaringType, eventDef)),
46                                 eXamlContext.GetValueString(eXamlContext.definedMethods.GetIndex(Value.DeclaringType, Value)));
47
48             return ret;
49         }
50
51         public EXamlAddEvent(EXamlContext context, EXamlCreateObject instance, EXamlCreateObject element, string eventName, MethodDefinition value)
52             : base(context)
53         {
54             TypeReference typeref;
55             var eventDef = instance.Type.GetEvent(fi=>fi.Name==eventName, out typeref);
56             if (null != eventDef)
57             {
58                 Instance = instance;
59                 Element = element;
60                 Value = value;
61                 DeclaringType = typeref;
62
63                 this.eventDef = eventDef;
64
65                 Instance.AddEvent(DeclaringType, eventDef);
66
67                 eXamlContext.eXamlOperations.Add(this);
68                 eXamlContext.eXamlAddEventList.Add(this);
69             }
70             else
71             {
72                 throw new Exception("Property is not element");
73             }
74         }
75
76         internal EXamlCreateObject Instance
77         {
78             get;
79         }
80
81         internal EXamlCreateObject Element
82         {
83             get;
84         }
85
86         internal TypeReference DeclaringType
87         {
88             get;
89         }
90
91         internal MethodDefinition Value
92         {
93             get;
94         }
95
96         private EventDefinition eventDef;
97     }
98 }