summaryrefslogtreecommitdiff
path: root/Ivy/IvyApplicationBinding.cs
blob: 203e51fe7139f5861ccae98d11a89d696d119206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

namespace IvyBus
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Text;
    using System.Threading;
    using System.Text.RegularExpressions;
    using System.Collections.ObjectModel;

    /* This is the Application side of binding storage */
    /* association of a generated Key and a delegate and the expression */
    /* this is SEND to other client */

    public class IvyApplicationBinding 
    {
        private BindingType binding;

        // Argument formatting Expression to replace argument in in regular expresion binding
        // format %number[:objectFormat]%
        //    where number in argument index and objectFormat the ToString format passed to argument
        private Regex re = new Regex(@"%(\d+)(|:(.*))%");

        public BindingType Binding
        {
            get { return binding; }
            set { binding = value; }
        }
        private int key;

 
        public int Key
        {
            get { return key; }
            set { key = value; }
        }

        private object[] args;

        private string expression;

        public string Expression
        {
            get { return expression; }
            set { expression = value; }
        }
        private string formatted_expression;
        public string FormattedExpression
        {
            get
            { 
                FormatExpression();
                return formatted_expression; 
            }
        }

        private Collection<string> arguments;
        ///<summary>SentMessageClasses the first word token of sent messages
        ///<remarks> optimise the parsing process when sending messages </remarks>
        ///</summary>

        public Collection<string> Arguments
        {
            get
            {
                return arguments;
            }
        }
        public object[] CallbackArguments
        {
            get
            {
                return args;
            }
        }
        public event EventHandler<IvyMessageEventArgs> Callback;

        public IvyApplicationBinding()
            :this(BindingType.RegularExpression,string.Empty,null)
        {
          
        }
        internal IvyApplicationBinding(BindingType type, string expression,  params object[] args)
        {
            arguments = new Collection<string>();
            this.Binding = type;
            this.Expression = expression;
            this.args = args;
        }
        
        private string ReplaceArgs(Match m)
        {
            string s = string.Empty;
            // Get the matched string. convert to argindex
            int argindex = int.Parse(m.Groups[1].Value);
            //  use %... to specify formating
            object arg = args[argindex];
            IFormattable fmt = arg as IFormattable;
            if (fmt != null)
                s = fmt.ToString(m.Groups[3].Value, null);
            else if (arg != null)
                s = arg.ToString();

            return s;
        }
        // translate part of expression to object property
        public void FormatExpression()
        {
            //// Safely :
#if (!PocketPC)//TODO Pocket PC doesn't have Target Member
            EventHandler<IvyMessageEventArgs> temp = Callback;
            if (temp != null)
            {
                //TODO Pocket PC doesn't have Target Member
                object target = temp.Target;
               if (args == null)
                {
                    args = new object[arguments.Count];
                    for (int i = 0; i < arguments.Count; i++)
                    {
                        System.Reflection.PropertyInfo prop = target.GetType().GetProperty(arguments[i]);
                        if (prop != null)
                            args[i] = prop.GetValue(target, null);
                        else //TODO what else BUG msgbox in desing mode !!!
                            args[i] = arguments[i];
                    }
                }

               formatted_expression = re.Replace(expression, ReplaceArgs);
            }
            else //TODO Abnormal condition  Design Time 
#endif
            formatted_expression = expression;
            
        }

#if ( PocketPC )
        internal void Firevent(System.Windows.Forms.Control control, IvyMessageEventArgs e)
        {
            //// Safely invoke an event:
            EventHandler<IvyMessageEventArgs> temp = Callback;

            if (temp == null)
            {
                throw new IvyException("(callCallback) Not callback for id " + e.Id);
            }
            if (control != null)
            {
                control.Invoke(temp, this, e);
            }
            else
                temp(this, e);
        }
#else
        internal void Firevent(System.Threading.SynchronizationContext syncContext, bool synchronous , IvyMessageEventArgs e)
        {
            //// Safely invoke an event:
            EventHandler<IvyMessageEventArgs> temp = Callback;

            if (temp == null)
            {
                throw new IvyException("(callCallback) Not callback for id " + e.Id);
            }
            if (synchronous )
            {
                SendOrPostCallback update = delegate(object state)
                              {
                                  IvyMessageEventArgs args = (IvyMessageEventArgs)state;
                                  temp(this, args);
                              };
                syncContext.Post(update, e);
            }
            else
                temp(this, e);
        }

#endif
    }
  
}