Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CaseKeyBox.ViewModel.cs « Presentation « Core « Activities « System « System.Activities.Core.Presentation « System.Activities.Presentation « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ab276e3f51ecadecb0ce9bb19eca968016880bb8 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities.Core.Presentation
{
    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Linq;
    using System.ComponentModel;
    using System.Runtime;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Activities.Presentation.Model;

    class CaseKeyBoxViewModel : DependencyObject
    {
        static readonly string Null = "(null)";
        static readonly string Empty = "(empty)";

        public static readonly DependencyProperty ComboBoxIsEditableProperty =
            DependencyProperty.Register("ComboBoxIsEditable", typeof(bool), typeof(CaseKeyBoxViewModel), new UIPropertyMetadata(false));

        public static readonly DependencyProperty ComboBoxVisibilityProperty =
            DependencyProperty.Register("ComboBoxVisibility", typeof(Visibility), typeof(CaseKeyBoxViewModel), new UIPropertyMetadata(Visibility.Collapsed));

        public static readonly DependencyProperty ComboBoxItemsProperty =
            DependencyProperty.Register("ComboBoxItems", typeof(ObservableCollection<string>), typeof(CaseKeyBoxViewModel));

        public static readonly DependencyProperty DataTemplateNameProperty =
            DependencyProperty.Register("DataTemplateName", typeof(string), typeof(CaseKeyBoxViewModel), new UIPropertyMetadata("Label"));

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(CaseKeyBoxViewModel), new UIPropertyMetadata(String.Empty));

        public static readonly DependencyProperty TextBoxVisibilityProperty =
            DependencyProperty.Register("TextBoxVisibility", typeof(Visibility), typeof(CaseKeyBoxViewModel), new UIPropertyMetadata(Visibility.Visible));

        public const string BoxesTemplate = "Boxes";
        public const string LabelTemplate = "Label";

        string oldText = String.Empty;

        public CaseKeyBoxViewModel(ICaseKeyBoxView view)
        {
            this.View = view;
        }

        public bool ComboBoxIsEditable
        {
            get { return (bool)GetValue(ComboBoxIsEditableProperty); }
            set { SetValue(ComboBoxIsEditableProperty, value); }
        }

        public ObservableCollection<string> ComboBoxItems
        {
            get { return (ObservableCollection<string>)GetValue(ComboBoxItemsProperty); }
            set { SetValue(ComboBoxItemsProperty, value); }
        }

        public Visibility ComboBoxVisibility
        {
            get { return (Visibility)GetValue(ComboBoxVisibilityProperty); }
            set { SetValue(ComboBoxVisibilityProperty, value); }
        }

        public string DataTemplateName
        {
            get { return (string)GetValue(DataTemplateNameProperty); }
            set { SetValue(DataTemplateNameProperty, value); }
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public Visibility TextBoxVisibility
        {
            get { return (Visibility)GetValue(TextBoxVisibilityProperty); }
            set { SetValue(TextBoxVisibilityProperty, value); }
        }

        public bool IsBoxOnly
        {
            get;
            set;
        }

        public bool OnEnterPressed()
        {
            return this.CommitChanges();
        }

        public void OnEscapePressed()
        {
            this.Text = oldText;
            if (!this.IsBoxOnly)
            {
                this.DataTemplateName = CaseKeyBoxViewModel.LabelTemplate;
            }
            this.View.OnEditCancelled();
        }

        public void OnLabelGotFocus()
        {
            this.DataTemplateName = CaseKeyBoxViewModel.BoxesTemplate;
        }

        public bool OnLostFocus()
        {
            return CommitChanges();
        }

        public void OnValueChanged()
        {
            if (this.Value is ModelItem)
            {
                // Since Value is a DP, this code will trigger OnValueChanged once more.
                this.Value = ((ModelItem)this.Value).GetCurrentValue();
                return;
            }

            if (this.DataTemplateName != LabelTemplate && !this.IsBoxOnly)
            {
                this.DataTemplateName = LabelTemplate;
            }

            if (this.DisplayHintText)
            {
                this.Text = string.Empty;
                return;
            }
            if (this.ValueType == null)
            {
                return;
            }
            if (this.ValueType.IsValueType)
            {
                if (this.Value == null)
                {
                    this.Value = Activator.CreateInstance(this.ValueType);
                }
            }
            if (this.Value == null)
            {
                this.Text = Null;
            }
            else if ((this.ValueType == typeof(string)) && string.Equals(this.Value, String.Empty))
            {
                this.Text = Empty;
            }
            else
            {
                TypeConverter converter = XamlUtilities.GetConverter(this.ValueType);
                Fx.Assert(converter != null, "TypeConverter is not available");
                try
                {
                    this.Text = converter.ConvertToString(this.Value);
                }
                catch (ArgumentException)
                {
                    this.Text = this.Value.ToString();
                }
            }
        }

        public void OnValueTypeChanged()
        {
            if (this.ValueType == null)
            {
                return;
            }
            bool isBool = this.ValueType == typeof(bool);
            bool isEnum = this.ValueType.IsEnum;
            if (isBool || isEnum)
            {
                this.ComboBoxVisibility = Visibility.Visible;
                this.TextBoxVisibility = Visibility.Collapsed;
                this.ComboBoxIsEditable = false;
                if (isBool)
                {
                    this.ComboBoxItems = new ObservableCollection<string> { "True", "False" };
                }
                else
                {
                    this.ComboBoxItems = new ObservableCollection<string>(Enum.GetNames(this.ValueType).ToList());
                }
            }
            else if (this.ValueType.IsValueType)
            {
                this.ComboBoxVisibility = Visibility.Collapsed;
                this.TextBoxVisibility = Visibility.Visible;
                this.ComboBoxIsEditable = false;
            }
            else
            {
                this.ComboBoxVisibility = Visibility.Visible;
                this.TextBoxVisibility = Visibility.Collapsed;
                this.ComboBoxIsEditable = true;
                this.ComboBoxItems = new ObservableCollection<string> { Null };
                if (this.ValueType == typeof(string))
                {
                    this.ComboBoxItems.Add(Empty);
                }
            }
            OnValueChanged();
        }

        public void SaveOldText()
        {
            this.oldText = this.Text;
        }

        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
            Justification = "If conversion fails, the exception type is System.Exception.So we must catch all types of exceptions here.")]
        [SuppressMessage("Reliability", "Reliability108:IsFatalRule",
            Justification = "Catch all exceptions to prevent crash.")]
        bool CommitChanges()
        {
            object result = null;

            try
            {
                result = ResolveInputText();
            }
            catch
            {
                // ---- all
                Fx.Assert(false, "Result should have been valid. Preview event handler should have handled the validation.");
                return false;
            }

            this.Value = result;
            if (this.DataTemplateName != CaseKeyBoxViewModel.LabelTemplate && !this.IsBoxOnly)
            {
                // this is for the case when setting this.Value to null. It looks like
                // OnValueChanged won't get called because NULL is a default value for
                // the CaseKeyBox instance in SwitchDesigner.
                this.DataTemplateName = CaseKeyBoxViewModel.LabelTemplate;
            }
            this.View.OnValueCommitted();

            return true;
        }

        object ResolveInputText()
        {
            object result = null;
            if (this.ValueType == typeof(string))
            {
                if (this.Text.Equals(Null))
                {
                    result = null;
                }
                else if (this.Text.Equals(Empty))
                {
                    result = string.Empty;
                }
                else
                {
                    result = this.Text;
                }
            }
            else if (!this.ValueType.IsValueType && this.Text.Equals(Null))
            {
                result = null;
            }
            else
            {
                TypeConverter converter = XamlUtilities.GetConverter(this.ValueType);
                Fx.Assert(converter != null, "TypeConverter is not available");

                if (!converter.CanConvertFrom(typeof(string)) || !converter.CanConvertTo(typeof(string)))
                {
                    throw FxTrace.Exception.AsError(new NotSupportedException(SR.NotSupportedCaseKeyStringConversion));
                }

                result = converter.ConvertFromString(this.Text);
                // See if the result can be converted back to a string.
                // For example, we have a enum Color {Black, White}.
                // String "3" can be converted to integer 3, but integer 3
                // cannot be converted back to a valid string for enum Color.
                // In this case, we disallow string "3".
                converter.ConvertToString(result);
            }

            string reason;
            if (this.CaseKeyValidationCallback != null && !this.CaseKeyValidationCallback(result, out reason))
            {
                throw FxTrace.Exception.AsError(new ArgumentException(reason));
            }

            return result;
        }

        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
            Justification = "If conversion fails, the exception type is System.Exception.So we must catch all types of exceptions here.")]
        [SuppressMessage("Reliability", "Reliability108:IsFatalRule",
            Justification = "Catch all exceptions to prevent crash.")]
        public bool CanResolveInputText(out string reason)
        {
            reason = string.Empty;
            try
            {
                ResolveInputText();
                return true;
            }
            catch (Exception e)
            {
                reason = e.Message;
                return false;
            }
        }

        public bool TextHasBeenChanged()
        {
            string normalizedOldText = this.oldText;
            string normalizedNewText = this.Text;
            
            // Tricky: this.DisplayHintText = false => This CaseKeyBox is in CaseDesigner
            // Here, when changing value of string value type from "(empty)" to "", we must
            // consider the text hasn't been changed, such that we don't do commit-change.
            // We normalize the strings for empty-string situation before we do comparison.
            if (this.ValueType == typeof(string) && !this.DisplayHintText)
            {
                normalizedOldText = normalizedOldText == Empty ? string.Empty : normalizedOldText;
                normalizedNewText = normalizedNewText == Empty ? string.Empty : normalizedNewText;
            }

            return normalizedOldText != normalizedNewText;
        }

        ICaseKeyBoxView View { get; set; }

        bool DisplayHintText
        {
            get { return this.View.DisplayHintText; }
        }
        
        object Value
        {
            get { return this.View.Value; }
            set { this.View.Value = value; }
        }

        Type ValueType
        {
            get { return this.View.ValueType; }
        }

        CaseKeyValidationCallbackDelegate CaseKeyValidationCallback
        {
            get { return this.View.CaseKeyValidationCallback;  }
        }

        public void ResetText()
        {
            this.Text = string.Empty;
            this.oldText = string.Empty;
        }
    }
}