tripmate.java

来自「Program helping you to remember the rout」· Java 代码 · 共 245 行

JAVA
245
字号
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
import java.util.*;

public class TripMate extends MIDlet implements CommandListener  
{ 
    static Command QUIT_CMD = new Command("Quit", Command.BACK, 4);
    static Command BACK_CMD = new Command("Back", Command.BACK, 4);
    static Command SELECT_CMD = new Command("Select", Command.ITEM, 2);
    static Command DELETE_CMD = new Command("Delete", Command.ITEM, 3);
    static Command UPDATE_CMD = new Command("Update", Command.ITEM, 3);

    static Command NAVIGATE_CMD = new Command("Navigate", Command.ITEM, 1);
    static Command BACKWARD_CMD = new Command("Navigate Back", Command.ITEM, 2);
    static Command LIST_CMD = new Command("List", Command.ITEM, 3);

    static Command NEW_CMD = new Command("New", Command.ITEM, 1);
    static Command ENTER_CMD = new Command("Enter", Command.BACK, 1);

    static Command NEXT_CMD = new Command("Next", Command.ITEM, 1);
    static Command GRAPHIC_CMD = new Command("Graphic", Command.ITEM, 2);
    static Command TEXT_CMD = new Command("Text", Command.ITEM, 2);
    static Command SUSPEND_CMD = new Command("Suspend", Command.ITEM, 3);
    static Command RESUME_CMD = new Command("Resume", Command.ITEM, 3);
    static Command STOP_CMD = new Command("Stop", Command.BACK, 1);

    static Command FINISH_CMD = new Command("Finish", Command.BACK, 2);
    static final int FREQUENCY = 1000;

    static final String TITLE = "TripMate";
 
    static final String[] directions = {"Forward", "Right", "Backward", "Left"};
    static final Image[]  arrows;

    static final byte FORWARD  = (byte)0;
    static final byte RIGHT    = (byte)1;
    static final byte BACKWARD = (byte)2;
    static final byte LEFT     = (byte)3;
    static final int  CHANGE_DIRECTION_MASK = 2;

    static {  
        arrows = new Image[4];
        try { 
            arrows[LEFT] = Image.createImage("/left.png");
            arrows[FORWARD] = Image.createImage("/forward.png");
            arrows[RIGHT] = Image.createImage("/right.png");
            arrows[BACKWARD] = Image.createImage("/backward.png");
        } catch (java.io.IOException err) {}
    }


    private void openStore() {
        try { 
            store = RecordStore.openRecordStore("TRIPMATE", true);
            Trip trip = null;
            int  nPoints = 0;
            Point[] pointBuf = new Point[32];
            for (int recordId = 1, last = store.getNextRecordID(); recordId < last; recordId++) {
                byte[] record = null;
                try { 
                    record = store.getRecord(recordId);                    
                } catch (InvalidRecordIDException x) {
                    // record was deleted 
                }
                if (record == null) { 
                    continue;
                }
                if (recordId == 1) { 
                    state = new TripState();
                    state.unpack(record);
                    continue;
                }
                int tripId = Converter.unpackInt(record, 0);
                if (tripId == 0) { 
                    // new trip
                    if (trip != null) {
                        if (nPoints == 0) { 
                            deleteTrip(trip);
                        } else { 
                            trip.points = new Point[nPoints];
                            trips.addElement(trip);
                            System.arraycopy(pointBuf, 0, trip.points, 0, nPoints);
                        }                        
                    }
                    trip = new Trip(recordId, record);
                    nPoints = 0;
                } else { 
                    if (nPoints == pointBuf.length) { 
                        Point[] newPointBuf = new Point[nPoints*2];
                        System.arraycopy(pointBuf, 0, newPointBuf, 0, nPoints);
                        pointBuf = newPointBuf;
                    }
                    pointBuf[nPoints++] = new Point(recordId, record);
                }
            }
            if (trip != null) {
                if (nPoints == 0) { 
                    deleteTrip(trip);
                } else { 
                    trip.points = new Point[nPoints];
                    trips.addElement(trip);
                    System.arraycopy(pointBuf, 0, trip.points, 0, nPoints);
                }
            }                        
            if (state == null) { 
                state = new TripState();
                byte[] record = state.pack();
                store.addRecord(record, 0, record.length);
            }
       } catch(Exception x) { 
        }
    }

    void updatePoint(Point point) { 
        try { 
            byte[] record = point.pack();
            store.setRecord(point.id, record, 0, record.length);
        } catch (Exception x) {}
    }

    void addPoint(Point point) { 
        try { 
            byte[] record = point.pack();
            point.id = store.addRecord(record, 0, record.length);
        } catch (Exception x) {}
    }

    void updateTrip(Trip trip) { 
        try { 
            byte[] record = trip.pack();
            store.setRecord(trip.id, record, 0, record.length);
        } catch (Exception x) {}
    }

    void updateState() { 
        try { 
            byte[] record = state.pack();
            store.setRecord(1, record, 0, record.length);
        } catch (Exception x) {}
    }
       

    void deleteTrip(Trip trip) { 
        try { 
            store.deleteRecord(trip.id);
        } catch (Exception x) {}
    }

    void deletePoint(Point point) { 
        try { 
            store.deleteRecord(point.id);
        } catch (Exception x) {}
    }

    void addTrip(Trip trip) { 
        try { 
            byte[] record = trip.pack();
            trip.id = store.addRecord(record, 0, record.length);
            trips.addElement(trip);
        } catch (Exception x) {}
    }

    private void closeStore() 
    {
        if (store != null) { 
            try { 
                store.closeRecordStore();
                store = null;
            } catch (Exception x) {}
        }
    }

    public TripMate() { 
        trips = new Vector();
        openStore();
	mainMenu = new List(TITLE, List.IMPLICIT);
        for (int i = trips.size(); --i >= 0;) {
            mainMenu.append(((Trip)trips.elementAt(i)).description, null);
        }
        mainMenu.setCommandListener(this);
	mainMenu.addCommand(QUIT_CMD);
	mainMenu.addCommand(NEW_CMD);
	mainMenu.addCommand(SELECT_CMD);
	mainMenu.addCommand(DELETE_CMD);
    }
    
    protected void destroyApp(boolean unconditional) {
        closeStore();
        mainMenu = null;
    }    

    protected  void pauseApp() {
    }

    protected void startApp() 
    {
        switch (state.tripState) { 
        case TripState.NEW_TRIP:
            new AddPointForm((Trip)trips.elementAt(trips.size()-1), this);
            break;
        case TripState.NAVIGATE_FORWARD:
        case TripState.NAVIGATE_BACKWARD:
            new NavigateCanvas((Trip)trips.elementAt(state.tripNo), this, mainMenu); 
            break;
        default:
            Display.getDisplay(this).setCurrent(mainMenu);
        }
    }

    void quit() { 
        destroyApp(true);
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == QUIT_CMD) {
            quit();
        } else if (c == NEW_CMD) {
            new AddTripForm(this);
        } else { 
            int i = mainMenu.getSelectedIndex();
            if (i >= 0) {
                int j = trips.size() - i - 1;
                if (c == DELETE_CMD) {
                    Trip trip = (Trip)trips.elementAt(j);
                    trips.removeElementAt(j);
                    mainMenu.delete(i);
                    deleteTrip(trip);
                    for (int k = 0; k < trip.points.length; k++) {
                        deletePoint(trip.points[k]);
                    }
                } else {
                    new TripForm(j, this);
                }
            }
        }
    }    

    List mainMenu;
    Vector trips;
    RecordStore store;
    TripState   state;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?