📄 chatpanel.java
字号:
/************************************************************************
*
* $Id: ChatPanel.java,v 1.6 2002/07/29 18:02:45 kuldeep 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.doja.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 com.nttdocomo.util.Timer;
import com.nttdocomo.ui.IApplication;
import com.nttdocomo.ui.Component;
import com.nttdocomo.ui.Frame;
import com.nttdocomo.ui.Panel;
import com.nttdocomo.ui.ListBox;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Dialog;
import com.nttdocomo.ui.TextBox;
import com.nttdocomo.ui.Label;
import com.nttdocomo.ui.Canvas;
import com.nttdocomo.ui.Graphics;
import com.nttdocomo.ui.ShortTimer;
import com.nttdocomo.ui.SoftKeyListener;
import com.nttdocomo.ui.ComponentListener;
import javax.microedition.io.Connector;
import net.jxta.j2me.PeerNetwork;
import net.jxta.j2me.Message;
import net.jxta.j2me.Element;
public class ChatPanel extends Panel
implements SoftKeyListener, ComponentListener, Runnable {
private static final int DEFAULT_POLL_INTERVAL = 1;
private static final int DEFAULT_ALERT_TIMEOUT = 5000;
private static final int FOREVER = -1;
private static final int DEFAULT_SCROLL = 3;
private static final int MAX_TEXTFIELD_SIZE = 256;
private static final String CONFIG_RECORD = "scratchpad:///0;pos=0";
private static final String BUDDYLIST_RECORD = "scratchpad:///1;pos=0";
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 static final String[] CHAT_MODE_CHOICES = { "Group", "Private" };
//private Display display = null;
private Panel mainPanel = null;
private Panel replyForm = null;
private Panel configForm = null;
private Panel addBuddyForm = null;
private Panel editBuddyForm = null;
private Panel connectingForm = null;
private Panel confirmForm = null;
// private ImageCanvas imageCanvas = null;
private TextBox tbIdentity = null;
private TextBox tbBuddy = null;
private TextBox tbSentMsg = null;
private TextBox tbRcvdMsg = null;
private PeerNetwork peer = null;
private String currentBuddy = "IP2PGRP";
private String replyBuddy = null;
private String relayHost = null;
private Hashtable buddyIds = new Hashtable();
private ListBox 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;
private ListBox buddyList = null;
private Panel newPanel (String title) {
Panel panel = new Panel();
//panel.setTitle(title);
return panel;
}
public ChatPanel (String sourceURL){
// Under emulator, sourceURL is null. Use default value in that case
if (sourceURL != null) {
relayHost = sourceURL;
}
readConfig();
if (DEBUG) {
System.out.println ( "SourceURL: " + sourceURL);
}
setTitle ("Identity");
add(new Label("Name: " ));
add(tbIdentity);
setSoftLabel(Frame.SOFT_KEY_1, "Exit");
setSoftLabel(Frame.SOFT_KEY_2, "OK");
setComponentListener(this);
setSoftKeyListener(this);
}
public void softKeyPressed(int index) {}
public void softKeyReleased(int index) {
if (index == Frame.SOFT_KEY_1) {
// If the current displayed panel is either or ChatPanel
// or mainPanel -> SOFT_KEY_1 corresponds to "Exit"
if (Display.getCurrent() == mainPanel ||
Display.getCurrent() == this) {
Chat.app.terminate();
}
else {
// If the current displayed panel is neither nor ChatPanel
// or mainPanel -> SOFT_KEY_1 corresponds to "Back".
// So, display the mainPanel
Display.setCurrent(mainPanel);
}
}
if (index == Frame.SOFT_KEY_2) {
if (connected) {
sendPanel();
} else {
// Stores config, starts app and initiates connection
initiateConnect();
}
}
}
public void componentAction (Component c, int type, int param){
if (c == tbSentMsg){
if (send()) {
Display.setCurrent (mainPanel);
}
} else if (c == buddyList) {
String label = buddyList.getItem(buddyList.getSelectedIndex());
System.out.println ("Selected item is: " + label);
// TBD need to handle setting of buddy/buddygroup
} else if (c == tbIdentity){
// Stores config, starts app and initiates connection
initiateConnect();
}
}
private void createMainPanel() {
mainPanel = newPanel ("JXTA Chat");
buddyList = new ListBox (ListBox.CHOICE);
buddyList.append("IP2PGRP");
buddyList.append("PicShare");
buddyList.append("Groups");
tbRcvdMsg = new TextBox(null,
Display.getWidth(),
4,
TextBox.DISPLAY_ANY);
tbRcvdMsg.setEditable(false);
mainPanel.add(buddyList);
//add(new Button("Send"));
mainPanel.add(tbRcvdMsg);
mainPanel.setSoftLabel(Frame.SOFT_KEY_1, "Exit");
mainPanel.setSoftLabel(Frame.SOFT_KEY_2, "Send");
mainPanel.setComponentListener(this);
mainPanel.setSoftKeyListener(this);
}
private void startApp() {
stopPolling = false;
pollThread = new Thread(this);
pollThread.start();
}
private 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(Dialog.DIALOG_ERROR,
"Reply",
"No record of an incoming message or sender");
return;
}
// switch the current buddy only in 1:1 chat
if (!isGroupChat) {
currentBuddy = replyBuddy;
mainPanel.setTitle(currentBuddy);
replyBuddy = null;
}
boolean isbuddy = false;
int size = buddyList.getItemCount();
for (int i=0; i < size; i++) {
String buddy = buddyList.getItem(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);
storeBuddies();
}
sendPanel();
}
private void sendPanel() {
if (currentBuddy == null) {
showAlert(Dialog.DIALOG_ERROR,
"Send",
"Please first select a buddy to chat with");
return;
}
if (replyForm == null) {
replyForm = newPanel(currentBuddy);
tbSentMsg = new TextBox(null,
40,
5,
TextBox.DISPLAY_ANY);
tbSentMsg.setEditable(true);
replyForm.add(tbSentMsg);
replyForm.setSoftLabel(Frame.SOFT_KEY_1, "Back");
replyForm.setSoftLabel (Frame.SOFT_KEY_2, "Send");
replyForm.setComponentListener(this);
replyForm.setSoftKeyListener(this);
}
Display.setCurrent(replyForm);
}
private void readConfig() {
String prop = null;
//prop = getProperty("Identity");
tbIdentity = new TextBox(prop == null ? "" : prop,
20,
1,
TextBox.DISPLAY_ANY);
/*
//prop = getAppProperty("PollInterval");
tbPollInterval = new TextBox(prop == null ?
Integer.toString(pollInterval) : prop,
20,
1,
TextBox.NUMBER);
*/
DataInputStream disSP = null;
try {
disSP = Connector.openDataInputStream (CONFIG_RECORD);
int dataLen = disSP.readInt();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -