001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas, Frithjof Kurtz |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.client.internal;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Container;
017: import java.awt.GridLayout;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import java.awt.event.FocusAdapter;
021: import java.awt.event.FocusEvent;
022:
023: import javax.swing.Action;
024: import javax.swing.BorderFactory;
025: import javax.swing.Box;
026: import javax.swing.BoxLayout;
027: import javax.swing.JButton;
028: import javax.swing.JComponent;
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.JPasswordField;
032: import javax.swing.JTextField;
033:
034: import org.rapla.components.layout.TableLayout;
035: import org.rapla.components.xmlbundle.I18nBundle;
036: import org.rapla.components.xmlbundle.LocaleChangeEvent;
037: import org.rapla.components.xmlbundle.LocaleChangeListener;
038: import org.rapla.components.xmlbundle.LocaleSelector;
039: import org.rapla.framework.RaplaContext;
040: import org.rapla.framework.RaplaException;
041: import org.rapla.gui.toolkit.RaplaFrame;
042:
043: public final class LoginDialog extends RaplaFrame implements
044: LocaleChangeListener {
045: private static final long serialVersionUID = -1887723833652617352L;
046:
047: JPanel jPanelFields = new JPanel();
048:
049: JPanel langSelection = new JPanel();
050: JLabel chooseLanguageLabel = new JLabel();
051:
052: JTextField username = new JTextField(15);
053: JPasswordField password = new JPasswordField(15);
054:
055: JLabel usernameLabel = new JLabel();
056: JLabel passwordLabel = new JLabel();
057: JButton loginBtn = new JButton();
058: JButton exitBtn = new JButton();
059: JPanel content = new JPanel();
060: I18nBundle i18n;
061: protected LocaleSelector localeSelector;
062:
063: public LoginDialog(RaplaContext sm) throws RaplaException {
064: super (sm);
065: i18n = (I18nBundle) sm.lookup(I18nBundle.ROLE
066: + "/org.rapla.RaplaResources");
067: localeSelector = (LocaleSelector) sm
068: .lookup(LocaleSelector.ROLE);
069: localeSelector.addLocaleChangeListener(this );
070: }
071:
072: public static LoginDialog create(RaplaContext sm,
073: JComponent infoPanel, JComponent languageSelector)
074: throws RaplaException {
075: LoginDialog dlg = new LoginDialog(sm);
076: dlg.init(infoPanel, languageSelector);
077: return dlg;
078: }
079:
080: public void setLoginAction(Action action) {
081: loginBtn.setAction(action);
082: }
083:
084: public void setExitAction(Action action) {
085: exitBtn.setAction(action);
086: }
087:
088: private void init(JComponent infoPanel, JComponent languageSelector) {
089: Container contentPane = getContentPane();
090: content
091: .setBorder(BorderFactory
092: .createEmptyBorder(0, 10, 0, 10));
093: content.setLayout(new BorderLayout());
094: content.add(jPanelFields, BorderLayout.SOUTH);
095: content.add(infoPanel, BorderLayout.CENTER);
096:
097: langSelection.setLayout(new BoxLayout(langSelection,
098: BoxLayout.X_AXIS));
099: langSelection.add(languageSelector);
100: langSelection.add(Box.createHorizontalGlue());
101:
102: double pre = TableLayout.PREFERRED;
103: double fill = TableLayout.FILL;
104: double[][] sizes = { { pre, 10, fill },
105: { pre, 5, pre, 5, pre, 5 } };
106: TableLayout tableLayout = new TableLayout(sizes);
107: jPanelFields.setLayout(tableLayout);
108: jPanelFields.add(chooseLanguageLabel, "0,0");
109: jPanelFields.add(usernameLabel, "0,2");
110: jPanelFields.add(passwordLabel, "0,4");
111: jPanelFields.add(langSelection, "2,0");
112: jPanelFields.add(username, "2,2");
113: jPanelFields.add(password, "2,4");
114:
115: username.setColumns(14);
116: password.setColumns(14);
117: Listener listener = new Listener();
118: password.addActionListener(listener);
119: languageSelector.addFocusListener(listener);
120:
121: contentPane.setLayout(new BorderLayout());
122: contentPane.add(content, BorderLayout.CENTER);
123: JPanel buttonPanel = new JPanel();
124: GridLayout gridLayout = new GridLayout(1, 2);
125: buttonPanel.setLayout(gridLayout);
126: buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10,
127: 10, 10));
128: gridLayout.setHgap(20);
129: contentPane.add(buttonPanel, BorderLayout.SOUTH);
130: buttonPanel.add(exitBtn);
131: buttonPanel.add(loginBtn);
132: this .getRootPane().setDefaultButton(loginBtn);
133: // setDefault(1);
134: setLocale();
135: username.requestFocus();
136: }
137:
138: public String getUsername() {
139: return username.getText();
140: }
141:
142: public char[] getPassword() {
143: return password.getPassword();
144: }
145:
146: /** overrides localeChanged from DialogUI*/
147: public void localeChanged(LocaleChangeEvent evt) {
148: setLocale();
149: }
150:
151: private I18nBundle getI18n() {
152: return i18n;
153: }
154:
155: private void setLocale() {
156: chooseLanguageLabel.setText(getI18n().getString(
157: "choose_language"));
158: exitBtn.setText(getI18n().getString("exit"));
159: loginBtn.setText(getI18n().getString("login"));
160: usernameLabel.setText(getI18n().getString("username") + ":");
161: passwordLabel.setText(getI18n().getString("password") + ":");
162: setTitle(getI18n().getString("logindialog.title"));
163: repaint();
164: }
165:
166: public void dispose() {
167: super .dispose();
168: localeSelector.removeLocaleChangeListener(this );
169: }
170:
171: public void testEnter(String newUsername, String newPassword) {
172: username.setText(newUsername);
173: password.setText(newPassword);
174: }
175:
176: class Listener extends FocusAdapter implements ActionListener {
177: boolean bInit = false;
178:
179: public void focusGained(FocusEvent e) {
180: if (!bInit) {
181: username.requestFocus();
182: bInit = true;
183: }
184: }
185:
186: public void actionPerformed(ActionEvent event) {
187: if (event.getSource() == password) {
188: loginBtn.doClick();
189: return;
190: } // end of if ()
191: }
192: }
193:
194: }
|