Java tutorial
// The contents of this file are subject to the Mozilla Public License Version // 1.1 //(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.mozilla.org/MPL/ // //Software distributed under the License is distributed on an "AS IS" basis, //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License //for the specific language governing rights and //limitations under the License. // //The Original Code is "The Columba Project" // //The Initial Developers of the Original Code are Frederik Dietz and Timo // Stich. //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003. // //All Rights Reserved. package org.columba.chat.ui.dialog; import java.awt.BorderLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import org.columba.addressbook.util.AddressbookResourceLoader; import org.columba.chat.config.api.IAccount; import org.columba.core.gui.base.ButtonWithMnemonic; import org.columba.core.gui.base.SingleSideEtchedBorder; import com.jgoodies.forms.builder.DefaultFormBuilder; import com.jgoodies.forms.layout.FormLayout; /** * * @author fdietz */ public class AccountDialog extends JDialog implements ActionListener { private JTextField serverTextField; private JTextField idTextField; // private JPasswordField passwordTextField; private JTextField resourceTextField; private JSpinner portSpinner; private JCheckBox enableSSLCheckBox; private JButton okButton; private JButton registerButton; private IAccount account; /** * @throws java.awt.HeadlessException */ public AccountDialog(IAccount account) throws HeadlessException { super(new Frame(), true); this.account = account; initComponents(); layoutComponents(); updateComponents(true); pack(); setLocationRelativeTo(null); setVisible(true); } private void updateComponents(boolean b) { if (b) { idTextField.setText(account.getId()); serverTextField.setText(account.getHost()); // passwordTextField.setText(account.getPassword()); enableSSLCheckBox.setSelected(account.isEnableSSL()); resourceTextField.setText(account.getResource()); } else { account.setHost(getServer()); account.setEnableSSL(new Boolean(enableSSLCheckBox.isSelected()).booleanValue()); account.setResource(resourceTextField.getText()); account.setId(idTextField.getText()); } } /** * */ private void layoutComponents() { JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); getContentPane().add(panel); JPanel center = new JPanel(); center.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); panel.add(center, BorderLayout.CENTER); FormLayout layout = new FormLayout("right:default, 3dlu, default:grow", ""); DefaultFormBuilder b = new DefaultFormBuilder(layout, center); b.setRowGroupingEnabled(true); // b.addSeparator("Account Options"); b.append("&Server:", serverTextField); b.append("&User:", idTextField); // b.append("Pass&word:", passwordTextField); b.append("&Resource:", resourceTextField); b.append("&Port:", portSpinner); b.append(enableSSLCheckBox, 3); JPanel bottomPanel = new JPanel(new BorderLayout()); bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP)); JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 0)); buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); okButton = new ButtonWithMnemonic(AddressbookResourceLoader.getString("global", "ok")); okButton.setActionCommand("OK"); okButton.addActionListener(this); registerButton = new JButton("Register..."); registerButton.setActionCommand("REGISTER"); registerButton.addActionListener(this); buttonPanel.add(registerButton); buttonPanel.add(okButton); bottomPanel.add(buttonPanel, BorderLayout.EAST); panel.add(bottomPanel, BorderLayout.SOUTH); getRootPane().setDefaultButton(okButton); } /** * */ private void initComponents() { enableSSLCheckBox = new JCheckBox("Use Secure Connection (SSL)"); enableSSLCheckBox.setMnemonic('s'); serverTextField = new JTextField(10); idTextField = new JTextField(10); // passwordTextField = new JPasswordField(10); resourceTextField = new JTextField(10); portSpinner = new JSpinner(new SpinnerNumberModel(5222, 1, 65535, 1)); portSpinner.setEditor(new JSpinner.NumberEditor(portSpinner, "#####")); } public String getServer() { return serverTextField.getText(); } public String getUser() { return idTextField.getText(); } public String getResource() { return resourceTextField.getText(); } /* * public String getPassword() { return passwordTextField.getText(); } */ public boolean enableSSL() { return enableSSLCheckBox.isSelected(); } /** * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent arg0) { String action = arg0.getActionCommand(); if (action.equals("OK")) { updateComponents(false); setVisible(false); } } }