📄 addbuddydialog.java
字号:
/* Copyright (C) 2003 Adam Olsen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother;import java.awt.Container;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.Iterator;import java.util.Locale;import java.util.ResourceBundle;import javax.swing.BorderFactory;import javax.swing.BoxLayout;import javax.swing.DefaultComboBoxModel;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SwingUtilities;import org.jivesoftware.smack.Roster;import org.jivesoftware.smack.RosterEntry;import org.jivesoftware.smack.RosterGroup;import org.jivesoftware.smack.XMPPException;import com.valhalla.gui.DialogTracker;import com.valhalla.gui.MJTextField;import com.valhalla.gui.NMOptionDialog;import com.valhalla.gui.Standard;import com.valhalla.gui.WaitDialog;import com.valhalla.jbother.jabber.BuddyStatus;/** * Displays a dialog allowing to you add or modify a buddy. It displays their * JID, Alias, and group * * @author Adam Olsen * @version 1.0 */public class AddBuddyDialog extends JDialog { private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault()); private JComboBox buddyGroups; private MJTextField buddyIDBox = new MJTextField(20); private MJTextField buddyAliasBox = new MJTextField(20); private MJTextField newGroupBox; private JPanel container = new JPanel(); private RosterEntry entry; private String currentGroup = ""; //the buttons private JButton okButton = new JButton(resources.getString("okButton")); private JButton cancelButton = new JButton(resources .getString("cancelButton")); //this part is for laying out the rows for the dialog private int row = 0; private GridBagLayout grid = new GridBagLayout(); private GridBagConstraints c = new GridBagConstraints(); private boolean modify = false; /** * The add buddy constructor */ public AddBuddyDialog() { super(BuddyList.getInstance().getContainerFrame(), "Add/Modify Buddy", false); setTitle(resources.getString("addBuddyDialogTitle")); this.initComponents(); container.setBorder(BorderFactory.createEmptyBorder(5, 25, 5, 25)); container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); JLabel newBuddyLabel = new JLabel(resources .getString("addBuddyDialogTitle")); newBuddyLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); newBuddyLabel.setAlignmentX(Container.CENTER_ALIGNMENT); container.add(newBuddyLabel); JPanel newBuddyPanel = new JPanel(); newBuddyPanel.setLayout(grid); createInputBox(newBuddyPanel, grid, resources.getString("buddyId") + ":", buddyIDBox); createInputBox(newBuddyPanel, grid, resources.getString("alias") + ":", buddyAliasBox); createInputBox(newBuddyPanel, grid, resources.getString("buddyGroup") + ":", buddyGroups); createInputBox(newBuddyPanel, grid, resources.getString("newGroup") + ":", newGroupBox); container.add(newBuddyPanel); //add the buttons JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); buttonPanel.add(okButton); buttonPanel.add(cancelButton); container.add(buttonPanel); DialogTracker.addDialog(this, true, true); setResizable(false); pack(); setLocationRelativeTo(null); } /** * Destroys the dialog */ public void delete() { DialogTracker.removeDialog(this); } /** * The modify buddy constructor * * @param entry * the entry to modify */ public void setBuddy(RosterEntry entry) { this.entry = entry; this.buddyIDBox.setText(entry.getUser()); this.buddyIDBox.setEnabled(false); this.modify = true; this.buddyAliasBox.setText(entry.getName()); String currentGroup = ""; Iterator iterator = entry.getGroups(); while (iterator.hasNext()) { RosterGroup group = (RosterGroup) iterator.next(); currentGroup = group.getName(); } if (!currentGroup.equals("")) this.currentGroup = currentGroup; //buddyGroups = new JComboBox( getRosterGroups() ); buddyGroups.setModel(new DefaultComboBoxModel(getRosterGroups())); validate(); } /** * Called by the cancel button - destroys the dialog */ private void cancelButtonHandler() { DialogTracker.removeDialog(this); } /** * Sets the JID in the dialog * * @param id * the JID */ public void setBuddyId(String id) { buddyIDBox.setText(id); validate(); } /** * Sets up visual components */ private void initComponents() { setContentPane(container); buddyGroups = new JComboBox(getRosterGroups()); newGroupBox = new MJTextField(15); newGroupBox.setText(resources.getString("newGroup")); newGroupBox.setEnabled(false); //add the handlers cancelButton.addActionListener(new ActionHandler()); okButton.addActionListener(new ActionHandler()); newGroupBox.addActionListener(new ActionHandler()); buddyAliasBox.addActionListener(new ActionHandler()); buddyGroups.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { String item = (String) e.getItem(); if (item.equals(resources.getString("newGroup"))) { newGroupBox.setEnabled(true); newGroupBox.setText(""); newGroupBox.grabFocus(); } else { newGroupBox.setEnabled(false); if (newGroupBox.getText().equals("")) newGroupBox.setText(resources.getString("newGroup")); } } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { cancelButtonHandler(); } }); } /** * assures that all required information has been filled out in the dialog */ private boolean checkInformation() { if (buddyIDBox.getText().equals("")) return Standard.warningMessage(this, resources .getString("addBuddyDialogTitle"), resources .getString("noIdError")); if (buddyAliasBox.getText().equals("")) return Standard.warningMessage(this, resources .getString("addBuddyDialogTitle"), resources .getString("noAliasError")); if (buddyGroups.getSelectedItem().equals( resources.getString("newGroup"))) { if (newGroupBox.getText().equals("") || newGroupBox.getText().equals( resources.getString("newGroup")))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -