⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapbrowser.java

📁 Online Map for Mobile
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
import java.util.*;
import java.io.*;

public class MapBrowser extends MIDlet implements Receiver 
{ 
    static final String STORE_NAME="MAPBROWSER";
    static final String INDEX_FILE="/index";
    static final String ENCODING = "UTF-8";

    static Command QUIT_CMD;
    static Command BACK_CMD;
    static Command SEARCH_CMD;
    static Command SELECT_CMD;
    static Command MAP_CMD;
    static Command INDEX_CMD;
    static Command ZOOM_IN_CMD;
    static Command ZOOM_OUT_CMD;
    static Command SETTINGS_CMD;
    static Command HELP_CMD;
    static Command ABOUT_CMD;
    static Command ALL_CMD;
    static Command NEXT_CMD;
    static Command INFO_CMD;
    static Command CLEAN_CMD;
    static Command SAVE_CMD;
    static Command LOAD_CMD;

    static final String DEFAULT_MAP_LOCATION = "www.garret.ru/~knizhnik/Moscow/";

    // Map location 
    String url = DEFAULT_MAP_LOCATION;

    // Map characterisics
    String city;
    int width;
    int height;
    int zoom;
    int levels;
    int cell;

    RecordStore   store;
    Settings      settings;
    BrowserCanvas canvas;
    Form          main;
    Vector        indices;

    private void openStore() {
        try { 
            store = RecordStore.openRecordStore(STORE_NAME, true);
            settings = new Settings();
            try { 
                byte[] record = store.getRecord(1);                    
                if (record.length == settings.pack().length) { 
                    settings = new Settings(record);
                } else {
                    record = settings.pack();
                    store.setRecord(1, record, 0, record.length);
                }
            } catch (InvalidRecordIDException x) {
                byte[] record = settings.pack();
                store.addRecord(record, 0, record.length);
            }
        } catch(Exception x) {
            System.out.println("Failed to load settings: " + x);
            settings = new Settings(); 
        }
    }

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

    void saveSettings() { 
        try { 
            byte[] record = settings.pack();
            store.setRecord(1, record, 0, record.length);
        } catch (Exception x) {}
    }   
     
    void setLocale() {
        Locale loc = new Locale(settings.locale);
        QUIT_CMD = new Command(loc.getResource("Quit"), Command.CANCEL, 3);
        BACK_CMD = new Command(loc.getResource("Back"), Command.BACK, 3);
        SEARCH_CMD = new Command(loc.getResource("Search"), Command.SCREEN, 3);
        SELECT_CMD = new Command(loc.getResource("Select"), Command.SCREEN, 1);
        MAP_CMD = new Command(loc.getResource("Map"), Command.SCREEN, 1);
        INDEX_CMD = new Command(loc.getResource("Index"), Command.SCREEN, 1);
        ZOOM_IN_CMD = new Command(loc.getResource("Zoom In"), Command.SCREEN, 2);
        ZOOM_OUT_CMD = new Command(loc.getResource("Zoom Out"), Command.SCREEN, 2);
        SETTINGS_CMD = new Command(loc.getResource("Settings"), Command.SCREEN, 7);
        HELP_CMD = new Command(loc.getResource("Help"), Command.HELP, 8);
        ABOUT_CMD = new Command(loc.getResource("About"), Command.SCREEN, 9);
        ALL_CMD = new Command(loc.getResource("Select All"), Command.SCREEN, 2);
        NEXT_CMD = new Command(loc.getResource("Next"), Command.SCREEN, 4);
        INFO_CMD = new Command(loc.getResource("Info"), Command.SCREEN, 5);
        CLEAN_CMD = new Command(loc.getResource("Clean"), Command.SCREEN, 6);
        SAVE_CMD = new Command(loc.getResource("Save"), Command.SCREEN, 1);
        LOAD_CMD = new Command(loc.getResource("Load"), Command.SCREEN, 1);
    }

    public void failure(String url) { 
        error(Locale.current.getResource("Connection Failed"), main);
    }

    void error(String text, final Displayable parent) {
        Alert a = new Alert(Locale.current.getResource("Error"));
        a.setType(AlertType.ERROR);
        a.setString(text);
        a.setTimeout(Alert.FOREVER);
        if ("Nokia6600".equals(System.getProperty("microedition.platform"))) { 
            Display.getDisplay(this).setCurrent(parent);
            Display.getDisplay(this).setCurrent(a, parent);
            a.setCommandListener(new CommandListener() { 
                    public void commandAction(Command c, Displayable d) { 
                        Display.getDisplay(MapBrowser.this).setCurrent(parent);
                    }
                }
            );
        } else { 
            Display.getDisplay(this).setCurrent(a, parent);        
        }         
    }

    public void received(String url, byte[] body, int len) { 
        InputStream in = new ByteArrayInputStream(body, 0, len);
        XMLParser parser;
        if ("ru".equals(Locale.current.getLanguage())) { 
            parser = new XMLParser(new CyrillicWinReader(in));
        } else { 
            // parser = new XMLParser(new ArabicWinReader(in));
            try { 
                parser = new XMLParser(new SafeInputStreamReader(in, ENCODING));
                //parser = new XMLParser(new InputStreamReader(in, ENCODING));
            } catch (UnsupportedEncodingException err) {
                parser = new XMLParser(new InputStreamReader(in));
            }
        }
        indices = new Vector();
        parser.parse(new XMLBuilder() {
                public XMLBuilder addElement(String name) {
                    if (!name.equals("city")) { 
                        throw new XMLException("<city> element expected");
                    }
                    return new XMLBuilder() {
                            public void addAttribute(String name, String value) { 
                                if ("name".equals(name)) { 
                                    city = value.toLowerCase();
                                } else if ("cell".equals(name)) { 
                                    cell = Integer.parseInt(value);
                                } else if ("width".equals(name)) { 
                                    width = Integer.parseInt(value);
                                } else if ("height".equals(name)) { 
                                    height = Integer.parseInt(value);
                                } else if ("levels".equals(name)) { 
                                    levels = Integer.parseInt(value);
                                } else if ("zoom".equals(name)) { 
                                    zoom = Integer.parseInt(value);
                                }
                            }
                            public XMLBuilder addElement(String name) {
                                if (!name.equals("index")) { 
                                    throw new XMLException("<index> element expected");
                                }
                                Index index = new Index();
                                indices.addElement(index);
                                return new IndexBuilder(index);
                            }
                        };
                }
            });

        showMap();
    }
    
    void showMap() { 
        canvas = new BrowserCanvas(this);
    }

    protected void destroyApp(boolean unconditional) {
        closeStore();
    }    

    protected  void pauseApp() {
    }

    protected void startApp() 
    {
        openStore();
        setLocale();
        main = new UrlForm(this);
    }

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

⌨️ 快捷键说明

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