aodl /AODL/Document/Forms/Controls/ODFComboBox.cs

Language C# Lines 362
MD5 Hash 9023909195f57a8e6f6fee2ddfd5d2dc
Repository https://bitbucket.org/chrisc/aodl View Raw File
  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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
 * 
 * Use is subject to license terms.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
 * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ************************************************************************/

using System.Xml.Linq;
using AODL.Document.Content;

namespace AODL.Document.Forms.Controls
{
    public class ODFComboBox : ODFFormControl
    {
        private ODFItemCollection _items;

        /// <summary>
        /// Creates an ODFComboBox
        /// </summary>
        /// <param name="parentForm">Parent form that the control belongs to</param>
        /// <param name="contentCollection">Collection of content where the control will be referenced</param>
        /// <param name="id">Control ID. Please specify a unique ID!</param>
        public ODFComboBox(ODFForm parentForm, ContentCollection contentCollection, string id)
            : base(parentForm, contentCollection, id)
        {
            _items = new ODFItemCollection();
            RestoreItemEvents();
        }

        /// <summary>
        /// Creates an ODFComboBox
        /// </summary>
        /// <param name="parentForm">Parent form that the control belongs to</param>
        /// <param name="contentCollection">Collection of content where the control will be referenced</param>
        /// <param name="id">Control ID. Please specify a unique ID!</param>
        /// <param name="x">X coordinate of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)</param>
        /// <param name="y">Y coordinate of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)</param>
        /// <param name="width">Width of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)</param>
        /// <param name="height">Height of the control in ODF format (eg. "1cm", "15mm", 3.2cm" etc)</param>
        public ODFComboBox(ODFForm parentForm, ContentCollection contentCollection, string id, string x, string y,
                           string width, string height) : base(parentForm, contentCollection, id, x, y, width, height)
        {
            _items = new ODFItemCollection();
            RestoreItemEvents();
        }

        public ODFComboBox(ODFForm parentForm, XElement node) : base(parentForm, node)
        {
            _items = new ODFItemCollection();
            RestoreItemEvents();
        }

        /// <summary>
        /// Collection of combo box items
        /// </summary>
        public ODFItemCollection Items
        {
            get { return _items; }
            set { _items = value; }
        }

        public override string Type
        {
            get { return "combobox"; }
        }

        /// <summary>
        /// Specifies whether or not a control can accept user input
        /// </summary>
        public bool? Disabled
        {
            get { return (bool?) Node.Attribute(Ns.Form + "disabled"); }
            set { Node.SetAttributeValue(Ns.Form + "disabled", value); }
        }

        /// <summary>
        /// Specifies whether, when the user enters text in the 
        ///	combobox that matches one of the list items in the combobox, the application automatically 
        /// completes the text for the user.
        /// </summary>
        public bool? AutoComplete
        {
            get { return (bool?) Node.Attribute(Ns.Form + "auto-complete"); }
            set { Node.SetAttributeValue(Ns.Form + "auto-complete", value); }
        }

        /// <summary>
        /// Contains additional information about a control.
        /// </summary>
        public string Title
        {
            get { return (string) Node.Attribute(Ns.Form + "title"); }
            set { Node.SetAttributeValue(Ns.Form + "title", value); }
        }

        /// <summary>
        /// Specifies the source used to populate the list in a list box or 
        /// combo box. The first column of the list source result set populates the list.
        /// </summary>
        public string ListSource
        {
            get { return (string) Node.Attribute(Ns.Form + "list-source"); }
            set { Node.SetAttributeValue(Ns.Form + "list-source", value); }
        }


        /// <summary>
        /// Specifies the name of a result set column. The result set is 
        /// determined by the form which the control belongs to
        /// </summary>
        public string DataField
        {
            get { return (string) Node.Attribute(Ns.Form + "data-field"); }
            set { Node.SetAttributeValue(Ns.Form + "data-field", value); }
        }

        /// <summary>
        /// Specifies the type of data source that is used to 
        /// populate the list data in a list box or combo box
        /// </summary>
        public ListSourceType? ListSourceType
        {
            get
            {
                string s = (string) Node.Attribute(Ns.Form + "list-source-type");
                if (s == null) return null;

                switch (s)
                {
                    case "table":
                        return Forms.ListSourceType.Table;
                    case "query":
                        return Forms.ListSourceType.Query;
                    case "sql":
                        return Forms.ListSourceType.Sql;
                    case "sql-pass-through":
                        return Forms.ListSourceType.SqlPassThrough;
                    case "value-list":
                        return Forms.ListSourceType.ValueList;
                    case "table-fields":
                        return Forms.ListSourceType.TableFields;
                    default:
                        return null;
                }
            }
            set
            {
                string s;
                switch (value)
                {
                    case Forms.ListSourceType.Table:
                        s = "table";
                        break;
                    case Forms.ListSourceType.Query:
                        s = "query";
                        break;
                    case Forms.ListSourceType.Sql:
                        s = "sql";
                        break;
                    case Forms.ListSourceType.SqlPassThrough:
                        s = "sql-pass-through";
                        break;
                    case Forms.ListSourceType.ValueList:
                        s = "value-list";
                        break;
                    case Forms.ListSourceType.TableFields:
                        s = "table-fields";
                        break;
                    default:
                        s = null;
                        break;
                }
                Node.SetAttributeValue(Ns.Form + "list-source-type", s);
            }
        }

        /// <summary>
        /// Specifies the tabbing navigation order of a control within a form
        /// </summary>
        public int TabIndex
        {
            get { return (int) Node.Attribute(Ns.Form + "tab-index"); }
            set { Node.SetAttributeValue(Ns.Form + "tab-index", value); }
        }

        /// <summary>
        /// Specifies whether or not a control is included in the tabbing 
        /// navigation order
        /// </summary>
        public bool? TabStop
        {
            get { return (bool?) Node.Attribute(Ns.Form + "tab-stop"); }
            set { Node.SetAttributeValue(Ns.Form + "tab-stop", value); }
        }

        /// <summary>
        /// Specifies whether or not a control is printed when a user prints 
        /// the document in which the control is contained
        /// </summary>
        public bool? Printable
        {
            get { return (bool?) Node.Attribute(Ns.Form + "printable"); }
            set { Node.SetAttributeValue(Ns.Form + "printable", value); }
        }

        /// <summary>
        /// Specifies whether or not a user can modify the value of a control
        /// </summary>
        public bool? ReadOnly
        {
            get { return (bool?) Node.Attribute(Ns.Form + "readonly"); }
            set { Node.SetAttributeValue(Ns.Form + "readonly", value); }
        }

        /// <summary>
        /// Specifies whether the list in a combo box or list box is always 
        /// visible or is only visible when the user clicks the drop-down button
        /// </summary>
        public bool? DropDown
        {
            get { return (bool?) Node.Attribute(Ns.Form + "dropdown"); }
            set { Node.SetAttributeValue(Ns.Form + "dropdown", value); }
        }

        /// <summary>
        /// specifies the number of rows that are visible at a time in a combo box 
        /// list or a list box list
        /// </summary>
        public int Size
        {
            get { return (int?) Node.Attribute(Ns.Form + "size") ?? -1; }
            set
            {
                if (value <= 0)
                    return;
                Node.SetAttributeValue(Ns.Form + "size", value);
            }
        }

        /// <summary>
        /// specifies whether or not empty current values are regarded as NULL
        /// </summary>
        public bool? ConvertEmptyToNull
        {
            get { return (bool?) Node.Attribute(Ns.Form + "convert-empty-to-null"); }
            set { Node.SetAttributeValue(Ns.Form + "convert-empty-to-null", value); }
        }

        /// <summary>
        /// Specifies the current status of an input control
        /// </summary>
        public string CurrentValue
        {
            get { return (string) Node.Attribute(Ns.Form + "current-value"); }
            set { Node.SetAttributeValue(Ns.Form + "current-value", value); }
        }

        /// <summary>
        /// Specifies the maximum number of characters that a user can 
        /// enter in an input control
        /// </summary>
        public int MaxLength
        {
            get { return (int?) Node.Attribute(Ns.Form + "max-length") ?? -1; }
            set
            {
                if (value <= 0)
                    return;
                Node.SetAttributeValue(Ns.Form + "max-length", value);
            }
        }

        /// <summary>
        /// Specifies the default value of the control
        /// </summary>
        public string Value
        {
            get { return (string) Node.Attribute(Ns.Form + "value"); }
            set { Node.SetAttributeValue(Ns.Form + "value", value); }
        }

        public void SuppressItemEvents()
        {
            _items.Inserted -= ItemCollection_Inserted;
            _items.Removed -= ItemCollection_Removed;
        }

        public void RestoreItemEvents()
        {
            _items.Inserted += ItemCollection_Inserted;
            _items.Removed += ItemCollection_Removed;
        }

        private void ItemCollection_Inserted(int index, object value)
        {
            ODFItem opt = (ODFItem) value;
            Node.Add(opt.Node);
        }

        private static void ItemCollection_Removed(int index, object value)
        {
            ODFItem opt = value as ODFItem;
            if (opt != null)
                opt.Node.Remove();
        }

        public void FixItemCollection()
        {
            _items.Clear();
            SuppressItemEvents();
            foreach (XElement nodeChild in Node.Elements())
            {
                if (nodeChild.Name == Ns.Form + "item" && nodeChild.Parent == Node)
                {
                    ODFItem sp = new ODFItem(_document, nodeChild);
                    _items.Add(sp);
                }
            }
            RestoreItemEvents();
        }

        /// <summary>
        /// Looks for a specified item by its label
        /// </summary>
        /// <param name="label">Option value</param>
        /// <returns></returns>
        public ODFItem GetItemByLabel(string label)
        {
            foreach (ODFItem it in _items)
            {
                if (it.Label == label)
                {
                    return it;
                }
            }
            return null;
        }

        protected override void CreateBasicNode()
        {
            XElement xe = new XElement(Ns.Form + "combobox");
            Node.Add(xe);
            Node = xe;
            ControlImplementation = "ooo:com.sun.star.form.component.ComboBox";
        }
    }
}
Back to Top