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   *
41   */
42  public class ConnectDialog extends JDialog {
43      private static final long serialVersionUID = 2009384520250666216L;
44  
45      private String serverAddress;
46  
47      private String username;
48  
49      private boolean useSsl;
50  
51      private boolean cancelled = false;
52  
53      public ConnectDialog(Frame owner) throws HeadlessException {
54          super(owner, "Connect", true);
55  
56          serverAddress = "localhost:1234";
57          username = "user" + Math.round(Math.random() * 10);
58  
59          final JTextField serverAddressField = new JTextField(serverAddress);
60          final JTextField usernameField = new JTextField(username);
61          final JCheckBox useSslCheckBox = new JCheckBox("Use SSL", false);
62  
63          JPanel content = new JPanel();
64          content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
65          content.add(new JLabel("Server address"));
66          content.add(serverAddressField);
67          content.add(new JLabel("Username"));
68          content.add(usernameField);
69          content.add(useSslCheckBox);
70  
71          JButton okButton = new JButton();
72          okButton.setAction(new AbstractAction("OK") {
73              private static final long serialVersionUID = -2292183622613960604L;
74  
75              public void actionPerformed(ActionEvent e) {
76                  serverAddress = serverAddressField.getText();
77                  username = usernameField.getText();
78                  useSsl = useSslCheckBox.isSelected();
79                  ConnectDialog.this.dispose();
80              }
81          });
82  
83          JButton cancelButton = new JButton();
84          cancelButton.setAction(new AbstractAction("Cancel") {
85              private static final long serialVersionUID = 6122393546173723305L;
86  
87              public void actionPerformed(ActionEvent e) {
88                  cancelled = true;
89                  ConnectDialog.this.dispose();
90              }
91          });
92  
93          JPanel buttons = new JPanel();
94          buttons.add(okButton);
95          buttons.add(cancelButton);
96  
97          getContentPane().add(content, BorderLayout.CENTER);
98          getContentPane().add(buttons, BorderLayout.SOUTH);
99      }
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 }