File size: 7,907 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace Microsoft.Management.UI.Internal
{
    /// <content>
    /// Partial class implementation for ListOrganizerItem control.
    /// </content>
    [SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")]
    public partial class ListOrganizerItem : Control
    {
        private string startingText;
        private FrameworkElement templatedParent;

        /// <summary>
        /// Creates a new instance of the ListOrganizerItem class.
        /// </summary>
        public ListOrganizerItem()
        {
            // empty
        }

        /// <summary>
        /// Gets a value indicating whether the item is in edit mode.
        /// </summary>
        public bool IsInEditMode
        {
            get
            {
                return (this.renameButton != null) ? this.renameButton.IsChecked.Value : false;
            }
        }

        /// <summary>
        /// Selects the current item.
        /// </summary>
        public void Select()
        {
            if (!this.IsLoaded)
            {
                this.Loaded += this.ListOrganizerItem_Loaded_SelectItem;
                this.ApplyTemplate();
                return;
            }

            CommandHelper.ExecuteCommand(this.linkButton.Command, this.linkButton.CommandParameter, this.linkButton.CommandTarget);
        }

        /// <summary>
        /// Allows modification of the item.
        /// </summary>
        public void Rename()
        {
            this.renameButton.IsChecked = true;
        }

        /// <summary>
        /// Deletes the item.
        /// </summary>
        public void Delete()
        {
            CommandHelper.ExecuteCommand(this.deleteButton.Command, this.deleteButton.CommandParameter, this.deleteButton.CommandTarget);
        }

        /// <summary>
        /// Provides class handling for the KeyDown routed event that
        /// occurs when the user presses a key while this control has focus.
        /// </summary>
        /// <param name="e">The event data.</param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (this.IsInEditMode)
            {
                return;
            }

            switch (e.Key)
            {
                case Key.Insert:
                    this.Rename();
                    e.Handled = true;
                    break;

                case Key.Delete:
                    this.Delete();
                    e.Handled = true;
                    break;

                case Key.Enter:
                case Key.Space:
                    this.Select();
                    e.Handled = true;
                    break;

                default:
                    break;
            }
        }

        private void TemplatedParent_OnKeyDown(object sender, KeyEventArgs e)
        {
            this.OnKeyDown(e);
        }

        private void ListOrganizerItem_Loaded_SelectItem(object sender, RoutedEventArgs e)
        {
            this.Loaded -= this.ListOrganizerItem_Loaded_SelectItem;
            this.Select();
        }

        #region EditBox Event Handlers

        private void EditBox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.ChangeFromEditToDisplayMode();
        }

        private void EditBox_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    this.ChangeFromEditToDisplayMode();
                    e.Handled = true;
                    break;

                case Key.Escape:
                    this.RevertTextAndChangeFromEditToDisplayMode();
                    e.Handled = true;
                    break;

                default:
                    break;
            }
        }

        private void RevertTextAndChangeFromEditToDisplayMode()
        {
            this.editBox.Text = this.startingText;
            this.ChangeFromEditToDisplayMode();
        }

        private void ChangeFromEditToDisplayMode()
        {
            // NOTE : This is to resolve a race condition where clicking
            // on the rename button causes the edit box to change and
            // then have re-toggle.
            DependencyObject d = Mouse.DirectlyOver as DependencyObject;
            if (d == null || !(this.renameButton.IsAncestorOf(d) && Mouse.LeftButton == MouseButtonState.Pressed))
            {
                this.renameButton.IsChecked = false;
            }

            if (!this.IsKeyboardFocusWithin)
            {
                this.Focus();
            }
        }

        private void EditBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.editBox.IsVisible)
            {
                this.startingText = this.editBox.Text;

                this.editBox.Focus();
                this.editBox.SelectAll();
            }
        }

        #endregion EditBox Event Handlers

        partial void OnTextContentPropertyNameChangedImplementation(PropertyChangedEventArgs<string> e)
        {
            this.UpdateTextContentBindings();
        }

        private void UpdateTextContentBindings()
        {
            if (!this.IsLoaded)
            {
                this.Loaded += this.ListOrganizerItem_Loaded_UpdateTextContentBindings;
                this.ApplyTemplate();
                return;
            }

            if (!string.IsNullOrEmpty(this.TextContentPropertyName))
            {
                Binding b = new Binding(this.TextContentPropertyName);
                b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

                this.linkButton.SetBinding(Button.ContentProperty, b);
                this.editBox.SetBinding(TextBox.TextProperty, b);
            }
            else
            {
                BindingOperations.ClearBinding(this.linkButton, Button.ContentProperty);
                BindingOperations.ClearBinding(this.editBox, TextBox.TextProperty);
            }
        }

        private void ListOrganizerItem_Loaded_UpdateTextContentBindings(object sender, RoutedEventArgs e)
        {
            this.Loaded -= this.ListOrganizerItem_Loaded_UpdateTextContentBindings;
            this.UpdateTextContentBindings();
        }

        #region OnApplyTemplate Helpers

        partial void PreOnApplyTemplate()
        {
            this.DetachFromVisualTree();
        }

        partial void PostOnApplyTemplate()
        {
            this.AttachToVisualTree();
        }

        private void AttachToVisualTree()
        {
            this.editBox.IsVisibleChanged += new DependencyPropertyChangedEventHandler(this.EditBox_IsVisibleChanged);
            this.editBox.KeyDown += this.EditBox_KeyDown;
            this.editBox.LostFocus += this.EditBox_LostFocus;

            this.templatedParent = this.TemplatedParent as FrameworkElement;
            if (this.templatedParent != null)
            {
                this.templatedParent.KeyDown += this.TemplatedParent_OnKeyDown;
            }
        }

        private void DetachFromVisualTree()
        {
            if (this.editBox != null)
            {
                this.editBox.IsVisibleChanged -= this.EditBox_IsVisibleChanged;
                this.editBox.KeyDown -= this.EditBox_KeyDown;
                this.editBox.LostFocus -= this.EditBox_LostFocus;
            }

            if (this.templatedParent != null)
            {
                this.templatedParent.KeyDown -= this.TemplatedParent_OnKeyDown;
            }
        }

        #endregion OnApplyTemplate Helpers
    }
}