File: ObjectListCommandCollection.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (249 lines) | stat: -rw-r--r-- 8,492 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------------------------
// <copyright file="ObjectListCommandCollection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Permissions;

namespace System.Web.UI.MobileControls
{
    /*
     * Object List Command Collection class.
     *
     * Copyright (c) 2000 Microsoft Corporation
     */

    /// <include file='doc\ObjectListCommandCollection.uex' path='docs/doc[@for="ObjectListCommandCollection"]/*' />
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
    public class ObjectListCommandCollection : ArrayListCollectionBase, IStateManager
    {
        // ObjectListCommandCollection has a special form of viewstate management.
        // In normal operation, if you add, remove or modify commands in the 
        // collection, the results are saved as part of view state.
        //
        // However, when showing the commands for an item, the ObjectList control
        // raises an event that allows the application to alter the commands
        // specifically for the item. These changes are considered local to the
        // item, and are not saved.
        //
        // To do this, the class uses a private state, declared in the MarkState
        // enumeration below. The following sequence of events are used:
        //
        // 1) Class is created. MarkState is set to MarkState.NotMarked.
        // 2) Class is initialized from persistent values.
        // 3) Framework calls TrackViewState. MarkState is set to MarkState.MArked.
        // 4) Properties can be changed. Changes are reflected in viewstate.
        // 5) Just before raising the ShowItemCommands event, the ObjectList control
        //    calls GlobalStateSet. PreSaveViewState is called to create a 
        //    snapshot of viewstate, and MarkState is set to MarkState.PostMarked.
        // 6) If any changes are made after this, they will be ignored for view state
        //    purposes.
        // 7) The framework calls SaveViewState. Instead of the current view state
        //    (which would include the changes made in Step 6), the snapshot view state
        //     from step 5 is returned.

        private enum MarkState
        {
            NotMarked,
            Marked,
            PostMarked,
        }

        private MarkState _markState = MarkState.NotMarked;
        private bool _dirty;
        private String[] _savedState;

        internal ObjectListCommandCollection()
        {
        }

        /// <include file='doc\ObjectListCommandCollection.uex' path='docs/doc[@for="ObjectListCommandCollection.this"]/*' />
        public ObjectListCommand this[int index]
        {
            get
            {
                return (ObjectListCommand)Items[index];
            }
        }

        /// <include file='doc\ObjectListCommandCollection.uex' path='docs/doc[@for="ObjectListCommandCollection.Clear"]/*' />
        public void Clear()
        {
            foreach (ObjectListCommand command in Items)
            {
                command.Owner = null;
            }
            Items.Clear();
            SetDirty();
        }

        /// <include file='doc\ObjectListCommandCollection.uex' path='docs/doc[@for="ObjectListCommandCollection.Add"]/*' />
        public void Add(ObjectListCommand command)
        {
            AddAt(-1, command);
        }

        /// <include file='doc\ObjectListCommandCollection.uex' path='docs/doc[@for="ObjectListCommandCollection.AddAt"]/*' />
        public void AddAt(int index, ObjectListCommand command)
        {
            if (index == -1)
            {
                Items.Add(command);
            }
            else
            {
                Items.Insert(index, command);
            }
            command.Owner = this;
            SetDirty();
        }

        /// <include file='doc\ObjectListCommandCollection.uex' path='docs/doc[@for="ObjectListCommandCollection.Remove"]/*' />
        public void Remove(String s)
        {
            RemoveAt(IndexOf(s));
        }    

        /// <include file='doc\ObjectListCommandCollection.uex' path='docs/doc[@for="ObjectListCommandCollection.RemoveAt"]/*' />
        public void RemoveAt(int index)
        {
            (this[index]).Owner = null;
            Items.RemoveAt(index);
            SetDirty();
        }

        /// <include file='doc\ObjectListCommandCollection.uex' path='docs/doc[@for="ObjectListCommandCollection.IndexOf"]/*' />
        public int IndexOf(String s)
        {
            int index = 0;
            foreach (ObjectListCommand command in Items)
            {
                if (String.Compare(command.Name, s, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return index;
                }
                index++;
            }
            return -1;
        }

        internal void GlobalStateSet()
        {
            // Base commands have been set. From here, commands will only be
            // modified on a per-item, non-persistent basis.

            PreSaveViewState();
            _markState = MarkState.PostMarked;
        }

        internal void SetDirty()
        {
            if (_markState == MarkState.Marked)
            {
                _dirty = true;
            }
        }

        /////////////////////////////////////////////////////////////////////////
        //  STATE MANAGEMENT
        /////////////////////////////////////////////////////////////////////////

        private void PreSaveViewState()
        {
            if (_dirty)
            {
                ArrayList commands = Items;
                _savedState = new String[commands.Count * 2];
                int i = 0;
                foreach (ObjectListCommand command in commands)
                {
                    _savedState[i] = command.Name;
                    _savedState[i + 1] = command.Text;
                    i += 2;
                }
            }
            else
            {
                _savedState = null;
            }
        }

        /// <internalonly/>
        protected bool IsTrackingViewState
        {
            get
            {
                return _markState != MarkState.NotMarked;
            }
        }

        /// <internalonly/>
        protected void TrackViewState() 
        {
            _markState = MarkState.Marked;
        }

        /// <internalonly/>
        protected void LoadViewState(Object state) 
        {
            if (state != null)
            {
                String[] commandStates = (String[])state;
                int count = commandStates.Length;
                Clear();
                for (int i = 0; i < count; i += 2)
                {
                    Add(new ObjectListCommand(commandStates[i], commandStates[i + 1]));
                }
            }
        }

        /// <internalonly/>
        protected Object SaveViewState() 
        {
            if (_markState == MarkState.Marked)
            {
                PreSaveViewState();
            }
            return _savedState;
        }

        #region Implementation of IStateManager
        /// <internalonly/>
        bool IStateManager.IsTrackingViewState {
            get {
                return IsTrackingViewState;
            }
        }

        /// <internalonly/>
        void IStateManager.LoadViewState(object state) {
            LoadViewState(state);
        }

        /// <internalonly/>
        void IStateManager.TrackViewState() {
            TrackViewState();
        }

        /// <internalonly/>
        object IStateManager.SaveViewState() {
            return SaveViewState();
        }
        #endregion
    }
}