?? sdadataset.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package cn.sda.ui;import cn.sda.enterprise.*;import cn.sda.event.DataFilterEvent;import cn.sda.event.DataScrollChangeEvent;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.util.Enumeration;import java.util.Vector;import javax.microedition.rms.RecordEnumeration;import javax.microedition.rms.RecordStore;/** * * @author Administrator *///數據集合類public class SDADataSet { //rms private DataSetRMS dataRms = null; //字段類型 public static final byte FieldBoolean = 0; public static final byte FieldByte = 1; public static final byte FieldShort = 2; public static final byte FieldInteger = 3; public static final byte FieldLong = 4; public static final byte FieldFloat = 5; public static final byte FieldDouble = 6; public static final byte FieldString = 7; public static final byte FieldAudoID = 8; //排序方式 public static final int sortDesc = 0; public static final int sortAsc = 1; //字段集合 private Vector Fields = null; //可見值集合 private Vector Rows = null; //所有值集合 private Vector allRows = null; //delta記錄 private Vector deltaFields = null; private Vector deltaRows = null; //Key private String keyName = ""; //數據集合指針位置 public int RecNo = -1; //表達式計算器 private SDAExpression exp; //記錄merge private boolean recChangeLog = false; //是否打開自動ID計算 private boolean autoID = false; //需要刷新的表格列表 private Vector dataControlList = null; //滾動事件 private DataScrollChangeEvent onDataScrollChange = null; //數據執行過濾的事件 private DataFilterEvent onDataFilter = null; //對鏈接組件通知變動 private boolean enableControls = true; //字段描述 public class Field { //字段名 private String FiledName = ""; //字段類型 private byte FiledType = FieldString; //是否為計算字段 private boolean CalField = false; //計算字段表達式 private String CalExpression = ""; public Field() { } public Field(String fieldName) { this.FiledName = fieldName; } public Field(String fieldName, byte fieldType) { this.FiledName = fieldName; if ((fieldType > -1) && (fieldType < 8)) { this.FiledType = fieldType; } } public Field(String fieldName, byte fieldType, boolean isCalField, String CalExpression) { this.FiledName = fieldName; if ((fieldType > -1) && (fieldType < 8)) { this.FiledType = fieldType; } this.CalField = isCalField; this.CalExpression = CalExpression; } public String getCalExpression() { return CalExpression; } public void setCalExpression(String CalExpression) { if (isCanEditFields()) { this.CalExpression = CalExpression; } else { System.out.println("Can not change Expression."); } } public String getFiledName() { return FiledName; } public void setFiledName(String FiledName) { this.FiledName = FiledName; } public int getFiledType() { return FiledType; } public void setFiledType(byte FiledType) { if (isCanEditFields()) { this.FiledType = FiledType; } else { System.out.println("Can not change FiledType."); } } public boolean isCalFiled() { return CalField; } public void setCalFiled(boolean isCalFiled) { if (isCanEditFields()) { this.CalField = isCalFiled; } else { System.out.println("Can not change CalField."); } } } //列構造方法 public Field newField() { return new Field(); } public Field newField(String fieldName) { return new Field(fieldName); } public Field newField(String fieldName, byte fieldType) { return new Field(fieldName, fieldType); } public Field newField(String fieldName, byte fieldType, boolean isCalField, String CalExpression) { return new Field(fieldName, fieldType, isCalField, CalExpression); } //行數據 public class Row { private Vector valueList; private boolean visible = true; //是否可見(過濾的時候需要) private void internalRow() { valueList = new Vector(); //增加空值 for (int i = 0; i < Fields.size(); i++) { valueList.addElement(null); } } public Row() { internalRow(); } public boolean isVisible() { return visible; } public void setVisible(boolean visible) { this.visible = visible; //設置是否在Rows if (!visible) { if (isInRows()) { Rows.removeElement(this); setAutoID(); refreshDataControl(); } } else { if (!isInRows()) { Rows.addElement(this); setAutoID(); refreshDataControl(); } } } //獲取指定字段值 public String getValueByFieldName(String fieldName) { return internalGetValueByFieldName(fieldName); } private String internalGetValueByFieldName(String fieldName) { Field fd; String result = null; for (int i = 0; i < Fields.size(); i++) { fd = (Field) Fields.elementAt(i); if (fd.FiledName.equals(fieldName)) { result = (String) valueList.elementAt(i); break; } } return result; } public String getValueByFieldIndex(int fieldIndex) { return internalGetValueByFieldIndex(fieldIndex); } private String internalGetValueByFieldIndex(int fieldIndex) { if ((fieldIndex > -1) && (fieldIndex < valueList.size())) { //得到字段以后就從當前的行中獲取值 return (String) valueList.elementAt(fieldIndex); } else { return null; } } public Object getObjectByFieldIndex(int fieldIndex) { return internalGetObjectByFieldIndex(fieldIndex); } private Object internalGetObjectByFieldIndex(int fieldIndex) { if ((fieldIndex > -1) && (fieldIndex < valueList.size())) { //得到字段以后就從當前的行中獲取值 return valueList.elementAt(fieldIndex); } else { return null; } } public void setValueByFieldName(String fieldName, String fieldValue) { internalSetValueByFieldName(fieldName, fieldValue); } private void internalSetValueByFieldName(String fieldName, String fieldValue) { Field fd; for (int i = 0; i < Fields.size(); i++) { fd = (Field) Fields.elementAt(i); if ((fd.FiledName.equals(fieldName)) && (fd.FiledType != SDADataSet.FieldAudoID)) { valueList.setElementAt(fieldValue, i); //計算 try { calField(this); } catch (Exception e) { } if (isInRows()) { refreshDataControl(); } break; } } } public void setValueByFieldIndex(int fieldIndex, String fieldValue) { internalSetValueByFieldIndex(fieldIndex, fieldValue); } private void internalSetValueByFieldIndex(int fieldIndex, String fieldValue) { if ((fieldIndex > -1) && (fieldIndex < valueList.size())) { Field fd = null; fd = (Field) Fields.elementAt(fieldIndex); if (fd.FiledType != SDADataSet.FieldAudoID) { valueList.setElementAt(fieldValue, fieldIndex); //計算 try { calField(this); } catch (Exception e) { } if (isInRows()) { refreshDataControl(); } } } } //判斷是否在Rows中 private boolean isInRows() { return Rows.indexOf(this) > -1; } } //RMS操作類 private class DataSetRMS { public DataSetRMS() { } public RecordStore OpenRS(String tableName) { RecordStore rs = null; if (tableName.length() > 32) { return null; } try { rs = RecordStore.openRecordStore(tableName, true); return rs; } catch (Exception e) { return null; } } //寫入數據庫 public void WriteDataSet(String tableName) { //寫入(先刪除) deleteTable(tableName); //打開空的 RecordStore rs = OpenRS(tableName); ByteArrayOutputStream bos = null; DataOutputStream dos = null; Field fd = null; Row row = null; byte[] SendByte = null; //創建 bos = new ByteArrayOutputStream(); dos = new DataOutputStream(bos); try { //寫入字段數 dos.writeShort((short) Fields.size()); //寫入字段 for (int i = 0; i < Fields.size(); i++) { fd = (Field) Fields.elementAt(i); //字段名 dos.writeUTF(fd.FiledName); //類型 dos.writeByte(fd.FiledType); //是否計算字段 dos.writeBoolean(fd.CalField); //表達式 dos.writeUTF(fd.CalExpression); } SendByte = bos.toByteArray(); rs.addRecord(SendByte, 0, SendByte.length); bos.close(); dos.close(); //寫入具體內容 for (int i = 0; i < allRows.size(); i++) { bos = null; dos = null; bos = new ByteArrayOutputStream(); dos = new DataOutputStream(bos); row = (Row) allRows.elementAt(i); dos.writeBoolean(row.visible); for (int j = 0; j < Fields.size(); j++) { dos.writeUTF(row.getValueByFieldIndex(j)); } //寫入 SendByte = bos.toByteArray(); rs.addRecord(SendByte, 0, SendByte.length); bos.close(); dos.close(); bos = null; dos = null; } rs.closeRecordStore(); } catch (Exception e) { } } public void deleteTable(String tableName) { try { RecordStore.deleteRecordStore(tableName); } catch (Exception e) { } } //裝載 public void loadTable(String tableName) { //清空當前記錄 internalClearFields(); //id數組 int[] arrayID = null; //讀新記錄 RecordStore rs = OpenRS(tableName); try { if (rs.getNumRecords() == 0) { rs.closeRecordStore(); return; } } catch (Exception e) { } ByteArrayInputStream bis = null; DataInputStream dis = null; byte[] recByte = null; try { RecordEnumeration rem = rs.enumerateRecords(null, null, false); //記錄的順序要重新排布 arrayID = new int[rem.numRecords()]; int index = 0; while (rem.hasNextElement()) { arrayID[index] = rem.nextRecordId(); index++; } boolean asc = arrayID[0] < arrayID[arrayID.length - 1] ? true : false; //讀字段 recByte = rs.getRecord(arrayID[asc?0:arrayID.length-1]); bis = new ByteArrayInputStream(recByte); dis = new DataInputStream(bis); //字段數目 short fieldNum = dis.readShort(); for (int i = 0; i < fieldNum; i++) { Field fd = new Field(); fd.FiledName = dis.readUTF(); fd.FiledType = dis.readByte(); fd.CalField = dis.readBoolean(); fd.CalExpression = dis.readUTF(); Fields.addElement(fd); } bis.close(); dis.close(); bis = null; dis = null; //讀具體內容 for (int i = 1; i < arrayID.length; i++) { recByte = rs.getRecord(arrayID[asc?i:arrayID.length-i-1]); bis = new ByteArrayInputStream(recByte); dis = new DataInputStream(bis); Row row = new Row(); row.visible = dis.readBoolean(); for (int j = 0; j < Fields.size(); j++) { String ok=dis.readUTF(); row.setValueByFieldIndex(j, ok); } allRows.addElement(row); if(row.visible)Rows.addElement(row); dis.close(); bis.close(); bis = null; dis = null; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -