?? speciesedit.java
字號:
import litebase.*;
import litebase.ui.*;
import waba.fx.Font;
import waba.fx.Sound;
import waba.sys.Convert;
import waba.sys.Time;
import waba.sys.Vm;
import waba.ui.Button;
import waba.ui.ComboBox;
import waba.ui.Container;
import waba.ui.Control;
import waba.ui.ControlEvent;
import waba.ui.Edit;
import waba.ui.Event;
import waba.ui.Label;
import waba.ui.MainWindow;
import waba.ui.MessageBox;
import waba.ui.PushButtonGroup;
import waba.ui.TabPanel;
import waba.ui.Window;
/*
* speciesEdit pops up a window allowing the user to edit values in the lookup
* table that load the "Species" combobox.
*
*/
public class speciesEdit extends Window {
public static speciesEdit instance = new speciesEdit();
dataCapture dataCapture;
gpsDataLogger gpsDataLogger;
Button btnTest,btnPurge,btnFirstRecord, btnNextRecord, btnPreviousRecord, btnLastRecord, btnClear, btnSave, btnUpdate,btnDelete,btnExit;
Edit edSpecies,edRowId;
String szInput = "",s;
MessageBox mb1;
private PushButtonGroup pbgBrowseButtons;
String szBrowseButtons[] = {" Save ","Update","Delete","Clear "," ||<<"," << "," >> "," >>|| "};
String szHeight, szWidth, szPlatform, szRowId;
int rowId;
char c;
private Font bigFont=new Font("SW",Font.PLAIN,72);
LitebaseConnection driver = LitebaseConnection.getInstance("AZoe");
public speciesEdit(){
super ("Species Table Edit",ROUND_BORDER);
szWidth = gpsDataLogger.szWidth;
szHeight = gpsDataLogger.szHeight;
szPlatform = gpsDataLogger.szPlatform;
int iTmpWidth=this.width;
int iTmpHeight=this.height;
setRect(CENTER,CENTER,iTmpWidth/32 * 31,iTmpWidth/8 * 5);
LoadSpeciesComboBoxStringArray();
add(new Label("Row Id"),LEFT+2,TOP+2);
edRowId = new Edit();
add (edRowId);
edRowId.setRect(AFTER+2,SAME-1,25,PREFERRED);
edRowId.setMaxLength(5);
edRowId.setEditable(false);
btnExit = new Button(" Exit ");
btnExit.setGap(1);
add (btnExit,RIGHT-32,SAME);
add(new Label("Species"),LEFT+2,AFTER+4);
edSpecies = new Edit();
add (edSpecies);
edSpecies.setRect(AFTER+2,SAME,iTmpWidth/16 * 10,PREFERRED);
edSpecies.setMaxLength(20);
pbgBrowseButtons = new PushButtonGroup(szBrowseButtons,false,-1,2,6,2,true,PushButtonGroup.BUTTON);
add(pbgBrowseButtons,CENTER,AFTER+2);
btnPurge = new Button("Purge Species Lookup Table");
btnPurge.setGap(1);
add (btnPurge,CENTER,AFTER+2);
//setFocusLess(true);
}
public void onStart() {
setFont(bigFont);
}
private boolean verifyFields(){
boolean speciesOk = edSpecies.getText().length() > 0;
StringBuffer sb = new StringBuffer();
if (!speciesOk) sb.append("species|");
if (sb.length() > 0) {
sb.setLength(sb.length()-1); // remove the last |
new MessageBox("Attention","You must fill/correct|the following fields:|"+sb).popupModal();
return false;
}
return true;
}
public void onEvent(Event e){
int ipbg;
Control edAtual;
if (e.type == ControlEvent.PRESSED){
if ((e.target == pbgBrowseButtons &&
(ipbg = pbgBrowseButtons.getSelected()) != -1)){
switch (ipbg){
case -1:break; //no item selected
case 0:{ // Save button
edAtual = getParentWindow().getFocus();
if (edAtual != null) {
if (verifyFields())
doInsertUpdate(true);
clear();
gpsDataLogger.iEmptySpeciesLookupTable = 0; //reset flag to 'not empty'
}
}break;
case 1:{ // Update button
edAtual = getParentWindow().getFocus();
if (edAtual != null) {
if (verifyFields()){
if (rowId > 0){
doInsertUpdate(false);
clear();}
else
Sound.beep();
}
}
}break;
case 2:{ // Delete button
edAtual = getParentWindow().getFocus();
if (edAtual != null) {
szRowId = edRowId.getText();
rowId = Convert.toInt(szRowId);
if (rowId > 0)
doDelete();
else
Sound.beep();
}
}break;
case 3:{ // Clear button
edAtual = getParentWindow().getFocus();
if (edAtual != null) {
clear();
}
}break;
case 4:{ // First Record, ("||<<") button
edAtual = getParentWindow().getFocus();
if (edAtual != null) {
firstRecord();
}
}break;
case 5:{ // Previous Record, ("<<") button
edAtual = getParentWindow().getFocus();
if (edAtual != null) {
previousRecord();
}
}break;
case 6:{ // Next Record, (">>") button
edAtual = getParentWindow().getFocus();
if (edAtual != null) {
nextRecord();
}
}break;
case 7:{ // Last Record, (">>||") button
edAtual = getParentWindow().getFocus();
if (edAtual != null) {
lastRecord();
}
}break;
}
}
else if (e.target == btnExit){
LoadSpeciesComboBoxStringArray();
Exit();
}
else if (e.target == btnPurge){
String []szButtonArray = {"Yes","No"};
mb1 = new MessageBox("Attention","Do you really want to purge | the lookup table???", szButtonArray);
mb1.popupBlockingModal();
int idx = mb1.getPressedButtonIndex();
if (idx == 0){
PurgeSpeciesLookupTable();
CreateSpeciesLookupTable();
CreateSpeciesLookupTableIndex();
clear();
gpsDataLogger.iEmptySpeciesLookupTable = 1; //reset flag to 'empty'
}
else if (idx == 1){
}
}
}
}
public void firstRecord(){
ResultSet rs = driver.executeQuery("select rowid,species, from speciesLookupDB");
rs.first();
edSpecies.setText(rs.getString("species"));
edRowId.setText(rs.getString("rowid"));
rs.close();
}
public void previousRecord(){//tweaked to keep rowId zero from displaying
szRowId = edRowId.getText();
rowId = Convert.toInt(szRowId);
ResultSet rs = driver.executeQuery("select rowid, species from speciesLookupDB where rowid <"+rowId); //single record is returned??
if ((rs.getString("rowid").compareTo("0")==0)){//keeps always empty rowId #0 from being displayed and confusing people...
rs.close();
rs = driver.executeQuery("select rowid,species, from speciesLookupDB");//full recordset is returned
}else{//...if rowId 1 or >...
rs.last();
edSpecies.setText(rs.getString("species"));
edRowId.setText(rs.getString("rowid"));
rs.close();
}
}
public void nextRecord(){//cleaned up so that rowId 0 is ignored
szRowId = edRowId.getText();
rowId = Convert.toInt(szRowId);
ResultSet rs = driver.executeQuery("select rowid,species from speciesLookupDB where rowid >"+rowId);//...returns all records greater than current, unless there are no more in which case zero is returned.
if ((rs.getString("rowid").compareTo("0")==0)){//keeps always empty rowId #0 from being displayed and confusing people...
rs.close(); //dumps rs, below makes full rs
rs = driver.executeQuery("select rowid,species, from speciesLookupDB");//full recordset is returned
rs.last(); //only last record in rs is shown
}else{
rs.first(); //shows first record of first rs above w/all of the > rowId records
}
edSpecies.setText(rs.getString("species"));
edRowId.setText(rs.getString("rowid"));
rs.close();
}
public void lastRecord(){
ResultSet rs = driver.executeQuery("select rowid,species from speciesLookupDB");
rs.last();
edSpecies.setText(rs.getString("species"));
edRowId.setText(rs.getString("rowid"));
rs.close();
}
private void doInsertUpdate(boolean isInsert){
String szSpecies = edSpecies.getText();
String szRowId = edRowId.getText();
long lastUpdated = new Time().getTimeLong();
int rows = -1;
try{
if (isInsert){//Save
rows = driver.executeUpdate("insert into speciesLookupDB values ('"+szSpecies+"',"+lastUpdated+")");
}else//update
rows = driver.executeUpdate("update speciesLookupDB set species='"+szSpecies+"', lastUpdated="+lastUpdated+" where rowid="+szRowId);
if (rows == 1){
clear();
}
else Sound.beep();
} catch (Throwable t) {Vm.debug(t.getMessage());}
}
private void doDelete(){
String szRowId = edRowId.getText();
int rows = driver.executeUpdate("delete speciesLookupDB where rowid="+szRowId);
clear();
if (rows == 1){
clear();
}else Sound.beep();
}
public void clear(){
edSpecies.setText("");
edRowId.setText("");
}
public void Exit(){
gpsDataLogger.iFirstSpeciesIteration = 2;
unpop();
}
/***************************************************************************************
* LoadSpeciesComboBoxStringArray() This function moves strings that have been entered
* in the lookup table into the string array (szSpeciesLookup) that populates the ComboBox.
* It does this by:
* 1) Querying the lookup table creating a resultset with the species categories
* that need to be loaded into the ComboBox.
* 2) Creates a temporary string array, (szTemp1)and then copies the szSpeciesLookup
* string array into the szTemp1. The array reference for szTemp1 is then copied
* to szSpeciesLookup. This is useful for removing the "Species Lookup Table Empty" string that
* is inserted when appropriate. Part 1 also allows for reductions in the array
* size.
* 3) Part 2 expands the array by methods similar to above only that a WHILE loop is used
* while rs.next is true, and that the szTemp2 string is incremented 1 string longer at
* each loop to allow for the returned rs.next string.
******************************************************************************************/
public void LoadSpeciesComboBoxStringArray(){
ResultSet rs = driver.executeQuery("select rowid,species from speciesLookupDB");
//Part 1: resets szSpeciesLookup string to allow for reductions in table records
//gpsDataLogger.szSpeciesLookup = {""};
String []szTemp1 = {""};
Vm.copyArray(gpsDataLogger.szSpeciesLookup, 0, szTemp1, 0, szTemp1.length);
gpsDataLogger.szSpeciesLookup = szTemp1;
//Part 2: populates remainder of table, expanding the array length as needed
rs.first();
gpsDataLogger.szSpeciesLookup[0] = rs.getString("species");
int i = 1;
while (rs.next()){
//make string array dynamic
if (i >= gpsDataLogger.szSpeciesLookup.length) {
String[] szTemp2 = new String[gpsDataLogger.szSpeciesLookup.length + 1];
Vm.copyArray(gpsDataLogger.szSpeciesLookup, 0, szTemp2, 0, gpsDataLogger.szSpeciesLookup.length);
gpsDataLogger.szSpeciesLookup = szTemp2;
}
gpsDataLogger.szSpeciesLookup[i] = rs.getString("species");
++i;
}
rs.close();
}
// below Part 2 causes arrayIndexOutOfBoundsException() to be thrown, but why?
/* String[] ret = new String[rs.getRowCount()];
rs.beforeFirst();
for (int i=0; rs.next(); i++)
ret[i] = rs.getString("species");
gpsDataLogger.szSpeciesLookup = ret;
rs.close();
} */
private void PurgeSpeciesLookupTable(){ //deletes lookup table
driver.executeUpdate("drop table speciesLookupDB");
}
private void CreateSpeciesLookupTable(){ //deletes lookup table
driver.execute("create table speciesLookupDB(species char(50), lastUpdated long)");
}
private void CreateSpeciesLookupTableIndex(){ //deletes lookup table
driver.execute("CREATE INDEX IDX_0 ON speciesLookupDB(rowid)");
driver.execute("CREATE INDEX IDX_1 ON speciesLookupDB(species)");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -