AS 3.0/ Flex

TabNavigator com Botão de fechar, drag e drop habilitados

hahaha

Sajid é realmente um animal, ele é um dos engenheiros do Flex Team na India e em seu blog ele posta várias possibilidades de extender os componentes. Darron fez até um porém não liberou o código fonte, ele enquanto eu descia as escadas para tomar café ele fez esse exemplo e publicou em seu site.

Check this out. Sajid you are insane man.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package sjd.controls{
 
  import flash.events.MouseEvent;
  import flash.display.DisplayObject;
 
  import mx.core.DragSource;
  import mx.core.IUIComponent;
  import mx.containers.TabNavigator;
  import mx.controls.tabBarClasses.Tab;
  import mx.controls.Button;
  import mx.controls.TabBar;
  import mx.events.FlexEvent;
  import mx.events.DragEvent;
  import mx.managers.DragManager;
 
  /**
   * @class AdvanceTabNavigator
   * @brief A New TabNavigator with Drag/Drop Enabled and Close Button
   * @author Jove
   * @version 1.0
   */
   public class AdvanceTabNavigator extends TabNavigator  {
 
    [Embed(source="assets/WindowCloseButton.gif")]
    private var windowCloseButtonImg:Class;
    [Embed(source="assets/WindowCloseButton2.gif")]
    private var windowCloseButtonImg2:Class;
 
    protected var tabCloseButton:Button = new Button();
    protected var currentTab:Tab;
 
    private var _tabDragEnable:Boolean = false;
    private var _tabCloseEnable:Boolean = false;
 
    public function set tabDragEnable(enable:Boolean):void{
      this._tabDragEnable = enable;
      enableTabDrag(enable);
    }
 
    public function get tabDragEnable():Boolean{
       return this._tabDragEnable;
    }
 
    public function set tabCloseEnable(enable:Boolean):void{
      this._tabCloseEnable = enable;
      enableButton(enable);
    }
 
    public function get tabCloseEnable():Boolean{
       return this._tabCloseEnable;
    }
 
    public function AdvanceTabNavigator(){
      super();
      this.addEventListener(FlexEvent.CREATION_COMPLETE, buildButton);
    }
 
    protected function buildButton(enable:Boolean):void{
      tabCloseButton.width=10;
      tabCloseButton.height=10;
      tabCloseButton.focusEnabled=false;
      tabCloseButton.setStyle("upSkin", windowCloseButtonImg);
      tabCloseButton.setStyle("overSkin", windowCloseButtonImg2);
      tabCloseButton.setStyle("downSkin", windowCloseButtonImg2);
      tabCloseButton.visible = false;
      rawChildren.addChild(tabCloseButton);
    }
 
    protected function enableButton(enable:Boolean):void{
      if(enable){
        this.addEventListener(MouseEvent.MOUSE_OVER, addCloseButton);
        tabCloseButton.addEventListener(MouseEvent.CLICK, tabCloseButton_clickHandler);
      }else{
        this.removeEventListener(MouseEvent.MOUSE_OVER, addCloseButton);
        tabCloseButton.removeEventListener(MouseEvent.CLICK, tabCloseButton_clickHandler);
      }
    }
 
    protected function enableTabDrag(enable:Boolean):void{
      if(enable){
        this.addEventListener(MouseEvent.MOUSE_MOVE, moveTab);
        tabBar.addEventListener(DragEvent.DRAG_ENTER, doDragEnter);
        tabBar.addEventListener(DragEvent.DRAG_DROP, doDragDrop);
        tabBar.addEventListener(DragEvent.DRAG_OVER, doDragOver);
        tabBar.addEventListener(DragEvent.DRAG_EXIT, doDragExit);
      }else{
        this.removeEventListener(MouseEvent.MOUSE_MOVE, moveTab);
        tabBar.removeEventListener(DragEvent.DRAG_ENTER, doDragEnter);
        tabBar.removeEventListener(DragEvent.DRAG_DROP, doDragDrop);
        tabBar.removeEventListener(DragEvent.DRAG_OVER, doDragOver);
        tabBar.removeEventListener(DragEvent.DRAG_EXIT, doDragExit);
      }
    }
 
    protected function addCloseButton(event:MouseEvent):void{
      if(event.target != currentTab && event.target != tabCloseButton ){
        tabCloseButton.visible = false;
      }
      if(event.target is Tab && !DragManager.isDragging){
        currentTab= Tab(event.target);
        tabCloseButton.x = currentTab.x + currentTab.width - 14;
        tabCloseButton.y = currentTab.y + ( currentTab.height - 10 ) / 2;
        tabCloseButton.visible = true;
      }
    }
 
    protected function tabCloseButton_clickHandler(event:MouseEvent):void{
      removeChildAt(tabBar.getChildIndex(currentTab));
      tabCloseButton.visible = false;
     }
 
     protected function moveTab(event:MouseEvent):void{
      if(event.target is Tab){
        var dragInitiator:Tab=Tab(event.target);
        var ds:DragSource = new DragSource();
        var tabProxy:Tab = new Tab();
        tabProxy.label = dragInitiator.label;
        DragManager.doDrag(dragInitiator, ds, event, tabProxy);
      }
     }
 
     protected function doDragEnter(event:DragEvent):void {
   if(tabBar.getChildIndex(DisplayObject(event.dragInitiator)) >= 0){
     var dropTarget:IUIComponent=IUIComponent(event.currentTarget);
     DragManager.acceptDragDrop(dropTarget);
   }
 }
 
 protected function doDragOver(event:DragEvent):void {
   if(tabBar.getChildIndex(DisplayObject(event.dragInitiator)) >= 0){
     tabCloseButton.visible = false;
     var dragX:Number = tabBar.mouseX;
     var targetTabIndex:Number = -1;
     for(var i:Number = 0; i < tabBar.getChildren().length; i++){
       if(dragX < tabBar.getChildAt(i).x){
         targetTabIndex = i;
         tabBar.graphics.clear();
            tabBar.graphics.beginFill(0x000000);
            tabBar.graphics.lineStyle(0, 0x000000);
            tabBar.graphics.drawRect(
                      tabBar.getChildAt(targetTabIndex).x - 2,
                      2,
                      1,
                      tabBar.getChildAt(targetTabIndex).height - 3);
            tabBar.graphics.endFill();
         break;
       }
     }
     if(targetTabIndex < 0
       && dragX > tabBar.getChildAt(tabBar.getChildren().length - 1).x
       && dragX < tabBar.getChildAt(tabBar.getChildren().length - 1).x + tabBar.getChildAt(tabBar.getChildren().length - 1).width){
       targetTabIndex = tabBar.getChildren().length;
       tabBar.graphics.clear();
          tabBar.graphics.beginFill(0x000000);
          tabBar.graphics.lineStyle(0, 0x000000);
          tabBar.graphics.drawRect(
                    tabBar.getChildAt(targetTabIndex - 1).x + tabBar.getChildAt(targetTabIndex - 1).width - 3,
                    2,
                    1,
                    tabBar.getChildAt(targetTabIndex - 1).height - 3);
          tabBar.graphics.endFill();
     }
     if(targetTabIndex >= 0){
       event.dragSource.addData(targetTabIndex, "targetTabIndex");
     }
   }
 }
 
 protected function doDragDrop(event:DragEvent):void {
      if(tabBar.getChildIndex(DisplayObject(event.dragInitiator)) >= 0 && event.dragSource.hasFormat("targetTabIndex")){
     tabBar.graphics.clear();
        var sourceTabIndex:Number = tabBar.getChildIndex(DisplayObject(event.dragInitiator));
        var targetTabIndex:Number = Number(event.dragSource.dataForFormat("targetTabIndex"));
 
        if(sourceTabIndex < targetTabIndex || targetTabIndex == tabBar.getChildren().length){
          targetTabIndex--;
        }
        if(sourceTabIndex == targetTabIndex){
          return;
        }
 
        var sourceTab:DisplayObject = this.getChildAt(sourceTabIndex);
        this.removeChildAt(sourceTabIndex);
        this.addChildAt(sourceTab, targetTabIndex);
        this.selectedIndex = targetTabIndex;
 
      }
 }
 
 protected function doDragExit(event:DragEvent):void {
   if(tabBar.getChildIndex(DisplayObject(event.dragInitiator)) >= 0){
        tabBar.graphics.clear();
   }
 }
}
}

Um exemplo funcionando.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="sjd.controls.*" layout="vertical">
 
  <advanceTabNavigator id="tab1" width="600" height="300">
 
    <mx:Canvas label="0000">
      <mx:Text text="0" />
 
    </mx:Canvas>
 
    <mx:Canvas label="1111">
      <mx:Text text="1" />
    </mx:Canvas>
 
    <mx:Canvas label="2222">
      <mx:Text text="2" />
    </mx:Canvas>
 
    <mx:Canvas label="3333">
      <mx:Text text="3" />
    </mx:Canvas>
 
    <mx:Canvas label="4444">
      <mx:Text text="4" />
    </mx:Canvas>
 
    <mx:Canvas label="5555">
      <mx:Text text="5" />
    </mx:Canvas>
 
  </advanceTabNavigator>
 
  <mx:Button id="b1" label="close" click="tab1.tabCloseEnable = (!tab1.tabCloseEnable); b1.label='close: ' + tab1.tabCloseEnable" />
 
  <mx:Button id="b2" label="drag" click="tab1.tabDragEnable = (!tab1.tabDragEnable); b2.label='drag: ' + tab1.tabDragEnable" />
 
</mx:Application>

Testem e se divirtam, enquanto isso chequem o post original que ele fez em seu Site