Wednesday, September 2, 2009

Creating widgets in Flex

In addition to the widgets provided with ArcWeb Explorer (Base Maps, Find, and Pan&Zoom), you can also create your own widgets. You can create your own widgets in ArcWeb Explorer Flex API using these setup instructions.
View a live sample.
Requirements
Adobe Flex 2.01 with or without HotFix 1 (newer hot fixes are not supported)—If you don't have a Flex development environment, you can download a free trial of the Flex Builder IDE.
Download the following files:
SWC file
Widget SWC file
Creating widgets
To create your own widget, you need to:
Implement IWidgetView and create default widget state (visual).
Extend BaseWidget (non-visual).
Wrap BaseWidget in MXML (wrapping).
Step 1: Implementing IWidgetView and creating default widget state (visual)
First, create the visual parts of the widget. You need to implement the six functions of the IWidgetView interface, and create the default widget state.
MyImplementationOfWidgetView.mxml contains an example:

xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:widget="com.esri.aws.awx.widget.*"
implements="com.esri.aws.awx.widget.IWidgetView">

import com.esri.aws.awx.widget.WidgetStates;
import com.esri.aws.awx.widget.IWidget;
private var theWidget:MyWidget;
public function get widget():IWidget
{
return theWidget;
}
public function set widget(value:IWidget):void
{
theWidget = value as MyWidget;
}
public function set widgetState(value:String):void
{
currentState = value;
}
public function get widgetState():String
{
return currentState;
}
private var theWidgetContainer:IWidgetContainer;
public function get widgetContainer():IWidgetContainer{
return theWidgetContainer;
}
public function set widgetContainer(container:IWidgetContainer):void{
theWidgetContainer = container;
}
]]>



Step 2: Extending BaseWidget (non-visual)
Extend the BaseWidget class using the createWidgetView method. The createWidgetView method should return the widget view created above (MyImplementationOfWidgetView). This is usually where your business logic goes (non-visual).
MyExtendedBaseWidget.as contains an example:
package com.esri.aws.awx.widget
{
public class MyExtendedBaseWidget extends BaseWidget
{
override public function createWidgetView(widgetState:String = null):IWidgetView
{
var theCustomView:MyImplentationOfWidgetView = new MyImplentationOfWidgetView();
theCustomView.widget = this;
theCustomView.widgetState = widgetState;
return theCustomView;
}
}
}
Step 3: Wrapping BaseWidget in MXML
The last step is to wrap the extended BaseWidget in MXML. This wrapper contains the metadata associated with the widget and declares the states available within the widget view. It is referenced from the MyImplementationOfWidgetView.mxml.
MyFirstWidget.mxml contains an example:

xmlns:widget="com.esri.aws.awx.widget.*"
name="Widget #1"
widgetStates="{[WidgetStates.DEFAULT]}" />



Using widgets in Flex

Using widgets in Flex
You can easily use the built-in widgets in your own applications with the WidgetPopupManager. Currently there are three built-in widgets: "Find", "Pan & Zoom", and "Base Maps". The sample below illustrates how to use the DockWidgetContainer to add widgets for "Find", "Pan & Zoom", and "Base Maps". See the using a widget sample for details.
You can also use the lazy loaded version of these widgets. See the reference documentation on BaseMapWidgetLoader, FindWidgetLoader, and PanZoomWidgetLoader for more information.

xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:awx="http://www.arcwebservices.com/2007/awx"
xmlns:widget="com.esri.aws.awx.widget.*"
layout="absolute">










About widgets

Widgets in ArcWeb Explorer are easy to use, and re-use modules that provide specific functionality. You can use one of the provided widgets, use someone else’s widget, or create and use your own widgets. For example, you might want to create a widget that provides easy access to your data or services; a widget that’s almost a complete application in its own right; or a widget that creates driving directions to your specific locations. The possibilities are endless.
Widgets are based on three important concepts:
Widget containers: Widget containers are used to “house” the widgets, for example, FloatingWidgetContainer.
The non-UI part of the widget: This contains the business logic for the widget.
Widget view: The widget view can contain one or more widget states, for example, DEFAULT, INFO, and ICON.
To display widgets, you put them in widget containers. You can either use one of the existing four widget containers, or create your own. The existing widget containers are:
AccordionWidgetContainer: AccordionWidgetContainer can be used to group several widgets into one. It supports the DEFAULT widget state.
BarWidgetContainer: BarWidgetContainer is a type of "carousel container" that provides the ability to scroll through its content, either horizontally or vertically. It uses the ICON and ICON_ACTION widget states.
DockWidgetContainer: DockWidgetContainer is an interactive widget container that can be "docked" to the side of an application. It contains a BarWidgetContainer, and thus uses the ICON and ICON_ACTION widget states.
FloatingWidgetContainer: FloatingWidgetContainer is a "popup" widget container that "floats" atop the application. This is the widget container used by the "Base Maps", "Find", and "Pan and Zoom" widgets. Alternatively you could put those widgets in a different widget container. FloatingWidgetContainer uses the DEFAULT, INFO, and ICON widget states. Use the WidgetPopupManager to open (“pop up”) floating widget containers.
NOTE: This page does not discuss creating your own widget container (HINT: extend BaseWidgetContainer).
If you style your widgets correctly, they will have the same look and feel as the widgets provided in ArcWeb Explorer. Using stylesheets, you can change the look and feel of all widgets (including your own).