001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.mina.example.chat.client;
021
022import java.awt.BorderLayout;
023import java.awt.Frame;
024import java.awt.HeadlessException;
025import java.awt.event.ActionEvent;
026
027import javax.swing.AbstractAction;
028import javax.swing.BoxLayout;
029import javax.swing.JButton;
030import javax.swing.JCheckBox;
031import javax.swing.JDialog;
032import javax.swing.JLabel;
033import javax.swing.JPanel;
034import javax.swing.JTextField;
035
036/**
037 * TODO Add documentation
038 * 
039 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
040 *
041 */
042public class ConnectDialog extends JDialog {
043    private static final long serialVersionUID = 2009384520250666216L;
044
045    private String serverAddress;
046
047    private String username;
048
049    private boolean useSsl;
050
051    private boolean cancelled = false;
052
053    public ConnectDialog(Frame owner) throws HeadlessException {
054        super(owner, "Connect", true);
055
056        serverAddress = "localhost:1234";
057        username = "user" + Math.round(Math.random() * 10);
058
059        final JTextField serverAddressField = new JTextField(serverAddress);
060        final JTextField usernameField = new JTextField(username);
061        final JCheckBox useSslCheckBox = new JCheckBox("Use SSL", false);
062
063        JPanel content = new JPanel();
064        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
065        content.add(new JLabel("Server address"));
066        content.add(serverAddressField);
067        content.add(new JLabel("Username"));
068        content.add(usernameField);
069        content.add(useSslCheckBox);
070
071        JButton okButton = new JButton();
072        okButton.setAction(new AbstractAction("OK") {
073            private static final long serialVersionUID = -2292183622613960604L;
074
075            public void actionPerformed(ActionEvent e) {
076                serverAddress = serverAddressField.getText();
077                username = usernameField.getText();
078                useSsl = useSslCheckBox.isSelected();
079                ConnectDialog.this.dispose();
080            }
081        });
082
083        JButton cancelButton = new JButton();
084        cancelButton.setAction(new AbstractAction("Cancel") {
085            private static final long serialVersionUID = 6122393546173723305L;
086
087            public void actionPerformed(ActionEvent e) {
088                cancelled = true;
089                ConnectDialog.this.dispose();
090            }
091        });
092
093        JPanel buttons = new JPanel();
094        buttons.add(okButton);
095        buttons.add(cancelButton);
096
097        getContentPane().add(content, BorderLayout.CENTER);
098        getContentPane().add(buttons, BorderLayout.SOUTH);
099    }
100
101    public boolean isCancelled() {
102        return cancelled;
103    }
104
105    public String getServerAddress() {
106        return serverAddress;
107    }
108
109    public String getUsername() {
110        return username;
111    }
112
113    public boolean isUseSsl() {
114        return useSsl;
115    }
116}