📄 bsaddcontactdialog.java
字号:
package edu.ou.kmi.buddyspace.gui;
/*
* BSAddContactDialog.java
*
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2002
*
*
* Created on 31 July 2002, 13:57
*/
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.beans.*; // Property change stuff
import org.jabber.jabberbeans.util.JID;
import edu.ou.kmi.buddyspace.core.*;
/**
* Dialog for adding and changing contacts.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
*/
public class BSAddContactDialog extends javax.swing.JDialog {
private BSRosterBean rosterBean = null;
private BSPresenceBean presenceBean = null;
private String oldGroup = null;
private JOptionPane optionPane;
public String group = null;
public JID jid = null;
public String nick = null;
private Frame parent;
private JTextField jidTextField;
private JTextField nickTextField;
private JComboBox groupComboBox;
/** Creates new form BSAddContactDialog for adding contact */
public BSAddContactDialog(Frame parent,
BSRosterBean rosterBean, BSPresenceBean presenceBean) {
super(parent, "Add contact", true);
init(parent, false, rosterBean, presenceBean);
}
/** Creates new form BSAddContactDialog for changing contact */
public BSAddContactDialog(Frame parent,
BSRosterBean rosterBean, BSPresenceBean presenceBean,
JID jid, String nick, String group) {
super(parent, "Change contact", true);
init(parent, true, rosterBean, presenceBean);
setJID(jid);
setNick(nick);
setGroup(group);
}
/** Inits components and values common for adding and changing contact */
private void init(Frame _parent, boolean _changeContact,
BSRosterBean rosterBean, BSPresenceBean presenceBean) {
parent = _parent;
final boolean changeContact = _changeContact;
this.rosterBean = rosterBean;
this.presenceBean = presenceBean;
jidTextField = new JTextField();
nickTextField = new JTextField();
groupComboBox = new JComboBox();
if (changeContact)
jidTextField.setEditable(false);
// fills groups hashtable
Iterator groups = rosterBean.getGroups();
TreeSet tmpSet = new TreeSet();
while(groups.hasNext()) {
String groupName = (String) groups.next();
//groupComboBox.addItem(groupName);
tmpSet.add(groupName);
}
// fills sorted groups combo box
while (!tmpSet.isEmpty()) {
Object o = tmpSet.first();
groupComboBox.addItem(o);
tmpSet.remove(o);
}
groupComboBox.setEditable(true);
Object[] fields = {"Jabber ID (JID): ", jidTextField,
"Nickname: ", nickTextField,
"Group: (select an existing one, or enter a new one)", groupComboBox};
final String okButton = "OK";
final String cancelButton = "Cancel";
Object[] options = {okButton, cancelButton};
optionPane = new JOptionPane(fields,
JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
setContentPane(optionPane);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
pack();
setLocationRelativeTo(parent);
// action listening
optionPane.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (isVisible()
&& (e.getSource() == optionPane)
&& (prop.equals(JOptionPane.VALUE_PROPERTY) ||
prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
Object value = optionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_VALUE) {
// ignore reset
return;
}
// Reset the JOptionPane's value.
// If you don't do this, then if the user
// presses the same button next time, no
// property change event will be fired.
optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
if (value.equals(okButton)) {
if (changeContact)
changeContact();
else
addContact();
return;
}
else if (value.equals(cancelButton)) {
jid = null;
setVisible(false);
}
}
}
});
}
/** Sets JID */
public void setJID(JID jid) {
if (jid != null)
jidTextField.setText(jid.toString());
}
/** Sets selected group */
public void setGroup(String group) {
groupComboBox.setSelectedItem(group);
oldGroup = group;
}
/** Sets nickname */
public void setNick(String nick) {
nickTextField.setText(nick);
}
/**
* Adds contact into the roster.
* Uses call to given <code>BSRosterBean</code>.
*/
private void addContact() {
JID jid = JID.fromString(jidTextField.getText());
if (jid == null) {
JOptionPane.showMessageDialog(this, "Invalid JID",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
String nick = nickTextField.getText();
if (nick == null || nick.equals("")) {
JOptionPane.showMessageDialog(this, "Nickname cannot be empty",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
// if there is the same jid with different nick already in roster
String oldNick = rosterBean.getFriendlyName(jid);
if (oldNick != null && !nick.equals(oldNick)) {
/*Object[] options = {"Yes, change the nick",
"No, add new contact with old nick"};
int result = JOptionPane.showOptionDialog(
this,
"Contact with the JID already exists under nick \""
+ oldNick
+ "\". Do you want to change the nick?\n",
"Change nick?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, options, options[1]);
if (result == 1) {
nick = oldNick;
}*/
Object[] options = {"Use new nickname",
"Keep old nickname",
"Cancel"};
int result = JOptionPane.showOptionDialog(
this,
"Possible duplicate contact for JID \"" +
jid.toString() + "\".\n" +
"It already exists in your roster under nickname \"" +
oldNick + "\".\n Do you want to use the new nickname \"" +
nick + "\" everywhere\n or add the contact with the old nickname?\n",
"Change nickname?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, options, options[2]);
if (result == 1)
nick = oldNick;
else if (result == 2)
return;
}
Object o = groupComboBox.getSelectedItem();
String group = (o != null)? o.toString() : "";
// finally adds the contact into roster
if (rosterBean != null) {
if (!rosterBean.addContact(jid, nick, group)) {
JOptionPane.showMessageDialog(this, "Contact could not be added",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
// and sends request for subscription
if (presenceBean != null)
presenceBean.sendSubscriptionRequest(jid);
}
setVisible(false);
}
/**
* Changes contact in the roster.
* Uses call to given <code>BSRosterBean</code>.
*/
private void changeContact() {
JID jid = JID.fromString(jidTextField.getText());
String nick = nickTextField.getText();
if (nick == null || nick.equals("")) {
JOptionPane.showMessageDialog(this, "Nickname cannot be empty",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
Object o = groupComboBox.getSelectedItem();
String group = (o != null)? o.toString() : "";
if (rosterBean != null) {
if (BSMainFrame.NO_GROUP_NAME.equals(oldGroup))
oldGroup = null;
if (group == null || group.equals("") ||
BSMainFrame.NO_GROUP_NAME.equals(group))
group = null;
if (!rosterBean.changeContact(jid, nick, group, oldGroup)) {
JOptionPane.showMessageDialog(this, "Contact could not be added",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
}
setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -