trouble passing data between widgets

3966
16
08-16-2011 06:19 AM
WadeGivens
New Contributor II
I have a custom query tool that was developed for me, and I would like to pass 2 combobox selections to a print widget to use as the title of a print template in a print widget.  I have taken a look at the post where Robert explained how to do this (http://forums.arcgis.com/threads/13863-Communication-between-widgets?highlight=widget+communication), but I still seem to be having a bit trouble.  the code I have put together is below.  I think the problem may be in how I'm building my array in the custom query widget and/or how I'm trying to recall the items in the array in my print widget.  Any suggestions out there?


// CUSTOM QUERY WIDGET

// share data
var layoutArr:ArrayCollection = new ArrayCollection();   
layoutArr.addItemAt(String(cboLayerHybrid.selectedLabel), 0);
layoutArr.addItemAt(String(cboLayerTrait.selectedLabel), 1);
addSharedData("LayoutTitle", layoutArr);

// CUSTOM PRINT WIDGET

private function init():void
    {
 trace("MyPrintWidget init()");
 AppEvent.addListener(AppEvent.DATA_PUBLISH, sharedDataUpdated);
    }
   
private function sharedDataUpdated(event:AppEvent):void
    {
        var data:Object = event.data;    
        if (data.key == "LayoutTitle")
 
        {
            var theObj:Object = data.collection[0];
            layoutArr.addItem(theObj);
            mapproduct = layoutArr.getItemAt(0);
            maptrait = layoutArr.getItemAt(1);
            title = mapproduct + maptrait;
        }
    }



Thanks,
Wade
Tags (2)
0 Kudos
16 Replies
WadeGivens
New Contributor II
Okay, dummy me.  I had a line of code missing from the widget I was wanting to share data from ( the line "addSharedData("LayoutTitle", layoutArr);" apparently is pretty important).  Everything works fine if I have the print widget set to preload.  Is it possible to make this work without requiring the print widget to be preloaded?

Wade
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Wade,

  Sure it just a bit more code to add:

<?xml version="1.0" encoding="utf-8"?>

<viewer:BaseWidget xmlns:esri="http://www.esri.com/2008/ags"
         xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" 
         xmlns:mxeffects="com.adobe.ac.mxeffects.*"
         xmlns:flash="flash.text.*"
         xmlns:viewer="com.esri.viewer.*"
         xmlns:widgets="widgets.*"
         x="600" y="300"
         widgetConfigLoaded="init()">
    
    <fx:Script>
        <![CDATA[
            import com.esri.ags.Graphic;
            import com.esri.ags.Map;
            import com.esri.ags.events.GeometryServiceEvent;
            import com.esri.ags.layers.GraphicsLayer;
            import com.esri.ags.layers.Layer;
            import com.esri.ags.symbols.SimpleFillSymbol;
            import com.esri.ags.symbols.SimpleLineSymbol;
            import com.esri.viewer.ViewerContainer;
            import com.esri.viewer.AppEvent;
            import com.esri.viewer.utils.Hashtable;
            
            import flash.display.Sprite;
            import flash.net.SharedObject;
            import flash.printing.PrintJob;
            import flash.text.TextField;
            
            import mx.collections.ArrayCollection;
            import mx.containers.Canvas;
            import mx.controls.Alert;
            import mx.controls.Image;
            import mx.core.UIComponent;
            import mx.graphics.ImageSnapshot;
            import mx.graphics.codec.JPEGEncoder;
            import mx.graphics.codec.PNGEncoder;
            import mx.managers.CursorManager;
            import mx.managers.PopUpManager;
            
            import widgets.myPrint.PrintPreview;
            import widgets.myPrint.layouts.A5_Landscape;
            import widgets.myPrint.utils.PrintMapUtil;

            
            //labels
            [Bindable]
            private var image:Image;
            
            [Bindable]
            private var graphic:Graphic;
            [Bindable]
            private var title:String;

            [Bindable]
            private var titleLabel:String;
            
            [Bindable]
            private var mapproduct:String;
            [Bindable]
            private var maptrait:String;
            
            private function init():void
            {
                trace("MyPrintWidget init()");
                AppEvent.addListener(AppEvent.DATA_PUBLISH, sharedDataUpdated);
                AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdated2);
                fetchSharedData();
            }
            
            private function sharedDataUpdated(event:AppEvent):void
            {
                var data:Object = event.data;                
                if (data.key == "LayoutTitle")
                {
                    var theObj:Object = data.collection[0];
                    mapproduct = theObj.mapproduct;
                    maptrait = theObj.maptrait;
                    title = mapproduct + maptrait;
                }
            }

            private function sharedDataUpdated2(event:AppEvent):void
            {
                var dataTable:Hashtable = event.data as Hashtable;
                if (dataTable.containsKey("LayoutTitle"))
                {
                    var recAC:ArrayCollection = dataTable.find("LayoutTitle") as ArrayCollection;
                    var theObj:Object = recAC[0];
                    mapproduct = theObj.mapproduct;
                    maptrait = theObj.maptrait;
                    title = mapproduct + maptrait;
                }
            }
            
    
            private function landscapePrintPreview():void
            {
                graphicsLayer.clear();
                // create instance of our map layout
                var mapLayout:A5_Landscape = new A5_Landscape();
                mapLayout.initialize();
                mapLayout.map = map;
                mapLayout.inputTxt.text = title;                
                
                // Create the popup and set its map layout 
                var popup:PrintPreview = PrintPreview(PopUpManager.createPopUp(map,PrintPreview,false));
                popup.mapLayout = mapLayout;
            }
            
            private function imageHandler(image:Image):void 
            {
                map.zoomSliderVisible = false;
                map.scaleBarVisible = false;
                
                var graphic:Graphic = PrintMapUtil.trimmedExtent(map,
                    image.width,
                    image.height);
                graphicsLayer.clear();
                graphicsLayer.add(graphic);
                
                image.source = PrintMapUtil.trimmedMap(map,
                    image.width,
                    image.height);
                map.zoomSliderVisible = false;
                map.scaleBarVisible = false;
            }
            
            private function widgetClosedHandler(event:Event):void
            {
                graphicsLayer.visible = false;
                setMapNavigation(null, null);
            }        
            
            private function widgetMinimizedHandler(event:Event):void
            {
                this.widgetClosedHandler(event); 
            }
            
            private function widgetOpenedHandler(event:Event):void
            {
                graphicsLayer.visible = true; 
            }
        ]]>
    </fx:Script>
    
    <fx:Declarations>
        <esri:SimpleFillSymbol id="sfs" color="0xE95020" alpha="1" style="solid">
            <esri:SimpleLineSymbol color="0xFF0000" width="4" alpha="1" style="solid" />
        </esri:SimpleFillSymbol>
        <esri:GraphicsLayer id="graphicsLayer" symbol="{sfs}"/>
    </fx:Declarations>
    <viewer:WidgetTemplate id="wTemplate"
                           closed="widgetClosedHandler(event)" 
                           open="widgetOpenedHandler(event)"
                           minimized="widgetMinimizedHandler(event)"
                           skinClass="com.esri.viewer.skins.WidgetTemplateSkin"
                           width="260" height="134">
        <s:VGroup width="100%" height="100%" verticalAlign="top" horizontalAlign="center"> 
            <mx:Form id="frmPrint" verticalScrollPolicy="off" paddingBottom="0" width="100%" height="86">
                <s:HGroup horizontalAlign="center" width="100%" verticalAlign="middle">
                    <mx:Text id="labelTxt" text="Map Title: " textAlign="right"/>
                    <s:TextInput id="input" text="{title}"/>
                </s:HGroup>
                <s:HGroup horizontalAlign="center" width="100%">
                    <s:Button label="Print Preview" click="landscapePrintPreview()"/>
                </s:HGroup>
            </mx:Form>
        </s:VGroup>
    </viewer:WidgetTemplate>

</viewer:BaseWidget>
0 Kudos
WadeGivens
New Contributor II
Robert, you cease to amaze me with what you can do in Flex 😄

Thank you so much for what you contribute on a day to day basis on these forums!  It makes us hobby programmers spend less and less time wanting to take baseball bats to our computers 🙂

Wade
0 Kudos
WadeGivens
New Contributor II
Would asking how to receive the data in a popup window be pushing my luck for today?  Thought I could just implement the code you provided and be good to go until I found out the FetchSharedData() is inherited from the BaseWidget.  Is there something similar to use in popups?

Thanks,
Wade
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Wade,

   The only thing fetchSharedData does is dispatch this appevent:

AppEvent.dispatch(AppEvent.DATA_FETCH_ALL);
0 Kudos
GabrielSuárez
New Contributor
I'm getting this errors on the lines:

private function init():void
   {
    trace("LabsWidget init()");
    AppEvent.addListener(AppEvent.DATA_PUBLISH, sharedDataUpdated);
    AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdated2);
    fetchSharedData();
   }

it says, "1061: Call to a possibly undefined method addListener through a reference with static type Class."

on the 2 AppEvent.addListener calls :S... im trying to sent 1 number from one widget as an input of the other so it shows some information about the subject the user selects :S... any help would be appreciated

nvm, already fix taht... but now i face another problem, im using the same widget to display some content on it, so, i would like to know how do i refresh it's content without restarting the whole application, i mean, when i open the widget the first time it shows me what i need, but if i click on another option, this widget stills shows me the information from the first selection, and i don't know how to make the widget to refrehs it's content S:.. any help?
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Dex,

   You are getting those errors because the code your are attempting to use in for Flex Viewer 2.4 only.

In 2.3.1 and earlier it would be:

ViewerContainer.addEventListener(AppEvent.DATA_PUBLISH, sharedDataUpdated);
0 Kudos