001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing;
024:
025: import java.awt.BorderLayout;
026: import java.awt.Component;
027: import java.awt.Container;
028: import java.awt.Frame;
029: import java.awt.GridBagConstraints;
030: import java.awt.GridBagLayout;
031: import java.awt.Insets;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.awt.event.WindowEvent;
035:
036: import javax.swing.Box;
037: import javax.swing.JButton;
038: import javax.swing.JComponent;
039: import javax.swing.JDialog;
040: import javax.swing.JFrame;
041: import javax.swing.JLabel;
042: import javax.swing.JPanel;
043: import javax.swing.JPasswordField;
044: import javax.swing.JTextField;
045:
046: import org.isqlviewer.util.LocalMessages;
047:
048: /**
049: * @author Markus A. Kobold <mkobold at sprintpcs dot com>
050: */
051: public class LoginDialog extends JDialog implements ActionListener {
052:
053: private static final long serialVersionUID = -1870688453773372222L;
054:
055: private static final GridBagConstraints UI_CONSTRAINT = new GridBagConstraints(
056: 0, 0, 0, 0, 0, 0, 0, 0, new Insets(1, 1, 1, 1), 0, 0);
057:
058: private static final String BUNDLE_IDENTIFIER = "org.isqlviewer.swing.ResourceBundle";
059: private static final LocalMessages messages = new LocalMessages(
060: BUNDLE_IDENTIFIER);
061:
062: private JTextField principal = new JTextField("", 16);
063: private JPasswordField credentials = new JPasswordField("", 16);
064: private JButton okButton = new JButton(messages
065: .getMessage("logindialog.okbutton"));
066: private JButton cancelButton = new JButton(messages
067: .getMessage("logindialog.cancelbutton"));
068: private String[] tokens = null;
069:
070: public LoginDialog(Frame frameParent) {
071:
072: super (frameParent, messages.getMessage("logindialog.title"),
073: true);
074: initializeUI();
075: }
076:
077: public String[] getAuthTokens() {
078:
079: return tokens;
080: }
081:
082: public void actionPerformed(ActionEvent e) {
083:
084: Object src = e.getSource();
085: if (src.equals(credentials) || src.equals(okButton)) {
086: tokens = new String[2];
087: tokens[0] = principal.getText();
088: tokens[1] = new String(credentials.getPassword());
089: dispose();
090: } else if (src.equals(cancelButton)) {
091: tokens = null;
092: dispose();
093: } else if (src.equals(principal)) {
094: credentials.requestFocus();
095: }
096: }
097:
098: @Override
099: protected void processWindowEvent(WindowEvent e) {
100:
101: switch (e.getID()) {
102: case WindowEvent.WINDOW_OPENED:
103: principal.requestFocus();
104: principal.setText(System.getProperty("user.name"));
105: setLocationRelativeTo(getOwner());
106: break;
107: }
108: super .processWindowEvent(e);
109: }
110:
111: private void initializeUI() {
112:
113: Component component = null;
114: Object constraint = null;
115:
116: JPanel contentPanel = new JPanel(new GridBagLayout());
117:
118: component = new JLabel(messages
119: .getMessage("logindialog.username.title"));
120: ((JLabel) component).setLabelFor(principal);
121: constraint = constrain(0, 0, 1, 1, 0.0d, 0.0d,
122: GridBagConstraints.WEST, GridBagConstraints.NONE);
123: contentPanel.add(component, constraint);
124:
125: component = new JLabel(messages
126: .getMessage("logindialog.password.title"));
127: ((JLabel) component).setLabelFor(credentials);
128: constraint = constrain(0, 1, 1, 1, 0.0d, 0.0d,
129: GridBagConstraints.WEST, GridBagConstraints.NONE);
130: contentPanel.add(component, constraint);
131:
132: component = principal;
133: constraint = constrain(1, 0, 1, 1, 1.0d, 1.0d,
134: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
135: contentPanel.add(component, constraint);
136:
137: component = credentials;
138: constraint = constrain(1, 1, 1, 1, 1.0d, 1.0d,
139: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
140: contentPanel.add(component, constraint);
141:
142: component = Box.createVerticalStrut(10);
143: constraint = constrain(0, 2, 2, 1, 0.0d, 0.0d,
144: GridBagConstraints.WEST, GridBagConstraints.NONE);
145: contentPanel.add(component, constraint);
146:
147: JComponent commandPanel = new JPanel(new GridBagLayout());
148: JButton actionButton = null;
149:
150: constraint = constrain(0, 0, 1, 1, 1.0d, 0.0d,
151: GridBagConstraints.CENTER,
152: GridBagConstraints.HORIZONTAL);
153: commandPanel.add(Box.createHorizontalStrut(10), constraint);
154:
155: constraint = constrain(1, 0, 1, 1, 1.0d, 0.0d,
156: GridBagConstraints.CENTER,
157: GridBagConstraints.HORIZONTAL);
158: commandPanel.add(Box.createHorizontalGlue(), constraint);
159:
160: actionButton = okButton;
161: actionButton.addActionListener(this );
162: actionButton.setActionCommand(Integer.toString(0));
163: actionButton.setToolTipText(messages
164: .getMessage("logindialog.okbutton.tip"));
165: constraint = constrain(2, 0, 1, 1, 0.0d, 0.0d,
166: GridBagConstraints.CENTER, GridBagConstraints.NONE);
167: commandPanel.add(actionButton, constraint);
168:
169: actionButton = cancelButton;
170: actionButton.addActionListener(this );
171: actionButton.setActionCommand(Integer.toString(1));
172: actionButton.setToolTipText(messages
173: .getMessage("logindialog.cancelbutton.tip"));
174: constraint = constrain(3, 0, 1, 1, 0.0d, 0.0d,
175: GridBagConstraints.CENTER, GridBagConstraints.NONE);
176: commandPanel.add(actionButton, constraint);
177:
178: constraint = constrain(4, 0, 1, 1, 0.0d, 0.0d,
179: GridBagConstraints.CENTER,
180: GridBagConstraints.HORIZONTAL);
181: commandPanel.add(Box.createHorizontalStrut(10), constraint);
182:
183: constraint = constrain(0, 1, 5, 1, 0.0d, 0.0d,
184: GridBagConstraints.CENTER,
185: GridBagConstraints.HORIZONTAL);
186: commandPanel.add(Box.createVerticalStrut(10), constraint);
187:
188: getRootPane().setDefaultButton(okButton);
189: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
190:
191: Container container = new JPanel(new BorderLayout());
192: container.add(contentPanel, BorderLayout.CENTER);
193: container.add(commandPanel, BorderLayout.SOUTH);
194: container.add(Box.createVerticalStrut(10), BorderLayout.NORTH);
195: container.add(Box.createHorizontalStrut(10), BorderLayout.WEST);
196: container.add(Box.createHorizontalStrut(10), BorderLayout.EAST);
197:
198: setContentPane(container);
199: setResizable(false);
200: pack();
201: }
202:
203: private static GridBagConstraints constrain(int x, int y, int w,
204: int h, double wx, double wy, int a, int f) {
205:
206: UI_CONSTRAINT.gridx = x;
207: UI_CONSTRAINT.gridy = y;
208: UI_CONSTRAINT.gridwidth = w;
209: UI_CONSTRAINT.gridheight = h;
210: UI_CONSTRAINT.weightx = wx;
211: UI_CONSTRAINT.weighty = wy;
212: UI_CONSTRAINT.anchor = a;
213: UI_CONSTRAINT.fill = f;
214: return UI_CONSTRAINT;
215: }
216: }
|