📄 logindialog.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas, Frithjof Kurtz |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.client.internal;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.rapla.components.layout.TableLayout;
import org.rapla.components.xmlbundle.I18nBundle;
import org.rapla.components.xmlbundle.LocaleChangeEvent;
import org.rapla.components.xmlbundle.LocaleChangeListener;
import org.rapla.components.xmlbundle.LocaleSelector;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.toolkit.RaplaFrame;
public final class LoginDialog extends RaplaFrame
implements
LocaleChangeListener
{
private static final long serialVersionUID = -1887723833652617352L;
JPanel jPanelFields = new JPanel();
JPanel langSelection = new JPanel();
JLabel chooseLanguageLabel = new JLabel();
JTextField username = new JTextField(15);
JPasswordField password = new JPasswordField(15);
JLabel usernameLabel = new JLabel();
JLabel passwordLabel = new JLabel();
JButton loginBtn = new JButton();
JButton exitBtn = new JButton();
JPanel content = new JPanel();
I18nBundle i18n;
protected LocaleSelector localeSelector;
public LoginDialog(RaplaContext sm) throws RaplaException {
super( sm) ;
i18n = (I18nBundle) sm.lookup(I18nBundle.ROLE + "/org.rapla.RaplaResources");
localeSelector = (LocaleSelector) sm.lookup(LocaleSelector.ROLE);
localeSelector.addLocaleChangeListener(this);
}
public static LoginDialog create(RaplaContext sm,JComponent infoPanel, JComponent languageSelector) throws RaplaException {
LoginDialog dlg = new LoginDialog(sm);
dlg.init(infoPanel,languageSelector);
return dlg;
}
public void setLoginAction(Action action) {
loginBtn.setAction( action );
}
public void setExitAction(Action action) {
exitBtn.setAction( action );
}
private void init(JComponent infoPanel, JComponent languageSelector)
{
Container contentPane = getContentPane();
content.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
content.setLayout( new BorderLayout());
content.add(jPanelFields,BorderLayout.SOUTH);
content.add(infoPanel,BorderLayout.CENTER);
langSelection.setLayout(new BoxLayout(langSelection,BoxLayout.X_AXIS));
langSelection.add(languageSelector);
langSelection.add(Box.createHorizontalGlue());
double pre =TableLayout.PREFERRED;
double fill =TableLayout.FILL;
double[][] sizes = {{pre,10, fill},
{pre,5,pre,5,pre,5}};
TableLayout tableLayout = new TableLayout(sizes);
jPanelFields.setLayout(tableLayout);
jPanelFields.add(chooseLanguageLabel,"0,0");
jPanelFields.add(usernameLabel,"0,2");
jPanelFields.add(passwordLabel,"0,4");
jPanelFields.add(langSelection,"2,0");
jPanelFields.add(username,"2,2");
jPanelFields.add(password,"2,4");
username.setColumns( 14 );
password.setColumns( 14 );
Listener listener = new Listener();
password.addActionListener(listener);
languageSelector.addFocusListener(listener);
contentPane.setLayout( new BorderLayout());
contentPane.add( content, BorderLayout.CENTER );
JPanel buttonPanel = new JPanel();
GridLayout gridLayout = new GridLayout(1,2);
buttonPanel.setLayout(gridLayout);
buttonPanel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10 ));
gridLayout.setHgap( 20);
contentPane.add( buttonPanel,BorderLayout.SOUTH );
buttonPanel.add( exitBtn );
buttonPanel.add( loginBtn );
this.getRootPane().setDefaultButton( loginBtn );
// setDefault(1);
setLocale();
username.requestFocus();
}
public String getUsername() {
return username.getText();
}
public char[] getPassword() {
return password.getPassword();
}
/** overrides localeChanged from DialogUI*/
public void localeChanged(LocaleChangeEvent evt) {
setLocale();
}
private I18nBundle getI18n() {
return i18n;
}
private void setLocale() {
chooseLanguageLabel.setText(getI18n().getString("choose_language"));
exitBtn.setText(getI18n().getString("exit"));
loginBtn.setText(getI18n().getString("login"));
usernameLabel.setText(getI18n().getString("username") + ":");
passwordLabel.setText(getI18n().getString("password") + ":");
setTitle(getI18n().getString("logindialog.title"));
repaint();
}
public void dispose() {
super.dispose();
localeSelector.removeLocaleChangeListener(this);
}
public void testEnter(String newUsername,String newPassword) {
username.setText(newUsername);
password.setText(newPassword);
}
class Listener extends FocusAdapter implements ActionListener {
boolean bInit = false;
public void focusGained(FocusEvent e) {
if (!bInit) {
username.requestFocus();
bInit = true;
}
}
public void actionPerformed(ActionEvent event) {
if ( event.getSource() == password) {
loginBtn.doClick();
return;
} // end of if ()
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -