categories of additional tags for roads
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / editors / ChoiceComboBox.as
1 package net.systemeD.potlatch2.mapfeatures.editors {
2
3     import mx.controls.*;
4     import mx.containers.*;
5     import mx.core.*;
6     import flash.display.*;
7     import flash.events.*;
8     import mx.events.*;
9     import mx.utils.*;
10
11         public class ChoiceComboBox extends ComboBox {
12
13         protected var textInputReplacement:UIComponent;
14
15         override protected function createChildren():void {
16                 super.createChildren();
17
18                 if ( !textInputReplacement ) {
19                         if ( itemRenderer != null ) {
20                                 //remove the default textInput
21                                 removeChild(textInput);
22
23                                 //create a new itemRenderer to use in place of the text input
24                                 textInputReplacement = itemRenderer.newInstance();
25                                 IDataRenderer(textInputReplacement).data = selectedItem;
26                                 textInputReplacement.mouseChildren = false;
27                                 textInputReplacement.mouseEnabled = false;
28                                 addChild(textInputReplacement);
29                         }
30                 }
31         }
32
33         override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
34             super.updateDisplayList(unscaledWidth, unscaledHeight);
35
36             if ( textInputReplacement ) {
37                 IDataRenderer(textInputReplacement).data = selectedItem;
38
39                 var arrowWidth:Number = getStyle("arrowButtonWidth");\r
40                 var itemHeight:Number = textInputReplacement.getExplicitOrMeasuredHeight();\r
41                 var itemWidth:Number = textInputReplacement.getExplicitOrMeasuredWidth();\r
42 \r
43                 if (isNaN(arrowWidth))\r
44                     arrowWidth = 0;\r
45
46                 var bm:EdgeMetrics = borderMetrics;
47
48                 textInputReplacement.setActualSize(unscaledWidth - arrowWidth, itemHeight);\r
49                 textInputReplacement.move(bm.left, bm.top);
50             }
51         }
52
53         override protected function measure():void {
54             super.measure();
55
56             if ( textInputReplacement ) {
57                 IDataRenderer(textInputReplacement).data = selectedItem;
58                 var prefSize:Object = calculatePreferredSizeFromData(collection.length);
59
60                 var arrowWidth:Number = getStyle("arrowButtonWidth");\r
61                 var itemHeight:Number = textInputReplacement.getExplicitOrMeasuredHeight();\r
62                 var itemWidth:Number = textInputReplacement.getExplicitOrMeasuredWidth();\r
63 \r
64                 if (isNaN(arrowWidth))\r
65                     arrowWidth = 0;\r
66
67                 var bm:EdgeMetrics = borderMetrics;
68                 itemHeight += bm.top + bm.bottom;\r
69                 itemWidth += bm.left + bm.right + arrowWidth + 8;
70                 prefSize.height += bm.top + bm.bottom;
71                 prefSize.width += bm.left + bm.right + arrowWidth + 8;\r
72
73                 measuredMinHeight = measuredHeight = Math.max(prefSize.height, itemHeight);\r
74                 measuredMinWidth = measuredWidth = Math.max(prefSize.width, itemWidth);\r
75             }
76         }
77
78         override protected function calculatePreferredSizeFromData(numItems:int):Object {
79             if ( collection == null ) return { width: 0, height: 0 };
80             
81             var maxWidth:Number = 0;
82             var maxHeight:Number = 0;
83             
84             var test:UIComponent = itemRenderer.newInstance();
85             addChild(test)
86             for ( var i:int = 0; i < numItems; i++ ) {
87                 IDataRenderer(test).data = collection[i];
88                 test.validateDisplayList();
89                 test.validateSize(true);
90                 maxWidth = Math.max(maxWidth, test.getExplicitOrMeasuredWidth());\r
91                 maxHeight = Math.max(maxHeight, test.getExplicitOrMeasuredHeight());\r
92             }
93             removeChild(test);
94             return {width: maxWidth, height: maxHeight};
95         }
96     }
97 }
98
99