1////////////////////////////////////////////////////////////////////////////////
2//
3//  ADOBE SYSTEMS INCORPORATED
4//  Copyright 2005-2007 Adobe Systems Incorporated
5//  All Rights Reserved.
6//
7//  NOTICE: Adobe permits you to use, modify, and distribute this file
8//  in accordance with the terms of the license agreement accompanying it.
9//
10////////////////////////////////////////////////////////////////////////////////
11
12package mx.messaging.events
13{
14
15import flash.events.Event;
16import mx.messaging.Channel;
17
18/**
19 *  The ChannelEvent is used to propagate channel events within the messaging system.
20 */
21public class ChannelEvent extends Event
22{
23    //--------------------------------------------------------------------------
24    //
25    // Static Constants
26    //
27    //--------------------------------------------------------------------------
28
29    /**
30     *  The CONNECT event type; indicates that the Channel connected to its
31     *  endpoint.
32     *  <p>The value of this constant is <code>"channelConnect"</code>.</p>
33     *
34     *  <p>The properties of the event object have the following values:</p>
35     *  <table class="innertable">
36     *     <tr><th>Property</th><th>Value</th></tr>
37     *     <tr><td><code>bubbles</code></td><td>false</td></tr>
38     *     <tr><td><code>cancelable</code></td><td>false</td></tr>
39     *     <tr><td><code>channel</code></td><td>The channel that generated this event.</td></tr>
40     *     <tr><td><code>currentTarget</code></td><td>The Object that defines the
41     *       event listener that handles the event. For example, if you use
42     *       <code>myButton.addEventListener()</code> to register an event listener,
43     *       myButton is the value of the <code>currentTarget</code>. </td></tr>
44     *     <tr><td><code>target</code></td><td>The Object that dispatched the event;
45     *       it is not always the Object listening for the event.
46     *       Use the <code>currentTarget</code> property to always access the
47     *       Object listening for the event.</td></tr>
48     *     <tr><td><code>reconnecting</code></td><td> Indicates whether the channel
49     *       that generated this event is reconnecting.</td></tr>
50     *     <tr><td><code>rejected</code></td><td> Indicates whether the channel that
51     *       generated this event was rejected. This would be true in the event that
52     *       the channel has been disconnected due to inactivity and should not attempt to
53     *       failover or connect on an alternate channel.</td></tr>
54     *  </table>
55     *  @eventType channelConnect
56     */
57    public static const CONNECT:String = "channelConnect";
58
59    /**
60     *  The DISCONNECT event type; indicates that the Channel disconnected from its
61     *  endpoint.
62     *  <p>The value of this constant is <code>"channelDisconnect"</code>.</p>
63     *
64     *  <p>The properties of the event object have the following values:</p>
65     *  <table class="innertable">
66     *     <tr><th>Property</th><th>Value</th></tr>
67     *     <tr><td><code>bubbles</code></td><td>false</td></tr>
68     *     <tr><td><code>cancelable</code></td><td>false</td></tr>
69     *     <tr><td><code>channel</code></td><td>The channel that generated this event.</td></tr>
70     *     <tr><td><code>currentTarget</code></td><td>The Object that defines the
71     *       event listener that handles the event. For example, if you use
72     *       <code>myButton.addEventListener()</code> to register an event listener,
73     *       myButton is the value of the <code>currentTarget</code>. </td></tr>
74     *     <tr><td><code>target</code></td><td>The Object that dispatched the event;
75     *       it is not always the Object listening for the event.
76     *       Use the <code>currentTarget</code> property to always access the
77     *       Object listening for the event.</td></tr>
78     *     <tr><td><code>reconnecting</code></td><td> Indicates whether the channel
79     *       that generated this event is reconnecting.</td></tr>
80     *     <tr><td><code>rejected</code></td><td> Indicates whether the channel that
81     *       generated this event was rejected. This would be true in the event that
82     *       the channel has been disconnected due to inactivity and should not attempt to
83     *       failover or connect on an alternate channel.</td></tr>
84     *  </table>
85     *  @eventType channelDisconnect
86     */
87    public static const DISCONNECT:String = "channelDisconnect";
88
89    //--------------------------------------------------------------------------
90    //
91    // Static Methods
92    //
93    //--------------------------------------------------------------------------
94
95    /**
96     *  Utility method to create a new ChannelEvent that doesn't bubble and
97     *  is not cancelable.
98     *
99     *  @param type The ChannelEvent type.
100     *
101     *  @param channel The Channel generating the event.
102     *
103     *  @param reconnecting Indicates whether the Channel is in the process of
104     *  reconnecting or not.
105     *
106     *  @param rejected Indicates whether the Channel's connection has been rejected,
107     *  which suppresses automatic reconnection.
108     *
109     *  @param connected Indicates whether the Channel that generated this event
110     *  is already connected.
111     *
112     *  @return New ChannelEvent.
113     */
114    public static function createEvent(type:String, channel:Channel = null,
115            reconnecting:Boolean = false, rejected:Boolean = false, connected:Boolean = false):ChannelEvent
116    {
117        return new ChannelEvent(type, false, false, channel, reconnecting, rejected, connected);
118    }
119
120    //--------------------------------------------------------------------------
121    //
122    // Constructor
123    //
124    //--------------------------------------------------------------------------
125
126    /**
127     *  Constructs an instance of this event with the specified type and Channel
128     *  instance.
129     *
130     *  @param type The ChannelEvent type.
131     *
132     *  @param bubbles Specifies whether the event can bubble up the display
133     *  list hierarchy.
134     *
135     *  @param cancelable Indicates whether the behavior associated with the
136     *  event can be prevented; used by the RPC subclasses.
137     *
138     *  @param channel The Channel generating the event.
139     *
140     *  @param reconnecting Indicates whether the Channel is in the process of
141     *  reconnecting or not.
142     *
143     *  @param rejected Indicates whether the Channel's connection has been rejected,
144     *  which suppresses automatic reconnection.
145     *
146     *  @param connected Indicates whether the Channel that generated this event
147     *  is already connected.
148     */
149    public function ChannelEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false,
150            channel:Channel = null, reconnecting:Boolean = false, rejected:Boolean = false,
151            connected:Boolean = false)
152    {
153        super(type, bubbles, cancelable);
154
155        this.channel = channel;
156        this.reconnecting = reconnecting;
157        this.rejected = rejected;
158        this.connected = connected;
159    }
160
161    //--------------------------------------------------------------------------
162    //
163    // Variables
164    //
165    //--------------------------------------------------------------------------
166
167    /**
168     *  The Channel that generated this event.
169     */
170    public var channel:Channel;
171
172    /**
173     * Indicates whether the Channel that generated this event is already connected.
174     */
175    public var connected:Boolean;
176
177    /**
178     *  Indicates whether the Channel that generated this event is reconnecting.
179     */
180    public var reconnecting:Boolean;
181
182    /**
183     *  Indicates whether the Channel that generated this event was rejected.
184     *  This would be true in the event that the channel has been
185     *  disconnected due to inactivity and should not attempt to failover or
186     *  connect on an alternate channel.
187     */
188    public var rejected:Boolean;
189
190    //--------------------------------------------------------------------------
191    //
192    // Properties
193    //
194    //--------------------------------------------------------------------------
195
196	//----------------------------------
197	//  channelId
198	//----------------------------------
199
200    /**
201     * @private
202     */
203    public function get channelId():String
204    {
205        if (channel != null)
206        {
207            return channel.id;
208        }
209        return null;
210    }
211
212    //--------------------------------------------------------------------------
213    //
214    // Overridden Methods
215    //
216    //--------------------------------------------------------------------------
217
218    /**
219     *  Clones the ChannelEvent.
220     *
221     *  @return Copy of this ChannelEvent.
222     */
223    override public function clone():Event
224    {
225        return new ChannelEvent(type, bubbles, cancelable, channel, reconnecting, rejected, connected);
226    }
227
228    /**
229     *  Returns a string representation of the ChannelEvent.
230     *
231     *  @return String representation of the ChannelEvent.
232     */
233    override public function toString():String
234    {
235        return formatToString("ChannelEvent", "channelId", "reconnecting", "rejected", "type", "bubbles", "cancelable", "eventPhase");
236    }
237}
238
239}