0 Replies Latest reply on Jan 11, 2008 4:55 AM by Venkateshwarlu

    Dynamic Creation of Objects using Tree Control

    Venkateshwarlu
      I am able to Create Dynamic Objets using List control in flex,but not able to create objects using TreeControl,currently iam using switch case to do that iam embedding source code please help me how to do that


      <?xml version="1.0" encoding="utf-8"?>
      <!--This Application Deals With How to Create Objects Dynamically -->
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      layout="absolute">
      <mx:XML id="treeDP">


      <node label="Controls">
      <node label="Button"/>
      <node label="ComboBox"/>
      <node label="ColorPicker"/>
      <node label="Hslider"/>
      <node label="Vslider"/>
      <node label="Checkbox"/>
      </node>
      </mx:XML>
      <mx:Script>
      <![CDATA[
      import mx.core.UIComponentGlobals;
      import mx.containers.HBox;
      import mx.controls.*;
      import mx.controls.VSlider;
      import mx.controls.Button;
      import mx.controls.Alert;
      import mx.core.UIComponent;
      import mx.controls.Image;
      import mx.managers.DragManager;
      import mx.events.DragEvent;
      import mx.controls.Tree;
      import mx.core.DragSource
      import mx.core.IFlexDisplayObject;

      /*This function accepts the item as on when it is dragged from tree Component */
      private function ondragEnter(event:DragEvent) : void
      {
      if (event.dragSource.hasFormat("treeItems"))
      {
      DragManager.acceptDragDrop(Canvas(event.currentTarget));
      DragManager.showFeedback(DragManager.COPY);
      return;
      }
      else{
      DragManager.acceptDragDrop(Canvas(event.currentTarget));
      return;
      }
      }
      /*This Function creates objects as the items are Dragged from the TreeComponent
      And Creates Objects as and When They Are Dropped on the Container */
      private function ondragDrop(event:DragEvent) : void
      {
      if (event.dragSource.hasFormat("treeItems"))
      {
      var items:Array =event.dragSource.dataForFormat("treeItems") as Array;
      for (var i:int = items.length - 1; i >= 0; i--)
      {
      switch(items .@label.toString())
      {
      case "Button":
      {
      var b:Button=new Button();
      b.x = event.localX;
      b.y = event.localY;
      b.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
      myCanvas.addChild(b);
      break;
      }
      case "ComboBox":
      {
      var cb:ComboBox=new ComboBox();
      myCanvas.addChild(cb);
      cb.x = event.localX;
      cb.y = event.localY;
      cb.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
      break;
      }
      case "ColorPicker":
      {
      var cp:ColorPicker=new ColorPicker();
      myCanvas.addChild(cp);
      cp.x = event.localX;
      cp.y = event.localY;
      cp.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
      break;
      }
      case "Vslider":
      {
      var vs:VSlider=new VSlider();
      myCanvas.addChild(vs);
      vs.x = event.localX;
      vs.y = event.localY;
      vs.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
      break;
      }
      case "Hslider":
      {
      var hs:HSlider=new HSlider();
      myCanvas.addChild(hs);
      hs.x = event.localX;
      hs.y = event.localY;
      hs.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
      break;
      }
      case "Checkbox":
      {
      var check:CheckBox=new CheckBox();
      myCanvas.addChild(check);
      check.x = event.localX;
      check.y = event.localY;
      check.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
      break;
      }
      }
      }
      }
      else {
      var Component:UIComponent =
      event.dragSource.dataForFormat("items") as UIComponent ;
      Component.x = event.localX;
      Component.y = event.localY;
      Component.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
      myCanvas.addChild(Component);
      }
      }
      /*How to move the Objects within the Container */
      public function mouseMoveHandler(event:MouseEvent):void{
      var dragInitiator:UIComponent=UIComponent(event.currentTarget);
      var ds:DragSource = new DragSource();
      ds.addData(dragInitiator,"items")
      DragManager.doDrag(dragInitiator, ds, event);

      }
      ]]>
      </mx:Script>
      <mx:Tree dataProvider="{treeDP}" labelField="@label" dragEnabled="true" width="313" left="0" bottom="-193" top="0"/>
      <mx:Canvas id="myCanvas" dragEnter="ondragEnter(event)" dragDrop="ondragDrop(event)" backgroundColor="#DDDDDD" borderStyle="solid" left="321" right="-452" top="0" bottom="-194"/>
      </mx:Application>
      iwant to optimize the code in the place of switch case Text
      Text