/* * AddUserDialog.java * * Copyright 2004 Karl von Laudermann * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.volity.javolin.roster; import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.jivesoftware.smack.*; import org.jivesoftware.smack.util.StringUtils; import org.volity.javolin.BaseDialog; import org.volity.javolin.ErrorWrapper; import org.volity.javolin.JavolinApp; /** * The dialog for adding a user to the roster. */ public class AddUserDialog extends BaseDialog implements ActionListener { private final static String NODENAME = "AddUserDialog"; private JTextField mUserIdField; private JTextField mNicknameField; private JButton mCancelButton; private JButton mAddButton; private Roster mRoster; /** * Constructor. * * @param owner The Frame from which the dialog is displayed. * @param roster The roster to add a user to. * @param jid The JID we are adding. (May be null.) */ public AddUserDialog(Frame owner, Roster roster, String jid) { super(owner, "Add User", true, NODENAME); setTitle(JavolinApp.getAppName() + ": " + localize("WindowTitle")); mRoster = roster; // Set up dialog buildUI(); setResizable(false); pack(); if (jid != null) mUserIdField.setText(jid); // Restore saved window position mSizePosSaver.restoreSizeAndPosition(); } /** * ActionListener interface method implementation. * * @param e The action event to handle. */ public void actionPerformed(ActionEvent e) { if (e.getSource() == mAddButton) { doAdd(); } else if (e.getSource() == mCancelButton) { dispose(); } } /** * Handles the Add button. */ private void doAdd() { String jid = expandJIDField(mUserIdField); if (jid == null) { complainMustEnter(mUserIdField, localize("MustEnterJID")); return; } jid = StringUtils.parseBareAddress(jid); try { mRoster.createEntry(jid, mNicknameField.getText(), null); dispose(); } catch (XMPPException ex) { new ErrorWrapper(ex); JOptionPane.showMessageDialog(this, ex.toString(), JavolinApp.getAppName() + ": Error", JOptionPane.ERROR_MESSAGE); } } /** * Populates the dialog with controls. This method is called once, from the * constructor. */ private void buildUI() { getContentPane().setLayout(new GridBagLayout()); GridBagConstraints c; int gridY = 0; // Add user ID label JLabel someLabel = new JLabel(localize("LabelUserId")); c = new GridBagConstraints(); c.gridx = 0; c.gridy = gridY; c.insets = new Insets(MARGIN, MARGIN, 0, 0); c.anchor = GridBagConstraints.WEST; getContentPane().add(someLabel, c); // Add user ID field mUserIdField = new JTextField(20); c = new GridBagConstraints(); c.gridx = 1; c.gridy = gridY; c.insets = new Insets(MARGIN, SPACING, 0, MARGIN); getContentPane().add(mUserIdField, c); gridY++; // Add nickname label someLabel = new JLabel(localize("LabelNickname")); c = new GridBagConstraints(); c.gridx = 0; c.gridy = gridY; c.insets = new Insets(SPACING, MARGIN, 0, 0); c.anchor = GridBagConstraints.WEST; getContentPane().add(someLabel, c); // Add nickname field mNicknameField = new JTextField(20); c = new GridBagConstraints(); c.gridx = 1; c.gridy = gridY; c.insets = new Insets(SPACING, SPACING, 0, MARGIN); getContentPane().add(mNicknameField, c); gridY++; // Add panel with Cancel and Add buttons JPanel buttonPanel = new JPanel(new GridBagLayout()); c = new GridBagConstraints(); c.gridx = 0; c.gridy = gridY; c.gridwidth = GridBagConstraints.REMAINDER; c.insets = new Insets(GAP, MARGIN, MARGIN, MARGIN); c.anchor = GridBagConstraints.EAST; c.weightx = 0.5; getContentPane().add(buttonPanel, c); gridY++; // Add Cancel button mCancelButton = new JButton(localize("ButtonCancel")); mCancelButton.addActionListener(this); c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.insets = new Insets(0, 0, 0, 0); c.anchor = GridBagConstraints.EAST; c.weightx = 0.5; buttonPanel.add(mCancelButton, c); // Add Add button mAddButton = new JButton(localize("ButtonAdd")); mAddButton.addActionListener(this); c = new GridBagConstraints(); c.gridx = 1; c.gridy = 0; c.insets = new Insets(0, SPACING, 0, 0); c.anchor = GridBagConstraints.EAST; buttonPanel.add(mAddButton, c); // Make Add button default getRootPane().setDefaultButton(mAddButton); // Make the buttons the same width Dimension dim = mAddButton.getPreferredSize(); if (mCancelButton.getPreferredSize().width > dim.width) { dim = mCancelButton.getPreferredSize(); } mCancelButton.setPreferredSize(dim); mAddButton.setPreferredSize(dim); } }