Merge branch 'master' into history
[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(event)"
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                         if (event.key==event.oldKey && event.oldValue==event.newValue) return;
97                         var set:Boolean=false;
98                         for (var i:uint=0; i<tagDataProvider.length; i++) {
99                                 if (tagDataProvider[i].key==event.oldKey || tagDataProvider[i].key==event.key) {
100                                         if (event.newValue && event.key) {
101                                                 if (!set) {
102                                                         // this entry in the dataProvider has changed
103                                                         tagDataProvider[i].key=event.key;
104                                                         tagDataProvider[i].value=event.newValue;
105                                                         set=true;
106                                                 } else {
107                                                         // trap duplicate tags
108                                                         tagDataProvider.removeItemAt(i);
109                                                 }
110                                         } else {
111                                                 // this entry in the dataProvider has been deleted
112                                                 tagDataProvider.removeItemAt(i);
113                                         }
114                                 }
115                         }
116                         if (!set && event.newValue) tagDataProvider.addItem(new Tag(event.entity,event.key,event.newValue));
117
118                         // tell Flex to redraw the grid because we've changed a tag
119                         invalidateList();
120                 }
121
122                 /** Create editing controls for a new key/value pair, with default values. */
123                 public function addNewTag(event:Event=null):void {
124                         if (event && event.target is TextField) return;
125                         var newKey:String = "(new tag)";
126                         var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
127                         tagDataProvider.addItem(newTag);
128                         editedItemPosition = {rowIndex: tagDataProvider.getItemIndex(newTag), columnIndex: 0};
129                 }
130
131                 /** Remove the selected tag from the selected entity. */
132                 public function removeTag():void {
133                         var k:String = selectedItem.key;
134                         if (selectedEntity.getTag(k)) {
135                                 // genuinely a tag on the Entity, so remove it
136                                 selectedEntity.setTag(k, null, MainUndoStack.getGlobalStack().addAction);
137                         } else {
138                                 // a "(new key)=(new value)" pair, which is in the dataProvider but not the Entity
139                                 for (var i:uint=0; i<tagDataProvider.length; i++) {
140                                         if (tagDataProvider[i].key==k) {
141                                                 tagDataProvider.removeItemAt(i);
142                                                 return;
143                                         }
144                                 }
145                         }
146                 }
147
148                 /** Return the type ("node", "way", "relation") of the selected entity. */
149                 public function getEntityType():String {
150                         return selectedEntity.getType();
151                 }
152
153         ]]></fx:Script>
154 </mx:DataGrid>