📄 chatpanel.java
字号:
byte [] data = new byte[dataLen];
disSP.readFully (data);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
tbIdentity.setText(dis.readUTF());
/*
tbPollInterval.setText(dis.readUTF());
try {
pollInterval = Integer.parseInt(tbPollInterval.getText());
} catch (NumberFormatException ex) {
showAlert(Dialog.DIALOG_WARNING,
"Read Config",
"Error parsing poll interval: " +
tbPollInterval.getText());
}
*/
int stateLen = dis.readShort();
state = new byte[stateLen];
dis.readFully(state);
dis.close();
bais.close();
if (DEBUG) {
System.out.println("Read config: identity=" + tbIdentity.getText() +
" groupChat=" + isGroupChat +
" stateLen=" + stateLen);
}
} catch (Exception ex) {
if (DEBUG) {
System.out.println(ex);
}
} finally {
try {
if (disSP != null) {
disSP.close();
}
} catch (Exception ex) {
if (DEBUG) {
System.out.println(ex);
}
}
}
}
private void storeConfig() {
DataOutputStream dosSP = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeUTF(tbIdentity.getText());
//dos.writeUTF(tbPollInterval.getText());
//dos.writeBoolean(isGroupChat);
dos.writeShort(state.length);
dos.write(state);
dos.close();
byte [] data = baos.toByteArray();
dosSP = Connector.openDataOutputStream(CONFIG_RECORD);
dosSP.writeInt(data.length);
dosSP.write (data);
if (DEBUG) {
System.out.println("Saved config: identity=" + tbIdentity.getText() +
" groupChat=" + isGroupChat +
" stateLen=" + state.length);
}
} catch (Exception ex) {
if (DEBUG) {
System.out.println(ex);
}
} finally {
try {
if (dosSP != null) {
dosSP.close();
}
dos.close();
baos.close();
} catch (Exception ex) {
if (DEBUG) {
System.out.println(ex);
}
}
}
}
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.getText(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.getText(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.getText(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(tbBuddy);
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.getText(i);
editBuddyForm = new Form("Edit Buddy");
tbBuddy = new TextField("Edit Buddy: ",
buddy,
MAX_TEXTFIELD_SIZE,
TextField.ANY);
editBuddyForm.append(tbBuddy);
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 = tbSentMsg.getText();
Element[] elm = new Element[3];
elm[0] = new Element("JxtaTalkSenderName",
tbIdentity.getText().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);
try {
String chatBuddy = TALKNAME_PREFIX + currentBuddy;
String pipeId = (String) buddyIds.get(currentBuddy);
String pipeType = isGroupChat ? "JxtaPropagate" : "JxtaUnicast";
peer.send(chatBuddy, pipeId, pipeType, m);
} catch (IOException ex) {
showAlert(Dialog.DIALOG_ERROR,
"Send",
"Error sending message: " + ex.getMessage());
return false;
}
return true;
}
private void initiateConnect() {
storeConfig();
createMainPanel();
startApp();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -