refactoring and some bugfixes
[potlatch2.git] / net / systemeD / potlatch2 / TagGrid.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <!--
4         Advanced tag grid
5         ** TODO: the 'X' button should be positioned more prettily
6 -->
7
8 <mx:DataGrid editable="true" doubleClickEnabled="true" doubleClick="addNewTag()"
9         xmlns:controls="net.systemeD.controls.*"
10         xmlns:mx="http://www.adobe.com/2006/mxml">
11         <mx:columns>
12
13                 <!-- Key -->
14
15                 <mx:DataGridColumn editable="true" dataField="key" headerText="Key">
16                         <mx:itemEditor>
17                                 <mx:Component>
18                                         <controls:AutoComplete 
19                                                 dataProvider="{MapFeatures.getInstance().getAutoCompleteKeys(outerDocument.getEntityType())}" 
20                                                 labelField="name" 
21                                                 rowCount="10"
22                                                 typedText="{outerDocument.selectedItem.key}">
23                                         <mx:Script><![CDATA[ import net.systemeD.potlatch2.mapfeatures.*; ]]></mx:Script>
24                                         </controls:AutoComplete>
25                                 </mx:Component>
26                         </mx:itemEditor>
27                 </mx:DataGridColumn>
28
29                 <!-- Value -->
30
31                 <mx:DataGridColumn editable="true" dataField="value" headerText="Value">
32                         <mx:itemEditor>
33                                 <mx:Component>
34                                         <controls:AutoComplete 
35                                                 dataProvider="{MapFeatures.getInstance().getAutoCompleteValues(outerDocument.getEntityType(),outerDocument.selectedItem.key)}" 
36                                                 labelField="name" 
37                                                 rowCount="10"
38                                                 typedText="{outerDocument.selectedItem.value}">
39                                         <mx:Script><![CDATA[ import net.systemeD.potlatch2.mapfeatures.*; ]]></mx:Script>
40                                         </controls:AutoComplete>
41                                 </mx:Component>
42                         </mx:itemEditor>
43                 </mx:DataGridColumn>
44
45                 <!-- Delete button -->
46
47                 <mx:DataGridColumn width="12" editable="false">
48                         <mx:itemRenderer>
49                                 <mx:Component>
50                                         <mx:Image source="@Embed('../../../embedded/delete_small.svg')" 
51                                                 click='event.stopPropagation();outerDocument.removeTag();'
52                                                 buttonMode="true" useHandCursor="true">
53                                         </mx:Image>
54                                 </mx:Component>
55                         </mx:itemRenderer>
56                 </mx:DataGridColumn>
57         </mx:columns>
58
59         <mx:Script><![CDATA[
60
61                 import net.systemeD.halcyon.connection.*;
62                 import mx.collections.*;
63
64                 private var selectedEntity:Entity;
65                 private var tagDataProvider:ArrayCollection;
66
67                 public function init(entity:Entity):void {
68                         if ( tagDataProvider == null ) {
69                                 tagDataProvider = new ArrayCollection();
70                                 dataProvider = tagDataProvider;
71                         }
72
73                         selectedEntity=entity;
74                         updateTagDataProvider();
75                 }
76
77                 private function updateTagDataProvider():void {
78                         tagDataProvider.removeAll();
79                         if (selectedEntity==null) { return; }
80                         var tags:Array = selectedEntity.getTagArray();
81                         tags.sortOn("key");
82                         for each(var tag:Tag in tags) { tagDataProvider.addItem(tag); }
83                 }
84
85                 public function addNewTag():void {
86                         var newKey:String = "(new tag)";
87                         var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
88                         tagDataProvider.addItem(newTag);
89                         editedItemPosition = {rowIndex: tagDataProvider.getItemIndex(newTag), columnIndex: 0};
90                 }
91
92                 public function removeTag():void {
93                         var k:String = selectedItem.key;
94                         selectedEntity.setTag(k, null, MainUndoStack.getGlobalStack().addAction);
95                         updateTagDataProvider();
96                 }
97                 
98                 public function getEntityType():String {
99                         return selectedEntity.getType();
100                 }
101
102         ]]></mx:Script>
103 </mx:DataGrid>