📄 bsformdialog.java
字号:
package edu.ou.kmi.buddyspace.gui;
/*
* BSFormDialog.java
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2003
*
*
* Created on 5 February 2003, 16:16
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*; // Property change stuff
import java.util.*;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
import edu.ou.kmi.buddyspace.gui.*;
import edu.ou.kmi.buddyspace.core.*;
import edu.ou.kmi.buddyspace.xml.*;
/**
* Dialog for jabber:x:data forms. Constructor takes the empty form and the result
* is stored in <code>resultXData</code> variable.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
*/
public class BSFormDialog extends JDialog {
protected JOptionPane optionPane;
protected TreeMap jidTreeMap = null;
protected Frame frame;
public XData resultXData = null;
/** Constructor */
public BSFormDialog(Frame _parent, XData _xData, String comment, BSRosterBean _rosterBean) {
super(_parent,
(_xData == null || _xData.getTitle() == null)? "Form" : _xData.getTitle(),
true);
frame = _parent;
final XData xData = _xData;
final BSRosterBean rosterBean = _rosterBean;
Vector fields = new Vector();
final Hashtable textFields = new Hashtable();
JLabel label;
JTextArea textArea;
JScrollPane scrollPane;
JTextField textField;
JComboBox comboBox;
JList list;
JCheckBox checkBox;
// comment
if (comment != null) {
label = new JLabel(comment);
fields.add(label);
label = new JLabel(" ");
fields.add(label);
}
// instructions
if (xData.getInstructions() != null) {
label = new JLabel("Instructions:");
fields.add(label);
textArea = new JTextArea(xData.getInstructions(), 5, 25);
textArea.setBackground(getBackground());
textArea.setForeground(getForeground());
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPane = new JScrollPane(textArea);
fields.add(scrollPane);
}
label = new JLabel(" ");
fields.add(label);
Enumeration fieldEnum = xData.fields();
while (fieldEnum.hasMoreElements()) {
XDataField f = (XDataField) fieldEnum.nextElement();
if (!"hidden".equals(f.getType())) {
if (!"boolean".equals(f.getType()) &&
!"fixed".equals(f.getType())) {
String name = f.getLabel();
if (name == null || name.equals(""))
name = f.getVar();
label = new JLabel(name);
fields.add(label);
}
if ("text-private".equals(f.getType())) {
textField = new JPasswordField();
Enumeration vals = f.values();
if (vals != null && vals.hasMoreElements())
textField.setText((String)vals.nextElement());
if (f.getDesc() != null)
textField.setToolTipText(f.getDesc());
fields.add(textField);
textFields.put(f.getVar(), textField);
}
else if ("text-multi".equals(f.getType())) {
textArea = new JTextArea(3, 25);
Enumeration vals = f.values();
boolean firstLine = true;
while (vals != null && vals.hasMoreElements()) {
textArea.append((firstLine? "" : "\n")
+ (String)vals.nextElement());
firstLine = false;
}
if (f.getDesc() != null)
textArea.setToolTipText(f.getDesc());
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPane = new JScrollPane(textArea);
fields.add(scrollPane);
textFields.put(f.getVar(), textArea);
}
else if ("list-single".equals(f.getType())) {
comboBox = new JComboBox();
Enumeration options = f.options();
Hashtable optionsHash = new Hashtable();
while (options != null && options.hasMoreElements()) {
XDataFieldOption xdfo = (XDataFieldOption)options.nextElement();
String valueStr = xdfo.getValue();
if (valueStr == null || valueStr.equals(""))
valueStr = xdfo.getData();
String labelStr = xdfo.getLabel();
if (labelStr == null || labelStr.equals(""))
labelStr = valueStr;
comboBox.addItem(labelStr);
optionsHash.put(valueStr, labelStr);
}
Enumeration vals = f.values();
if (vals != null && vals.hasMoreElements()) {
String selectedLabel = (String) optionsHash.get((String)vals.nextElement());
if (null != selectedLabel)
comboBox.setSelectedItem(selectedLabel);
}
if (f.getDesc() != null)
comboBox.setToolTipText(f.getDesc());
fields.add(comboBox);
textFields.put(f.getVar(), comboBox);
}
else if ("list-multi".equals(f.getType())) {
list = new JList();
DefaultListModel model = new DefaultListModel();
Enumeration options = f.options();
Hashtable optionsHash = new Hashtable();
while (options != null && options.hasMoreElements()) {
XDataFieldOption xdfo = (XDataFieldOption)options.nextElement();
String labelStr = xdfo.getLabel();
if (labelStr == null || labelStr.equals(""))
labelStr = xdfo.getValue();
model.addElement(labelStr);
optionsHash.put(xdfo.getValue(), labelStr);
}
list.setModel(model);
// sets selected values
Enumeration vals = f.values();
Vector indicesVec = new Vector();
while (vals != null && vals.hasMoreElements()) {
String selectedLabel = (String) optionsHash.get((String)vals.nextElement());
if (null != selectedLabel) {
int index = model.indexOf(selectedLabel);
if (-1 != index)
indicesVec.addElement(new Integer(index));
}
}
int[] indices = new int[indicesVec.size()];
for (int i=0; i < indicesVec.size(); i++)
indices[i] = ((Integer)indicesVec.elementAt(i)).intValue();
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setSelectedIndices(indices);
list.setVisibleRowCount(3);
// sets description
if (f.getDesc() != null)
list.setToolTipText(f.getDesc());
scrollPane = new JScrollPane(list);
fields.add(scrollPane);
textFields.put(f.getVar(), list);
}
else if ("boolean".equals(f.getType())) {
String labelStr = f.getLabel();
if (labelStr == null || labelStr.equals(""))
labelStr = f.getVar();
checkBox = new JCheckBox(labelStr);
Enumeration vals = f.values();
if (vals != null && vals.hasMoreElements())
checkBox.setSelected("1".equals((String)vals.nextElement()));
if (f.getDesc() != null)
checkBox.setToolTipText(f.getDesc());
fields.add(checkBox);
textFields.put(f.getVar(), checkBox);
}
else if ("fixed".equals(f.getType())) {
textArea = new JTextArea(3, 25);
Enumeration vals = f.values();
if (vals != null && vals.hasMoreElements())
textArea.setText((String)vals.nextElement());
if (f.getDesc() != null)
textArea.setToolTipText(f.getDesc());
textArea.setBackground(getBackground());
textArea.setForeground(getForeground());
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
scrollPane = new JScrollPane(textArea);
fields.add(scrollPane);
}
else if ("jid-single".equals(f.getType())) {
textField = new JTextField();
Enumeration vals = f.values();
if (vals != null && vals.hasMoreElements())
textField.setText((String)vals.nextElement());
if (f.getDesc() != null)
textField.setToolTipText(f.getDesc());
JButton button = new JButton("From roster");
final JTextField tf = textField;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
BSChooseJIDDialog dlg = new BSChooseJIDDialog(frame,
"Choose JID", rosterBean.entries());
dlg.show();
if (dlg.jid != null)
tf.setText(dlg.jid.toString());
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(textField, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
fields.add(panel);
textFields.put(f.getVar(), textField);
}
else if ("jid-multi".equals(f.getType())) {
addJIDMulti(f, textFields, fields, rosterBean);
}
// else takes it as text-single
else { //if ("text-single".equals(f.getType())) {
textField = new JTextField();
Enumeration vals = f.values();
if (vals != null && vals.hasMoreElements())
textField.setText((String)vals.nextElement());
if (f.getDesc() != null)
textField.setToolTipText(f.getDesc());
fields.add(textField);
textFields.put(f.getVar(), textField);
}
label = new JLabel(" ");
fields.add(label);
}
}
final String okButton = "OK";
final String cancelButton = "Cancel";
Object[] options = {okButton, cancelButton};
optionPane = new JOptionPane(fields.toArray(),
JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
options,
options[0]);
setContentPane(optionPane);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
pack();
setLocationRelativeTo(frame);
// handles actions
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -