001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package chatserver.client;
016:
017: import java.awt.*;
018: import java.awt.event.*;
019: import javax.swing.*;
020: import javax.swing.event.*;
021: import javax.swing.border.*;
022: import java.io.IOException;
023:
024: /**
025: * Login Dialog GUI
026: * @author Akshathkumar Shetty
027: */
028: public class LoginDialog extends JDialog {
029:
030: private JPanel topPanel;
031: private JPanel ipPanel;
032: private JPanel authPanel;
033: private JPanel buttonPanel;
034:
035: private JLabel productName;
036: private JLabel ipLabel;
037: private JTextField ipField;
038: private JLabel portLabel;
039: private JTextField portField;
040: private JLabel loginLabel;
041: private JTextField loginField;
042: private JLabel passwordLabel;
043: private JPasswordField passwordField;
044: private JButton loginButton;
045: private JButton cancelButton;
046:
047: private String statusTxt1 = "<html><font style=\"font-size:15pt;color:#535353\"><b>";
048: private String statusTxt2 = "</b></font>";
049: private GridBagConstraints gbc;
050:
051: //for storing the values
052: private String values[] = new String[4];
053: private boolean isOk = false;
054:
055: public LoginDialog(Frame parent, String args[]) {
056: super (parent, "Please Login");
057: gbc = new GridBagConstraints();
058: productName = new JLabel(
059: statusTxt1 + "Chat Login" + statusTxt2, JLabel.CENTER);
060:
061: ipLabel = new JLabel("IP Address");
062: if (args != null && args.length >= 2)
063: ipField = new JTextField(args[1]);
064: else
065: ipField = new JTextField("127.0.0.1");
066: portLabel = new JLabel("Port");
067: if (args != null && args.length >= 3)
068: ipField = new JTextField(args[2]);
069: else
070: portField = new JTextField("7412");
071:
072: loginLabel = new JLabel("Login");
073: loginField = new JTextField("user1");
074: passwordLabel = new JLabel("Password");
075: passwordField = new JPasswordField("user1");
076:
077: loginButton = new JButton("Login");
078: loginButton.setMnemonic('L');
079: cancelButton = new JButton("Cancel");
080: cancelButton.setMnemonic('C');
081: cancelButton.addActionListener(new ActionListener() {
082: public void actionPerformed(ActionEvent e) {
083: hide();
084: }
085: });
086:
087: //--- Action
088: ipField.addActionListener(new ActionListener() {
089: public void actionPerformed(ActionEvent e) {
090: portField.requestFocus();
091: }
092: });
093: portField.addActionListener(new ActionListener() {
094: public void actionPerformed(ActionEvent e) {
095: loginField.requestFocus();
096: }
097: });
098: loginField.addActionListener(new ActionListener() {
099: public void actionPerformed(ActionEvent e) {
100: passwordField.requestFocus();
101: }
102: });
103:
104: ActionListener loginAl = new ActionListener() {
105: public void actionPerformed(ActionEvent e) {
106: isOk = false;
107: if (ipField.getText().equals("")) {
108: showError("Blank IP Address");
109: return;
110: }
111: if (portField.getText().equals("")) {
112: showError("Blank Port Number");
113: return;
114: } else {
115: try {
116: Integer.parseInt(portField.getText());
117: } catch (Exception ex) {
118: showError("Bad Port Number.");
119: return;
120: }
121: }
122: if (loginField.getText().equals("")) {
123: showError("Blank Login");
124: return;
125: }
126: char p[] = passwordField.getPassword();
127: if (p == null || p.length == 0) {
128: showError("Blank password");
129: return;
130: }
131: p = null;
132: isOk = true;
133: hide();
134: }
135: };
136:
137: loginButton.addActionListener(loginAl);
138: passwordField.addActionListener(loginAl);
139:
140: cancelButton.addActionListener(new ActionListener() {
141: public void actionPerformed(ActionEvent e) {
142: isOk = false;
143: hide();
144: }
145: });
146: //---- Action
147:
148: Container cp = getContentPane();
149:
150: //--- Top Panel
151: topPanel = new JPanel();
152: topPanel.setLayout(new GridBagLayout());
153: gbc.insets = new Insets(2, 2, 2, 2);
154: gbc.weighty = 0.0;
155: gbc.weightx = 0.0;
156: gbc.gridx = 0;
157: gbc.gridy = 0;
158: gbc.gridheight = 1;
159: gbc.gridwidth = 1;
160: gbc.anchor = GridBagConstraints.CENTER;
161: gbc.fill = GridBagConstraints.NONE;
162: topPanel.add(productName, gbc);
163:
164: //-- IP Panel
165: ipPanel = new JPanel();
166: ipPanel.setLayout(new GridBagLayout());
167: gbc.gridx = 0;
168: gbc.gridy = 0;
169: gbc.weightx = 0.0;
170: gbc.weighty = 0.0;
171: gbc.gridheight = 1;
172: gbc.gridwidth = 1;
173: gbc.anchor = GridBagConstraints.WEST;
174: gbc.fill = GridBagConstraints.NONE;
175: ipPanel.add(ipLabel, gbc);
176:
177: gbc.gridx = 1;
178: gbc.gridy = 0;
179: gbc.weightx = 1.0;
180: gbc.fill = GridBagConstraints.BOTH;
181: ipPanel.add(ipField, gbc);
182:
183: gbc.gridx = 0;
184: gbc.gridy = 1;
185: gbc.weightx = 0.0;
186: gbc.fill = GridBagConstraints.NONE;
187: ipPanel.add(portLabel, gbc);
188:
189: gbc.gridx = 1;
190: gbc.gridy = 1;
191: gbc.weightx = 1.0;
192: gbc.fill = GridBagConstraints.BOTH;
193: ipPanel.add(portField, gbc);
194: ipPanel.setBorder(BorderFactory.createTitledBorder(
195: new EtchedBorder(), "Location"));
196:
197: //-- Login Panel
198: authPanel = new JPanel();
199: authPanel.setLayout(new GridBagLayout());
200: gbc.gridx = 0;
201: gbc.gridy = 0;
202: gbc.weightx = 0.0;
203: gbc.weighty = 0.0;
204: gbc.gridheight = 1;
205: gbc.gridwidth = 1;
206: gbc.anchor = GridBagConstraints.WEST;
207: gbc.fill = GridBagConstraints.NONE;
208: authPanel.add(loginLabel, gbc);
209:
210: gbc.gridx = 1;
211: gbc.gridy = 0;
212: gbc.weightx = 1.0;
213: gbc.fill = GridBagConstraints.BOTH;
214: authPanel.add(loginField, gbc);
215:
216: gbc.gridx = 0;
217: gbc.gridy = 1;
218: gbc.weightx = 0.0;
219: gbc.fill = GridBagConstraints.NONE;
220: authPanel.add(passwordLabel, gbc);
221:
222: gbc.gridx = 1;
223: gbc.gridy = 1;
224: gbc.weightx = 1.0;
225: gbc.fill = GridBagConstraints.BOTH;
226: authPanel.add(passwordField, gbc);
227: authPanel.setBorder(BorderFactory.createTitledBorder(
228: new EtchedBorder(), "Authentication"));
229:
230: //-- buttonPanel
231: buttonPanel = new JPanel();
232: buttonPanel.setLayout(new GridBagLayout());
233: gbc.gridx = 0;
234: gbc.gridy = 0;
235: gbc.weightx = 0.0;
236: gbc.weighty = 0.0;
237: gbc.gridheight = 1;
238: gbc.gridwidth = 1;
239: gbc.anchor = GridBagConstraints.CENTER;
240: gbc.fill = GridBagConstraints.NONE;
241: buttonPanel.add(loginButton, gbc);
242:
243: gbc.gridx = 1;
244: buttonPanel.add(cancelButton, gbc);
245:
246: cp.setLayout(new GridBagLayout());
247: gbc.gridx = 0;
248: gbc.gridy = 0;
249: gbc.weightx = 1.0;
250: gbc.weighty = 1.0;
251: gbc.gridheight = 1;
252: gbc.gridwidth = 1;
253: gbc.anchor = GridBagConstraints.CENTER;
254: gbc.fill = GridBagConstraints.HORIZONTAL;
255: cp.add(topPanel, gbc);
256: gbc.fill = GridBagConstraints.BOTH;
257: gbc.gridy = 1;
258: cp.add(ipPanel, gbc);
259: gbc.fill = GridBagConstraints.BOTH;
260: gbc.gridy = 2;
261: cp.add(authPanel, gbc);
262: gbc.fill = GridBagConstraints.HORIZONTAL;
263: gbc.gridy = 3;
264: cp.add(buttonPanel, gbc);
265: pack();
266: setSize(240, 250);
267: setResizable(false);
268: setModal(true);
269: centerWindow(this );
270: }
271:
272: private void showError(String msg) {
273: JOptionPane.showMessageDialog(LoginDialog.this , msg, "Error",
274: JOptionPane.ERROR_MESSAGE);
275: }
276:
277: public String[] getValues() {
278: values[0] = ipField.getText();
279: values[1] = portField.getText();
280: values[2] = loginField.getText();
281: values[3] = new String(passwordField.getPassword());
282: return values;
283: }
284:
285: public boolean isOk() {
286: return isOk;
287: }
288:
289: public void clearStatus() {
290: isOk = false;
291: }
292:
293: public static void centerWindow(Window window) {
294: Dimension dim = window.getToolkit().getScreenSize();
295: window.setLocation(dim.width / 2 - window.getWidth() / 2,
296: dim.height / 2 - window.getHeight() / 2);
297: }
298: }
|