001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2013-01-16 23:42:20 +0000 (Wed, 16 Jan 2013) $' 
007 * '$Revision: 31342 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.authentication.gui;
031
032import java.awt.BorderLayout;
033import java.awt.FlowLayout;
034import java.awt.event.ActionEvent;
035import java.awt.event.ActionListener;
036import java.awt.event.WindowAdapter;
037import java.awt.event.WindowEvent;
038import java.io.IOException;
039import java.util.Vector;
040
041import javax.swing.BoxLayout;
042import javax.swing.JButton;
043import javax.swing.JComboBox;
044import javax.swing.JFrame;
045import javax.swing.JLabel;
046import javax.swing.JOptionPane;
047import javax.swing.JPanel;
048import javax.swing.JPasswordField;
049import javax.swing.JTextField;
050import javax.swing.UIManager;
051
052import org.kepler.authentication.DomainList;
053import org.kepler.authentication.ProxyRepository;
054
055/**
056 * LoginGUI pops up the login dialog to let user login, retrieves credential
057 * from GAMA server, and stores the credential into ProxyRepository.
058 * 
059 * @author Zhijie Guan guan@sdsc.edu
060 * 
061 */
062
063public class GAMALoginGUI extends JPanel implements ActionListener {
064        private static ProxyRepository proxyRepository; // the proxyrepository got
065                                                                                                        // from
066                                                                                                        // AuthenticationManager
067        private static JFrame controllingFrame; // The Frame of login dialog
068
069        // Command button constants
070        private String OK = "ok";
071        private String RESET = "reset";
072
073        // Input fields
074        private JTextField userNameField;
075        private JPasswordField passwordField;
076        private JComboBox domainField;
077        private static Vector domainList;
078
079        private static String username = null;
080        private static String password = null;
081        
082        // XXX magic string, get rid of this
083        public static String USERNAME_BREAK = "BREAK";
084
085        /**
086         * The constructor is used to build all the display components
087         * 
088         */
089        public GAMALoginGUI() {
090                // Create everything.
091                // User Name field, label, and pane
092                try {
093                        domainList = DomainList.getInstance().getDomainList();
094                } catch (IOException ioe) {
095                        JOptionPane.showMessageDialog(this, "There was an error "
096                                        + "reading the domain list: " + ioe.getMessage());
097                }
098                userNameField = new JTextField(20);
099                JLabel userNameLabel = new JLabel("Enter your user name: ");
100                userNameLabel.setLabelFor(userNameField);
101                JPanel userNamePane = new JPanel(new FlowLayout());
102                userNamePane.add(userNameLabel);
103                userNamePane.add(userNameField);
104
105                // Password field, label, and pane
106                passwordField = new JPasswordField(20);
107                passwordField.setEchoChar('#');
108                passwordField.setActionCommand(OK);
109                passwordField.addActionListener(this);
110                JLabel passwordLabel = new JLabel("Enter the password: ");
111                passwordLabel.setLabelFor(passwordField);
112                JPanel passwordPane = new JPanel(new FlowLayout());
113                passwordPane.add(passwordLabel);
114                passwordPane.add(passwordField);
115
116                // Domain field, label, and pane
117
118                domainField = new JComboBox(domainList);
119                domainField.setSelectedIndex(0);
120                ComboBoxRenderer renderer = new ComboBoxRenderer();
121                domainField.setRenderer(renderer);
122                JLabel domainLabel = new JLabel("Select the domain/subdomain: ");
123                domainLabel.setLabelFor(domainField);
124                JPanel domainPane = new JPanel(new FlowLayout());
125                domainPane.add(domainLabel);
126                domainPane.add(domainField);
127
128                // Lay out input fields and labels on inputPane
129                JPanel inputPane = new JPanel();
130                inputPane.setLayout(new BoxLayout(inputPane, BoxLayout.Y_AXIS));
131                inputPane.add(userNamePane);
132                inputPane.add(passwordPane);
133                inputPane.add(domainPane);
134
135                // Buttons and their buttonPane
136                JButton okButton = new JButton("OK");
137                JButton resetButton = new JButton("Cancel");
138                okButton.setActionCommand(OK);
139                okButton.addActionListener(this);
140                resetButton.setActionCommand(RESET);
141                resetButton.addActionListener(this);
142                JPanel buttonPane = new JPanel(new FlowLayout());
143                buttonPane.add(okButton);
144                buttonPane.add(resetButton);
145
146                // Pane for the whole window
147                setLayout(new BorderLayout());
148                add(inputPane, BorderLayout.CENTER);
149                add(buttonPane, BorderLayout.PAGE_END);
150        }
151
152        /**
153         * Event listener
154         */
155        public void actionPerformed(ActionEvent e) {
156                String cmd = e.getActionCommand();
157
158                if (OK.equals(cmd)) { // User login and click OK button
159                        char[] passwd = passwordField.getPassword();
160                        password = new String(passwd);
161
162                        // Zero out the possible password, for security.
163                        for (int i = 0; i < passwd.length; i++) {
164                                passwd[i] = 0;
165                        }
166
167                        username = userNameField.getText();
168                } else if (RESET.equals(cmd)) { // CANCEL button is clicked.
169                        controllingFrame.dispose();
170                        username = USERNAME_BREAK;
171                }
172        }
173
174        // Method to reset the focus of the login dialog
175        // Must be called from the event-dispatching thread.
176        protected void resetFocus() {
177                userNameField.requestFocusInWindow();
178        }
179
180        /**
181         * Create the GUI and show it. For thread safety, this method should be
182         * invoked from the event-dispatching thread.
183         */
184        private static void createAndShowGUI() {
185                // Make sure we have nice window decorations.
186                try {
187                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
188                } catch (Exception e) {
189                        e.printStackTrace();
190                }
191
192                // Create and set up the window.
193                controllingFrame = new JFrame("GAMA Login");
194                //controllingFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
195                controllingFrame.addWindowListener(new WindowAdapter(){
196      public void windowClosing(WindowEvent e) {
197        username = USERNAME_BREAK;
198        controllingFrame.dispose();
199      }
200    }
201    );
202
203                // Create and set up the content pane.
204                final GAMALoginGUI contentPane = new GAMALoginGUI();
205                contentPane.setOpaque(true); // content panes must be opaque
206                controllingFrame.setContentPane(contentPane);
207
208                // Make sure the focus goes to the right component
209                // whenever the frame is initially given the focus.
210                controllingFrame.addWindowListener(new WindowAdapter() {
211                        public void windowActivated(WindowEvent e) {
212                                contentPane.resetFocus();
213                        }
214                });
215
216                // Display the window.
217                controllingFrame.pack();
218                controllingFrame.setLocationRelativeTo(null); // stay in the center
219                controllingFrame.setVisible(true);
220        }
221
222        /**
223         * Function to show the login dialog It adds the login dialog into the
224         * event-dispatching thread
225         * 
226         * @param p
227         *            The ProxyRepository got from the AuthenticationManager
228         */
229        public static void fire() {
230                // Schedule a job for the event-dispatching thread:
231                // creating and showing this application's GUI.
232                javax.swing.SwingUtilities.invokeLater(new Runnable() {
233                        public void run() {
234                                createAndShowGUI();
235                        }
236                });
237        }
238
239        /**
240         * get the username from the gui
241         */
242        public static String getUsername() {
243                return username;
244        }
245
246        /**
247         * get the password from the gui
248         */
249        public static String getPassword() {
250                return password;
251        }
252}