13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Copy and paste<br />

Pasting text using a deferred r<strong>en</strong>dering function<br />

Flash Player 10 and later, Adobe AIR 1.0 and later<br />

The following example illustrates how to implem<strong>en</strong>t a deferred r<strong>en</strong>dering function.<br />

Wh<strong>en</strong> the user presses the Copy button, the application clears the system clipboard to <strong>en</strong>sure that no data is left over<br />

from previous clipboard operations. The setDataHandler() method th<strong>en</strong> sets the r<strong>en</strong>derData() function as the<br />

clipboard r<strong>en</strong>derer.<br />

Wh<strong>en</strong> the user selects the Paste command from the context m<strong>en</strong>u of the destination text field, the application accesses<br />

the clipboard and sets the destination text. Since the text data format on the clipboard has be<strong>en</strong> set with a function<br />

rather than a string, the clipboard calls the r<strong>en</strong>derData() function. The r<strong>en</strong>derData() function returns the text in<br />

the source text, which is th<strong>en</strong> assigned to the destination text.<br />

Notice that if you edit the source text before pressing the Paste button, the edit will be reflected in the pasted text, ev<strong>en</strong><br />

wh<strong>en</strong> the edit occurs after the copy button was pressed. This is because the r<strong>en</strong>dering function doesn’t copy the source<br />

text until the paste button is pressed. (Wh<strong>en</strong> using deferred r<strong>en</strong>dering in a real application, you might want to store or<br />

protect the source data in some way to prev<strong>en</strong>t this problem.)<br />

Flash example<br />

package {<br />

import flash.desktop.Clipboard;<br />

import flash.desktop.ClipboardFormats;<br />

import flash.desktop.ClipboardTransferMode;<br />

import flash.display.Sprite;<br />

import flash.text.TextField;<br />

import flash.text.TextFormat;<br />

import flash.text.TextFieldType;<br />

import flash.ev<strong>en</strong>ts.MouseEv<strong>en</strong>t;<br />

import flash.ev<strong>en</strong>ts.Ev<strong>en</strong>t;<br />

public class DeferredR<strong>en</strong>deringExample ext<strong>en</strong>ds Sprite<br />

{<br />

private var sourceTextField:TextField;<br />

private var destination:TextField;<br />

private var copyText:TextField;<br />

public function DeferredR<strong>en</strong>deringExample():void<br />

{<br />

sourceTextField = createTextField(10, 10, 380, 90);<br />

sourceTextField.text = "Neque porro quisquam est qui dolorem "<br />

+ "ipsum quia dolor sit amet, consectetur, adipisci velit.";<br />

copyText = createTextField(10, 110, 35, 20);<br />

copyText.htmlText = "Copy";<br />

copyText.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.CLICK, onCopy);<br />

destination = createTextField(10, 145, 380, 90);<br />

destination.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.PASTE, onPaste);<br />

}<br />

private function createTextField(x:Number, y:Number, width:Number,<br />

height:Number):TextField<br />

{<br />

var newTxt:TextField = new TextField();<br />

newTxt.x = x;<br />

newTxt.y = y;<br />

newTxt.height = height;<br />

Last updated 6/6/2012<br />

601

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!