File: LayoutEditorPart.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (271 lines) | stat: -rw-r--r-- 10,673 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//------------------------------------------------------------------------------
// <copyright file="LayoutEditorPart.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls.WebParts {

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Globalization;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public sealed class LayoutEditorPart : EditorPart {

        private DropDownList _chromeState;
        private DropDownList _zone;
        // 

        private TextBox _zoneIndex;

        private string _chromeStateErrorMessage;
        private string _zoneIndexErrorMessage;

        private const int TextBoxColumns = 10;
        private const int MinZoneIndex = 0;

        private bool CanChangeChromeState {
            get {
                WebPart webPart = WebPartToEdit;
                if (!webPart.Zone.AllowLayoutChange) {
                    return false;
                }
                else if (!webPart.AllowMinimize) {
                    // If AllowMinimize is false, the dropdown can be used to restore the WebPart
                    // but not to minimize the WebPart.
                    return (webPart.ChromeState == PartChromeState.Minimized);
                }
                else {
                    return true;
                }
            }
        }

        private bool CanChangeZone {
            get {
                WebPart webPart = WebPartToEdit;
                WebPartZoneBase currentZone = webPart.Zone;
                return (currentZone.AllowLayoutChange && webPart.AllowZoneChange);
            }
        }

        private bool CanChangeZoneIndex {
            get {
                return WebPartToEdit.Zone.AllowLayoutChange;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), Themeable(false)]
        public override string DefaultButton {
            get { return base.DefaultButton; }
            set { base.DefaultButton = value; }
        }

        public override bool Display {
            get {
                // Don't return base.Display, since we want to show the layout editor part even
                // if AllowEdit is false on the WebPartToEdit, or the WebPartToEdit is a ProxyWebPart.
                return true;
            }
        }

        private bool HasError {
            get {
                return (_chromeStateErrorMessage != null || _zoneIndexErrorMessage != null);
            }
        }

        [
        WebSysDefaultValue(SR.LayoutEditorPart_PartTitle),
        ]
        public override string Title {
            get {
                string s = (string)ViewState["Title"];
                return (s != null) ? s : SR.GetString(SR.LayoutEditorPart_PartTitle);
            }
            set {
                ViewState["Title"] = value;
            }
        }

        public override bool ApplyChanges() {
            WebPart webPart = WebPartToEdit;
            if (webPart != null) {
                EnsureChildControls();

                try {
                    if (CanChangeChromeState) {
                        TypeConverter chromeStateConverter = TypeDescriptor.GetConverter(typeof(PartChromeState));
                        webPart.ChromeState = (PartChromeState)chromeStateConverter.ConvertFromString(_chromeState.SelectedValue);
                    }
                }
                catch (Exception e) {
                    _chromeStateErrorMessage = CreateErrorMessage(e.Message);
                }

                int zoneIndex = webPart.ZoneIndex;
                if (CanChangeZoneIndex) {
                    if (Int32.TryParse(_zoneIndex.Text, NumberStyles.Integer, CultureInfo.CurrentCulture, out zoneIndex)) {
                        if (zoneIndex < MinZoneIndex) {
                            _zoneIndexErrorMessage = SR.GetString(SR.EditorPart_PropertyMinValue, MinZoneIndex.ToString(CultureInfo.CurrentCulture));
                        }
                    }
                    else {
                        _zoneIndexErrorMessage = SR.GetString(SR.EditorPart_PropertyMustBeInteger);
                    }
                }

                WebPartZoneBase oldZone = webPart.Zone;
                WebPartZoneBase newZone = oldZone;
                if (CanChangeZone) {
                    newZone = WebPartManager.Zones[_zone.SelectedValue];
                }

                // Do not call MoveWebPart if the WebPart is currently in the correct position (VSWhidbey 374634)
                if (_zoneIndexErrorMessage == null && oldZone.AllowLayoutChange && newZone.AllowLayoutChange &&
                    (webPart.Zone != newZone || webPart.ZoneIndex != zoneIndex)) {
                    try {
                        WebPartManager.MoveWebPart(webPart, newZone, zoneIndex);
                    }
                    catch (Exception e) {
                        // Zone and ZoneIndex are set at the same time.  Use the _zoneIndexErrorMessage, since it is
                        // more likely that a bogus ZoneIndex would cause an error.
                        _zoneIndexErrorMessage = CreateErrorMessage(e.Message);
                    }
                }
            }

            return !HasError;
        }

        protected internal override void CreateChildControls() {
            ControlCollection controls = Controls;
            controls.Clear();

            TypeConverter chromeStateConverter = TypeDescriptor.GetConverter(typeof(PartChromeState));
            _chromeState = new DropDownList();
            _chromeState.Items.Add(new ListItem(SR.GetString(SR.PartChromeState_Normal),
                                               chromeStateConverter.ConvertToString(PartChromeState.Normal)));
            _chromeState.Items.Add(new ListItem(SR.GetString(SR.PartChromeState_Minimized),
                                               chromeStateConverter.ConvertToString(PartChromeState.Minimized)));
            controls.Add(_chromeState);

            // Add all zones to dropdown in CreateChildControls.  Items will be selected and/or disabled
            // in SyncChanges.  Assumes no Zones are added or removed during page execution.
            _zone = new DropDownList();
            WebPartManager manager = WebPartManager;
            if (manager != null) {
                WebPartZoneCollection zones = manager.Zones;
                if (zones != null) {
                    foreach (WebPartZoneBase zone in zones) {
                        ListItem item = new ListItem(zone.DisplayTitle, zone.ID);
                        _zone.Items.Add(item);
                    }
                }
            }
            controls.Add(_zone);

            _zoneIndex = new TextBox();
            _zoneIndex.Columns = TextBoxColumns;
            controls.Add(_zoneIndex);

            // We don't need viewstate enabled on our child controls.  Disable for perf.
            foreach (Control c in controls) {
                c.EnableViewState = false;
            }
        }

        protected internal override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);

            // We want to synchronize the EditorPart to the state of the WebPart on every page load,
            // so we stay current if the WebPart changes in the background.
            if (Display && Visible && !HasError) {
                SyncChanges();
            }
        }

        protected internal override void RenderContents(HtmlTextWriter writer) {
            if (Page != null) {
                Page.VerifyRenderingInServerForm(this);
            }

            // HACK: Need this for child controls to be created at design-time when control is inside template
            EnsureChildControls();

            if (DesignMode) {
                // Add sample zone to dropdown
                _zone.Items.Add(SR.GetString(SR.Zone_SampleHeaderText));
            }

            string[] propertyDisplayNames = new string[] {
                SR.GetString(SR.LayoutEditorPart_ChromeState),
                SR.GetString(SR.LayoutEditorPart_Zone),
                SR.GetString(SR.LayoutEditorPart_ZoneIndex),
            };

            WebControl[] propertyEditors = new WebControl[] {
                _chromeState,
                _zone,
                _zoneIndex,
            };

            string[] errorMessages = new string[] {
                _chromeStateErrorMessage,
                null,
                _zoneIndexErrorMessage,
            };

            RenderPropertyEditors(writer, propertyDisplayNames, null /* propertyDescriptions */,
                                  propertyEditors, errorMessages);
        }

        public override void SyncChanges() {
            WebPart webPart = WebPartToEdit;

            Debug.Assert(webPart != null);
            if (webPart != null) {
                WebPartZoneBase currentZone = webPart.Zone;
                bool allowLayoutChange = currentZone.AllowLayoutChange;

                EnsureChildControls();

                TypeConverter chromeStateConverter = TypeDescriptor.GetConverter(typeof(PartChromeState));
                _chromeState.SelectedValue = chromeStateConverter.ConvertToString(webPart.ChromeState);
                _chromeState.Enabled = CanChangeChromeState;

                WebPartManager manager = WebPartManager;
                Debug.Assert(manager != null);
                if (manager != null) {
                    WebPartZoneCollection zones = manager.Zones;
                    bool allowZoneChange = webPart.AllowZoneChange;

                    _zone.ClearSelection();
                    foreach (ListItem item in _zone.Items) {
                        string zoneID = item.Value;
                        WebPartZoneBase zone = zones[zoneID];
                        if (zone == currentZone || (allowZoneChange && zone.AllowLayoutChange)) {
                            item.Enabled = true;
                        }
                        else {
                            item.Enabled = false;
                        }

                        if (zone == currentZone) {
                            item.Selected = true;
                        }
                    }

                    _zone.Enabled = CanChangeZone;
                }

                _zoneIndex.Text = webPart.ZoneIndex.ToString(CultureInfo.CurrentCulture);
                _zoneIndex.Enabled = CanChangeZoneIndex;
            }
        }
    }
}