Add and populate a relations member panel
[potlatch2.git] / net / systemeD / potlatch2 / TagViewer.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:VBox
3         xmlns:mx="http://www.adobe.com/2006/mxml"
4         xmlns:flexlib="flexlib.containers.*"
5     backgroundColor="white"
6     initialize="loadFeatures()">
7
8   <mx:ViewStack id="stack" width="100%" height="100%">
9   <mx:VBox width="100%" height="100%" label="Simple">
10     <mx:HBox borderStyle="inset" verticalAlign="middle" width="100%" paddingLeft="3" id="iconContainer">
11         <mx:Image id="iconImage"/>
12         <mx:VBox width="100%" verticalGap="1">
13           <mx:PopUpButton id="popupChange" creationComplete="initFeatureBox()" openAlways="true" width="100%"/>
14           <mx:Text condenseWhite="true" width="100%" id="iconText"/>
15         </mx:VBox>
16         <mx:LinkButton label="?" click="openDescription()" id="helpLabel"/>
17     </mx:HBox>
18     <flexlib:SuperTabNavigator id="editorStack" width="100%" height="100%" paddingLeft="2" paddingRight="2"
19         allowTabSqueezing="false" minTabWidth="10" closePolicy="close_never"
20         scrollSpeed="20" change="ensureEditorsPopulated(IndexChangedEvent(event).relatedObject as VBox)"/>
21   </mx:VBox>
22
23   <mx:VBox width="100%" height="100%" label="Advanced" initialize="checkAdvanced()" verticalGap="1">
24     <mx:Label id="advancedID">
25       <mx:htmlText><![CDATA[<i>No Selection</i>]]></mx:htmlText>
26     </mx:Label>
27
28     <mx:DataGrid editable="true" width="100%" height="75%" id="advancedTagGrid">
29             <mx:columns>
30                 <mx:DataGridColumn editable="true" dataField="key" headerText="Key"/>
31                 <mx:DataGridColumn editable="true" dataField="value" headerText="Value"/>
32             </mx:columns>
33     </mx:DataGrid>
34
35     <mx:HBox horizontalAlign="right" width="100%">
36       <mx:LinkButton label="Delete" click="removeTag()" enabled="{advancedTagGrid.selectedItem != null? true : false}"/>
37       <mx:LinkButton label="Add" click="addNewTag()"/>
38     </mx:HBox>
39     
40     <mx:DataGrid editable="true" width="100%" height="25%" id="relationsGrid"
41         doubleClickEnabled="true"
42         itemDoubleClick="editRelation(relationsGrid.selectedItem.id)">
43         <mx:columns>
44             <mx:DataGridColumn editable="false" dataField="description" headerText="Relation"/>
45             <mx:DataGridColumn editable="false" dataField="id_idx" headerText="ID"/>
46             <mx:DataGridColumn editable="true" dataField="role" headerText="Role"/>
47         </mx:columns>
48     </mx:DataGrid>
49
50     <mx:HBox horizontalAlign="right" width="100%">
51       <mx:LinkButton label="Remove from" click="removeFromRelation(relationsGrid.selectedItem.id, relationsGrid.selectedItem.index)" 
52                       enabled="{relationsGrid.selectedItem != null? true : false}"/>
53       <mx:LinkButton label="Add to" click="addToRelation()"/>
54     </mx:HBox>
55
56   </mx:VBox>
57
58   <mx:VBox width="100%" height="100%" label="Members" id="membersVBox" initialize="checkMembers()" verticalGap="1">
59     <mx:Label id="membersText" text="Relation Members"/>
60     <mx:DataGrid editable="true" width="100%" height="100%" id="membersGrid">
61       <mx:columns>
62         <mx:DataGridColumn editable="false" dataField="typeDesc" headerText="Type"/>
63         <mx:DataGridColumn editable="false" dataField="id" headerText="ID"/>
64         <mx:DataGridColumn editable="true" dataField="role" headerText="Role"/>
65       </mx:columns>
66     </mx:DataGrid>
67   </mx:VBox>
68   
69   </mx:ViewStack>
70
71   <mx:LinkBar dataProvider="{stack}"/>
72
73   <mx:Script><![CDATA[
74       import net.systemeD.halcyon.connection.*;
75       import net.systemeD.potlatch2.mapfeatures.*;
76
77       import mx.collections.*;
78       import mx.containers.*;
79       import mx.events.*;
80       import mx.core.*;
81       import mx.managers.PopUpManager;
82       import flash.geom.Point;
83       import flash.net.*;
84       
85       private var mapFeatures:MapFeatures;
86       private var selectedEntity:Entity;
87       private var tagDataProvider:ArrayCollection;
88       private var tw:CategorySelector = null;
89       private var feature:Feature = null;
90
91       public function setEntity(entity:Entity):void {
92           if ( selectedEntity != entity ) {
93               if ( selectedEntity != null )
94                   selectedEntity.removeEventListener(Connection.TAG_CHANGE, tagChanged);
95               selectedEntity = entity;
96               if ( selectedEntity != null )
97                   selectedEntity.addEventListener(Connection.TAG_CHANGE, tagChanged);
98           }
99
100           if ( advancedID != null )
101               setupAdvanced(entity);
102           if (entity is Relation) {
103               stack.addChild(membersVBox);
104           }
105           refreshFeatureIcon();
106       }
107
108       private function refreshFeatureIcon():void {
109           var oldFeature:Feature = feature;
110           feature = selectedEntity == null ? null : mapFeatures.findMatchingFeature(selectedEntity);
111           if ( feature != oldFeature )
112               initialiseEditors();
113
114           if ( feature != null )
115               setFeatureIcon(selectedEntity, feature);
116           else
117               blankFeatureIcon(selectedEntity);
118       }
119
120       private function setFeatureIcon(entity:Entity, feature:Feature):void {
121           //blankFeatureIcon(entity);
122           
123           iconImage.source = feature.image;
124
125           var txt:String = feature.htmlDetails(entity);
126           iconText.htmlText = txt;
127           popupChange.label = feature.name;
128           setLimitTypes(entity);
129           tw.setSelectedFeature(feature);
130           helpLabel.visible = feature.hasHelpURL();
131       }
132       
133       private function setLimitTypes(entity:Entity):void {
134           var type:String = null;
135           if ( entity is Node )
136               type = "point";
137           else if ( entity is Way )
138               type = Way(entity).isArea() ? "area" : "line";
139           else if ( entity is Relation )
140               type = "relation";
141           tw.setLimitTypes(type);
142       }
143
144       private function blankFeatureIcon(entity:Entity):void {
145           iconImage.source = null;
146           iconText.htmlText = entity == null ?
147                "<i>Nothing selected</i>" :
148                "<b>Not recognised</b><br/>Try looking at the tags under the advanced properties";
149           popupChange.label = "unknown";
150           setLimitTypes(entity);
151           tw.setSelectedFeature(null);
152           helpLabel.visible = false;
153       }
154
155       private var tabComponents:Object = {};
156       
157       private function initialiseEditors():void {
158           editorStack.removeAllChildren();
159           if ( selectedEntity == null || feature == null )
160               return;
161           
162           var editorBox:VBox = createEditorBox();
163           editorBox.label = "Basic";
164           editorStack.addChild(editorBox);
165           
166           var tabs:Object = {};
167           tabComponents = {};
168           
169           for each (var factory:EditorFactory in feature.editors) {
170               if ( factory.presence.isEditorPresent(factory, selectedEntity, null) ) {
171                   var editor:DisplayObject = factory.createEditorInstance(selectedEntity);
172                   if ( editor != null )
173                       editorBox.addChild(editor);
174               }
175               var category:String = factory.category;
176               var tab:VBox = tabs[category];
177               if ( tab == null ) {
178                   tab = createEditorBox();
179                   tab.label = category;
180                   editorStack.addChild(tab);
181                   tabs[category] = tab;
182                   tabComponents[tab] = [];
183               }
184               var catEditor:DisplayObject = factory.createEditorInstance(selectedEntity);
185               if ( catEditor != null )
186                   tabComponents[tab].push(catEditor);
187               //    tab.addChild(catEditor);
188           }
189       }
190       
191       private function createEditorBox():VBox {
192           var box:VBox = new VBox();
193           box.percentWidth = 100;
194           box.percentHeight = 100;
195           return box;
196       }
197
198       private function ensureEditorsPopulated(tab:VBox):void {
199           var components:Array = tabComponents[tab];
200           if ( components == null || tab == null || tab.numChildren >= components.length )
201               return;
202           for each (var component:DisplayObject in components ) {
203               tab.addChild(component);
204           }
205       }
206       
207       private function checkAdvanced():void {
208           if ( selectedEntity != null )
209              setupAdvanced(selectedEntity);
210       }
211
212       private function setupAdvanced(entity:Entity):void {
213           if ( tagDataProvider == null ) {
214               tagDataProvider = new ArrayCollection();
215               advancedTagGrid.dataProvider = tagDataProvider;
216           }
217
218           tagDataProvider.removeAll();
219           
220           if ( entity == null ) {
221               advancedID.htmlText = "";
222           } else {
223               var entityText:String = "xx";
224               if ( entity is Node ) entityText = "Node";
225               else if ( entity is Way ) entityText = "Way";
226               else if ( entity is Relation ) entityText = "Relation";
227               advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
228
229               var tags:Array = entity.getTagArray();
230               tags.sortOn("key");
231               for each(var tag:Tag in tags)
232                   tagDataProvider.addItem(tag);
233           }
234           
235           if ( entity == null ) {
236               relationsGrid.dataProvider = null;
237           } else {
238               var relations:Array = [];
239               for each( var rel:Relation in entity.parentRelations ) {
240                   for each( var memberIndex:int in rel.findEntityMemberIndexes(entity)) {
241                     var props:Object = {};
242                     props["relation"] = rel;
243                     props["id"] = rel.id;
244                     props["index"] = memberIndex;
245                     props["role"] = rel.getMember(memberIndex).role;
246                     props["description"] = rel.getDescription();
247                     props["id_idx"] = rel.id + "/"+memberIndex;
248                     
249                     relations.push(props);
250                   }
251               }
252               relationsGrid.dataProvider = relations;
253           }
254       }
255       
256       private function checkMembers():void {
257           if (selectedEntity is Relation) {
258             setupMembers(selectedEntity as Relation);
259           }
260       }
261       
262       private function setupMembers(rel:Relation):void {
263           var members:Array = [];
264           for (var i:int=0 ; i<rel.length; i++) {
265             var props:Object = {};
266             var member:RelationMember = rel.getMember(i);
267             props["id"] = member.entity.id;
268             props["typeDesc"] = member.entity.getType();
269             props["role"] = member.role;
270             
271             members.push(props);
272           }
273           membersGrid.dataProvider = members;
274       }
275       
276       private function editRelation(id:uint):void {
277           trace("edit relation "+id);
278           var panel:RelationEditorPanel = RelationEditorPanel(
279               PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
280           panel.setRelation(Connection.getConnectionInstance().getRelation(id));
281           PopUpManager.centerPopUp(panel);
282       }
283       
284       private function tagChanged(event:TagEvent):void {
285           refreshFeatureIcon();
286           
287           if ( tagDataProvider != null ) {
288               // check to see if the key is already in our list
289               var exists:Boolean = false;
290               var tag:Tag = null;
291               var i:uint;
292               for ( i = 0; i < tagDataProvider.length && !exists; i++ ) {
293                   tag = Tag(tagDataProvider.getItemAt(i));
294                   exists = tag.key == event.key;
295               }
296               if ( !exists ) {
297                   tag = new Tag(selectedEntity, event.key, event.newValue);
298                   tagDataProvider.addItem(tag);
299                   tagDataProvider.refresh();
300               } else {
301                   if ( event.newValue == null ) {
302                       tagDataProvider.removeItemAt(i-1);
303                       tagDataProvider.refresh();
304                   } else {
305                       tagDataProvider.itemUpdated(tag, "value");
306                   }
307               }
308           }
309       }
310
311       public function loadFeatures():void {
312           mapFeatures = MapFeatures.getInstance();
313           stack.removeChild(membersVBox); // remove by default, will be added if relation
314       }
315
316       public function openDescription():void {
317           trace("open description here");
318           if ( feature != null && feature.hasHelpURL() )
319               navigateToURL(new URLRequest(feature.helpURL), "potlatch_help");
320       }
321
322       public function addNewTag():void {
323           var newKey:String = "(new tag)";
324           var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
325           tagDataProvider.addItem(newTag);
326           advancedTagGrid.editedItemPosition = {rowIndex: tagDataProvider.getItemIndex(newTag), columnIndex: 0};
327       }
328
329       public function removeTag():void {
330           var k:String = advancedTagGrid.selectedItem.key;
331           selectedEntity.setTag(k, null);
332       }
333       
334       public function addToRelation():void {
335           new RelationSelectPanel().init(selectedEntity);        
336       }
337       
338       public function removeFromRelation(id:Number, index:int):void {
339           Connection.getConnectionInstance().getRelation(id).removeMemberByIndex(index);
340       }
341       
342       public function initFeatureBox():void {
343           tw = new CategorySelector();
344           tw.addEventListener("selectedType", changeFeatureType);
345           popupChange.popUp = tw;
346       }
347       
348       public function changeFeatureType(event:Event):void {
349           if ( selectedEntity == null )
350               return;
351
352           var newFeature:Feature = tw.selectedType;
353           
354           // remove tags from the current feature
355           if ( feature != null ) {
356               for each( var oldtag:Object in feature.tags ) {
357                   selectedEntity.setTag(oldtag["k"], null);
358               }
359           }
360           
361           // set tags for new feature
362           if ( newFeature != null ) {
363               for each( var newtag:Object in newFeature.tags ) {
364                   selectedEntity.setTag(newtag["k"], newtag["v"]);
365               }
366           }
367           
368           popupChange.close();
369       }
370   ]]></mx:Script>
371 </mx:VBox>
372