1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.example.chat.client;
20
21 import java.awt.BorderLayout;
22 import java.awt.Frame;
23 import java.awt.HeadlessException;
24 import java.awt.event.ActionEvent;
25
26 import javax.swing.AbstractAction;
27 import javax.swing.BoxLayout;
28 import javax.swing.JButton;
29 import javax.swing.JCheckBox;
30 import javax.swing.JDialog;
31 import javax.swing.JLabel;
32 import javax.swing.JPanel;
33 import javax.swing.JTextField;
34
35
36
37
38
39
40
41 public class ConnectDialog extends JDialog {
42 private static final long serialVersionUID = 2009384520250666216L;
43
44 private String serverAddress;
45
46 private String username;
47
48 private boolean useSsl;
49
50 private boolean cancelled = false;
51
52 public ConnectDialog(Frame owner) throws HeadlessException {
53 super(owner, "Connect", true);
54
55 serverAddress = "localhost:1234";
56 username = "user" + Math.round(Math.random() * 10);
57
58 final JTextField serverAddressField = new JTextField(serverAddress);
59 final JTextField usernameField = new JTextField(username);
60 final JCheckBox useSslCheckBox = new JCheckBox("Use SSL", false);
61
62 JPanel content = new JPanel();
63 content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
64 content.add(new JLabel("Server address"));
65 content.add(serverAddressField);
66 content.add(new JLabel("Username"));
67 content.add(usernameField);
68 content.add(useSslCheckBox);
69
70 JButton okButton = new JButton();
71 okButton.setAction(new AbstractAction("OK") {
72 private static final long serialVersionUID = -2292183622613960604L;
73
74 public void actionPerformed(ActionEvent e) {
75 serverAddress = serverAddressField.getText();
76 username = usernameField.getText();
77 useSsl = useSslCheckBox.isSelected();
78 ConnectDialog.this.dispose();
79 }
80 });
81
82 JButton cancelButton = new JButton();
83 cancelButton.setAction(new AbstractAction("Cancel") {
84 private static final long serialVersionUID = 6122393546173723305L;
85
86 public void actionPerformed(ActionEvent e) {
87 cancelled = true;
88 ConnectDialog.this.dispose();
89 }
90 });
91
92 JPanel buttons = new JPanel();
93 buttons.add(okButton);
94 buttons.add(cancelButton);
95
96 getContentPane().add(content, BorderLayout.CENTER);
97 getContentPane().add(buttons, BorderLayout.SOUTH);
98 }
99
100 public boolean isCancelled() {
101 return cancelled;
102 }
103
104 public String getServerAddress() {
105 return serverAddress;
106 }
107
108 public String getUsername() {
109 return username;
110 }
111
112 public boolean isUseSsl() {
113 return useSsl;
114 }
115 }