fix minor breakage
[potlatch2.git] / potlatch2.mxml
index c3ff6c6..660b8d9 100755 (executable)
@@ -4,6 +4,8 @@
        xmlns:halcyon="net.systemeD.halcyon.*"
        xmlns:potlatch2="net.systemeD.potlatch2.*"
        layout="vertical"
+       horizontalScrollPolicy="off"
+       verticalScrollPolicy="off"
        horizontalAlign="center" 
        addedToStage="initApp()">
        
@@ -27,6 +29,7 @@
             creationComplete="bgButton.popUp = new BackgroundSelector();"/>
         <mx:PopUpButton id="styleButton" label="Map Style" openAlways="true"
             creationComplete="styleButton.popUp = new StyleSelector();"/>
+        <mx:Button label="GPS" click="trackLoader.load();"/>
         <mx:Spacer width="100%"/>
         <mx:Button label="Undo" click="MainUndoStack.getGlobalStack().undo();"
             enabled="{MainUndoStack.getGlobalStack().canUndo()}"/>
@@ -34,8 +37,9 @@
             enabled="{MainUndoStack.getGlobalStack().canRedo()}"/>
         <mx:Spacer width="100%"/>
         <mx:Button label="Help" click="new HelpDialog().init();" />
-        <mx:Button label="Options" click="new OptionsDialog().init();" />
-        <mx:Button label="Save" icon="@Embed('embedded/save.svg')" click="SaveManager.saveChanges();"/>
+        <mx:Button label="Options" click="new OptionsDialog().init();" /> 
+        <mx:Button label="Save" icon="@Embed('embedded/save.svg')" click="SaveManager.saveChanges();" id="saveButton" 
+                       enabled="false"/>
     </mx:ApplicationControlBar>
     
     <mx:HDividedBox width="100%" height="100%">
             enabled="false" borderThickness="0"
             showEffect="{wipeIn}" hideEffect="{wipeOut}"/>
 
-               <potlatch2:Toolbox id="toolbox" 
-                       height="46" width="253" 
-                       headerHeight="6" headerColors="[black, gray]" 
-                       borderThicknessRight="0" borderThicknessLeft="0" borderThicknessBottom="0" 
-                       paddingLeft="4" paddingTop="4" layout="horizontal"
-                       x="10" y="300">
-
-               <mx:Button icon="@Embed('embedded/delete.svg')"
-                               click='toolbox.doDelete();' 
-                               width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/direction.svg')" 
-                click='toolbox.doReverseDirection();'
-                               width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/cut.svg')" 
-                click='toolbox.doSplit();'
-                               width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/straighten.svg')" 
-                               click='toolbox.doStraighten();' 
-                               width="28" height="28" textAlign="left" paddingLeft="5" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/circle.svg')" 
-                               click='toolbox.doCircularise();' 
-                               width="28" height="28" textAlign="left" paddingLeft="4" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/quadrilateralise.svg')" 
-                               click='toolbox.doQuadrilateralise();' 
-                               width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/parallel.svg')" 
-                               click='toolbox.doParallelise();' 
-                               width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
-
-               </potlatch2:Toolbox>
-
     </mx:Canvas>
 
     </mx:HDividedBox>
                import mx.core.IChildList;
         import mx.containers.Canvas;
                import mx.core.Application;
-               import com.yahoo.maps.api.YahooMap;
-               import com.yahoo.maps.api.YahooMapEvent;
-               import com.yahoo.maps.api.core.location.LatLon;
         import mx.events.DragEvent;
         import mx.managers.DragManager;
         import mx.core.DragSource;
+        import mx.controls.Alert;
 
         public var theMap:Map;
         public var theController:EditController;
-               public var yahooListener:Object = new Object();
-               public var yahoo:YahooMap = new YahooMap(); 
+               public var yahoo:Yahoo;
+               public var trackLoader:TrackLoader;
+               public var toolbox:Toolbox;
+
+               private var savecount:uint=0;
+               private var loadcount:uint=0;
+               
+               public var version:String="(Foxbase) Alpha";                    // mxml won't read it if it's a const. Go figure
 
                private function initApp():void {
 
                        Globals.vars.map_area = map_area;
-                       Globals.vars.yahoo = yahoo;
                        Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
                        var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
+                       Globals.vars.nocache = loaderInfo.parameters['nocache'] == 'true';
+
+                       // populate sharedObject with loaderInfo parameters if supplied
+                       var obj:SharedObject = SharedObject.getLocal("user_state");
+                       var objChanged:Boolean = false;
+                       if (loaderInfo.parameters['tileurl']) {
+                               obj.setProperty('background_url',loaderInfo.parameters['tileurl']); 
+                               obj.setProperty('background_name','Custom'); 
+                               objChanged=true;
+                       }
+                       if (loaderInfo.parameters['style']) { 
+                               obj.setProperty('stylesheet_url',loaderInfo.parameters['style']);
+                               obj.setProperty('stylesheet_name','Custom');
+                               objChanged=true; 
+                       }
+                       if (objChanged) { obj.flush(); }
+
+                       // load imagery and style XML
+               var request:DebugURLRequest = new DebugURLRequest("imagery.xml");
+               var loader:URLLoader = new URLLoader();
+               loader.addEventListener(Event.COMPLETE, onImageryLoad);
+               loader.load(request.request);
+
+                       var request2:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
+                       var loader2:URLLoader = new URLLoader();
+               loader2.addEventListener(Event.COMPLETE, onStylesheetsLoad);
+               loader2.load(request2.request);
 
                        // map backdrop object
             var w:uint = map_area.width;
             b.graphics.endFill();
                        _root.addChild(b);
 
-                       // Add Yahoo! background
-                       yahoo.init("f0a.sejV34HnhgIbNSmVHmndXFpijgGeun0fSIMG9428hW_ifF3pYKwbV6r9iaXojl1lU_dakekR", w, h);  
-                       yahoo.mapType="satellite";
-                       _root.addChild(yahoo);
-                       yahoo.visible=false;
-                       yahoo.alpha=0.5;
-
-                       // Yahoo! listeners
-                       yahooListener.yahooInit=function(event:YahooMapEvent):void {
-                               yahoo.zoomLevel = 18-theMap.scale;
-                               yahoo.centerLatLon = new LatLon(theMap.centre_lat, theMap.centre_lon);
-
-                               yahooListener.moveHandler=function(event:MapEvent):void {
-                                       if (!yahoo.visible) { return; }
-                                       yahoo.zoomLevel=18-event.params.scale;
-                                       yahoo.centerLatLon=new LatLon(event.params.lat, event.params.lon);
-                               };
-                               theMap.addEventListener(MapEvent.MOVE, yahooListener.moveHandler);
-
-                               yahooListener.resizeHandler=function(event:MapEvent):void {
-                                       yahoo.setSize(event.params.width, event.params.height);
-                               };
-                               theMap.addEventListener(MapEvent.RESIZE, yahooListener.resizeHandler);
-                       }
-                       yahoo.addEventListener(YahooMapEvent.MAP_INITIALIZE, yahooListener.yahooInit);
-
-                       // add map
+                       // create map and Yahoo
                        theMap=new Map(this.loaderInfo.parameters);
                        theMap.backdrop=b;
+                       yahoo=new Yahoo(w,h,theMap);
+                       yahoo.hide();
+                       _root.addChild(yahoo);
                        _root.addChild(theMap);
-            theMap.updateSize(w, h);
+            theMap.updateSize(w,h);
 
                        // add mask for map
                        var s:Sprite=new Sprite();
                        _root.addChild(s);
                        theMap.mask=s;
 
-                       // add crosshair
-                       /*s=new Sprite();
-                       s.graphics.lineStyle(2,0);
-                       s.graphics.moveTo(stage.stageWidth/2-20,stage.stageHeight/2);
-                       s.graphics.lineTo(stage.stageWidth/2+20,stage.stageHeight/2);
-                       s.graphics.moveTo(stage.stageWidth/2,stage.stageHeight/2-20);
-                       s.graphics.lineTo(stage.stageWidth/2,stage.stageHeight/2+20);
-                       _root.addChild(s);*/
-
                        // mouse-up handler attached to stage, so the user can release outside the map
                        stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
                        Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
                        // keyboard event attached to stage
                        stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
 
-                       // example listener event
-                       var myListenerObj:Object = new Object();
-                       myListenerObj.mapHandler=function(event:MapEvent):void {
-                               Globals.vars.debug.appendText("Download event fired - "+event.params.minlat+","+event.params.minlon+"\n");
-                       };
-                       theMap.addEventListener(MapEvent.DOWNLOAD, myListenerObj.mapHandler);
+                       // position toolbox
+                       toolbox=Toolbox(PopUpManager.createPopUp(this,Toolbox,false));
+                       toolbox.init(theController);
+                       toolbox.x=stage.stageWidth-toolbox.width-15;
+                       toolbox.y=stage.stageHeight-toolbox.height-15;
 
                        // add debug field
                        var t:TextField=new TextField();
             var conn:Connection = Connection.getConnectionInstance();
             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
-
-                       // and to import a shapefile...
-//                     var importer:ShpImporter = new ShpImporter(theMap, ["http://127.0.0.1/~richard/shp/wildrnp020.shp","http://127.0.0.1/~richard/shp/wildrnp020.shx","http://127.0.0.1/~richard/shp/wildrnp020.dbf"]);
+            conn.addEventListener(Connection.SAVE_STARTED, onDataStart);
+            conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
+            conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
+            conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
+                       conn.addEventListener(MapEvent.ERROR, onMapError);
+
+                       // create GPS trackloader
+                       trackLoader=new TrackLoader(theMap,conn.apiBase);
+
+            // show help dialog on startup, if required
+            // show_help == always => on every startup
+            // show_help == once => show on first startup only
+            var show_help:String = loaderInfo.parameters["show_help"];
+
+            if (show_help == 'always' || (show_help == 'once' && obj.data["help_shown"] != "true")) {
+              new HelpDialog().init();
+            }
+               }
+               
+               public function onMapError(event:MapEvent):void {
+                       Alert.show(event.params.message, 'Error', mx.controls.Alert.OK);
                }
 
         public function onResizeMap():void {
             if ( theMap != null )
                 theMap.updateSize(map_area.width, map_area.height);
+
+                       if (toolbox) {
+                               toolbox.x=Math.min(toolbox.x,stage.stageWidth-toolbox.width-15);
+                               toolbox.y=Math.min(toolbox.y,stage.stageHeight-toolbox.height-15);
+                       }
         }
 
         private function onDataStart(event:Event):void {
-            dataWorking.text = event.type == Connection.LOAD_STARTED ? "Loading data..." : "Saving Data...";
-            dataWorking.visible = true;
+                       switch (event.type) {
+                               case Connection.LOAD_STARTED:   loadcount++; break;
+                               case Connection.SAVE_STARTED:   savecount++; break;
+                       }
+                       updateDataWorking();
         }
         private function onDataComplete(event:Event):void {
-            dataWorking.visible = false;
+                       switch (event.type) {
+                               case Connection.LOAD_COMPLETED: loadcount--; break;
+                               case Connection.SAVE_COMPLETED: savecount--; break;
+                       }
+            updateDataWorking();
         }
-        
+               private function updateDataWorking():void {
+                       if (loadcount>0 && savecount>0) { dataWorking.text="Loading/saving..."; }
+                       else if (loadcount>0)           { dataWorking.text="Loading data..."; }
+                       else if (savecount>0)           { dataWorking.text="Saving data..."; }
+                       else                            { dataWorking.text=""; }
+                       dataWorking.visible=(dataWorking.text!="");
+               }
+        private function onDataDirty(event:Event):void {
+                       saveButton.enabled=true;
+            if (ExternalInterface.available) {
+              ExternalInterface.call("markChanged", false);
+            }
+               }
+               private function onDataClean(event:Event):void {
+                       saveButton.enabled=false;
+            if (ExternalInterface.available) {
+              ExternalInterface.call("markChanged", true);
+            }
+               }
+               
+        private function onImageryLoad(event:Event):void {
+                       var xml:XML = new XML(URLLoader(event.target).data);
+                       var saved_url:String = SharedObject.getLocal("user_state").data['background_url'];
+                       var saved_name:String= SharedObject.getLocal("user_state").data['background_name'];
+                       var xml_url:String, xml_name:String, isSet:Boolean=false;
+            theController.imagery=new Array(
+                               { label: "None", data: "" },
+                               { label: "Yahoo", data: "yahoo" } );
+                       for each(var set:XML in xml.set) {
+                               xml_url= set.child("url");
+                               xml_name=set.child("name");
+                theController.imagery.push({ label:xml_name, data:xml_url });
+                               if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isSet=true; }
+                       }
+                       if (!isSet) { theController.imagery.push({ label:saved_name, data:saved_url }); }
+
+                       for each (var bg:Object in theController.imagery) {
+                               if (bg['label']==saved_nameĀ || bg['data']==saved_url) {
+                                       setBackground(bg['label'], bg['data']);
+                               }
+                       }
+               }
+               
+               public function setBackground(name:String,url:String):void {
+                       // ** this should take an object with all parameters (source tag, etc.)
+                       if (url=='yahoo') { theMap.setBackground('') ; yahoo.show(); }
+                                    else { theMap.setBackground(url); yahoo.hide(); }
+                       var obj:SharedObject = SharedObject.getLocal("user_state");
+                       obj.setProperty("background_url",url );
+                       obj.setProperty("background_name",name);
+                       obj.flush();
+               }
+
+               private function onStylesheetsLoad(event:Event):void {
+                       var xml:XML = new XML(URLLoader(event.target).data);
+                       var saved_url:String = SharedObject.getLocal("user_state").data['stylesheet_url'];
+                       var saved_name:String= SharedObject.getLocal("user_state").data['stylesheet_name'];
+                       var xml_url:String, xml_name:String, isInMenu:Boolean=false, isSet:Boolean=false;
+
+            // first, build the menu from the stylesheet list.
+            // Also ensure the saved_url is in the menu (might be either saved from before, or supplied via loaderInfo)
+            theController.stylesheets=new Array();
+                       for each(var set:XML in xml.stylesheet) {
+                               xml_url= set.child("url");
+                               xml_name=set.child("name");
+                theController.stylesheets.push({ label:xml_name, data:xml_url });
+                               if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isInMenu=true; }
+                       }
+                       if (saved_url && !isInMenu) { theController.stylesheets.push({ label:saved_name, data:saved_url }); }
+
+            // pick a stylesheet to be set. It should be the saved one, if it is in the menu
+            // or alternatively the first one on the menu,
+            // or finally try 'potlatch.css'
+                       for each (var ss:Object in theController.stylesheets) {
+                               if (ss['label']==saved_nameĀ || ss['data']==saved_url) {
+                                       setStylesheet(ss['label'], ss['data']);
+                    isSet = true;
+                    break;
+                               }
+                       }
+            if (!isSet) {
+              if(theController.stylesheets.length > 0) {
+                var s:Object = theController.stylesheets[0];
+                setStylesheet(s['label'], s['data']);
+              } else {
+                //hit and hope. FIXME should this be an error state?
+                theController.stylesheets.push({ label:'Potlatch', data:'potlatch.css'});
+                setStylesheet('Potlatch','potlatch.css');
+              }
+            }
+               }
+
+               public function setStylesheet(name:String,url:String):void {
+                       theMap.setStyle(url);
+                       var obj:SharedObject = SharedObject.getLocal("user_state");
+                       obj.setProperty("stylesheet_url",url);
+                       obj.setProperty("stylesheet_name",name);
+                       obj.flush();
+               }
+
 
-        
         private function dragEnterHandler(event:DragEvent):void {
             // Get the drop target component from the event object.
             var dropTarget:Canvas=event.currentTarget as Canvas;
         }
         
         private function dragDropHandler(event:DragEvent):void {
-            // Get the data identified by the color format from the drag source.
-            // Blame http://www.adobe.com/devnet/flex/quickstart/adding_drag_and_drop/#manual
-            // for whatever horrid abuse of "color format" this is doing
+                       // Deselect the dragged icon
+                       if (event.dragSource.dataForFormat('container')) {
+                               event.dragSource.dataForFormat('container').selectedItem=-1;
+                       }
+
+                       // Get the tags
             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);