/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Title: <p> * Description: <p> * Copyright: Copyright (c) <p> * Company: <p> * @author * @version 1.0 */ package org.pegadi.client; import org.apache.commons.io.IOUtils; import org.pegadi.server.Server; import org.pegadi.util.SpringUtilities; import org.slf4j.LoggerFactory; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import java.io.*; import java.net.URL; import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; import java.util.Set; public class LoginDialog extends JDialog { protected JLabel loginLabel = new JLabel(); protected JLabel userNameLabel = new JLabel(); protected JTextField userNameField = new JTextField(); protected JButton okButton = new JButton(); protected JButton quitButton = new JButton(); protected JLabel passwordLabel = new JLabel(); protected JPasswordField passwordField = new JPasswordField(); protected JLabel serverLabel = new JLabel(); protected JComboBox serverChooser = new JComboBox(); private String homePath; private String userNamesFile = ".pegadi.usernames"; ResourceBundle str; private Map servers; /** * @param servers The servers to set. */ public void setServers(Map servers) { this.servers = servers; } /** * @return Returns the server. */ public Server getServer() { return (Server) servers.get(serverChooser.getSelectedItem()); } /** * Empty constructor needed by spring ioc container */ public LoginDialog() { super(); setModal(true); } public LoginDialog(JFrame frame) { super(frame, true); } public void init() { str = ResourceBundle.getBundle("org.pegadi.client.CommonStrings"); ImageIcon icon = new ImageIcon(getClass().getResource("/images/pegadi.gif")); setIconImage(icon.getImage()); jbInit(); getPreviousLogin(); } public String getUserName() { return userNameField.getText(); } public String getPassword() { char[] pw = passwordField.getPassword(); if (pw == null || pw.length == 0) return ""; else return new String(pw); } private void jbInit() { Locale.setDefault(new Locale("no", "NO")); this.setTitle(str.getString("title")); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowOpened(WindowEvent e) { this_windowOpened(e); } }); loginLabel.setText(str.getString("login")); userNameLabel.setText(str.getString("username")); okButton.setText("OK"); okButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { okButton_actionPerformed(e); } }); quitButton.setText(str.getString("quit")); quitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { quitButton_actionPerformed(e); } }); userNameField.setColumns(10); userNameField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(KeyEvent e) { userNameField_keyReleased(e); } }); passwordLabel.setText(str.getString("password")); passwordField.setColumns(10); passwordField.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(KeyEvent e) { passwordField_keyReleased(e); } }); serverLabel.setText(str.getString("server")); Set keyset = servers.keySet(); for (Object aKeyset : keyset) { String serverKey = (String) aKeyset; serverChooser.addItem(serverKey); } serverChooser.setEnabled(false); // gui starts JPanel mainPanel = new JPanel(); this.getContentPane().add(mainPanel); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); JPanel labelPanel = new JPanel(); loginLabel.setFont(new Font(null, Font.PLAIN, 20)); // add icon URL iu = getClass().getResource("/images/pegadi_icon.png"); Image icon = Toolkit.getDefaultToolkit().getImage(iu); MediaTracker mt = new MediaTracker(this); mt.addImage(icon, 0); try { mt.waitForID(0); } catch (InterruptedException ie) { icon = null; } if (icon != null) { loginLabel.setIcon(new ImageIcon(icon)); loginLabel.setVerticalTextPosition(JLabel.BOTTOM); loginLabel.setHorizontalTextPosition(JLabel.CENTER); } labelPanel.add(loginLabel); labelPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); mainPanel.add(labelPanel); JPanel fieldPanel = new JPanel(new SpringLayout()); fieldPanel.add(userNameLabel); fieldPanel.add(userNameField); fieldPanel.add(passwordLabel); fieldPanel.add(passwordField); fieldPanel.add(serverLabel); fieldPanel.add(serverChooser); SpringUtilities.makeCompactGrid(fieldPanel, 3, 2, //rows, cols 5, 5, //initialX, initialY 10, 5);//xPad, yPad mainPanel.add(fieldPanel); JPanel buttonPanel = new JPanel(); JPanel buttonWrapperPanel = new JPanel(); buttonPanel.setLayout(new BorderLayout()); buttonWrapperPanel.add(okButton); buttonWrapperPanel.add(quitButton); buttonPanel.add(buttonWrapperPanel, BorderLayout.CENTER); buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); mainPanel.add(buttonPanel); } private void getPreviousLogin() { homePath = System.getProperty("user.home"); if(homePath == null) return; FileInputStream in = null; try { in = new FileInputStream(homePath + "/" + userNamesFile); String content = IOUtils.toString(in); userNameField.setText(content); userNameField.selectAll(); } catch (IOException e) { LoggerFactory.getLogger(getClass()).debug("[WARNING] Exception occurred while attempting to read usernamesfile"); } finally { try { if(in != null) in.close(); } catch (IOException e) { LoggerFactory.getLogger(getClass()).debug("[ERROR] Could not close filestream"); } } } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public void loginSuccessful() { if(homePath == null) return; String filePath = homePath + "/" + userNamesFile; FileOutputStream out = null; try { out = new FileOutputStream(filePath); IOUtils.write(getUserName(), out); out.close(); } catch (IOException e) { LoggerFactory.getLogger(getClass()).debug("[ERROR] Could not write username to {}", filePath); } finally { try { if(out != null) out.close(); } catch(IOException e) { LoggerFactory.getLogger(getClass()).debug("[ERROR] Could not close filestream"); } } } void okButton_actionPerformed(ActionEvent e) { dispose(); } void quitButton_actionPerformed(ActionEvent e) { System.exit(0); } void userNameField_keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { passwordField.requestFocus(); } } void passwordField_keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { okButton.doClick(); } } void this_windowOpened(WindowEvent e) { userNameField.requestFocus(); } public void deletePassword() { passwordField.setText(""); } }