1.Create a Java Class that extends RuntimeException.
package com.flex3.exception;
public class FlexException extends RuntimeException{
public FlexException(String message) {
super(message);
} }
2. Create a matching flex class called FlexException, and put the usual RemoteObject tag in their so BlazeDS will know what to look for. Also remember in actionscript, each object must be instantiated at least once, so be sure to do that somewhere with FlexException.
3. In Java, whenever there is a known exception, or some error where the end user needs to be shown a message, throw a FlexException and pass in the message the user should see in the constructor. Let’s say the user just entered an invalid password. throw new FlexException(”Wrong Password Entered, Please Try Again.”);
4. Flex receives the Exception in the form of a fault. In the fault handler, you call this method:/*** Parse the incoming fault for an FlexException class. If the exception class is found and it has a matching actionscript class,* show an alert to the user with the correspoding messages. If we don’t recognize the Exception type, show a generic error.** @param fault**/public static function handleException(fault:Fault): void{var msg : String = fault.faultString;var clazz : Class = getExceptionClass(fault);var instance : Error = null;if (clazz != null){Alert.show(msg,’Error’);}else{Alert.show(’Error! Please try again. If this issue persists, contact the system administrator’);}}/**** Return the Class corresponding to the exception by parsing the fault string. If a corresponding actionscript error class is found,* return it. Otherwise, return null.** @return**/
public static function getExceptionClass(fault:Fault) : Class{
var clazz:Class = null;
var index:int = myFault.faultString.indexOf(”FlexException”);
if (index != -1){var cname:String = myFault.faultString.substr(0,index-1);
try{
clazz = getDefinitionByName(”com.flexpasta.FlexException”) as Class;
}
catch (e:ReferenceError){
}
}
return clazz;
}
When we see a Flex Exception, we show the message to the end user. If for some reason we have an unplanned error in Java, like NullPointerException, a message is displayed to the end user: ‘Error! Please try again. If this issue persists, contact the system administrator’. A generic message to handle all unplanned errors.
5. What if the app needs to DO something special with certain Java Exceptions? Say the user has just entered 3 password attempts, and I want to be able to handle locking their account through an exception.
Create a new class in Java called UserLockedException that extends FlexException
Create a matching actionscript UserLockedException.
Add code in the flex fault handler to look for a UserLockedException, when the fault handler sees it, it can still display a message to the end user, but now flex knows to take a different code path to complete the process.
This is a place for learning new things. "The greatest pleasure in life is doing what people say you cannot do"
Saturday, September 5, 2009
Wednesday, September 2, 2009
Connecting widgets with maps
Many widgets need to interact with the map. To do this, simply connect the widget with the map in three steps:
Override the start method to access OSGi services
Create your logic
Create your user interface (UI)
View a live sample.
Step 1: Override the start method to access OSGi services
To enable your widget to interact with the map, you need to override the Start method of the Widget Base and create an instance of the Service Tracker which will allow you to directly access the OSGi services. package com.esri.aws.awx.widget
{
public class MyZoomBaseWidget extends BaseWidget
{
import com.esri.aws.awx.map.IMap;
import com.esri.aws.osgi.framework.IBundleContext;
import com.esri.aws.osgi.framework.ServiceTracker;
import flash.utils.getQualifiedClassName;
// instance of service tracker
private var theTracker:ServiceTracker;
// Override the start() method
override public function start(context:IBundleContext):void
{
super.start(context);
theTracker = new ServiceTracker(context, getQualifiedClassName(IMap));
theTracker.open();
}
override public function createWidgetView(widgetState:String = null):IWidgetView
{
var theCustomView:MyZoomWidgetView = new MyZoomWidgetView();
theCustomView.widget = this;
theCustomView.widgetState = widgetState;
return theCustomView;
}
}
}
Step 2: Create your logic
Add a function in widget base that “does something”, for example: public function onZoom(value:Number):void
{
var map:IMap = IMap(theTracker.getService());
if(map)
{
map.scale *= value;
}
}
Step 3: Create your UI
Create your user interface.
Add a Map to your main MXML file:
Add the UI:
mx:Text text="Widget that interacts with Map" />
Override the start method to access OSGi services
Create your logic
Create your user interface (UI)
View a live sample.
Step 1: Override the start method to access OSGi services
To enable your widget to interact with the map, you need to override the Start method of the Widget Base and create an instance of the Service Tracker which will allow you to directly access the OSGi services. package com.esri.aws.awx.widget
{
public class MyZoomBaseWidget extends BaseWidget
{
import com.esri.aws.awx.map.IMap;
import com.esri.aws.osgi.framework.IBundleContext;
import com.esri.aws.osgi.framework.ServiceTracker;
import flash.utils.getQualifiedClassName;
// instance of service tracker
private var theTracker:ServiceTracker;
// Override the start() method
override public function start(context:IBundleContext):void
{
super.start(context);
theTracker = new ServiceTracker(context, getQualifiedClassName(IMap));
theTracker.open();
}
override public function createWidgetView(widgetState:String = null):IWidgetView
{
var theCustomView:MyZoomWidgetView = new MyZoomWidgetView();
theCustomView.widget = this;
theCustomView.widgetState = widgetState;
return theCustomView;
}
}
}
Step 2: Create your logic
Add a function in widget base that “does something”, for example: public function onZoom(value:Number):void
{
var map:IMap = IMap(theTracker.getService());
if(map)
{
map.scale *= value;
}
}
Step 3: Create your UI
Create your user interface.
Add a Map to your main MXML file:
Add the UI:
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]}" />
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: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:
name="Widget #1"
widgetStates="{[WidgetStates.DEFAULT]}" />
Subscribe to:
Posts (Atom)