次の例では、遅延レンダリング関数の実装方法を示します。
ユーザーが「コピー」ボタンを押すと、アプリケーションによってシステムクリップボードがクリアされ、前のクリップボード操作のデータが消去されます。次に、
setDataHandler()
メソッドがクリップボードレンダラーとして
renderData()
関数を設定します。
目的のテキストフィールドのコンテキストメニューから「ペースト」コマンドを選択すると、アプリケーションはクリップボードにアクセスして、目的のテキストを設定します。クリップボードのテキストデータ形式はストリングではなく関数を使用して設定されるので、クリップボードは
renderData()
関数を呼び出します。
renderData()
関数はテキストをソーステキストで返し、それがペースト先のテキストに割り当てられます。
「ペースト」ボタンをクリックする前にソーステキストを編集した場合、「コピー」ボタンをクリックした後で編集を行ったとしても、ペーストされるテキストに編集が反映されます。これは、「ペースト」ボタンがクリックされてから、レンダリング関数がソーステキストをコピーするからです(実際のアプリケーションで遅延レンダリングを使用するときは、このような問題を防ぐため、ソースデータを何らかの方法で保存または保護することが必要になる場合があります)。
Flash の例
package {
import flash.desktop.Clipboard;
import flash.desktop.ClipboardFormats;
import flash.desktop.ClipboardTransferMode;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
import flash.events.MouseEvent;
import flash.events.Event;
public class DeferredRenderingExample extends Sprite
{
private var sourceTextField:TextField;
private var destination:TextField;
private var copyText:TextField;
public function DeferredRenderingExample():void
{
sourceTextField = createTextField(10, 10, 380, 90);
sourceTextField.text = "Neque porro quisquam est qui dolorem "
+ "ipsum quia dolor sit amet, consectetur, adipisci velit.";
copyText = createTextField(10, 110, 35, 20);
copyText.htmlText = "<a href='#'>Copy</a>";
copyText.addEventListener(MouseEvent.CLICK, onCopy);
destination = createTextField(10, 145, 380, 90);
destination.addEventListener(Event.PASTE, onPaste);
}
private function createTextField(x:Number, y:Number, width:Number,
height:Number):TextField
{
var newTxt:TextField = new TextField();
newTxt.x = x;
newTxt.y = y;
newTxt.height = height;
newTxt.width = width;
newTxt.border = true;
newTxt.multiline = true;
newTxt.wordWrap = true;
newTxt.type = TextFieldType.INPUT;
addChild(newTxt);
return newTxt;
}
public function onCopy(event:MouseEvent):void
{
Clipboard.generalClipboard.clear();
Clipboard.generalClipboard.setDataHandler(ClipboardFormats.TEXT_FORMAT,
renderData);
}
public function onPaste(event:Event):void
{
sourceTextField.text =
Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT).toString;
}
public function renderData():String
{
trace("Rendering data");
var sourceStr:String = sourceTextField.text;
if (sourceTextField.selectionEndIndex >
sourceTextField.selectionBeginIndex)
{
return sourceStr.substring(sourceTextField.selectionBeginIndex,
sourceTextField.selectionEndIndex);
}
else
{
return sourceStr;
}
}
}
}
Flex の例
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="326" height="330" applicationComplete="init()">
<mx:Script>
<![CDATA[
import flash.desktop.Clipboard;
import flash.desktop.ClipboardFormats;
public function init():void
{
destination.addEventListener("paste", doPaste);
}
public function doCopy():void
{
Clipboard.generalClipboard.clear();
Clipboard.generalClipboard.setDataHandler(ClipboardFormats.TEXT_FORMAT, renderData);
}
public function doPaste(event:Event):void
{
destination.text = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT).toString;
}
public function renderData():String{
trace("Rendering data");
return source.text;
}
]]>
</mx:Script>
<mx:Label x="10" y="10" text="Source"/>
<mx:TextArea id="source" x="10" y="36" width="300" height="100">
<mx:text>Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.</mx:text>
</mx:TextArea>
<mx:Label x="10" y="181" text="Destination"/>
<mx:TextArea id="destination" x="12" y="207" width="300" height="100"/>
<mx:Button click="doCopy();" x="91" y="156" label="Copy"/>
</mx:Application>