Skip to content

Instantly share code, notes, and snippets.

@odoe
Created June 29, 2010 15:22
Show Gist options
  • Save odoe/457363 to your computer and use it in GitHub Desktop.
Save odoe/457363 to your computer and use it in GitHub Desktop.
package org.lacsd.components {
import com.esri.ags.Map;
import com.esri.ags.events.MapEvent;
import com.esri.ags.layers.Layer;
import flash.events.Event;
import mx.collections.ArrayCollection;
import mx.logging.ILogger;
import org.lacsd.util.LogUtil;
import spark.components.List;
import spark.components.supportClasses.SkinnableComponent;
/**
* MapLayerList is a component that will add Layers from the Map object
* into a custom List for various Layer control
* @author rrubalcava
*/
public class MapLayerList extends SkinnableComponent {
private static const logger:ILogger = LogUtil.getLogger(MapLayerList);
/**
* Constructor
*/
public function MapLayerList() {
super();
}
[SkinPart(required="true")]
/**
* Required List that must be in Skin
* @default
*/
public var layerList:List;
/**
* Backing variable for <code>map</code> object
* @default
*/
protected var _map:Map;
[Bindable]
/**
* Collection hold Map Layers, used to control what layers are listed
* @default
*/
protected var layers:ArrayCollection = new ArrayCollection();
/**
* Regular Expression to prevent random GraphicsLayers from being added to List.
* Using regular expression, because there may be cases where you want
* a user to control a GraphicsLayer, in which case you should give it a
* distinctive name.
* @default
*/
protected var reg1:RegExp = /GraphicsLayer\w/;
/**
* Regular Expression to prevent random FeatureLayers from being added to List.
* Using regular expression, because there may be cases where you want
* a user to control a FeatureLayer, in which case you should give it a
* distinctive name.
* @default
*/
protected var reg2:RegExp = /FeatureLayer\w/;
/**
* Load the current contents of Map object
*/
protected function initialListLoad():void {
for each (var lyr:Layer in map.layers) {
if (!reg1.test(lyr.id) && !reg2.test(lyr.id)) {
layers.addItem(lyr);
logger.info("Adding layer to list: {0}", lyr);
}
}
}
/**
* Load any new layers that get added to Map object
* @param event
*/
protected function onMapLayerAdded(event:MapEvent):void {
if (!reg1.test(event.layer.id) && !reg2.test(event.layer.id))
layers.addItem(event.layer);
}
/**
* Remove Layers from List that get removed from Map object
* @param event
*/
protected function onMapLayerRemoved(event:MapEvent):void {
layers.removeItemAt(layers.getItemIndex(event.layer));
}
/**
* Clean up when component is closed
* @param event
*/
protected function onRemovedFromStage_handler(event:Event):void {
logger.debug("Removing from stage, prepare for gc: {0}", this);
this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage_handler);
_map.removeEventListener(MapEvent.LAYER_ADD, onMapLayerAdded);
reg1 = null;
reg2 = null;
layerList = null;
layers = null;
_map = null;
}
override protected function partAdded(partName:String, instance:Object):void {
super.partAdded(partName, instance);
if (instance == layerList) {
layerList.dataProvider = layers;
this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage_handler, false, 0, true);
}
}
[Bindable("mapChanged")]
/**
* Target Map object that contains Layers
* @return
*/
public function get map():Map {
return _map;
}
/**
* When Map object is set, add listeners and load initial Layers
* @param value
*/
public function set map(value:Map):void {
if (_map != value) {
_map = value;
_map.addEventListener(MapEvent.LAYER_ADD, onMapLayerAdded, false, 0, true);
this.initialListLoad();
dispatchEvent(new Event("mapChanged"));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment