| 1 |
/** |
|---|
| 2 |
* The SQL TableGrid Class for AIR Application |
|---|
| 3 |
* |
|---|
| 4 |
* @author Copyright (c) 2008 daoki2 |
|---|
| 5 |
* @version 1.0.1 |
|---|
| 6 |
* @link http://snippets.libspark.org/ |
|---|
| 7 |
* @link http://homepage.mac.com/daoki2/ |
|---|
| 8 |
* |
|---|
| 9 |
* Copyright (c) 2008 daoki2 |
|---|
| 10 |
* |
|---|
| 11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 12 |
* of this software and associated documentation files (the "Software"), to deal |
|---|
| 13 |
* in the Software without restriction, including without limitation the rights |
|---|
| 14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 15 |
* copies of the Software, and to permit persons to whom the Software is |
|---|
| 16 |
* furnished to do so, subject to the following conditions: |
|---|
| 17 |
* |
|---|
| 18 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 19 |
* all copies or substantial portions of the Software. |
|---|
| 20 |
* |
|---|
| 21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 27 |
* THE SOFTWARE. |
|---|
| 28 |
*/ |
|---|
| 29 |
|
|---|
| 30 |
package org.libspark.snippets.controls { |
|---|
| 31 |
import flash.data.SQLConnection; |
|---|
| 32 |
import mx.collections.ArrayCollection; |
|---|
| 33 |
import mx.controls.Alert; |
|---|
| 34 |
import mx.controls.DataGrid; |
|---|
| 35 |
import mx.controls.dataGridClasses.*; |
|---|
| 36 |
import org.libspark.utils.SqlUtil; |
|---|
| 37 |
import org.libspark.snippets.controls.SQLControlsClasses.checkBoxRenderer; |
|---|
| 38 |
import mx.core.ClassFactory; |
|---|
| 39 |
|
|---|
| 40 |
public class SQLTableGrid extends DataGrid { |
|---|
| 41 |
public var sqlConnection:SQLConnection = null; |
|---|
| 42 |
private static const typeData:Array = new Array("INTEGER", "FLOAT", "TEXT", "BLOB"); |
|---|
| 43 |
|
|---|
| 44 |
/** |
|---|
| 45 |
* Constructor |
|---|
| 46 |
*/ |
|---|
| 47 |
public function SQLTableGrid(mode:Boolean = false) { |
|---|
| 48 |
super(); |
|---|
| 49 |
var dataColumns:Array = new Array(); |
|---|
| 50 |
dataColumns.push(new DataGridColumn("name")); |
|---|
| 51 |
|
|---|
| 52 |
var typeColumn:DataGridColumn = new DataGridColumn("type"); |
|---|
| 53 |
var typeEditor:ClassFactory = new ClassFactory(mx.controls.ComboBox); |
|---|
| 54 |
typeEditor.properties = {dataProvider: typeData}; |
|---|
| 55 |
typeColumn.itemEditor = typeEditor; |
|---|
| 56 |
dataColumns.push(typeColumn); |
|---|
| 57 |
|
|---|
| 58 |
var pkColumn:DataGridColumn = new DataGridColumn("primaryKey"); |
|---|
| 59 |
var pkRenderer:ClassFactory = new ClassFactory(checkBoxRenderer); |
|---|
| 60 |
pkColumn.editorDataField = "checkBoxSelected"; |
|---|
| 61 |
pkRenderer.properties = {dataField: "primaryKey", checkBoxEnabled: mode}; |
|---|
| 62 |
pkColumn.itemRenderer = pkRenderer; |
|---|
| 63 |
pkColumn.rendererIsEditor = true; |
|---|
| 64 |
dataColumns.push(pkColumn); |
|---|
| 65 |
|
|---|
| 66 |
var aiColumn:DataGridColumn = new DataGridColumn("autoIncrement"); |
|---|
| 67 |
var aiRenderer:ClassFactory = new ClassFactory(checkBoxRenderer); |
|---|
| 68 |
aiRenderer.properties = {dataField: "autoIncrement", checkBoxEnabled: mode}; |
|---|
| 69 |
aiColumn.itemRenderer = aiRenderer; |
|---|
| 70 |
aiColumn.editorDataField = "checkBoxSelected"; |
|---|
| 71 |
aiColumn.rendererIsEditor = true; |
|---|
| 72 |
dataColumns.push(aiColumn); |
|---|
| 73 |
|
|---|
| 74 |
var anColumn:DataGridColumn = new DataGridColumn("allowNull"); |
|---|
| 75 |
var anRenderer:ClassFactory = new ClassFactory(checkBoxRenderer); |
|---|
| 76 |
anRenderer.properties = {dataField: "allowNull", checkBoxEnabled: mode}; |
|---|
| 77 |
anColumn.itemRenderer = anRenderer; |
|---|
| 78 |
anColumn.editorDataField = "checkBoxSelected"; |
|---|
| 79 |
anColumn.rendererIsEditor = true; |
|---|
| 80 |
dataColumns.push(anColumn); |
|---|
| 81 |
|
|---|
| 82 |
this.columns = dataColumns; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
/** |
|---|
| 86 |
* Get the column information from the specified table and apply to DataGrid |
|---|
| 87 |
* @param table The table to get the column information |
|---|
| 88 |
*/ |
|---|
| 89 |
public function getColumnInfo(table:String):void { |
|---|
| 90 |
if (sqlConnection == null) { |
|---|
| 91 |
Alert.show("sqlConnection is null", "ERROR!"); |
|---|
| 92 |
return; |
|---|
| 93 |
} |
|---|
| 94 |
if (!sqlConnection.connected) { |
|---|
| 95 |
Alert.show("SQLConnection is not connected", "ERROR!"); |
|---|
| 96 |
return; |
|---|
| 97 |
} |
|---|
| 98 |
var columnDef:ArrayCollection = SqlUtil.getColumnDef(sqlConnection, table); |
|---|
| 99 |
this.dataProvider = columnDef; |
|---|
| 100 |
} |
|---|
| 101 |
} |
|---|
| 102 |
} |
|---|