Add subcategories for map features.
[potlatch2.git] / net / systemeD / potlatch2 / TagGrid.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <!--
4         Advanced tag grid
5 -->
6
7 <mx:DataGrid editable="true" doubleClickEnabled="true" doubleClick="addNewTag()"
8     xmlns:fx="http://ns.adobe.com/mxml/2009"
9         xmlns:controls="net.systemeD.controls.*"
10         xmlns:mx="library://ns.adobe.com/flex/mx">
11         <mx:columns>
12
13                 <!-- Key -->
14
15                 <mx:DataGridColumn editable="true" dataField="key" headerText="Key">
16                         <mx:itemEditor>
17                                 <fx:Component>
18                                         <controls:AutoComplete
19                                                 dataProvider="{MapFeatures.getInstance().getAutoCompleteKeys(outerDocument.getEntityType())}"
20                                                 labelField="name"
21                                                 rowCount="10"
22                                                 restrict="&#x0020;-&#x10FFFF;"
23                                                 maxChars="255" 
24                                                 typedText="{outerDocument.selectedItem.key}">
25                                         <fx:Script><![CDATA[ import net.systemeD.potlatch2.mapfeatures.*; ]]></fx:Script>
26                                         </controls:AutoComplete>
27                                 </fx:Component>
28                         </mx:itemEditor>
29                 </mx:DataGridColumn>
30
31                 <!-- Value -->
32
33                 <mx:DataGridColumn editable="true" dataField="value" headerText="Value" itemRenderer="net.systemeD.controls.DataGridWarningField" >
34                         <mx:itemEditor>
35                                 <fx:Component>
36                                         <controls:AutoComplete
37                                                 dataProvider="{MapFeatures.getInstance().getAutoCompleteValues(outerDocument.getEntityType(),outerDocument.selectedItem.key)}"
38                                                 labelField="name"
39                                                 rowCount="10"
40                                                 restrict="&#x0020;-&#x10FFFF;"
41                                                 maxChars="255" 
42                                                 typedText="{outerDocument.selectedItem.value}">
43                                         <fx:Script><![CDATA[ import net.systemeD.potlatch2.mapfeatures.*; ]]></fx:Script>
44                                         </controls:AutoComplete>
45                                 </fx:Component>
46                         </mx:itemEditor>
47                 </mx:DataGridColumn>
48
49                 <!-- Delete button -->
50
51                 <mx:DataGridColumn width="20" editable="false">
52                         <mx:itemRenderer>
53                                 <fx:Component>
54                     <mx:HBox horizontalAlign="center" verticalAlign="middle">
55                       <mx:Image source="@Embed('../../../embedded/delete_small.svg')"
56                           click='event.stopPropagation();outerDocument.removeTag();'
57                           buttonMode="true" useHandCursor="true" width="12" height="12">
58                       </mx:Image>
59                                         </mx:HBox>
60                                 </fx:Component>
61                         </mx:itemRenderer>
62                 </mx:DataGridColumn>
63         </mx:columns>
64
65         <fx:Script><![CDATA[
66
67                 import net.systemeD.halcyon.connection.*;
68                 import mx.collections.*;
69
70                 private var selectedEntity:Entity;
71                 private var tagDataProvider:ArrayCollection;
72
73                 /** Assign a new selected Entity, and update editing controls appropriately. */
74                 public function init(entity:Entity):void {
75                         if (selectedEntity==entity) return;
76                         if (selectedEntity) selectedEntity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
77
78                         selectedEntity=entity;
79                         if (!selectedEntity) return;
80                         selectedEntity.addEventListener(Connection.TAG_CHANGED, tagChanged, false, 0, true);
81
82                         if ( tagDataProvider == null ) {
83                                 tagDataProvider = new ArrayCollection();
84                                 dataProvider = tagDataProvider;
85                         } else {
86                                 tagDataProvider.removeAll();
87                         }
88
89                         var tags:Array = selectedEntity.getTagArray();
90                         tags.sortOn("key");
91                         for each(var tag:Tag in tags) { tagDataProvider.addItem(tag); }
92                 }
93
94                 /** Update dataProvider in response to a tag being edited or removed */
95                 private function tagChanged(event:TagEvent):void {
96                         var set:Boolean=false;
97                         for (var i:uint=0; i<tagDataProvider.length; i++) {
98                                 if (tagDataProvider[i].key==event.oldKey || tagDataProvider[i].key==event.key) {
99                                         if (event.newValue && event.key) {
100                                                 if (!set) {
101                                                         // this entry in the dataProvider has changed
102                                                         tagDataProvider[i].key=event.key;
103                                                         tagDataProvider[i].value=event.newValue;
104                                                         set=true;
105                                                 } else {
106                                                         // trap duplicate tags
107                                                         tagDataProvider.removeItemAt(i);
108                                                 }
109                                         } else {
110                                                 // this entry in the dataProvider has been deleted
111                                                 tagDataProvider.removeItemAt(i);
112                                         }
113                                 }
114                         }
115                         if (!set && event.newValue) tagDataProvider.addItem(new Tag(event.entity,event.key,event.newValue));
116
117                         // tell Flex to redraw the grid because we've changed a tag
118                         invalidateList();
119                 }
120
121                 /** Create editing controls for a new key/value pair, with default values. */
122                 public function addNewTag():void {
123                         var newKey:String = "(new tag)";
124                         var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
125                         tagDataProvider.addItem(newTag);
126                         editedItemPosition = {rowIndex: tagDataProvider.getItemIndex(newTag), columnIndex: 0};
127                 }
128
129                 /** Remove the selected tag from the selected entity. */
130                 public function removeTag():void {
131                         var k:String = selectedItem.key;
132                         selectedEntity.setTag(k, null, MainUndoStack.getGlobalStack().addAction);
133                 }
134
135                 /** Return the type ("node", "way", "relation") of the selected entity. */
136                 public function getEntityType():String {
137                         return selectedEntity.getType();
138                 }
139
140         ]]></fx:Script>
141 </mx:DataGrid>