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 org.quickserver.net.qsadmin.gui;
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: import org.quickserver.util.TextFile;
025: import org.quickserver.swing.JFrameUtilities;
026:
027: /**
028: * Login Dialog
029: * QuickServer Admin GUI - QSAdminGUI
030: * @author Akshathkumar Shetty
031: */
032: public class LoginDialog extends JDialog /*JFrame*/{
033: private JPanel topPanel;
034: private JPanel ipPanel;
035: private JPanel authPanel;
036: private JPanel buttonPanel;
037:
038: private JLabel productName;
039: private JLabel ipLabel;
040: private JTextField ipField;
041: private JLabel portLabel;
042: private JTextField portField;
043: private JLabel loginLabel;
044: private JTextField loginField;
045: private JLabel passwordLabel;
046: private JPasswordField passwordField;
047: private JButton loginButton;
048: private JButton cancelButton;
049:
050: private String statusTxt1 = "<html><font style=\"font-size:15pt;color:#535353\"><b>";
051: private String statusTxt2 = "</b></font>";
052: private GridBagConstraints gbc;
053:
054: //for storing the values
055: private String values[] = new String[4];
056: private boolean isOk = false;
057:
058: public LoginDialog(Frame parent) {
059: super (parent, "QSAdmin Login");
060: gbc = new GridBagConstraints();
061: productName = new JLabel(statusTxt1 + "QSAdmin Login"
062: + statusTxt2, JLabel.CENTER);
063:
064: ipLabel = new JLabel("IP Address");
065: ipField = new JTextField("127.0.0.1");
066: portLabel = new JLabel("Port");
067: portField = new JTextField("9876");
068:
069: loginLabel = new JLabel("Login");
070: loginField = new JTextField("Admin");
071: passwordLabel = new JLabel("Password");
072: passwordField = new JPasswordField("QsAdm1n");
073:
074: loginButton = new JButton(
075: "<html><font style=\"font-size:10pt;color:#535353\">"
076: + "<b>Login</b>" + "</font>");
077: loginButton.setMnemonic('L');
078: cancelButton = new JButton(
079: "<html><font style=\"font-size:10pt;color:#535353\">"
080: + "<b>Cancel</b>" + "</font>");
081: cancelButton.setMnemonic('C');
082: cancelButton.addActionListener(new ActionListener() {
083: public void actionPerformed(ActionEvent e) {
084: hide();
085: }
086: });
087:
088: //--- Action
089: ipField.addActionListener(new ActionListener() {
090: public void actionPerformed(ActionEvent e) {
091: portField.requestFocus();
092: }
093: });
094: portField.addActionListener(new ActionListener() {
095: public void actionPerformed(ActionEvent e) {
096: loginField.requestFocus();
097: }
098: });
099: loginField.addActionListener(new ActionListener() {
100: public void actionPerformed(ActionEvent e) {
101: passwordField.requestFocus();
102: }
103: });
104:
105: ActionListener loginAl = new ActionListener() {
106: public void actionPerformed(ActionEvent e) {
107: isOk = false;
108: if (ipField.getText().equals("")) {
109: showError("Blank IP Address");
110: return;
111: }
112: if (portField.getText().equals("")) {
113: showError("Blank Port Number");
114: return;
115: } else {
116: try {
117: Integer.parseInt(portField.getText());
118: } catch (Exception ex) {
119: showError("Bad Port Number.");
120: return;
121: }
122: }
123: if (loginField.getText().equals("")) {
124: showError("Blank Login");
125: return;
126: }
127: char p[] = passwordField.getPassword();
128: if (p == null || p.length == 0) {
129: showError("Blank password");
130: return;
131: }
132: p = null;
133: hide();
134: isOk = true;
135: }
136: };
137:
138: loginButton.addActionListener(loginAl);
139: passwordField.addActionListener(loginAl);
140:
141: cancelButton.addActionListener(new ActionListener() {
142: public void actionPerformed(ActionEvent e) {
143: hide();
144: isOk = false;
145: }
146: });
147: //---- Action
148:
149: Container cp = getContentPane();
150:
151: //--- Top Panel
152: topPanel = new JPanel();
153: topPanel.setLayout(new GridBagLayout());
154: gbc.insets = new Insets(2, 2, 2, 2);
155: gbc.weighty = 0.0;
156: gbc.weightx = 0.0;
157: gbc.gridx = 0;
158: gbc.gridy = 0;
159: gbc.gridheight = 1;
160: gbc.gridwidth = 1;
161: gbc.anchor = GridBagConstraints.CENTER;
162: gbc.fill = GridBagConstraints.NONE;
163: topPanel.add(productName, gbc);
164:
165: //-- IP Panel
166: ipPanel = new JPanel();
167: ipPanel.setLayout(new GridBagLayout());
168: gbc.gridx = 0;
169: gbc.gridy = 0;
170: gbc.weightx = 0.0;
171: gbc.weighty = 0.0;
172: gbc.gridheight = 1;
173: gbc.gridwidth = 1;
174: gbc.anchor = GridBagConstraints.WEST;
175: gbc.fill = GridBagConstraints.NONE;
176: ipPanel.add(ipLabel, gbc);
177:
178: gbc.gridx = 1;
179: gbc.gridy = 0;
180: gbc.weightx = 1.0;
181: gbc.fill = GridBagConstraints.BOTH;
182: ipPanel.add(ipField, gbc);
183:
184: gbc.gridx = 0;
185: gbc.gridy = 1;
186: gbc.weightx = 0.0;
187: gbc.fill = GridBagConstraints.NONE;
188: ipPanel.add(portLabel, gbc);
189:
190: gbc.gridx = 1;
191: gbc.gridy = 1;
192: gbc.weightx = 1.0;
193: gbc.fill = GridBagConstraints.BOTH;
194: ipPanel.add(portField, gbc);
195: ipPanel.setBorder(BorderFactory.createTitledBorder(
196: new EtchedBorder(), "Location"));
197:
198: //-- Login Panel
199: authPanel = new JPanel();
200: authPanel.setLayout(new GridBagLayout());
201: gbc.gridx = 0;
202: gbc.gridy = 0;
203: gbc.weightx = 0.0;
204: gbc.weighty = 0.0;
205: gbc.gridheight = 1;
206: gbc.gridwidth = 1;
207: gbc.anchor = GridBagConstraints.WEST;
208: gbc.fill = GridBagConstraints.NONE;
209: authPanel.add(loginLabel, gbc);
210:
211: gbc.gridx = 1;
212: gbc.gridy = 0;
213: gbc.weightx = 1.0;
214: gbc.fill = GridBagConstraints.BOTH;
215: authPanel.add(loginField, gbc);
216:
217: gbc.gridx = 0;
218: gbc.gridy = 1;
219: gbc.weightx = 0.0;
220: gbc.fill = GridBagConstraints.NONE;
221: authPanel.add(passwordLabel, gbc);
222:
223: gbc.gridx = 1;
224: gbc.gridy = 1;
225: gbc.weightx = 1.0;
226: gbc.fill = GridBagConstraints.BOTH;
227: authPanel.add(passwordField, gbc);
228: authPanel.setBorder(BorderFactory.createTitledBorder(
229: new EtchedBorder(), "Authentication"));
230:
231: //-- buttonPanel
232: buttonPanel = new JPanel();
233: buttonPanel.setLayout(new GridBagLayout());
234: gbc.gridx = 0;
235: gbc.gridy = 0;
236: gbc.weightx = 0.0;
237: gbc.weighty = 0.0;
238: gbc.gridheight = 1;
239: gbc.gridwidth = 1;
240: gbc.anchor = GridBagConstraints.CENTER;
241: gbc.fill = GridBagConstraints.NONE;
242: buttonPanel.add(loginButton, gbc);
243:
244: gbc.gridx = 1;
245: buttonPanel.add(cancelButton, gbc);
246:
247: cp.setLayout(new GridBagLayout());
248: gbc.gridx = 0;
249: gbc.gridy = 0;
250: gbc.weightx = 1.0;
251: gbc.weighty = 1.0;
252: gbc.gridheight = 1;
253: gbc.gridwidth = 1;
254: gbc.anchor = GridBagConstraints.CENTER;
255: gbc.fill = GridBagConstraints.HORIZONTAL;
256: cp.add(topPanel, gbc);
257: gbc.fill = GridBagConstraints.BOTH;
258: gbc.gridy = 1;
259: cp.add(ipPanel, gbc);
260: gbc.fill = GridBagConstraints.BOTH;
261: gbc.gridy = 2;
262: cp.add(authPanel, gbc);
263: gbc.fill = GridBagConstraints.HORIZONTAL;
264: gbc.gridy = 3;
265: cp.add(buttonPanel, gbc);
266: pack();
267: setSize(240, 250);
268: setResizable(false);
269: setModal(true);
270: JFrameUtilities.centerWindow(this );
271: }
272:
273: private void showError(String msg) {
274: JOptionPane.showMessageDialog(LoginDialog.this , msg, "Error",
275: JOptionPane.ERROR_MESSAGE);
276: }
277:
278: public String[] getValues() {
279: values[0] = ipField.getText();
280: values[1] = portField.getText();
281: values[2] = loginField.getText();
282: values[3] = new String(passwordField.getPassword());
283: return values;
284: }
285:
286: public boolean isOk() {
287: return isOk;
288: }
289: }
|