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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Working with local SQL databases in AIR<br />

Instance name Compon<strong>en</strong>t type Description<br />

instructions Label Contains the instructions giv<strong>en</strong> to the user<br />

passwordInput TextInput Input field where the user <strong>en</strong>ters the password<br />

op<strong>en</strong>Button Button Button the user clicks after <strong>en</strong>tering the password<br />

statusMsg Label Displays status (success or failure) messages<br />

The code for the application is defined on a keyframe on frame 1 of the main timeline. The following is the code for<br />

the application:<br />

import com.adobe.air.crypto.EncryptionKeyG<strong>en</strong>erator;<br />

const dbFileName:String = "<strong>en</strong>cryptedDatabase.db";<br />

var dbFile:File;<br />

var createNewDB:Boolean = true;<br />

var conn:SQLConnection;<br />

init();<br />

// ------- Ev<strong>en</strong>t handling -------<br />

function init():void<br />

{<br />

passwordInput.displayAsPassword = true;<br />

op<strong>en</strong>Button.addEv<strong>en</strong>tList<strong>en</strong>er(MouseEv<strong>en</strong>t.CLICK, op<strong>en</strong>Connection);<br />

statusMsg.setStyle("textFormat", new TextFormat(null, null, 0x990000));<br />

conn = new SQLConnection();<br />

dbFile = File.applicationStorageDirectory.resolvePath(dbFileName);<br />

if (dbFile.exists)<br />

{<br />

createNewDB = false;<br />

instructions.text = "Enter your database password to op<strong>en</strong> the <strong>en</strong>crypted database.";<br />

op<strong>en</strong>Button.label = "Op<strong>en</strong> Database";<br />

}<br />

else<br />

{<br />

instructions.text = "Enter a password to create an <strong>en</strong>crypted database. The next time<br />

you op<strong>en</strong> the application, you will need to re-<strong>en</strong>ter the password to op<strong>en</strong> the database again.";<br />

op<strong>en</strong>Button.label = "Create Database";<br />

}<br />

}<br />

function op<strong>en</strong>Connection(ev<strong>en</strong>t:MouseEv<strong>en</strong>t):void<br />

{<br />

var keyG<strong>en</strong>erator:EncryptionKeyG<strong>en</strong>erator = new EncryptionKeyG<strong>en</strong>erator();<br />

var password:String = passwordInput.text;<br />

if (password == null || password.l<strong>en</strong>gth

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

Saved successfully!

Ooh no, something went wrong!