View Javadoc

1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.mina.example.chat.client;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Frame;
24  import java.awt.HeadlessException;
25  import java.awt.event.ActionEvent;
26  
27  import javax.swing.AbstractAction;
28  import javax.swing.BoxLayout;
29  import javax.swing.JButton;
30  import javax.swing.JCheckBox;
31  import javax.swing.JDialog;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.JTextField;
35  
36  /**
37   * TODO Add documentation
38   * 
39   * @author The Apache MINA Project (dev@mina.apache.org)
40   * @version $Rev$, $Date$
41   *
42   */
43  public class ConnectDialog extends JDialog {
44      private static final long serialVersionUID = 2009384520250666216L;
45  
46      private String serverAddress;
47  
48      private String username;
49  
50      private boolean useSsl;
51  
52      private boolean cancelled = false;
53  
54      public ConnectDialog(Frame owner) throws HeadlessException {
55          super(owner, "Connect", true);
56  
57          serverAddress = "localhost:1234";
58          username = "user" + Math.round(Math.random() * 10);
59  
60          final JTextField serverAddressField = new JTextField(serverAddress);
61          final JTextField usernameField = new JTextField(username);
62          final JCheckBox useSslCheckBox = new JCheckBox("Use SSL", false);
63  
64          JPanel content = new JPanel();
65          content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
66          content.add(new JLabel("Server address"));
67          content.add(serverAddressField);
68          content.add(new JLabel("Username"));
69          content.add(usernameField);
70          content.add(useSslCheckBox);
71  
72          JButton okButton = new JButton();
73          okButton.setAction(new AbstractAction("OK") {
74              private static final long serialVersionUID = -2292183622613960604L;
75  
76              public void actionPerformed(ActionEvent e) {
77                  serverAddress = serverAddressField.getText();
78                  username = usernameField.getText();
79                  useSsl = useSslCheckBox.isSelected();
80                  ConnectDialog.this.dispose();
81              }
82          });
83  
84          JButton cancelButton = new JButton();
85          cancelButton.setAction(new AbstractAction("Cancel") {
86              private static final long serialVersionUID = 6122393546173723305L;
87  
88              public void actionPerformed(ActionEvent e) {
89                  cancelled = true;
90                  ConnectDialog.this.dispose();
91              }
92          });
93  
94          JPanel buttons = new JPanel();
95          buttons.add(okButton);
96          buttons.add(cancelButton);
97  
98          getContentPane().add(content, BorderLayout.CENTER);
99          getContentPane().add(buttons, BorderLayout.SOUTH);
100     }
101 
102     public boolean isCancelled() {
103         return cancelled;
104     }
105 
106     public String getServerAddress() {
107         return serverAddress;
108     }
109 
110     public String getUsername() {
111         return username;
112     }
113 
114     public boolean isUseSsl() {
115         return useSsl;
116     }
117 }