Migrating Widgets To The Arcgis Viewer For Flex: Some Tips and Tricks To Ease The Process
Migrating Widgets To The Arcgis Viewer For Flex: Some Tips and Tricks To Ease The Process
Migrating Widgets To The Arcgis Viewer For Flex: Some Tips and Tricks To Ease The Process
This article tells developers how to migrate widgets developed with the Sample Flex Viewer (SFV) to the ArcGIS Viewer for Flex. It assumes readers have experience using the ArcGIS API for Flex and are familiar with the Adobe Flash development environment, and experience developing with the Flex API and Adobe Flash is strongly recommended.
Released in November 2008, the SFV was a developer sample built on the ArcGIS API for Flex. It enabled nonprogrammers to deploy a rich Internet application for ArcGIS Server with minimal effort. Since its release, the SFV has been downloaded more than 30,500 times, and many sites have been built on the SFV. SFV also provided a framework for Flex API developers to customize and extend the viewer. One important area was the ability to create custom widgets. Widgets are modular pieces of code that extend or add to the functionality of the SFV. They can be tailored by the widget developer for specific tasks that require particular data and conditions to be present in the viewer, or they can be generic and allow SFV to be configured by nonprogrammers to work with their own data. More than 50 widgets have been created for SFV and shared on the ArcGIS API for Flex code gallery. Many of these widgets can still be accessed from the Esri ArcScripts site (arcscripts.esri.com/; search for flex AND viewer). In September 2010, Esri released the ArcGIS Viewer for Flexthe official product release of the SFV. It includes 20 core widgets that support many standard web mapping application functionalities. Many users wondered about the SFV widgets that were produced and shared on the ArcGIS API for Flex code gallery. Would these widgets just work in the new ArcGIS Viewer for Flex application? Unfortunately, the answer is no. The ArcGIS Viewer for Flex uses a framework that differs from SFV. It is based on a newer release of the ArcGIS API for Flex and utilizes the latest Adobe Flash technology. To use widgets previously developed for the SFV, the code base for those widgets must be migrated and recompiled for the new ArcGIS Viewer for Flex 2.x API libraries.
Do this Copy this Note this Avoid this Good practice Dont do this
G
M
< N
32
Developer's Corner
This article provides tips and recommended practices to help Flex developers easily migrate custom widgets from the SFV to the ArcGIS Viewer for Flex. Flex developers should be aware of the differences between the SFV and the ArcGIS Viewer for Flex. These differences are summarized in Table 1.
Table 1 highlights some of the subtlebut keychanges in development patterns between the SFV and the ArcGIS Viewer for Flex. The concepts are the same, even though they may have been renamed. However, this is not a comprehensive list of these differences. At the Adobe software development kit (SDK) level, Adobe recommends using the new Spark components. For example, s:VGroup is used instead of mx:HBox. For more detailed information, see the resources list at the end of this article.
Robert Scheitlin modifies the contents of the legend based on scale dependency and layer visibility. Map services and/or specific layers in a map service may also be excluded from the legend.
33
Jiang searches YouTube for videos, Flickr for photos, and Twitter for Tweets based on a keyword.
34
Developer's Corner
<viewer:states> <s:State name=filterResults/> <s:State name=resultsList/> </viewer:states>
Listing 1: Each state must have a name.
In the old SFV, an animation that occurred when moving from one view to another was handled by a custom ActionScript class called WidgetEffects. In the new viewer, transitions are used for this purpose. The targets for transitions will be the names of the states defined previously. An example is shown in Listing 2.
<viewer:transitions> <s:Transition autoReverse=true toState=*> <s:Fade targets={ [filterResults, resultsList] }/> </s:Transition> </viewer:transitions>
Listing 2: Handle animations between views with transitions.
Copy the MXML elements that define the UI of the old widget, paste them into the new
widget, and comment them out. The commented old code can serve as a reference. This will save some time because it eliminates the need to switch back and forth from old widget code to newboth versions will be present. Moving the widgets MXML code from mx components to Spark components during the code migration is recommended. Use Table 1 to determine the Spark equivalents for some mx components. The WidgetTemplate element is still the base for the widgets UI. The new widget template in the ArcGIS Viewer for Flex has renamed a few of the events. For example, the widgetClosed event is now just closed and the widgetOpened event is now open. The height and width of a widgets UI is defined in the widget template. Each widget state will be a Spark group element, and each ID will share the same name as the states defined earlier. Set the visibility of the group to false and add another attribute, visible. After the attribute visible is typed, add a dot after it, and the automatic code completion will display the available states (when using the Adobe Flash Builder IDE). Choose the state name of the current group.
<viewer:WidgetTemplate id=wTemplate width=430 height=240 open=widgetOpenedHandler(event) closed=widgetClosedHandler(event) minimized=widgetMinimizedHandler(event)> <s:Group id=resultsList width=100% height=100% visible=false visible.resultsList=true> <s:layout> <s:VerticalLayout gap=10 horizontalAlign=center verticalAlign=middle/> </s:layout> </s:Group> <s:Group id=filterResults width=100% height=100% visible=false visible.filterResults=true> <s:layout> <s:VerticalLayout gap=4 horizontalAlign=center verticalAlign=middle/> </s:layout> </s:Group> </viewer:WidgetTemplate>
Listing 3: Widget template
35
Examine the code for old mx components that could be Sparkified. For example, if the old code uses an mx:Text, then its Spark counterpart is s:Label; an mx:HBox could become an s:Hgroup, mx:Button could become s:Button, and mx:ComboBox could become an s:DropDownList. < Another practical tip is to copy all the old mx:Script code from the old widget and paste it inside the new fx:Script block. As mentioned earlier, don't copy the mx:script block in its entirety; just copy the contents between the <![CDATA[ ]]>. There will be several errors that will have to be addressed one at a time. M Replacing the import statements in the script block that have changed in the ArcGIS API for Flex 2.2 is important.
One simple way to fix these is to examine the reported compile error. Doubleclick it to go to the specific line; put the cursor at the end of the offending class; and press Ctrl+Spacebar for Content Assist, which will add the required import statement. < While it is not required, it is a good practice to migrate mx:Repeater to the Spark DataGroup class. Accomplishing this involves creating three new items, *Results.as, *ResultDataGroup.as, and *ResultItemRenderer.mxml. Fortunately, there are several examples of this code in the ArcGIS Viewer for Flex. A quick shortcut: simply copy and paste these three items from SearchWidget and rename them with the new widgets name. P If the old widget used an mx:Repeater, the code probably has many references to its dataProvider. It will be necessary to create a bindable private var of type ArrayCollection to replace it. Everywhere in the code that references the repeaters, dataProvider must be changed to reference this new ArrayCollection. N The new ArcGIS Viewer for Flex allows developers to specify a custom info window to use with a particular widget or one of the widget templates that comes standard with the viewer. Using this new capability involves several code additions and changes, such as overriding the widgets showInfoWindow function. Rather than identifying each line that must be changed and added in this article, look at one of the existing core Viewer widgets and search for info. That search will return items like the infoURL string, which holds the infoURL string from the widget configuration file, or the DATA_CREATE_INFOWIDGET event. When using the new info window and data group (when replacing mx:Repeater), P a couple of new import statements must be added:
import com.esri.viewer.IInfowindowTemplate; import mx.core.UIComponent; import spark.components.supportClasses.ItemRenderer; and import com.esri.viewer.AppEvent.
If the data group and item renderer will be updated, the mouseOverRecord, P
mouseOutRecord, and clickRecord event handlers must also be updated to convert events passed to these handlers to an itemRenderer instead of using the infoData object.
36
Developer's Corner
var llResult:LiveLayerResult = ItemRenderer(event.target) .data as LiveLayerResult;
Online Resources
Differences between Adobe Flex SDK 3 and Adobe Flex SDK 4
M When migrating widget code and using the queryTask, if the code is not connecting to an instance of ArcGIS Server 10 or higher, you need to set queryTask. useAMF = false. N Title bar buttons in the new ArcGIS Viewer for Flex no longer return events, so the click handler does not require an event.
adobe.com/devnet/flex/articles/ flex3and4_differences.html
Esri API changes between ArcGIS API for Flex 1.x and 2.x
Old format
private function toggleFilterPanel(event:MouseEvent):void
New format
private function showResultsList():void
P The order in which title bar buttons are added is the opposite order in which
they were added in the SFV (e.g., the first button to appear on the left should now be the last one added). < The third property for the addTitlebarButton function is used to designate whether the button is selectable. The default value is true. The assets directory in the SFV was com/esri/solutions/flexviewer/assets/ P images/icons/. The assets directory in ArcGIS Viewer for Flex is located at assets/images/. (Notice there is no subfolder of icons.)
To summarize, there are many key items that Flex developers should be aware of when
links.esri.com/flexviewer
migrating a custom widget from the Sample Flex Viewer to the ArcGIS Viewer for Flex. An example of migrated widget code can be found at gis.calhouncounty.org/DevSummit2011. It demonstrates the LiveLayerWidget code and includes developer comments.
series of NEXRAD radar reflectance images (indicating severe weather) over the US for the previous hour. It also demonstrates the use of WMS layers via the ArcGIS Flex API.
37