diff --git a/src/as/com/threerings/flex/GridUtil.as b/src/as/com/threerings/flex/GridUtil.as new file mode 100644 index 00000000..2c1f7548 --- /dev/null +++ b/src/as/com/threerings/flex/GridUtil.as @@ -0,0 +1,67 @@ +package com.threerings.flex { + +import mx.containers.Grid; +import mx.containers.GridItem; +import mx.containers.GridRow; + +import mx.controls.Label; + +import mx.core.UIComponent; + +/** + * Convenience methods for adding children to Grids. + */ +public class GridUtil +{ + /** + * Add a new row to the grid, containing the specified + * components. + * + * @param specs a list of components or Strings, or you can follow + * any component with a two-dimensional array that specifies + * grid width/height. + * + * Example: addRow(grid, "labeltxt", _entryField, _bigThing, [2, 2], _smallThing); + * + * All will be put in the same row, but bigThing will have + * colspan=2 rowspan=2 + */ + public static function addRow (grid :Grid, ... specs) :GridRow + { + var row :GridRow = new GridRow(); + var lastItem :GridItem; + for each (var o :Object in specs) { + if (o is String) { + var lbl :Label = new Label(); + lbl.text = String(o); + o = lbl; + } + if (o is UIComponent) { + lastItem = addToRow(row, UIComponent(o)); + + } else if (o is Array) { + var arr :Array = (o as Array); + lastItem.colSpan = int(arr[0]); + lastItem.rowSpan = int(arr[1]); + + } else { + throw new ArgumentError(); + } + } + grid.addChild(row); + return row; + } + + /** + * A convenience function to the specified component to the + * specified row. + */ + public static function addToRow (row :GridRow, comp :UIComponent) :GridItem + { + var item :GridItem = new GridItem(); + item.addChild(comp); + row.addChild(item); + return item; + } +} +} diff --git a/src/as/com/threerings/flex/LazyContainer.as b/src/as/com/threerings/flex/LazyContainer.as new file mode 100644 index 00000000..1902606d --- /dev/null +++ b/src/as/com/threerings/flex/LazyContainer.as @@ -0,0 +1,40 @@ +// +// $Id$ + +package com.threerings.flex { + +import flash.display.DisplayObjectContainer; + +import mx.core.ContainerCreationPolicy; +import mx.core.UIComponent; +import mx.containers.VBox; + +public class LazyContainer extends VBox +{ + /** + * Create a lazy container that will not create its children + * until needed. + * + * @param creation a function that takes no args and returns + * a UIComponent to be added to the container. + */ + public function LazyContainer (creation :Function) + { + _creation = creation; + creationPolicy = ContainerCreationPolicy.NONE; + } + + override public function createComponentsFromDescriptors ( + recurse :Boolean = true) :void + { + super.createComponentsFromDescriptors(recurse); + + if (_creation != null) { + addChild(_creation() as UIComponent); + _creation = null; // assist gc + } + } + + protected var _creation :Function; +} +} diff --git a/src/as/com/threerings/flex/LazyTabNavigator.as b/src/as/com/threerings/flex/LazyTabNavigator.as new file mode 100644 index 00000000..94d413b6 --- /dev/null +++ b/src/as/com/threerings/flex/LazyTabNavigator.as @@ -0,0 +1,42 @@ +package com.threerings.flex { + +import mx.core.ContainerCreationPolicy; + +import mx.core.UIComponent; + +import mx.containers.TabNavigator; +import mx.containers.VBox; + +/** + * A tab navigator that is specially set up for lazy-creating the + * content in each tab. + */ +public class LazyTabNavigator extends TabNavigator +{ + public function LazyTabNavigator () + { + super(); + } + + /** + * Add a tab to the container. The creation function takes no args and + * returns a UIComponent. + */ + public function addTab (label :String, creation :Function) :void + { + addTabAt(label, creation, numChildren); + } + + /** + * Add a tab to the container at the specified index. + * The creation function takes no args and returns a UIComponent. + */ + public function addTabAt ( + label :String, creation :Function, index :int) :void + { + var box :LazyContainer = new LazyContainer(creation); + box.label = label; + addChildAt(box, index); + } +} +}