summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/ui/Form.java
blob: 765fcc15b004128015e105f19e2f63a76731c8e4 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
/* *************************************************************************
 
 IT Mill Toolkit 

 Development of Browser User Interfaces Made Easy

 Copyright (C) 2000-2006 IT Mill Ltd
 
 *************************************************************************

 This product is distributed under commercial license that can be found
 from the product package on license.pdf. Use of this product might 
 require purchasing a commercial license from IT Mill Ltd. For guidelines 
 on usage, see licensing-guidelines.html

 *************************************************************************
 
 For more information, contact:
 
 IT Mill Ltd                           phone: +358 2 4802 7180
 Ruukinkatu 2-4                        fax:   +358 2 4802 7181
 20540, Turku                          email:  info@itmill.com
 Finland                               company www: www.itmill.com
 
 Primary source for information and releases: www.itmill.com

 ********************************************************************** */

package com.itmill.toolkit.ui;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import com.itmill.toolkit.data.Buffered;
import com.itmill.toolkit.data.Item;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.Validatable;
import com.itmill.toolkit.data.Validator;
import com.itmill.toolkit.data.Validator.InvalidValueException;
import com.itmill.toolkit.data.util.BeanItem;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;

/**
 * Form component provides easy way of creating and managing sets fields.
 * 
 * <p>
 * <code>Form</code> is a container for fields implementing {@link Field}
 * interface. It provides support for any layouts and provides buffering
 * interface for easy connection of commit and discard buttons. All the form
 * fields can be customized by adding validators, setting captions and icons,
 * setting immediateness, etc. Also direct mechanism for replacing existing
 * fields with selections is given.
 * </p>
 * 
 * <p>
 * <code>Form</code> provides customizable editor for classes implementing
 * {@link com.itmill.toolkit.data.Item} interface. Also the form itself
 * implements this interface for easier connectivity to other items. To use the
 * form as editor for an item, just connect the item to form with
 * {@link Form#setItemDataSource(Item)}. If only a part of the item needs to be
 * edited, {@link Form#setItemDataSource(Item,Collection)} can be used instead.
 * After the item has been connected to the form, the automatically created
 * fields can be customized and new fields can be added. If you need to connect
 * a class that does not implement {@link com.itmill.toolkit.data.Item}
 * interface, most properties of any class following bean pattern, can be
 * accessed trough {@link com.itmill.toolkit.data.util.BeanItem}.
 * </p>
 * 
 * @author IT Mill Ltd.
 * @version
 * @VERSION@
 * @since 3.0
 */
public class Form extends AbstractField implements Item.Editor, Buffered, Item,
        Validatable {

    private Object propertyValue;

    /**
     * Layout of the form.
     */
    private Layout layout;

    /**
     * Item connected to this form as datasource.
     */
    private Item itemDatasource;

    /**
     * Ordered list of property ids in this editor.
     */
    private LinkedList propertyIds = new LinkedList();

    /**
     * Current buffered source exception.
     */
    private Buffered.SourceException currentBufferedSourceException = null;

    /**
     * Is the form in write trough mode.
     */
    private boolean writeThrough = true;

    /**
     * Is the form in read trough mode.
     */
    private boolean readThrough = true;

    /**
     * Mapping from propertyName to corresponding field.
     */
    private HashMap fields = new HashMap();

    /**
     * Field factory for this form.
     */
    private FieldFactory fieldFactory;

    /**
     * Registered Validators.
     */
    private LinkedList validators;

    /**
     * Visible item properties.
     */
    private Collection visibleItemProperties;

    /**
     * Contructs a new form with default layout.
     * 
     * <p>
     * By default the form uses <code>OrderedLayout</code> with
     * <code>form</code>-style.
     * </p>
     * 
     * @param formLayout
     *                the layout of the form.
     */
    public Form() {
        this(null);
    }

    /**
     * Contructs a new form with given layout.
     * 
     * @param formLayout
     *                the layout of the form.
     */
    public Form(Layout formLayout) {
        this(formLayout, new BaseFieldFactory());
    }

    /**
     * Contructs a new form with given layout and FieldFactory.
     * 
     * @param formLayout
     *                the layout of the form.
     * @param fieldFactory
     *                the FieldFactory of the form.
     */
    public Form(Layout formLayout, FieldFactory fieldFactory) {
        super();
        setLayout(formLayout);
        setFieldFactory(fieldFactory);
    }

    /* Documented in interface */
    public String getTag() {
        return "form";
    }

    /* Documented in interface */
    public void paintContent(PaintTarget target) throws PaintException {
        super.paintContent(target);
        layout.paint(target);
    }

    /*
     * Commit changes to the data source Don't add a JavaDoc comment here, we
     * use the default one from the interface.
     */
    public void commit() throws Buffered.SourceException {

        LinkedList problems = null;

        // Try to commit all
        for (Iterator i = propertyIds.iterator(); i.hasNext();) {
            try {
                Field f = ((Field) fields.get(i.next()));
                // Commit only non-readonly fields.
                if (!f.isReadOnly()) {
                    f.commit();
                }
            } catch (Buffered.SourceException e) {
                if (problems == null) {
                    problems = new LinkedList();
                }
                problems.add(e);
            }
        }

        // No problems occurred
        if (problems == null) {
            if (currentBufferedSourceException != null) {
                currentBufferedSourceException = null;
                requestRepaint();
            }
            return;
        }

        // Commit problems
        Throwable[] causes = new Throwable[problems.size()];
        int index = 0;
        for (Iterator i = problems.iterator(); i.hasNext();) {
            causes[index++] = (Throwable) i.next();
        }
        Buffered.SourceException e = new Buffered.SourceException(this, causes);
        currentBufferedSourceException = e;
        requestRepaint();
        throw e;
    }

    /*
     * Discards local changes and refresh values from the data source Don't add
     * a JavaDoc comment here, we use the default one from the interface.
     */
    public void discard() throws Buffered.SourceException {

        LinkedList problems = null;

        // Try to discard all changes
        for (Iterator i = propertyIds.iterator(); i.hasNext();) {
            try {
                ((Field) fields.get(i.next())).discard();
            } catch (Buffered.SourceException e) {
                if (problems == null) {
                    problems = new LinkedList();
                }
                problems.add(e);
            }
        }

        // No problems occurred
        if (problems == null) {
            if (currentBufferedSourceException != null) {
                currentBufferedSourceException = null;
                requestRepaint();
            }
            return;
        }

        // Discards problems occurred
        Throwable[] causes = new Throwable[problems.size()];
        int index = 0;
        for (Iterator i = problems.iterator(); i.hasNext();) {
            causes[index++] = (Throwable) i.next();
        }
        Buffered.SourceException e = new Buffered.SourceException(this, causes);
        currentBufferedSourceException = e;
        requestRepaint();
        throw e;
    }

    /*
     * Is the object modified but not committed? Don't add a JavaDoc comment
     * here, we use the default one from the interface.
     */
    public boolean isModified() {
        for (Iterator i = propertyIds.iterator(); i.hasNext();) {
            Field f = (Field) fields.get(i.next());
            if (f != null && f.isModified()) {
                return true;
            }

        }
        return false;
    }

    /*
     * Is the editor in a read-through mode? Don't add a JavaDoc comment here,
     * we use the default one from the interface.
     */
    public boolean isReadThrough() {
        return readThrough;
    }

    /*
     * Is the editor in a write-through mode? Don't add a JavaDoc comment here,
     * we use the default one from the interface.
     */
    public boolean isWriteThrough() {
        return writeThrough;
    }

    /*
     * Sets the editor's read-through mode to the specified status. Don't add a
     * JavaDoc comment here, we use the default one from the interface.
     */
    public void setReadThrough(boolean readThrough) {
        if (readThrough != this.readThrough) {
            this.readThrough = readThrough;
            for (Iterator i = propertyIds.iterator(); i.hasNext();) {
                ((Field) fields.get(i.next())).setReadThrough(readThrough);
            }
        }
    }

    /*
     * Sets the editor's read-through mode to the specified status. Don't add a
     * JavaDoc comment here, we use the default one from the interface.
     */
    public void setWriteThrough(boolean writeThrough) {
        if (writeThrough != this.writeThrough) {
            this.writeThrough = writeThrough;
            for (Iterator i = propertyIds.iterator(); i.hasNext();) {
                ((Field) fields.get(i.next())).setWriteThrough(writeThrough);
            }
        }
    }

    /**
     * Adds a new property to form and create corresponding field.
     * 
     * @see com.itmill.toolkit.data.Item#addItemProperty(Object, Property)
     */
    public boolean addItemProperty(Object id, Property property) {

        // Checks inputs
        if (id == null || property == null) {
            throw new NullPointerException("Id and property must be non-null");
        }

        // Checks that the property id is not reserved
        if (propertyIds.contains(id)) {
            return false;
        }

        // Gets suitable field
        Field field = fieldFactory.createField(property, this);
        if (field == null) {
            return false;
        }

        // Configures the field
        try {
            field.setPropertyDataSource(property);
            String caption = id.toString();
            if (caption.length() > 50) {
                caption = caption.substring(0, 47) + "...";
            }
            if (caption.length() > 0) {
                caption = "" + Character.toUpperCase(caption.charAt(0))
                        + caption.substring(1, caption.length());
            }
            field.setCaption(caption);
        } catch (Throwable ignored) {
            return false;
        }

        addField(id, field);

        return true;
    }

    /**
     * Adds the field to form.
     * 
     * <p>
     * The property id must not be already used in the form.
     * </p>
     * 
     * <p>
     * This field is added to the form layout in the default position (the
     * position used by {@link Layout#addComponent(Component)} method. In the
     * special case that the underlying layout is a custom layout, string
     * representation of the property id is used instead of the default
     * location.
     * </p>
     * 
     * @param propertyId
     *                the Property id the the field.
     * @param field
     *                the New field added to the form.
     */
    public void addField(Object propertyId, Field field) {

        if (propertyId != null && field != null) {
            dependsOn(field);
            field.dependsOn(this);
            fields.put(propertyId, field);
            propertyIds.addLast(propertyId);
            field.setReadThrough(readThrough);
            field.setWriteThrough(writeThrough);

            if (layout instanceof CustomLayout) {
                ((CustomLayout) layout).addComponent(field, propertyId
                        .toString());
            } else {
                layout.addComponent(field);
            }

            requestRepaint();
        }
    }

    /**
     * The property identified by the property id.
     * 
     * <p>
     * The property data source of the field specified with property id is
     * returned. If there is a (with specified property id) having no data
     * source, the field is returned instead of the data source.
     * </p>
     * 
     * @see com.itmill.toolkit.data.Item#getItemProperty(Object)
     */
    public Property getItemProperty(Object id) {
        Field field = (Field) fields.get(id);
        if (field == null) {
            return null;
        }
        Property property = field.getPropertyDataSource();

        if (property != null) {
            return property;
        } else {
            return field;
        }
    }

    /**
     * Gets the field identified by the propertyid.
     * 
     * @param propertyId
     *                the id of the property.
     */
    public Field getField(Object propertyId) {
        return (Field) fields.get(propertyId);
    }

    /* Documented in interface */
    public Collection getItemPropertyIds() {
        return Collections.unmodifiableCollection(propertyIds);
    }

    /**
     * Removes the property and corresponding field from the form.
     * 
     * @see com.itmill.toolkit.data.Item#removeItemProperty(Object)
     */
    public boolean removeItemProperty(Object id) {

        Field field = (Field) fields.get(id);

        if (field != null) {
            propertyIds.remove(id);
            fields.remove(id);
            removeDirectDependency(field);
            field.removeDirectDependency(this);
            layout.removeComponent(field);
            return true;
        }

        return false;
    }

    /**
     * Removes all properties and fields from the form.
     * 
     * @return the Success of the operation. Removal of all fields succeeded if
     *         (and only if) the return value is <code>true</code>.
     */
    public boolean removeAllProperties() {
        Object[] properties = propertyIds.toArray();
        boolean success = true;

        for (int i = 0; i < properties.length; i++) {
            if (!removeItemProperty(properties[i])) {
                success = false;
            }
        }

        return success;
    }

    /* Documented in the interface */
    public Item getItemDataSource() {
        return itemDatasource;
    }

    /**
     * Sets the item datasource for the form.
     * 
     * <p>
     * Setting item datasource clears any fields, the form might contain and
     * adds all the properties as fields to the form.
     * </p>
     * 
     * @see com.itmill.toolkit.data.Item.Viewer#setItemDataSource(Item)
     */
    public void setItemDataSource(Item newDataSource) {
        setItemDataSource(newDataSource, newDataSource != null ? newDataSource
                .getItemPropertyIds() : null);
    }

    /**
     * Set the item datasource for the form, but limit the form contents to
     * specified properties of the item.
     * 
     * <p>
     * Setting item datasource clears any fields, the form might contain and
     * adds the specified the properties as fields to the form, in the specified
     * order.
     * </p>
     * 
     * @see com.itmill.toolkit.data.Item.Viewer#setItemDataSource(Item)
     */
    public void setItemDataSource(Item newDataSource, Collection propertyIds) {

        // Removes all fields first from the form
        removeAllProperties();

        // Sets the datasource
        itemDatasource = newDataSource;

        // If the new datasource is null, just set null datasource
        if (itemDatasource == null) {
            return;
        }

        // Adds all the properties to this form
        for (Iterator i = propertyIds.iterator(); i.hasNext();) {
            Object id = i.next();
            Property property = itemDatasource.getItemProperty(id);
            if (id != null && property != null) {
                Field f = fieldFactory.createField(itemDatasource, id, this);
                if (f != null) {
                    f.setPropertyDataSource(property);
                    addField(id, f);
                }
            }
        }
    }

    /**
     * Gets the layout of the form.
     * 
     * <p>
     * By default form uses <code>OrderedLayout</code> with <code>form</code>-style.
     * </p>
     * 
     * @return the Layout of the form.
     */
    public Layout getLayout() {
        return layout;
    }

    /**
     * Sets the layout of the form.
     * 
     * <p>
     * By default form uses <code>OrderedLayout</code> with <code>form</code>-style.
     * </p>
     * 
     * @param newLayout
     *                the Layout of the form.
     */
    public void setLayout(Layout newLayout) {

        // Use orderedlayout by default
        if (newLayout == null) {
            newLayout = new FormLayout();
        }

        // Move components from previous layout
        if (layout != null) {
            newLayout.moveComponentsFrom(layout);
            layout.setParent(null);
        }

        // Replace the previous layout
        newLayout.setParent(this);
        layout = newLayout;
    }

    /**
     * Sets the form field to be selectable from static list of changes.
     * 
     * <p>
     * The list values and descriptions are given as array. The value-array must
     * contain the current value of the field and the lengths of the arrays must
     * match. Null values are not supported.
     * </p>
     * 
     * @param propertyId
     *                the id of the property.
     * @param values
     * @param descriptions
     * @return the select property generated
     */
    public Select replaceWithSelect(Object propertyId, Object[] values,
            Object[] descriptions) {

        // Checks the parameters
        if (propertyId == null || values == null || descriptions == null) {
            throw new NullPointerException("All parameters must be non-null");
        }
        if (values.length != descriptions.length) {
            throw new IllegalArgumentException(
                    "Value and description list are of different size");
        }

        // Gets the old field
        Field oldField = (Field) fields.get(propertyId);
        if (oldField == null) {
            throw new IllegalArgumentException("Field with given propertyid '"
                    + propertyId.toString() + "' can not be found.");
        }
        Object value = oldField.getValue();

        // Checks that the value exists and check if the select should
        // be forced in multiselect mode
        boolean found = false;
        boolean isMultiselect = false;
        for (int i = 0; i < values.length && !found; i++) {
            if (values[i] == value
                    || (value != null && value.equals(values[i]))) {
                found = true;
            }
        }
        if (value != null && !found) {
            if (value instanceof Collection) {
                for (Iterator it = ((Collection) value).iterator(); it
                        .hasNext();) {
                    Object val = it.next();
                    found = false;
                    for (int i = 0; i < values.length && !found; i++) {
                        if (values[i] == val
                                || (val != null && val.equals(values[i]))) {
                            found = true;
                        }
                    }
                    if (!found) {
                        throw new IllegalArgumentException(
                                "Currently selected value '" + val
                                        + "' of property '"
                                        + propertyId.toString()
                                        + "' was not found");
                    }
                }
                isMultiselect = true;
            } else {
                throw new IllegalArgumentException("Current value '" + value
                        + "' of property '" + propertyId.toString()
                        + "' was not found");
            }
        }

        // Creates the new field matching to old field parameters
        Select newField = new Select();
        if (isMultiselect) {
            newField.setMultiSelect(true);
        }
        newField.setCaption(oldField.getCaption());
        newField.setReadOnly(oldField.isReadOnly());
        newField.setReadThrough(oldField.isReadThrough());
        newField.setWriteThrough(oldField.isWriteThrough());

        // Creates the options list
        newField.addContainerProperty("desc", String.class, "");
        newField.setItemCaptionPropertyId("desc");
        for (int i = 0; i < values.length; i++) {
            Object id = values[i];
            if (id == null) {
                id = new Object();
                newField.setNullSelectionItemId(id);
            }
            Item item = newField.addItem(id);
            if (item != null) {
                item.getItemProperty("desc").setValue(
                        descriptions[i].toString());
            }
        }

        // Sets the property data source
        Property property = oldField.getPropertyDataSource();
        oldField.setPropertyDataSource(null);
        newField.setPropertyDataSource(property);

        // Replaces the old field with new one
        layout.replaceComponent(oldField, newField);
        fields.put(propertyId, newField);
        removeDirectDependency(oldField);
        oldField.removeDirectDependency(this);
        dependsOn(newField);
        newField.dependsOn(this);

        return newField;
    }

    /**
     * Notifies the component that it is connected to an application
     * 
     * @see com.itmill.toolkit.ui.Component#attach()
     */
    public void attach() {
        super.attach();
        layout.attach();
    }

    /**
     * Notifies the component that it is detached from the application.
     * 
     * @see com.itmill.toolkit.ui.Component#detach()
     */
    public void detach() {
        super.detach();
        layout.detach();
    }

    /**
     * Adds a new validator for this object.
     * 
     * @see com.itmill.toolkit.data.Validatable#addValidator(com.itmill.toolkit.data.Validator)
     */
    public void addValidator(Validator validator) {

        if (validators == null) {
            validators = new LinkedList();
        }
        validators.add(validator);
    }

    /**
     * Removes a previously registered validator from the object.
     * 
     * @see com.itmill.toolkit.data.Validatable#removeValidator(com.itmill.toolkit.data.Validator)
     */
    public void removeValidator(Validator validator) {
        if (validators != null) {
            validators.remove(validator);
        }
    }

    /**
     * Gets the Lists all validators currently registered for the object.
     * 
     * @see com.itmill.toolkit.data.Validatable#getValidators()
     */
    public Collection getValidators() {
        if (validators == null) {
            validators = new LinkedList();
        }
        return validators;
    }

    /**
     * Tests the current value of the object against all registered validators
     * 
     * @see com.itmill.toolkit.data.Validatable#isValid()
     */
    public boolean isValid() {
        boolean valid = true;
        for (Iterator i = propertyIds.iterator(); i.hasNext();) {
            valid &= ((Field) fields.get(i.next())).isValid();
        }
        return valid;
    }

    /**
     * Checks the validity of the validatable.
     * 
     * @see com.itmill.toolkit.data.Validatable#validate()
     */
    public void validate() throws InvalidValueException {
        for (Iterator i = propertyIds.iterator(); i.hasNext();) {
            ((Field) fields.get(i.next())).validate();
        }
    }

    /**
     * Checks the validabtable object accept invalid values.
     * 
     * @see com.itmill.toolkit.data.Validatable#isInvalidAllowed()
     */
    public boolean isInvalidAllowed() {
        return true;
    }

    /**
     * Should the validabtable object accept invalid values.
     * 
     * @see com.itmill.toolkit.data.Validatable#setInvalidAllowed(boolean)
     */
    public void setInvalidAllowed(boolean invalidValueAllowed)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Sets the component's to read-only mode to the specified state.
     * 
     * @see com.itmill.toolkit.ui.Component#setReadOnly(boolean)
     */
    public void setReadOnly(boolean readOnly) {
        super.setReadOnly(readOnly);
        for (Iterator i = propertyIds.iterator(); i.hasNext();) {
            ((Field) fields.get(i.next())).setReadOnly(readOnly);
        }
    }

    /**
     * Sets the field factory of Form.
     * 
     * <code>FieldFactory</code> is used to create fields for form properties.
     * By default the form uses BaseFieldFactory to create Field instances.
     * 
     * @param fieldFactory
     *                the New factory used to create the fields.
     * @see Field
     * @see FieldFactory
     */
    public void setFieldFactory(FieldFactory fieldFactory) {
        this.fieldFactory = fieldFactory;
    }

    /**
     * Get the field factory of the form.
     * 
     * @return the FieldFactory Factory used to create the fields.
     */
    public FieldFactory getFieldFactory() {
        return fieldFactory;
    }

    /**
     * Gets the field type.
     * 
     * @see com.itmill.toolkit.ui.AbstractField#getType()
     */
    public Class getType() {
        if (getPropertyDataSource() != null) {
            return getPropertyDataSource().getType();
        }
        return Object.class;
    }

    /**
     * Sets the internal value.
     * 
     * This is relevant when the Form is used as Field.
     * 
     * @see com.itmill.toolkit.ui.AbstractField#setInternalValue(java.lang.Object)
     */
    protected void setInternalValue(Object newValue) {
        // Stores the old value
        Object oldValue = propertyValue;

        // Sets the current Value
        super.setInternalValue(newValue);
        propertyValue = newValue;

        // Ignores form updating if data object has not changed.
        if (oldValue != newValue) {
            setFormDataSource(newValue, getVisibleItemProperties());
        }
    }

    /**
     * Gets the first field in form.
     * 
     * @return the Field.
     */
    private Field getFirstField() {
        Object id = null;
        if (getItemPropertyIds() != null) {
            id = getItemPropertyIds().iterator().next();
        }
        if (id != null) {
            return getField(id);
        }
        return null;
    }

    /**
     * Updates the internal form datasource.
     * 
     * Method setFormDataSource.
     * 
     * @param data
     * @param properties
     */
    protected void setFormDataSource(Object data, Collection properties) {

        // If data is an item use it.
        Item item = null;
        if (data instanceof Item) {
            item = (Item) data;
        } else if (data != null) {
            item = new BeanItem(data);
        }

        // Sets the datasource to form
        if (item != null && properties != null) {
            // Shows only given properties
            this.setItemDataSource(item, properties);
        } else {
            // Shows all properties
            this.setItemDataSource(item);
        }
    }

    /**
     * Returns the visibleProperties.
     * 
     * @return the Collection of visible Item properites.
     */
    public Collection getVisibleItemProperties() {
        return visibleItemProperties;
    }

    /**
     * Sets the visibleProperties.
     * 
     * @param visibleProperties
     *                the visibleProperties to set.
     */
    public void setVisibleItemProperties(Collection visibleProperties) {
        visibleItemProperties = visibleProperties;
        Object value = getValue();
        setFormDataSource(value, getVisibleItemProperties());
    }

    /**
     * Focuses the first field in the form.
     * 
     * @see com.itmill.toolkit.ui.Component.Focusable#focus()
     */
    public void focus() {
        Field f = getFirstField();
        if (f != null) {
            f.focus();
        }
    }

    /**
     * Sets the Tabulator index of this Focusable component.
     * 
     * @see com.itmill.toolkit.ui.Component.Focusable#setTabIndex(int)
     */
    public void setTabIndex(int tabIndex) {
        super.setTabIndex(tabIndex);
        for (Iterator i = getItemPropertyIds().iterator(); i.hasNext();) {
            (getField(i.next())).setTabIndex(tabIndex);
        }
    }
}