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

📄 chat.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        tfRelayHost = new TextField("Relay host: ", 
                                    prop == null ? "209.25.154.233" : prop, 
                                    MAX_TEXTFIELD_SIZE,
                                    TextField.ANY);

        prop = getAppProperty("RelayPort");
        tfRelayPort = new TextField("Relay port: ", 
                                    prop == null ? "9700" : prop,
                                    MAX_TEXTFIELD_SIZE,
                                    TextField.NUMERIC);

        prop = getAppProperty("Identity");
        tfIdentity = new TextField("Identity: ", 
                                   prop == null ? "" : prop,
                                   MAX_TEXTFIELD_SIZE,
                                   TextField.ANY);

        prop = getAppProperty("PollInterval");
        tfPollInterval = new TextField("Poll interval: ", 
                                       prop == null ? 
                                         Integer.toString(pollInterval) : prop,
                                       MAX_TEXTFIELD_SIZE,
                                       TextField.NUMERIC);

        cgChatMode = new ChoiceGroup("Chat Mode", Choice.EXCLUSIVE,
                                     CHAT_MODE_CHOICES,
                                     null);
        prop = getAppProperty("ChatMode");
        if ("group".equals(prop)) {
            cgChatMode.setSelectedIndex(0, true);
        } else {
            cgChatMode.setSelectedIndex(1, true);
        }

        RecordStore rs = null;
        try {
            rs = RecordStore.openRecordStore(RECORD_STORE_NAME, false);
            byte[] data = rs.getRecord(CONFIG_RECORD_INDEX);
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            DataInputStream dis = new DataInputStream(bais);
            tfRelayHost.setString(dis.readUTF());
            tfRelayPort.setString(dis.readUTF());
            tfIdentity.setString(dis.readUTF());

            tfPollInterval.setString(dis.readUTF());
            try {
                pollInterval = Integer.parseInt(tfPollInterval.getString());
            } catch (NumberFormatException ex) {
                showAlert("Read Config", 
                          "Error parsing poll interval: " + 
                          tfPollInterval.getString(),
                          AlertType.WARNING, 
                          DEFAULT_ALERT_TIMEOUT,
                          initForm);
            }

            isGroupChat = dis.readBoolean();
            if (isGroupChat) {
                cgChatMode.setSelectedIndex(0, true);
            } else {
                cgChatMode.setSelectedIndex(1, true);
            }

            int stateLen = dis.readShort();
            state = new byte[stateLen];
            dis.readFully(state);

            if (DEBUG) {
                System.out.println("Read config: host=" + 
                                   tfRelayHost.getString() +
                                   " port=" + tfRelayPort.getString() + 
                                   " identity=" + tfIdentity.getString() +
                                   " poll=" + tfPollInterval.getString() +
                                   " groupChat=" + isGroupChat +
                                   " stateLen=" + stateLen);
            }
        } catch (Exception ex) {
            if (DEBUG) {
                System.out.println(ex);
            }
        } finally {
            try {
                if (rs != null) {
                    rs.closeRecordStore();
                }
            } catch (Exception ex) {
                if (DEBUG) {
                    System.out.println(ex);
                }
            }
        }
    }

    private void storeConfig() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        RecordStore rs = null;
        try {
            dos.writeUTF(tfRelayHost.getString());
            dos.writeUTF(tfRelayPort.getString());
            dos.writeUTF(tfIdentity.getString());
            dos.writeUTF(tfPollInterval.getString());

            dos.writeBoolean(isGroupChat);
            dos.writeShort(state.length);
            dos.write(state);
            dos.close();
            byte[] data = baos.toByteArray();

            rs = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
            int recordId = CONFIG_RECORD_INDEX;
            try {
                rs.getRecord(recordId);
                rs.setRecord(recordId, data, 0, baos.size());
            } catch (RecordStoreException rex) {
                recordId = rs.addRecord(data, 0, baos.size());
                // assert (recordId == CONFIG_RECORD_INDEX)
            }

            if (DEBUG) {
                System.out.println("Saved config: recordId=" + recordId + 
                                   " host=" + tfRelayHost.getString() +
                                   " port=" + tfRelayPort.getString() + 
                                   " identity=" + tfIdentity.getString() +
                                   " poll=" + tfPollInterval.getString() +
                                   " groupChat=" + isGroupChat +
                                   " stateLen=" + state.length);
            }
        } catch (Exception ex) {
            if (DEBUG) {
                System.out.println(ex);
            }
        } finally {
            try {
                if (rs != null) {
                    rs.closeRecordStore();
                }
                dos.close();
                baos.close();
            } catch (Exception ex) {
                if (DEBUG) {
                    System.out.println(ex);
                }
            }
        }
    }

    private void resetConfig() {
        if (connected) {
            disconnect();
        }

        peer = null;
        state = new byte[0];

        try {
            RecordStore.deleteRecordStore(RECORD_STORE_NAME);
        } catch (Exception ex) {
            if (DEBUG) {
                System.out.println(ex);
            }
        }

	configForm = null;
	readBuddies();
	readConfig();
	currentBuddy = null;
	editConfig();
    }

    private void setupConfirmForm() {
        confirmForm = new Form("Config reset");
        Command cmdOK = new Command("OK", Command.OK, 1);
        Command cmdBack = new Command("Back", Command.BACK, 2);
	StringItem si = new StringItem(null, 
"This will restore the default configuration and buddy list, continue?");
        confirmForm.addCommand(cmdOK);
        confirmForm.addCommand(cmdBack);
        confirmForm.setCommandListener(this);
        confirmForm.append(si);
    }

    private void editBuddies() {
        display.setCurrent(buddyList);
    }

    private void readBuddies() {
        buddyList = new List("Buddy List", List.IMPLICIT);

        Command cmdChat = new Command("Chat", Command.SCREEN, 1);
        Command cmdAdd = new Command("Add", Command.SCREEN, 2);
        Command cmdEdit = new Command("Edit", Command.SCREEN, 3);
        Command cmdDelete = new Command("Delete", Command.SCREEN, 4);
        Command cmdBack = new Command("Back", Command.BACK, 5);
        buddyList.addCommand(cmdChat);
        buddyList.addCommand(cmdAdd);
        buddyList.addCommand(cmdEdit);
        buddyList.addCommand(cmdDelete);
        buddyList.addCommand(cmdBack);
        buddyList.setCommandListener(this) ;

        RecordStore rs = null;
        try {
            rs = RecordStore.openRecordStore(RECORD_STORE_NAME, false);
            byte[] data = rs.getRecord(BUDDYLIST_RECORD_INDEX);
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            DataInputStream dis = new DataInputStream(bais);
            int size = dis.readShort();
            if (DEBUG) {
                System.out.println("Reading buddies: count=" + size);
            }
            for (int i=0; i < size; i++) {
                String buddy = dis.readUTF();
                buddyList.append(buddy, null);

                String buddyId = dis.readUTF();
                buddyIds.put(buddy, buddyId);

                if (DEBUG) {
                    System.out.println("  Read buddy=\"" + buddy + 
                                       "\" id=\"" + buddyId + "\"");
                }
            }
            currentBuddy = dis.readUTF();
            if (DEBUG) {
                System.out.println("Read current buddy=" + currentBuddy);
            }
            initForm.setTitle(currentBuddy);
        } catch (Exception ex) {
            if (DEBUG) {
                System.out.println(ex);
            }
        } finally {
            try {
                if (rs != null) {
                    rs.closeRecordStore();
                }
            } catch (Exception ex) {
                if (DEBUG) {
                    System.out.println(ex);
                }
            }
        }

        boolean ip2p = false;
        boolean picshare = false;
        int size = buddyList.size();
        for (int i=0; i < size; i++) {
            String buddy = buddyList.getString(i);
            if (INSTANTP2P_GROUPNAME.equals(buddy)) {
                ip2p = true;
            } 
            if (PICSHARE_GROUPNAME.equals(buddy)) {
                picshare = true;
            }
        }

        if (!ip2p) {
            // pre-populate the buddy list with the group name of myJXTA
            // to enable interoperability with myJXTA aka InstantP2P
            buddyList.append(INSTANTP2P_GROUPNAME, null);
        }
        if (!picshare) {
            // pre-populate the buddy list with the group name of PicShare
            buddyList.append(PICSHARE_GROUPNAME, null);
        }
    }

    private void storeBuddies() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        RecordStore rs = null;
        try {
            int size = buddyList.size();
            if (DEBUG) {
                System.out.println("Saving buddies: count=" + size);
            }
            dos.writeShort(size);
            for (int i=0; i < size; i++) {
                String buddy = buddyList.getString(i);
                dos.writeUTF(buddy);
                String buddyId = (String) buddyIds.get(buddy);
                if (buddyId == null) {
                    // not yet discovered
                    buddyId = "";
                }
                dos.writeUTF(buddyId);
                if (DEBUG) {
                    System.out.println("  Saved buddy=\"" + 
                                       buddy + "\" id=\"" + buddyId + "\"");
                }
            }

            // also store the currently selected buddy, if any
            if (currentBuddy != null) {
                dos.writeUTF(currentBuddy);
                if (DEBUG) {
                    System.out.println("Saved current buddy=\"" + 
                                       currentBuddy + "\"");
                }
            } else {
                dos.writeUTF("");
            }
            dos.close();
            byte[] data = baos.toByteArray();

            rs = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
            int recordId = BUDDYLIST_RECORD_INDEX;
            try {
                rs.getRecord(recordId);
                rs.setRecord(recordId, data, 0, baos.size());
            } catch (RecordStoreException rex) {
                recordId = rs.addRecord(data, 0, baos.size());
                // assert (recordId == BUDDYLIST_RECORD_INDEX)
            }

            if (DEBUG) {
                System.out.println("Saved buddyList: recordId=" + recordId + 
                                   " count=" + size +
                                   " dlen=" + data.length);
            }
        } catch (Exception ex) {
            if (DEBUG) {
                System.out.println(ex);
            }
        } finally {
            try {
                if (rs != null) {
                    rs.closeRecordStore();
                }
                dos.close();
                baos.close();
            } catch (Exception ex) {
                if (DEBUG) {
                    System.out.println(ex);
                }
            }
        }
    }

    private void chatBuddy() {
        int i = buddyList.getSelectedIndex();
        String buddy = buddyList.getString(i);
        currentBuddy = buddy;
        initForm.setTitle(currentBuddy);
        display.setCurrent(initForm);
    }

    private void addBuddy() {
        int i = buddyList.getSelectedIndex();

        addBuddyForm = new Form("Add Buddy");
        tfBuddy = new TextField("New Buddy: ", 
                                "", 
                                MAX_TEXTFIELD_SIZE,
                                TextField.ANY);
        addBuddyForm.append(tfBuddy);
        Command cmdOK = new Command("OK", Command.OK, 1);
        addBuddyForm.addCommand(cmdOK);
        addBuddyForm.setCommandListener(this) ;
        display.setCurrent(addBuddyForm);
    }

    private void editBuddy() {
        int i = buddyList.getSelectedIndex();
        String buddy = buddyList.getString(i);

        editBuddyForm = new Form("Edit Buddy");
        tfBuddy = new TextField("Edit Buddy: ", 
                                buddy, 
                                MAX_TEXTFIELD_SIZE,
                                TextField.ANY);
        editBuddyForm.append(tfBuddy);
        Command cmdOK = new Command("OK", Command.OK, 1);
        editBuddyForm.addCommand(cmdOK);
        editBuddyForm.setCommandListener(this) ;
        display.setCurrent(editBuddyForm);
    }

    private void deleteBuddy() {
        int i = buddyList.getSelectedIndex();
        buddyList.delete(i);
    }

    private boolean send() {
        if (peer == null || !connected) {
            initiateConnect();
            sendPending = true;
            return false;
        }

        String msg = tfSentMsg.getString();
        Element[] elm = new Element[3];
        elm[0] = new Element("JxtaTalkSenderName", 
                             tfIdentity.getString().getBytes(), 
                             null, null);
        elm[1] = new Element("JxtaTalkSenderMessage", 
                             msg.getBytes(), 
                             null, null);
        // for compatibility with myJXTA aka InstantP2P
        elm[2] = new Element("GrpName", 
                             "NetPeerGroup".getBytes(), 
                             null, null);
        Message m = new Message(elm);

⌨️ 快捷键说明

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