▼Adobe AIRメモ▼
テキストエディタ
テキストエディタを作成する。
ソースコード
TextEditor.as package { import flash.desktop.*; import flash.display.*; import flash.events.*; import flash.filesystem.*; import flash.html.*; import flash.net.*; import flash.system.*; //テキストエディタ public class TextEditor extends Sprite { private var htmlControl:HTMLLoader;//HTMLコントロール private var file:File; //ファイル private var fileTitle:String; //タイトル private var fileRead:Boolean; //読み書きフラグ //コンストラクタ public function TextEditor() { //HTMLコントロールの生成 htmlControl=new HTMLLoader(); htmlControl.width =stage.stageWidth; htmlControl.height=stage.stageHeight; htmlControl.addEventListener(Event.COMPLETE,onComplete); addChild(htmlControl); htmlControl.load(new URLRequest("app:/root.html")); //ウィンドウサイズ調整 stage.scaleMode=StageScaleMode.NO_SCALE; stage.align=StageAlign.TOP_LEFT; stage.nativeWindow.addEventListener( NativeWindowBoundsEvent.RESIZE,onResizeWindow); //ファイル file=File.desktopDirectory; file.addEventListener(Event.SELECT,onSelect); fileRead=true; } //ウィンドウリサイズイベントの処理 private function onResizeWindow(evt:NativeWindowBoundsEvent):void { htmlControl.width =stage.stageWidth; htmlControl.height=stage.stageHeight; htmlControl.window.update(); } //HTML読み込み完了イベントの処理 private function onComplete(evt:Event):void { var exposed:Object=new Object(); exposed.onItemClick=onItemClick; htmlControl.window.setExposed(exposed); } //項目クリックイベントの処理 private function onItemClick(id:String):void { var text:String; var cb:Clipboard; if (id=="mi_new") { fileTitle=null; stage.nativeWindow.title="TextEditor" htmlControl.window.setText(""); } else if (id=="mi_open") { fileRead=true; var filter:FileFilter=new FileFilter( "テキスト","*.txt;*.html"); file.browseForOpen("開く",[filter]); } else if (id=="mi_save") { fileRead=false; if (fileTitle==null) { file.browseForSave("保存"); } else { onSelect(null); } } else if (id=="mi_saveas") { fileRead=false; file.browseForSave("保存"); } else if (id=="mi_exit") { stage.nativeWindow.close(); } else if (id=="mi_version") { htmlControl.window.showDialog( "バージョン情報", "テキストエディタ Version 1.0"); } } //選択イベントの処理 private function onSelect(evt:Event):void { var text:String; try { //読み込み if (fileRead) { var fsr:FileStream=new FileStream(); fsr.open(file,FileMode.READ); text=fsr.readMultiByte(file.size,"utf-8"); fsr.close(); htmlControl.window.setText(text); } //書き込み else { text=htmlControl.window.getText(); var fsw:FileStream=new FileStream(); fsw.open(file,FileMode.WRITE); fsw.writeMultiByte(text,"utf-8"); fsw.close(); } fileTitle=file.nativePath; stage.nativeWindow.title="TextEditor - "+fileTitle } catch (e:Error) { htmlControl.window.showDialog( "エラー","エラーです。"); } } } }
root.html <html> <head> <script type="text/javascript"> function test(){ alert('A'); } //公開関数の指定 function setExposed(exposed){ //アプリケーションサンドボックスの公開関数の指定 document.getElementById('UI' ).contentWindow.parentSandboxBridge=exposed; //クラシックサンドボックスから公開関数を取得 window.showDialog=document.getElementById('UI' ).contentWindow.childSandboxBridge.showDialog; window.update=document.getElementById('UI' ).contentWindow.childSandboxBridge.update; window.setText=document.getElementById('UI' ).contentWindow.childSandboxBridge.setText; window.getText=document.getElementById('UI' ).contentWindow.childSandboxBridge.getText; } </script> </head> <frameset> <frame id="UI" src="TextEditor.html" sandboxRoot="http://npaka.net/" documentRoot="app:/" width="100%" height="100%"> </frame> </frameset> </html>
TextEditor.js //メニュー textEditor=function(){ var BorderLayout=Ext.BorderLayout; var ContentPanel=Ext.ContentPanel; var Form=Ext.form.Form; var Menu=Ext.menu.Menu; var Toolbar=Ext.Toolbar; var layout;//レイアウト return { //初期化 init : function(){ //メニューの生成 var menubar=new Toolbar('menubar'); menubar.add( { text:'ファイル', cls :'x-btn-text-icon bmenu', menu:textEditor.makeFileMenu() }, { text:'ヘルプ', cls :'x-btn-text-icon bmenu', menu:textEditor.makeHelpMenu() } ); //レイアウトの生成 textEditor.layout=new BorderLayout(document.body,{ north: {}, center: { autoScroll:true//自動スクロール } }); //レイアウトの配置 textEditor.layout.beginUpdate(); textEditor.layout.add('north',new ContentPanel('menubar',{ fitToFrame:true })); textEditor.layout.add('center',new ContentPanel('editor',{ fitToFrame:true })); textEditor.layout.endUpdate(); }, //ファイルメニューの生成 makeFileMenu : function(){ //メニューの生成 var menu=new Menu({ id :'menu_file', items:[ { id :'mi_new', text :'新規作成', handler:textEditor.onItemClick }, { id :'mi_open', text :'開く', handler:textEditor.onItemClick }, { id :'mi_save', text :'上書き保存', handler:textEditor.onItemClick }, { id :'mi_saveas', text :'名前を付けて保存', handler:textEditor.onItemClick }, '-', { id :'mi_exit', text :'閉じる', handler:textEditor.onItemClick } ] }); return menu; }, //ヘルプメニューの生成 makeHelpMenu : function(){ var menu=new Menu({ id :'menu_help', items:[ { id :'mi_version', text :'バージョン情報', handler:textEditor.onItemClick } ] }); return menu; }, //項目クリックイベントの処理 onItemClick : function(item){ if (parentSandboxBridge) { parentSandboxBridge.onItemClick(item.id); } }, //更新 update : function(){ textEditor.layout.beginUpdate(); textEditor.layout.endUpdate(); }, //テキストの指定 setText : function(text){ Ext.get('editor').dom.value=text; }, //テキストの取得 getText : function(){ return Ext.get('editor').dom.value; }, //テキストの切り取り cutText : function(){ return Ext.get('editor').dom.value; }, //テキストのコピー copyText : function(){ return Ext.get('editor').dom.value; }, //テキストの貼り付け pasteText : function(){ return Ext.get('editor').dom.value; } } }(); Ext.onReady(textEditor.init,textEditor,true);
−戻る−