📄 addbuddydialog.java
字号:
return Standard.warningMessage(this, resources .getString("addBuddyDialogTitle"), resources .getString("newGroupError")); } return true; } /** * Called by OK button - checks information and adds the buddy */ private void okButtonHandler() { if (checkInformation()) { String buddyGroup = (String) buddyGroups.getSelectedItem(); if (buddyGroup.equals(resources.getString("newGroup"))) buddyGroup = newGroupBox.getText(); if (buddyGroup.equals(resources.getString("none"))) buddyGroup = null; addBuddy(buddyGroup, buddyAliasBox.getText(), buddyIDBox.getText()); } } /** * Handles all button events * * @author Adam Olsen * @version 1.0 */ class ActionHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() != cancelButton) okButtonHandler(); else cancelButtonHandler(); } } /** * Creates an input box with a corresponding label * * @param container * the container to add the input box to * @param grid * the GridBagLayout to use * @param label * the label to use * @param box * the input box to use */ private void createInputBox(Container container, GridBagLayout grid, String label, Container box) { JLabel labelBox = new JLabel(label + " "); c.gridy = row++; c.gridx = 0; c.anchor = GridBagConstraints.EAST; grid.setConstraints(labelBox, c); container.add(labelBox); c.gridx = 1; c.anchor = GridBagConstraints.WEST; grid.setConstraints(box, c); container.add(box); } /** * Gets the different available RosterGroups * * @return an array of strings representing the RosterGroups */ private String[] getRosterGroups() { Roster roster = ConnectorThread.getInstance().getRoster(); String rosterGroups[] = new String[roster.getGroupCount() + 2]; int i = 0; if ((!currentGroup.equals(""))) { rosterGroups[i] = currentGroup; i++; } rosterGroups[i++] = resources.getString("none"); rosterGroups[i++] = resources.getString("newGroup"); Iterator iterator = roster.getGroups(); while (iterator.hasNext()) { RosterGroup rosterGroup = (RosterGroup) iterator.next(); if ((currentGroup.equals("")) || (!rosterGroup.getName().equals(currentGroup))) { rosterGroups[i] = rosterGroup.getName(); i++; } } return rosterGroups; } /** * Runs the add buddy thread and adds or modifies a buddy in the Roster * * @param groupName * the group to put the buddy in * @param buddyAlias * the alias of the buddy * @param buddyId * the buddy's JID */ private void addBuddy(String groupName, String buddyAlias, String buddyId) { Roster buddyGroups = ConnectorThread.getInstance() .getRoster(); WaitDialog wait = new WaitDialog(this, null, resources .getString("pleaseWait")); wait.setVisible(true); setVisible(false); Thread thread = new Thread(new AddBuddyThread(wait, groupName, buddyAlias, buddyId, this)); thread.start(); } /** * Actually adds the buddy to the Roster * * @author Adam Olsen * @version 1.0 */ class AddBuddyThread implements Runnable { private String errorMessage; private String groupName; private String buddyAlias; private String buddyId; private AddBuddyDialog dialog; private WaitDialog wait; /** * Default constructor * * @param wait * the wait dialog * @param groupName * the group to use * @param buddyAlias * the buddy's alias * @param buddyId * the buddy's JID * @param dialog * the AddBuddyDialog that called this thread */ public AddBuddyThread(WaitDialog wait, String groupName, String buddyAlias, String buddyId, AddBuddyDialog dialog) { this.wait = wait; this.groupName = groupName; this.buddyAlias = buddyAlias; this.buddyId = buddyId.trim(); this.dialog = dialog; } /** * Called by the enclosing Thread - will attempt to add the buddy to the * Roster, and will display an error if it wasn't successfull */ public void run() { final Roster roster = ConnectorThread.getInstance() .getRoster(); final BuddyStatus buddy = BuddyList.getInstance().getBuddyStatus( buddyId); if (modify) { SwingUtilities.invokeLater(new Runnable() { public void run() { BuddyList.getInstance().getBuddyListTree().removeBuddy( buddy, buddy.getGroup(), true); } }); } try { if (modify) { RosterEntry entry = buddy.getRosterEntry(); entry.setName(buddyAlias); Iterator groups = entry.getGroups(); while (groups.hasNext()) { RosterGroup g = (RosterGroup) groups.next(); g.removeEntry(entry); } if (groupName != null) { RosterGroup newGroup = null; newGroup = roster.getGroup(groupName); if (newGroup == null) newGroup = roster.createGroup(groupName); newGroup.addEntry(entry); buddy.setTempGroup(groupName); } } /* if it's a new entry */ else { if (groupName == null) roster.createEntry(buddyId, buddyAlias, null); else { roster.createEntry(buddyId, buddyAlias, new String[] { groupName }); buddy.setTempGroup(groupName); } } } catch (XMPPException e) { if (e.getXMPPError() == null) errorMessage = e.getMessage(); else errorMessage = resources.getString("xmppError" + e.getXMPPError().getCode()); } SwingUtilities.invokeLater(new Runnable() { public void run() { wait.dispose(); if (errorMessage != null) { Standard .warningMessage(dialog, resources .getString("addBuddyDialogTitle"), errorMessage); dialog.setVisible(true); } else { try { Thread.sleep( 1000 ); } catch( InterruptedException exa ) { } buddy.setRemoved(false); NMOptionDialog.createMessageDialog(null, resources .getString("addBuddyDialogTitle"), resources .getString("buddyAdded")); BuddyList.getInstance().getBuddyListTree().addBuddy( buddy); DialogTracker.removeDialog(dialog); } } }); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -