rosterdialog.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 546 行 · 第 1/2 页

SVN-BASE
546
字号
        JPanel mainPanel = new JPanel() {            public Dimension getPreferredSize() {                final Dimension size = super.getPreferredSize();                size.width = 350;                return size;            }        };        mainPanel.setLayout(new BorderLayout());        mainPanel.add(titlePanel, BorderLayout.NORTH);        Object[] options = {                Res.getString("add"), Res.getString("cancel")        };        pane = new JOptionPane(panel, -1, 2, null, options, options[0]);        mainPanel.add(pane, BorderLayout.CENTER);        dialog = new JDialog(parent, Res.getString("title.add.contact"), false);        dialog.setContentPane(mainPanel);        dialog.pack();        dialog.setLocationRelativeTo(parent);        pane.addPropertyChangeListener(this);        dialog.setVisible(true);        dialog.toFront();        dialog.requestFocus();        jidField.requestFocus();    }    /**     * Display the RosterDialog using the MainWindow as the parent.     */    public void showRosterDialog() {        showRosterDialog(SparkManager.getMainWindow());    }    public void propertyChange(PropertyChangeEvent e) {        if (pane != null && pane.getValue() instanceof Integer) {            pane.removePropertyChangeListener(this);            dialog.dispose();            return;        }        String value = (String)pane.getValue();        String errorMessage = Res.getString("title.error");        if (Res.getString("cancel").equals(value)) {            dialog.setVisible(false);        }        else if (Res.getString("add").equals(value)) {            String jid = getJID();            String contact = UserManager.escapeJID(jid);            String nickname = nicknameField.getText();            String group = (String)groupBox.getSelectedItem();            Transport transport = null;            if (publicBox.isSelected()) {                AccountItem item = (AccountItem)accounts.getSelectedItem();                transport = item.getTransport();            }            if (transport == null) {                if (contact.indexOf("@") == -1) {                    contact = contact + "@" + SparkManager.getConnection().getServiceName();                }            }            else {                if (contact.indexOf("@") == -1) {                    contact = contact + "@" + transport.getServiceName();                }            }            if (!ModelUtil.hasLength(nickname) && ModelUtil.hasLength(contact)) {                // Try to load nickname from VCard                VCard vcard = new VCard();                try {                    vcard.load(SparkManager.getConnection(), contact);                    nickname = vcard.getNickName();                }                catch (XMPPException e1) {                    Log.error(e1);                }                // If no nickname, use first name.                if (!ModelUtil.hasLength(nickname)) {                    nickname = StringUtils.parseName(contact);                }                nicknameField.setText(nickname);            }            ContactGroup contactGroup = contactList.getContactGroup(group);            boolean isSharedGroup = contactGroup != null && contactGroup.isSharedGroup();            if (isSharedGroup) {                errorMessage = Res.getString("message.cannot.add.contact.to.shared.group");            }            else if (!ModelUtil.hasLength(contact)) {                errorMessage = Res.getString("message.specify.contact.jid");            }            else if (StringUtils.parseBareAddress(contact).indexOf("@") == -1) {                errorMessage = Res.getString("message.invalid.jid.error");            }            else if (!ModelUtil.hasLength(group)) {                errorMessage = Res.getString("message.specify.group");            }            else if (ModelUtil.hasLength(contact) && ModelUtil.hasLength(group) && !isSharedGroup) {                addEntry();                dialog.setVisible(false);                return;            }            JOptionPane.showMessageDialog(dialog, errorMessage, Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);            pane.setValue(JOptionPane.UNINITIALIZED_VALUE);        }    }    private void addEntry() {        Transport transport = null;        AccountItem item = null;        if (publicBox.isSelected()) {            item = (AccountItem)accounts.getSelectedItem();            transport = item.getTransport();        }        if (transport == null) {            String jid = getJID();            if (jid.indexOf("@") == -1) {                jid = jid + "@" + SparkManager.getConnection().getServiceName();            }            String nickname = nicknameField.getText();            String group = (String)groupBox.getSelectedItem();            jid = UserManager.escapeJID(jid);            // Add as a new entry            addRosterEntry(jid, nickname, group);        }        else {            String jid = getJID();            try {                jid = Gateway.getJID(transport.getServiceName(), jid);            }            catch (XMPPException e) {                Log.error(e);            }            String nickname = nicknameField.getText();            String group = (String)groupBox.getSelectedItem();            addRosterEntry(jid, nickname, group);        }    }    /**     * Returns the trimmed version of the JID.     *     * @return the trimmed version.     */    private String getJID() {        return jidField.getText().trim();    }    private void addRosterEntry(final String jid, final String nickname, final String group) {        final SwingWorker rosterEntryThread = new SwingWorker() {            public Object construct() {                return addEntry(jid, nickname, group);            }            public void finished() {                if (get() == null) {                    JOptionPane.showMessageDialog(dialog, Res.getString("label.unable.to.add.contact"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);                }            }        };        rosterEntryThread.start();    }    /**     * Adds a new entry to the users Roster.     *     * @param jid      the jid.     * @param nickname the nickname.     * @param group    the contact group.     * @return the new RosterEntry.     */    public RosterEntry addEntry(String jid, String nickname, String group) {        String[] groups = {group};        Roster roster = SparkManager.getConnection().getRoster();        RosterEntry userEntry = roster.getEntry(jid);        boolean isSubscribed = true;        if (userEntry != null) {            isSubscribed = userEntry.getGroups().size() == 0;        }        if (isSubscribed) {            try {                roster.createEntry(jid, nickname, new String[]{group});            }            catch (XMPPException e) {                Log.error("Unable to add new entry " + jid, e);            }            return roster.getEntry(jid);        }        try {            RosterGroup rosterGroup = roster.getGroup(group);            if (rosterGroup == null) {                rosterGroup = roster.createGroup(group);            }            if (userEntry == null) {                roster.createEntry(jid, nickname, groups);                userEntry = roster.getEntry(jid);            }            else {                userEntry.setName(nickname);                rosterGroup.addEntry(userEntry);            }            userEntry = roster.getEntry(jid);        }        catch (XMPPException ex) {            Log.error(ex);        }        return userEntry;    }    public List<AccountItem> getAccounts() {        List<AccountItem> list = new ArrayList<AccountItem>();        for (Transport transport : TransportUtils.getTransports()) {            if (TransportUtils.isRegistered(SparkManager.getConnection(), transport)) {                AccountItem item = new AccountItem(transport.getIcon(), transport.getName(), transport);                list.add(item);            }        }        return list;    }    class AccountItem extends JPanel {        private String name;        private Transport transport;        public AccountItem(Icon icon, String name, Transport transport) {            setLayout(new GridBagLayout());            this.name = name;            this.transport = transport;            JLabel iconLabel = new JLabel();            iconLabel.setIcon(icon);            JLabel label = new JLabel();            label.setText(name);            label.setFont(new Font("Dialog", Font.PLAIN, 11));            label.setHorizontalTextPosition(JLabel.CENTER);            add(iconLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));            add(label, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 0), 0, 0));            setBackground(Color.white);        }        public Transport getTransport() {            return transport;        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?