?? locationedit.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;
/*
* locationEdit pops up a window allowing the user to edit values in the lookup
* table that load the "Location" combobox.
*
*/
public class locationEdit extends Window {
public static locationEdit instance = new locationEdit();
dataCapture dataCapture;
gpsDataLogger gpsDataLogger;
Button btnTest,btnPurge,btnFirstRecord, btnNextRecord, btnPreviousRecord, btnLastRecord, btnClear, btnSave, btnUpdate,btnDelete,btnExit;
Edit edLocation,edRowId;
String szInput = "",s;
MessageBox mb1,mbLocationTableEmpty;
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 locationEdit(){
super ("Location Table Edit",ROUND_BORDER);
szWidth = gpsDataLogger.szWidth;
szHeight = gpsDataLogger.szHeight;
szPlatform = gpsDataLogger.szPlatform;
int iTmpWidth=this.width;
int iTmpHeight=this.height;//delete this???
setRect(CENTER,CENTER,iTmpWidth/32 * 31,iTmpWidth/8 * 5);
LoadLocationComboBoxStringArray();
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.setText("");
edRowId.setEditable(false);
btnExit = new Button(" Exit ");
btnExit.setGap(1);
add (btnExit,RIGHT-32,SAME);
add(new Label("Location"),LEFT+2,AFTER+4);
edLocation = new Edit();
add (edLocation);
edLocation.setRect(AFTER+2,SAME,iTmpWidth/16 * 10,PREFERRED);
edLocation.setMaxLength(40);
edLocation.setText("");
pbgBrowseButtons = new PushButtonGroup(szBrowseButtons,false,-1,2,6,2,true,PushButtonGroup.BUTTON);
add(pbgBrowseButtons,CENTER,AFTER+2);
btnPurge = new Button("Purge Location Lookup Table");
btnPurge.setGap(1);
add (btnPurge,CENTER,AFTER+2);
//setFocusLess(true);
}
public void onStart() {
setFont(bigFont);
}
private boolean verifyFields(){
boolean locationOk = edLocation.getText().length() > 0;
StringBuffer sb = new StringBuffer();
if (!locationOk) sb.append("location|");
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.iEmptyLocationLookupTable = 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){
LoadLocationComboBoxStringArray();
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){
PurgeLocationLookupTable();
CreateLocationLookupTable();
CreateLocationLookupTableIndex();
clear();
gpsDataLogger.iEmptyLocationLookupTable = 1; //reset flag to 'empty'
}
else if (idx == 1){
}
}
}
}
public void firstRecord(){
ResultSet rs = driver.executeQuery("select rowid,location, from locationLookupDB");
rs.first();
edLocation.setText(rs.getString("location"));
edRowId.setText(rs.getString("rowid"));
rs.close();
}
public void previousRecord(){//adjusted to keep rowId zero from displaying
szRowId = edRowId.getText();
rowId = Convert.toInt(szRowId);
ResultSet rs = driver.executeQuery("select rowid, location from locationLookupDB 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,location, from locationLookupDB");//full recordset is returned
}else{//...if rowId 1 or >...
rs.last();
edLocation.setText(rs.getString("location"));
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,location from locationLookupDB 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,location, from locationLookupDB");//full recordset is returned
rs.last(); //only last record in rs is shown
}else{ //shows first record of first rs above w/all of the > rowId records
rs.first();
}
edLocation.setText(rs.getString("location"));
edRowId.setText(rs.getString("rowid"));
rs.close();
}
public void lastRecord(){
ResultSet rs = driver.executeQuery("select rowid,location from locationLookupDB");
rs.last();
edLocation.setText(rs.getString("location"));
edRowId.setText(rs.getString("rowid"));
rs.close();
}
private void doInsertUpdate(boolean isInsert){
String szLocation = edLocation.getText();
String szRowId = edRowId.getText();
long lastUpdated = new Time().getTimeLong();
int rows = -1;
try{
if (isInsert){//Save
rows = driver.executeUpdate("insert into locationLookupDB values ('"+szLocation+"',"+lastUpdated+")");
}else//update
rows = driver.executeUpdate("update locationLookupDB set location='"+szLocation+"', 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 locationLookupDB where rowid="+szRowId);
clear();
if (rows == 1){
clear();
}else Sound.beep();
}
public void clear(){
edLocation.setText("");
edRowId.setText("");
}
public void Exit(){
gpsDataLogger.iFirstLocationIteration = 2;
unpop();
}
/***************************************************************************************
* LoadLocationComboBoxStringArray() This function moves strings that have been entered
* in the lookup table into the string array (szLocationLookup) that populates the ComboBox.
* It does this by:
* 1) Querying the lookup table creating a resultset with the location categories
* that need to be loaded into the ComboBox.
* 2) Creates a temporary string array, (szTemp1)and then copies the szLocationLookup
* string array into the szTemp1. The array reference for szTemp1 is then copied
* to szLocationLookup. This is useful for removing the "Location 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 LoadLocationComboBoxStringArray(){
ResultSet rs = driver.executeQuery("select rowid,location from locationLookupDB");
//Part 1: resets szLocationLookup string to allow for reductions in table records
//gpsDataLogger.szLocationLookup = {""};
String []szTemp1 = {""};
Vm.copyArray(gpsDataLogger.szLocationLookup, 0, szTemp1, 0, szTemp1.length);
gpsDataLogger.szLocationLookup = szTemp1;
//Part 2: populates remainder of table, expanding the array length as needed
rs.first();
gpsDataLogger.szLocationLookup[0] = rs.getString("location");
int i = 1;
while (rs.next()){
//make string array dynamic
if (i >= gpsDataLogger.szLocationLookup.length) {
String[] szTemp2 = new String[gpsDataLogger.szLocationLookup.length + 1];
Vm.copyArray(gpsDataLogger.szLocationLookup, 0, szTemp2, 0, gpsDataLogger.szLocationLookup.length);
gpsDataLogger.szLocationLookup = szTemp2;
}
gpsDataLogger.szLocationLookup[i] = rs.getString("location");
++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("location");
gpsDataLogger.szLocationLookup = ret;
rs.close();
} */
private void PurgeLocationLookupTable(){ //deletes lookup table
driver.executeUpdate("drop table locationLookupDB");
}
private void CreateLocationLookupTable(){ //deletes lookup table
driver.execute("create table locationLookupDB(location char(50), lastUpdated long)");
}
private void CreateLocationLookupTableIndex(){ //deletes lookup table
driver.execute("CREATE INDEX IDX_0 ON locationLookupDB(rowid)");
driver.execute("CREATE INDEX IDX_1 ON locationLookupDB(location)");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -