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

📄 chat.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/************************************************************************
 *
 * $Id: Chat.java,v 1.60 2002/05/28 22:45:06 akhil Exp $
 *
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
 *    must not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache
 * Foundation.
 **********************************************************************/

package net.jxta.midp.demo.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Gauge;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

import javax.microedition.midlet.MIDlet;

import net.jxta.j2me.PeerNetwork;
import net.jxta.j2me.Message;
import net.jxta.j2me.Element;

public final class Chat extends MIDlet 
    implements CommandListener, Runnable {

    private static final String RECORD_STORE_NAME = "jxta-midp-chat";
    private static final int CONFIG_RECORD_INDEX = 1;
    private static final int BUDDYLIST_RECORD_INDEX = 2;
    private static final int DEFAULT_POLL_INTERVAL = 1;
    private static final int DEFAULT_ALERT_TIMEOUT = 5000;
    private static final int DEFAULT_SCROLL = 3;

    private static final int MAX_TEXTFIELD_SIZE = 256;

    private static final String TALKNAME_PREFIX = "JxtaTalkUserName.";
    private static final String INSTANTP2P_GROUPNAME = "IP2PGRP";
    private static final String INSTANTP2P_PIPEID = "urn:jxta:uuid-" + 
        "59616261646162614E50472050325033" + 
        "D1D1D1D1D1D1D1D1D1D1D1D1D1D1D1D104";
    private static final String PICSHARE_GROUPNAME = "PicShare";
    private static final String PICSHARE_PIPEID = "urn:jxta:uuid-" +
        "59616261646162614E50472050325033" +
        "50696353686172652D5B46696C65436104";

    private static final boolean DEBUG = true;
    private static final boolean QUANTIFY = true;

    private Display display = null;
    private Form initForm = null;
    private Form replyForm = null;
    private Form configForm = null;
    private Form addBuddyForm = null;
    private Form editBuddyForm = null;
    private Form connectingForm = null;
    private Form confirmForm = null;
    private List buddyList = null;
    private ImageCanvas imageCanvas = null;

    private TextField tfRelayHost = null;
    private TextField tfRelayPort = null;
    private TextField tfIdentity = null;
    private TextField tfPollInterval = null;
    private TextField tfBuddy = null;
    private TextField tfSentMsg = null;

    private PeerNetwork peer = null;
    private String currentBuddy = null; 
    private String replyBuddy = null; 
    private Hashtable buddyIds = new Hashtable();

    private static final String[] CHAT_MODE_CHOICES = { "Group", "Private" };
    private ChoiceGroup cgChatMode = null;
    private boolean isGroupChat = true;

    private byte[] state = new byte[0];
    private int pollInterval = DEFAULT_POLL_INTERVAL;
    private Thread pollThread = null;
    private boolean stopPolling = false;
    private boolean connectInitiated = false;
    private boolean connected = false;
    private boolean sendPending = false;

    public Chat() {
        initForm = new Form("JXTA Chat");

        readBuddies();
        readConfig();
        setupConfirmForm();

        imageCanvas = new ImageCanvas(this);
        Command cmdOK = new Command("OK", Command.OK, 1);
        imageCanvas.addCommand(cmdOK);
        imageCanvas.setCommandListener(this);

        Command cmdSend = new Command("Send", Command.SCREEN, 1);
        Command cmdReply = new Command("Reply", Command.SCREEN, 2);
        Command cmdConnect = new Command("Connect", Command.SCREEN, 3);
        Command cmdBuddies = new Command("Buddy List", Command.SCREEN, 4);
        Command cmdConfig = new Command("Configuration", Command.SCREEN, 5);
        Command cmdDefault = new Command("Defaults", Command.SCREEN, 6);
        Command cmdExit = new Command("Exit", Command.EXIT, 6);
        initForm.addCommand(cmdSend);
        initForm.addCommand(cmdReply);
        initForm.addCommand(cmdConnect);
        initForm.addCommand(cmdBuddies);
        initForm.addCommand(cmdConfig);
        initForm.addCommand(cmdDefault);
        initForm.addCommand(cmdExit);

        initForm.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c.getCommandType() == Command.EXIT) {
            destroyApp(true);
            notifyDestroyed();
            return;
        } 

        String label = c.getLabel();
        if (d == initForm) {
            if (label.equals("Send")) {
                sendForm();
            } else if (label.equals("Reply")) {
                reply();
            } else if (label.equals("Configuration")) {
                editConfig();
            } else if (label.equals("Buddy List")) {
                editBuddies();
            } else if (label.equals("Connect")) {
                initiateConnect();
            } else if (label.equals("Defaults")) {
                display.setCurrent(confirmForm);
            }
        } else if (d == configForm) {
            if (c.getCommandType() == Command.OK) {
                int i = cgChatMode.getSelectedIndex();
                String chatMode = cgChatMode.getString(i);
                if (CHAT_MODE_CHOICES[0].equals(chatMode)) {
                    isGroupChat = true;
                } else {
                    isGroupChat = false;
                }
                storeConfig();

                // move to the buddy screen if there is'nt a current buddy
                if (currentBuddy == null) {
                    editBuddies();
                } else {
                    display.setCurrent(initForm);
                }
            } else if (c.getCommandType() == Command.BACK) {
                configForm = null;
                readConfig();
                display.setCurrent(initForm);
            }
        } else if (d == buddyList) {
            if (c.getCommandType() == Command.BACK) {
                display.setCurrent(initForm);
                storeBuddies();
            }

            if (label.equals("Chat")) {
                chatBuddy();
                storeBuddies();
            } else if (label.equals("Add")) {
                addBuddy();
            } else if (label.equals("Edit")) {
                editBuddy();
            } else if (label.equals("Delete")) {
                deleteBuddy();
                storeBuddies();
            }
        } else if (d == editBuddyForm) {
            if (c.getCommandType() == Command.OK) {
                int i = buddyList.getSelectedIndex();
                buddyList.set(i, tfBuddy.getString(), null);
                display.setCurrent(buddyList);
                storeBuddies();
            }
        } else if (d == addBuddyForm) {
            if (c.getCommandType() == Command.OK) {
                buddyList.append(tfBuddy.getString(), null);
                display.setCurrent(buddyList);
                storeBuddies();
            }
        } else if (d == replyForm) {
            if (label.equals("Send")) {
                if (send()) {
                    display.setCurrent(initForm);
                }
            } else if (label.equals("Back")) {
                display.setCurrent(initForm);
            }
        } else if (d == confirmForm) {
            if (c.getCommandType() == Command.OK) {
                resetConfig();
            } else {
                display.setCurrent(initForm);
            }
        } else if (d == imageCanvas) {
            if (c.getCommandType() == Command.OK) {
                display.setCurrent(initForm);
            }
        }
    }

    public void startApp() {
        display = Display.getDisplay(this);

        // startup in the config screen if the identity is not configured
        if ("".equals(tfIdentity.getString())) {
            editConfig();
        } else {
            display.setCurrent(initForm);
        }

        stopPolling = false;
        pollThread = new Thread(this);
        pollThread.start();
    }
    
    public void pauseApp() {
        stopPolling = true;
        pollThread = null;
    }

    public void destroyApp(boolean unconditional) {
        stopPolling = true;
        pollThread = null;

        storeConfig();
        storeBuddies();
        peer = null;
    }

    private void reply() {
        if (replyBuddy == null) {
            showAlert("Reply", 
                      "No record of an incoming message or sender",
                      AlertType.ERROR, 
                      DEFAULT_ALERT_TIMEOUT,
                      initForm);
            return;
        }

        // switch the current buddy only in 1:1 chat
        if (!isGroupChat) {
            currentBuddy = replyBuddy;
            initForm.setTitle(currentBuddy);
            replyBuddy = null;
        }

        boolean isbuddy = false;
        int size = buddyList.size();
        for (int i=0; i < size; i++) {
            String buddy = buddyList.getString(i);
            if (buddy.equals(currentBuddy)) {
                isbuddy = true;
                break;
            }
        }

        // make it convenient to add buddies - automatically add any
        // buddy replied to to our buddy list
        if (!isbuddy) {
            buddyList.append(currentBuddy, null);
            storeBuddies();
        }

        sendForm();
    }

    private void sendForm() {
        if (currentBuddy == null) {
            showAlert("Send", 
                      "Please first select a buddy to chat with",
                      AlertType.ERROR, 
                      DEFAULT_ALERT_TIMEOUT,
                      initForm);
            return;
        }

        if (replyForm == null) {
            replyForm = new Form(currentBuddy);
            tfSentMsg = new TextField(null, 
                                      null, 
                                      4096,
                                      TextField.ANY);
            replyForm.append(tfSentMsg);
            Command cmdSend = new Command("Send", Command.SCREEN, 1);
            Command cmdBack = new Command("Back", Command.BACK, 2);
            replyForm.addCommand(cmdSend);
            replyForm.addCommand(cmdBack);
            replyForm.setCommandListener(this) ;
        }

        replyForm.setTitle(currentBuddy);
        display.setCurrent(replyForm);
    }

    private void editConfig() {
        if (configForm == null) {
            configForm = new Form("Configuration");
            configForm.append(tfRelayHost);
            configForm.append(tfRelayPort);
            configForm.append(tfIdentity);
            configForm.append(tfPollInterval);
            configForm.append(cgChatMode);

            Command cmdOK = new Command("OK", Command.OK, 1);
            Command cmdBack = new Command("Back", Command.BACK, 2);
            configForm.addCommand(cmdOK);
            configForm.addCommand(cmdBack);
            configForm.setCommandListener(this) ;
        }

        if (isGroupChat) {
            cgChatMode.setSelectedIndex(0, true);
        } else {
            cgChatMode.setSelectedIndex(1, true);
        }
        display.setCurrent(configForm);
    }

    private void readConfig() {
        String prop = null;

        prop = getAppProperty("RelayHost");

⌨️ 快捷键说明

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