<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>markstar &#187; StatefulToolkit</title>
	<atom:link href="http://markstar.co.uk/blog/category/statefultoolkit/feed/" rel="self" type="application/rss+xml" />
	<link>http://markstar.co.uk/blog</link>
	<description>blogging about the flash platform</description>
	<lastBuildDate>Sat, 31 Jul 2010 09:16:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Clipboard Management using the StatefulToolkit</title>
		<link>http://markstar.co.uk/blog/2010/flashplatform/actionscript/clipboard-management-using-the-statefultoolkit/</link>
		<comments>http://markstar.co.uk/blog/2010/flashplatform/actionscript/clipboard-management-using-the-statefultoolkit/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 09:00:10 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[StatefulToolkit]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://markstar.co.uk/blog/?p=196</guid>
		<description><![CDATA[This tutorial will build upon the Introduction to using the StatefulToolkit example; it will add the ability to utilize the Clipboard in our example. The example will use the ClipboardUtil, which is a utility class included in the toolkit that allows the user to copy and paste objects using the clipboard. The example application will build upon the last one; it will allow the user to position four circles, to save and load the state of the circles (i.e. where the circles are positioned) and to copy and paste the circles.]]></description>
			<content:encoded><![CDATA[<h3>Tutorial Overview</h3>
<p>This tutorial will build upon the <a title="Introduction to using the StatefulToolkit" href="http://markstar.co.uk/blog/2010/statefultoolkit/introduction-to-using-statefultoolkit/" target="_blank">Introduction to using the StatefulToolkit</a> example; it will add the ability to utilize the Clipboard in our example. The example will use the ClipboardUtil, which is a utility class included in the toolkit that allows the user to copy and paste objects using the clipboard. The example application will build upon the last one; it will allow the user to position four circles, to save and load the state of the circles (i.e. where the circles are positioned) and to copy and paste the circles. The example can be <a title="View the compiled application" href="#compiledapplication">seen here</a> and <a title="Application Source Code" href="#sourcecode">source files are available</a>.</p>
<h3>Preparation</h3>
<p>Before you being implementing this tutorial make sure you have the latest version of the <a title="StatefulToolkit Downloads at GitHub" href="http://github.com/markstar/StatefulToolkit/downloads" target="_blank">StatefulToolkit SWC</a>, and that you’re project is set to target Flash Player 10 (if you want to target FP9, simple replace the Vector with an array in the circle container) . This tutorial also uses Keith Peter’s <a title="Minimal Comps at BIT-101" href="http://www.bit-101.com/minimalcomps/" target="_blank">Minimal Comps</a> component set to create the buttons in the application so you may want to grab the SWC for that too, but you can use your own buttons if you prefer. Both SWC files are included in the <a title="Application Source Code" href="#sourcecode">available flex project</a>.<br />
<span id="more-196"></span></p>
<h3>Creating the Statable Object</h3>
<p>The original circle class could be left as it is, however it would be helpful to know which circle we have selected, so we know which one we are adding to the clipboard. In order to do show this we will add two new methods to the Circle; select and deselect. Each method draws the circle in a different colour. The full contents of this class can be seen below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> couk.markstar.examples.ui
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.core.IStatable;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Circle extends <span style="color: #004993;">Sprite</span> implements IStatable
	<span style="color: #000000;">&#123;</span>
		<span style="color: #3f5fbf;">/**
		 * The constructor draws the circle with a grey outline
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Circle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> 0x999999 <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Change the display of the circle to show it's selected
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">select</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> 0x990000 <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Change the display of the circle to the default
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> deselect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> 0x999999 <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Retrieve the state of the circle storing the x and y properties
		 *
		 * @return The current state of the circle as an XML string
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> retrieveState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> xml<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">'&lt;object&gt;'</span>;
			xml <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;x&gt;'</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">'&lt;/x&gt;'</span>;
			xml <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;y&gt;'</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">'&lt;/y&gt;'</span>;
			xml <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;/object&gt;'</span>;
			<span style="color: #0033ff; font-weight: bold;">return</span> xml;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Set the state of the circle by getting it's x and y values from the xml
		 *
		 * @param state The new state of the circle
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setState<span style="color: #000000;">&#40;</span> state<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">x</span> = <span style="color: #004993;">Number</span><span style="color: #000000;">&#40;</span> state.<span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">y</span> = <span style="color: #004993;">Number</span><span style="color: #000000;">&#40;</span> state.<span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Used internally for creating the circles' graphics
		 * 
		 * @param lineColor The color of the line around the circle
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> lineColor<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span>, lineColor <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0xFFFFFFF <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h3>Creating the Container</h3>
<p>Previously the circle container implemented the IStatable interface but it now implements the IStateContainer interface, because it needs to interact with the ClipboardUtil. The IStateContainer interface inherits functionality from the IStatable interface, so the circle container is still required to have the methods setState and retrieveState, but it is also required to have the methods addObjectByState, hasSelectedObject and retrieveSelectedObject.</p>
<p>The addObjectByState method was already implemented in the previous function, but was used internally, now it will be used by the ClipboardUtil when the user is pasting an object. In order to implement hasSelectedObject and retrieveSelectedObject we must add to the circleDownListener. After setting the _currentCircle we can call the select method of the circle to show that it has been selected. Then, before we set the _currentCircle to the one the user has mouse downed, we must deselect the old one by calling it&#8217;s deselect method. The full contents of this class can be seen below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> couk.markstar.examples.ui
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.core.IStatable;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.core.IStateContainer;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Point</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CircleContainer extends <span style="color: #004993;">Sprite</span> implements IStateContainer
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _circles<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Circle<span style="color: #000000; font-weight: bold;">&gt;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _currentCircle<span style="color: #000000; font-weight: bold;">:</span>Circle;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _dragOffset<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span>;
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * The constructor the vector containing the circles shown in the container
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CircleContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			_circles = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Circle<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Retrieve the selected object
		 *
		 * @return The selected object
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> retrieveSelectedObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IStatable
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> _currentCircle;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Check whether the container has a selected object
		 *
		 * @return Boolean denoting if the container has a selected object
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> hasSelectedObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000000;">&#40;</span> _currentCircle <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">?</span> <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Retrieve the state of the container and all contained circles
		 *
		 * @return The current state of the container, including the state's of all circles
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> retrieveState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> xml<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&lt;container&gt;&quot;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> _circles.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				xml <span style="color: #000000; font-weight: bold;">+</span>= _circles<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span>.retrieveState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			xml <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;&lt;/container&gt;&quot;</span>;
			<span style="color: #0033ff; font-weight: bold;">return</span> xml;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Add an object to the container by it's state.
		 *
		 * @param state The state of the object to add, in this case the circle
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addObjectByState<span style="color: #000000;">&#40;</span> state<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> circle<span style="color: #000000; font-weight: bold;">:</span>Circle = <span style="color: #0033ff; font-weight: bold;">new</span> Circle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			circle.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, circleDownListener <span style="color: #000000;">&#41;</span>;
			circle.setState<span style="color: #000000;">&#40;</span> state <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> circle <span style="color: #000000;">&#41;</span>;
			_circles<span style="color: #000000;">&#91;</span> _circles.<span style="color: #004993;">length</span> <span style="color: #000000;">&#93;</span> = circle;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Set the state of the container by adding all circles contained within the state. The container will be cleared first.
		 *
		 * @param state The new state of the container
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setState<span style="color: #000000;">&#40;</span> state<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			clearContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> state.object.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				addObjectByState<span style="color: #000000;">&#40;</span> <span style="color: #004993;">XML</span><span style="color: #000000;">&#40;</span> state.object<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Clear the container of all circles
		 *
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> clearContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> _circles.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span> _circles<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			_circles.<span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, _circles.<span style="color: #004993;">length</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/*
		 * The following methods are all event listeners to enable the circles to be dragged
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> circleDownListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// Deselect the current circle (if there is one)</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> _currentCircle <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				_currentCircle.deselect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			_currentCircle = Circle<span style="color: #000000;">&#40;</span> e.<span style="color: #004993;">currentTarget</span> <span style="color: #000000;">&#41;</span>;
			_currentCircle.<span style="color: #004993;">select</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_MOVE</span>, circleMouseMoveListener <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_UP</span>, circleMouseUpListener <span style="color: #000000;">&#41;</span>;
&nbsp;
			_dragOffset = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> _currentCircle.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #004993;">mouseX</span>, _currentCircle.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #004993;">mouseY</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> circleMouseMoveListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			_currentCircle.<span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span> <span style="color: #000000; font-weight: bold;">+</span> _dragOffset.<span style="color: #004993;">x</span>;
			_currentCircle.<span style="color: #004993;">y</span> = <span style="color: #004993;">mouseY</span> <span style="color: #000000; font-weight: bold;">+</span> _dragOffset.<span style="color: #004993;">y</span>;
			e.<span style="color: #004993;">updateAfterEvent</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> circleMouseUpListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_MOVE</span>, circleMouseMoveListener <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_UP</span>, circleMouseUpListener <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h3>Creating the Document Class</h3>
<p>The document class for this application very similar to the previous one. It was already responsible for creating the save and load buttons, creating the circle container, registering the circle container with the FileUtil and initialising the container with a default state. The only thing we need to add is the registration of the circle container with the ClipboardUtil. The full contents of this class can be seen below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com.bit101.components.PushButton;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.examples.ui.CircleContainer;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.utils.ClipboardUtil;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.utils.FileUtil;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.utils.IClipboardUtil;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.utils.IFileUtil;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextField</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextFieldType</span>;
&nbsp;
	<span style="color: #000000;">&#91;</span>SWF<span style="color: #000000;">&#40;</span> <span style="color: #004993;">backgroundColor</span>=<span style="color: #990000;">'#F3F3F3'</span>,<span style="color: #004993;">frameRate</span>=<span style="color: #990000;">'30'</span>,<span style="color: #004993;">width</span>=<span style="color: #990000;">'592'</span>,<span style="color: #004993;">height</span>=<span style="color: #990000;">'300'</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ClipboardExample extends <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _saveButton<span style="color: #000000; font-weight: bold;">:</span>PushButton;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _loadButton<span style="color: #000000; font-weight: bold;">:</span>PushButton;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _circleContainer<span style="color: #000000; font-weight: bold;">:</span>CircleContainer;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _fileUtil<span style="color: #000000; font-weight: bold;">:</span>IFileUtil;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _clipboardUtil<span style="color: #000000; font-weight: bold;">:</span>IClipboardUtil;
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Create the save and load buttons to save and load the state. Then create the circle container and register it with the FileUtil.
		 * By registering the container with the FileUtil it means the FileUtil knows which statable object to retrieve and set the state on
		 * when save and load are executed.
		 * Register the container with the ClipboardUtil so it can capture keyboard shortcuts.
		 */</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ClipboardExample<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// PushButton( parent, x, y, label, MouseEvent.CLICK event listener )</span>
			_saveButton = <span style="color: #0033ff; font-weight: bold;">new</span> PushButton<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #000000; font-weight:bold;">196</span>, <span style="color: #000000; font-weight:bold;">275</span>, <span style="color: #990000;">&quot;Save&quot;</span>, saveClickListener <span style="color: #000000;">&#41;</span>;
			_loadButton = <span style="color: #0033ff; font-weight: bold;">new</span> PushButton<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #000000; font-weight:bold;">295</span>, <span style="color: #000000; font-weight:bold;">275</span>, <span style="color: #990000;">&quot;Load&quot;</span>, loadClickListener <span style="color: #000000;">&#41;</span>;
&nbsp;
			_circleContainer = <span style="color: #0033ff; font-weight: bold;">new</span> CircleContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			_fileUtil = <span style="color: #0033ff; font-weight: bold;">new</span> FileUtil<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			_fileUtil.registerObject<span style="color: #000000;">&#40;</span> _circleContainer <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// ClipboardUtil requires reference to the stage so it can manage the keyboard shortcuts</span>
			_clipboardUtil = <span style="color: #0033ff; font-weight: bold;">new</span> ClipboardUtil<span style="color: #000000;">&#40;</span> <span style="color: #004993;">stage</span> <span style="color: #000000;">&#41;</span>;
			_clipboardUtil.registerContainer<span style="color: #000000;">&#40;</span> _circleContainer <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> _circleContainer <span style="color: #000000;">&#41;</span>;
&nbsp;
			initDefaultState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> saveClickListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			_fileUtil.<span style="color: #004993;">save</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> loadClickListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			_fileUtil.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Initialise the circle container with four circles
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> initDefaultState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> defaultState<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">'&lt;container&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;object&gt;&lt;x&gt;196&lt;/x&gt;&lt;y&gt;100&lt;/y&gt;&lt;/object&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;object&gt;&lt;x&gt;196&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/object&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;object&gt;&lt;x&gt;396&lt;/x&gt;&lt;y&gt;100&lt;/y&gt;&lt;/object&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;object&gt;&lt;x&gt;396&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/object&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;/container&gt;'</span>;
&nbsp;
			_circleContainer.setState<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">XML</span><span style="color: #000000;">&#40;</span> defaultState <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h3>Testing the application</h3>
<p>You will now be able to run the application and test that the state of the application can be saved and loaded and that you can copy and paste circles. The structure of the project can be seen below. If you do have any questions about the tutorial, or constructive criticism on the toolkit, don’t hesitate leave a comment below.</p>
<p style="text-align: center;"><a href="http://markstar.co.uk/blog/wp-content/uploads/2010/02/ProjectStructure.jpg"><img class="aligncenter size-full wp-image-199" title="ClipboardExampleProjectStructure" src="http://markstar.co.uk/blog/wp-content/uploads/2010/02/ProjectStructure.jpg" alt="ClipboardExample Project Structure" width="592" height="306" /></a></p>
<h3><a name="compiledapplication"></a>Compiled Application</h3>
<p>Once you have compiled the application, you should get the same output as below. Try copying and pasting the circles, you will also be able to save and load the state like before.</p>
<div class="post_flash">
<p style="text-align: center;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ClipboardExample_911672506"
			class="flashmovie"
			width="592"
			height="300">
	<param name="movie" value="http://markstar.co.uk/flash/StatefulToolkit/ClipboardExample.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://markstar.co.uk/flash/StatefulToolkit/ClipboardExample.swf"
			name="fm_ClipboardExample_911672506"
			width="592"
			height="300">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<h3><a name="sourcecode"></a>Source Files</h3>
<p>Source files are available from <a title="StatefulToolkit Downloads at GitHub" href="http://github.com/markstar/StatefulToolkit/downloads" target="_blank">GitHub</a>, under the heading ClipboardExample.</p>
]]></content:encoded>
			<wfw:commentRss>http://markstar.co.uk/blog/2010/flashplatform/actionscript/clipboard-management-using-the-statefultoolkit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to using the StatefulToolkit</title>
		<link>http://markstar.co.uk/blog/2010/statefultoolkit/introduction-to-using-statefultoolkit/</link>
		<comments>http://markstar.co.uk/blog/2010/statefultoolkit/introduction-to-using-statefultoolkit/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 15:18:26 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[StatefulToolkit]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://markstar.co.uk/blog/?p=135</guid>
		<description><![CDATA[In this tutorial I’ll take you through the basic premise behind the StatefulToolkit and demonstrate how it can be used in a project. The example will also demonstrate use of the FileUtil, which is a utility class included in the toolkit that allows the user to load and save states.]]></description>
			<content:encoded><![CDATA[<h3>Tutorial Overview</h3>
<p>In this tutorial I’ll take you through the basic premise behind the StatefulToolkit and demonstrate how it can be used in a project. If you haven’t heard of StatefulToolkit before then be sure to <a title="StatefulToolkit for AS3 (Beta)" href="http://markstar.co.uk/blog/?p=134" target="_blank">read up on it</a>. This example will also demonstrate use of the FileUtil, which is a utility class included in the toolkit that allows the user to load and save states. The application is very basic and will allow the user to position four circles and to save and load the state of the circles (i.e. where the circles are positioned). The example can be <a title="View the compiled application" href="#compiledapplication" target="_self">seen here</a> and <a title="Application Source Code" href="#sourcecode" target="_self">source files are available</a>.</p>
<h3>Preparation</h3>
<p>Before you being implementing this tutorial make sure you have the latest version of the <a title="StatefulToolkit Downloads at GitHub" href="http://github.com/markstar/StatefulToolkit/downloads" target="_blank">StatefulToolkit SWC</a>, and that you&#8217;re project is set to target Flash Player 10 (if you want to target FP9, simple replace the Vector with an array in the circle container) . This tutorial also uses Keith Peter’s <a title="Minimal Comps at BIT-101" href="http://www.bit-101.com/minimalcomps/" target="_blank">Minimal Comps</a> component set to create the buttons in the application so you may want to grab the SWC for that too, but you can use your own buttons if you prefer.<br />
<span id="more-135"></span></p>
<h3>Creating the Statable Object</h3>
<p>We’re going to start by creating the circles that the user can move. In order for the position of the circles to be saved the class must implement the IStatable interface. The class itself is very simple; the constructor creates the graphics of the circle, the retrieveState method returns the position of the circle as a String, and the setState method sets the position of the Circle based on the new state. The retrieveState and setState methods are both required by the IStatable interface. Note that I’ve put the class in the couk.markstar.examples.ui package, but you don’t have to. The full contents of this class can be seen below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> couk.markstar.examples.ui
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.core.IStatable;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Circle extends <span style="color: #004993;">Sprite</span> implements IStatable
	<span style="color: #000000;">&#123;</span>
		<span style="color: #3f5fbf;">/**
		 * The constructor draws the circle with a grey outline
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Circle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> 0x999999 <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Change the display of the circle to show it's selected
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">select</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> 0x990000 <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Change the display of the circle to the default
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> deselect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> 0x999999 <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Retrieve the state of the circle storing the x and y properties
		 *
		 * @return The current state of the circle as an XML string
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> retrieveState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> xml<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">'&lt;object&gt;'</span>;
			xml <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;x&gt;'</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">'&lt;/x&gt;'</span>;
			xml <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;y&gt;'</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">'&lt;/y&gt;'</span>;
			xml <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;/object&gt;'</span>;
			<span style="color: #0033ff; font-weight: bold;">return</span> xml;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Set the state of the circle by getting it's x and y values from the xml
		 *
		 * @param state The new state of the circle
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setState<span style="color: #000000;">&#40;</span> state<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">x</span> = <span style="color: #004993;">Number</span><span style="color: #000000;">&#40;</span> state.<span style="color: #004993;">x</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">y</span> = <span style="color: #004993;">Number</span><span style="color: #000000;">&#40;</span> state.<span style="color: #004993;">y</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Used internally for creating the circles' graphics
		 *
		 * @param lineColor The color of the line around the circle
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> lineColor<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span>, lineColor <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0xFFFFFFF <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">10</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h3>Creating the Container</h3>
<p>In this example the container is responsible for managing the circles. This includes creating and deleting circles, setting and retrieving the state of the circles and adding behaviours to the circles so the user can position them.</p>
<p>The circle container must also implement the IStatable interface ,so it can be used with the FileUtil. The container is set up to assume that an unknown amount of circles could be stored in the state, therefore there’s some additional code in the class to create and remove circles. Firstly, there’s the addObjectByState method, which takes the state of a circle as it’s parameter and creates a circle based on the state, and there’s the clearContainer method that loops through all of the circles and removes them from the stage.</p>
<p>The retrieveState and setState methods are also slightly different to the ones found in the Circle class. The retrieveState method loops through all circles and adding their states to the string to be returned, and the setState method clears any circles currently there and loops through the new state creating new circles as necessary. The last three methods of the class are event listeners that are there to enable the positioning of circles. The full contents of this class can be seen below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> couk.markstar.examples.ui
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.core.IStatable;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.core.IStateSelectContainer;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Point</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CircleContainer extends <span style="color: #004993;">Sprite</span> implements IStateSelectContainer
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _circles<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Circle<span style="color: #000000; font-weight: bold;">&gt;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _currentCircle<span style="color: #000000; font-weight: bold;">:</span>Circle;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _dragOffset<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span>;
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * The constructor the vector containing the circles shown in the container
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CircleContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			_circles = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Circle<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Retrieve the selected object
		 *
		 * @return The selected object
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> retrieveSelectedObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IStatable
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> _currentCircle;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Check whether the container has a selected object
		 *
		 * @return Boolean denoting if the container has a selected object
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> hasSelectedObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000000;">&#40;</span> _currentCircle <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">?</span> <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Retrieve the state of the container and all contained circles
		 *
		 * @return The current state of the container, including the state's of all circles
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> retrieveState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> xml<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&lt;container&gt;&quot;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> _circles.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				xml <span style="color: #000000; font-weight: bold;">+</span>= _circles<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span>.retrieveState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			xml <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;&lt;/container&gt;&quot;</span>;
			<span style="color: #0033ff; font-weight: bold;">return</span> xml;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Add an object to the container by it's state.
		 *
		 * @param state The state of the object to add, in this case the circle
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addObjectByState<span style="color: #000000;">&#40;</span> state<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> circle<span style="color: #000000; font-weight: bold;">:</span>Circle = <span style="color: #0033ff; font-weight: bold;">new</span> Circle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			circle.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, circleDownListener <span style="color: #000000;">&#41;</span>;
			circle.setState<span style="color: #000000;">&#40;</span> state <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> circle <span style="color: #000000;">&#41;</span>;
			_circles<span style="color: #000000;">&#91;</span> _circles.<span style="color: #004993;">length</span> <span style="color: #000000;">&#93;</span> = circle;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Set the state of the container by adding all circles contained within the state. The container will be cleared first.
		 *
		 * @param state The new state of the container
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setState<span style="color: #000000;">&#40;</span> state<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			clearContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> state.object.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				addObjectByState<span style="color: #000000;">&#40;</span> <span style="color: #004993;">XML</span><span style="color: #000000;">&#40;</span> state.object<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Clear the container of all circles
		 *
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> clearContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> _circles.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span> _circles<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			_circles.<span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, _circles.<span style="color: #004993;">length</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/*
		 * The following methods are all event listeners to enable the circles to be dragged
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> circleDownListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// Deselect the current circle (if there is one)</span>
			<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> _currentCircle <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				_currentCircle.deselect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			_currentCircle = Circle<span style="color: #000000;">&#40;</span> e.<span style="color: #004993;">currentTarget</span> <span style="color: #000000;">&#41;</span>;
			_currentCircle.<span style="color: #004993;">select</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_MOVE</span>, circleMouseMoveListener <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_UP</span>, circleMouseUpListener <span style="color: #000000;">&#41;</span>;
&nbsp;
			_dragOffset = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span> _currentCircle.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #004993;">mouseX</span>, _currentCircle.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #004993;">mouseY</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> circleMouseMoveListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			_currentCircle.<span style="color: #004993;">x</span> = <span style="color: #004993;">mouseX</span> <span style="color: #000000; font-weight: bold;">+</span> _dragOffset.<span style="color: #004993;">x</span>;
			_currentCircle.<span style="color: #004993;">y</span> = <span style="color: #004993;">mouseY</span> <span style="color: #000000; font-weight: bold;">+</span> _dragOffset.<span style="color: #004993;">y</span>;
			e.<span style="color: #004993;">updateAfterEvent</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> circleMouseUpListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_MOVE</span>, circleMouseMoveListener <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_UP</span>, circleMouseUpListener <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h3>Creating the Document Class</h3>
<p>The document class for this application is pretty simple. It’s responsible for creating the save and load buttons, creating the circle container, registering the circle container with the FileUtil and initialising the container with a default state. The save and load buttons are created using the <a title="Minimal Comps at BIT-101" href="http://www.bit-101.com/minimalcomps/" target="_blank">Minimal Comps</a> component set, you can replace these with buttons of your own if you choose not to use them. The full contents of this class can be seen below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com.bit101.components.PushButton;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.examples.ui.CircleContainer;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.utils.FileUtil;
	<span style="color: #0033ff; font-weight: bold;">import</span> couk.markstar.statefultoolkit.utils.IFileUtil;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
	<span style="color: #000000;">&#91;</span>SWF<span style="color: #000000;">&#40;</span> <span style="color: #004993;">backgroundColor</span>=<span style="color: #990000;">'#F3F3F3'</span>,<span style="color: #004993;">frameRate</span>=<span style="color: #990000;">'30'</span>,<span style="color: #004993;">width</span>=<span style="color: #990000;">'592'</span>,<span style="color: #004993;">height</span>=<span style="color: #990000;">'300'</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SaveLoadExample extends <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _saveButton<span style="color: #000000; font-weight: bold;">:</span>PushButton;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _loadButton<span style="color: #000000; font-weight: bold;">:</span>PushButton;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _circleContainer<span style="color: #000000; font-weight: bold;">:</span>CircleContainer;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _fileUtil<span style="color: #000000; font-weight: bold;">:</span>IFileUtil;
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Create the save and load buttons to save and load the state. Then create the circle container and register it with the FileUtil,
		 * by registering the container with the FileUtil it means the FileUtil knows which statable object to retrieve and set the state on
		 * when save and load are executed.
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> SaveLoadExample<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// PushButton( parent, x, y, label, MouseEvent.CLICK event listener )</span>
			_saveButton = <span style="color: #0033ff; font-weight: bold;">new</span> PushButton<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #000000; font-weight:bold;">196</span>, <span style="color: #000000; font-weight:bold;">275</span>, <span style="color: #990000;">&quot;Save&quot;</span>, saveClickListener <span style="color: #000000;">&#41;</span>;
			_loadButton = <span style="color: #0033ff; font-weight: bold;">new</span> PushButton<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #000000; font-weight:bold;">295</span>, <span style="color: #000000; font-weight:bold;">275</span>, <span style="color: #990000;">&quot;Load&quot;</span>, loadClickListener <span style="color: #000000;">&#41;</span>;
&nbsp;
			_circleContainer = <span style="color: #0033ff; font-weight: bold;">new</span> CircleContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			_fileUtil = <span style="color: #0033ff; font-weight: bold;">new</span> FileUtil<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			_fileUtil.registerObject<span style="color: #000000;">&#40;</span> _circleContainer <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> _circleContainer <span style="color: #000000;">&#41;</span>;
&nbsp;
			initDefaultState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> saveClickListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			_fileUtil.<span style="color: #004993;">save</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> loadClickListener<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			_fileUtil.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/**
		 * Initialise the circle container with four circles
		 */</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> initDefaultState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> defaultState<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">'&lt;container&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;object&gt;&lt;x&gt;196&lt;/x&gt;&lt;y&gt;100&lt;/y&gt;&lt;/object&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;object&gt;&lt;x&gt;196&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/object&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;object&gt;&lt;x&gt;396&lt;/x&gt;&lt;y&gt;100&lt;/y&gt;&lt;/object&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;object&gt;&lt;x&gt;396&lt;/x&gt;&lt;y&gt;200&lt;/y&gt;&lt;/object&gt;'</span>;
			defaultState <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">'&lt;/container&gt;'</span>;
&nbsp;
			_circleContainer.setState<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">XML</span><span style="color: #000000;">&#40;</span> defaultState <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h3>Testing the application</h3>
<p>You will now be able to run the application and test that the state of the application can be saved and loaded. The structure of the project can be seen below. If you do have any questions about the tutorial, or constructive criticism on the toolkit, don&#8217;t hesitate leave a comment below.</p>
<p style="text-align: center;"><a href="http://markstar.co.uk/blog/wp-content/uploads/2010/01/SaveLoadExampleProjectStructure.jpg"><img class="size-full wp-image-149 aligncenter" title="SaveLoadExampleProjectStructure" src="http://markstar.co.uk/blog/wp-content/uploads/2010/01/SaveLoadExampleProjectStructure.jpg" alt="SaveLoadExample Project Structure" width="592" height="288" /></a></p>
<h3><a name="compiledapplication"></a>Compiled Application</h3>
<p>Once you have compiled the application, you should get the same output as below. Try moving the circles, saving the state then moving the circles again and loading the state. Notice that the circles go back to the saved state when it&#8217;s loaded.</p>
<div class="post_flash">
<p style="text-align: center;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_SaveLoadExample_342192043"
			class="flashmovie"
			width="592"
			height="300">
	<param name="movie" value="http://markstar.co.uk/flash/StatefulToolkit/SaveLoadExample.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://markstar.co.uk/flash/StatefulToolkit/SaveLoadExample.swf"
			name="fm_SaveLoadExample_342192043"
			width="592"
			height="300">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<h3><a name="sourcecode"></a>Source Files</h3>
<p>Source files are available from <a title="StatefulToolkit Downloads at GitHub" href="http://github.com/markstar/StatefulToolkit/downloads" target="_blank">GitHub</a>, under the heading SaveLoadExample.</p>
]]></content:encoded>
			<wfw:commentRss>http://markstar.co.uk/blog/2010/statefultoolkit/introduction-to-using-statefultoolkit/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>StatefulToolkit for AS3 (Beta)</title>
		<link>http://markstar.co.uk/blog/2010/statefultoolkit/statefultoolkit-for-as3-beta/</link>
		<comments>http://markstar.co.uk/blog/2010/statefultoolkit/statefultoolkit-for-as3-beta/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 14:45:28 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[StatefulToolkit]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://markstar.co.uk/blog/?p=134</guid>
		<description><![CDATA[Update: Don&#8217;t forget to take a look at the Introduction to using StatefulToolkit.
What is it?
The StatefulToolkit was developed to enable the simple management of states in AS3. At the heart of the toolkit is one interface called IStatable. Any object that requires the retrieval or setting of its state must implement this interface. The toolkit [...]]]></description>
			<content:encoded><![CDATA[<p>Update: Don&#8217;t forget to take a look at the <a title="Introduction to using StatefulToolkit" href="http://markstar.co.uk/blog/?p=135" target="_blank">Introduction to using StatefulToolkit</a>.</p>
<h3>What is it?</h3>
<p>The StatefulToolkit was developed to enable the simple management of states in AS3. At the heart of the toolkit is one interface called IStatable. Any object that requires the retrieval or setting of its state must implement this interface. The toolkit comes with utility classes to help carry out some of the basic tasks associated with state management.</p>
<h3>What’s included in the beta?</h3>
<p>The initial 0.1 beta release includes the interfaces to implement and two utility classes, one to aid in saving and loading states and another to enable clipboard management, so you can copy and paste objects.</p>
<p>The utility classes are both currently optimized for the Flash Player &#8211; that’s not to say they can’t be used in the AIR runtime (they’d work perfectly fine) but they take into account the security sandbox restrictions that are found in the Flash Player. For example, the FileUtil allows you to save and load states, but does this using the FileReference class whereas in an AIR application you may want this to be done silently (without the browse box appearing), which would use the File class.</p>
<p><span id="more-134"></span></p>
<h3>Why is it in beta?</h3>
<p>The main reason is that it&#8217;s still in early stages of development; I haven’t implemented all of the features yet and the code hasn&#8217;t been optimized, that being said it is stable in its current release. I’d also like to get some community feedback on the project before making a 1.0 release.</p>
<h3>Where can I get it?</h3>
<p>The <a title="StatefulToolkit at GitHub" href="http://github.com/markstar/StatefulToolkit" target="_blank">source code is available</a> at <a title="GitHub" href="http://github.com/" target="_blank">GitHub</a>, alternatively you can download the SWC on the <a title="StatefulToolkit Downloads at GitHub" href="http://github.com/markstar/StatefulToolkit/downloads" target="_blank">downloads page</a> of the project.</p>
<h3>Where can I find examples?</h3>
<p>I&#8217;ll be posting tutorials for some example projects on this blog and you can find all example projects on the <a title="StatefulToolkit Downloads at GitHub" href="http://github.com/markstar/StatefulToolkit/downloads" target="_blank">downloads page</a> of the project in GitHub. This will evolve as the toolkit expands.</p>
<h3>Where would I use it?</h3>
<p>Essentially it can be used for any application that needs it’s state saving. However, I think it would be useful in the following type of applications:</p>
<ul>
<li>Wireframing applications, such as <a title="Balsamiq Mockups" href="http://www.balsamiq.com/products/mockups" target="_blank">Balsamiq Mockups</a> and <a title="LovelyCharts" href="http://www.lovelycharts.com/" target="_blank">LovelyCharts</a></li>
<li>Game level editors</li>
<li>Drawing tools</li>
<li>General applications (for things like panel states, window positions,  settings panels, etc)</li>
</ul>
<h3>What have you got planned for the future?</h3>
<p>More utility classes! In the near future there will be a utility class to allow history management (undo and redo). Following this I want to create AIR versions of the utility classes and provide some form of automation for the setting and retrieval of states, so you don’t have to construct and parse the XML manually.</p>
<h3>How can I help?</h3>
<p>Feedback! Whether its feature requests, bugs, constructive criticism or just general questions about the toolkit, I want to hear it. You can either post comments on this blog, or <a title="StatefulToolkit issues at GitHub" href="http://github.com/markstar/StatefulToolkit/issues" target="_blank">file issues</a> for the project at GitHub.</p>
]]></content:encoded>
			<wfw:commentRss>http://markstar.co.uk/blog/2010/statefultoolkit/statefultoolkit-for-as3-beta/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>