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-17 00:52:56 +0000 (Thu, 17 Jan 2013) $' 
007 * '$Revision: 31343 $'
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.util.Iterator;
039import java.util.Vector;
040
041import javax.swing.BoxLayout;
042import javax.swing.JButton;
043import javax.swing.JComboBox;
044import javax.swing.JDialog;
045import javax.swing.JFrame;
046import javax.swing.JLabel;
047import javax.swing.JPanel;
048import javax.swing.JPasswordField;
049import javax.swing.JTextField;
050import javax.swing.UIManager;
051
052import org.ecoinformatics.seek.ecogrid.MetadataSpecificationInterface;
053import org.kepler.authentication.ProxyRepository;
054import org.kepler.configuration.ConfigurationManager;
055import org.kepler.configuration.ConfigurationProperty;
056
057import ptolemy.actor.gui.Configuration;
058import ptolemy.actor.gui.Effigy;
059import ptolemy.actor.gui.ModelDirectory;
060import ptolemy.actor.gui.Tableau;
061
062/**
063 * LoginGUI pops up the login dialog to let user login, retrieves credential
064 * from GAMA server, and stores the credential into ProxyRepository.
065 * 
066 * @author Zhijie Guan guan@sdsc.edu
067 * 
068 */
069
070public class LDAPLoginGUI extends JPanel implements ActionListener {
071        private static ProxyRepository proxyRepository; // the proxyrepository got
072                                                                                                        // from
073                                                                                                        // AuthenticationManager
074        private JDialog controllingDialog; // The Frame of login dialog
075
076        private static String organizationListXPath = "//"
077                        + MetadataSpecificationInterface.ECOGRIDPATH + "/"
078                        + "ldapOrganizations/organization";
079
080        // Command button constants
081        private String OK = "ok";
082        private String CANCEL = "cancel";
083        private String ANON = "anon";
084
085        // Input fields
086        private JTextField userNameField;
087        private JPasswordField passwordField;
088        private JComboBox domainField;
089        private static Vector domainList;
090
091        private String username;
092        private String password;
093        private String organization;
094
095        private String domainName;
096
097        /**
098         * The constructor is used to build all the display components
099         * 
100         */
101        public LDAPLoginGUI() {
102                // Create everything.
103                // User Name field, label, and pane
104                userNameField = new JTextField(20);
105                JLabel userNameLabel = new JLabel("Username: ");
106                userNameLabel.setLabelFor(userNameField);
107                JPanel userNamePane = new JPanel(new FlowLayout());
108                userNamePane.add(userNameLabel);
109                userNamePane.add(userNameField);
110
111                // Password field, label, and pane
112                passwordField = new JPasswordField(20);
113                passwordField.setEchoChar('*');
114                passwordField.setActionCommand(OK);
115                passwordField.addActionListener(this);
116                JLabel passwordLabel = new JLabel("Password: ");
117                passwordLabel.setLabelFor(passwordField);
118                JPanel passwordPane = new JPanel(new FlowLayout());
119                passwordPane.add(passwordLabel);
120                passwordPane.add(passwordField);
121
122                // Domain field, label, and pane
123                domainField = new JComboBox();
124
125                // get the organization names from the config
126                ConfigurationProperty ecogridProperty = ConfigurationManager
127                                .getInstance().getProperty(
128                                                ConfigurationManager.getModule("ecogrid"));
129                ConfigurationProperty ldapOrgProp = ecogridProperty
130                                .getProperty("ldapOrganizations");
131                Iterator orgs = ConfigurationProperty.getValueList(
132                                ldapOrgProp.getProperties(), "organization", false).iterator();
133
134                while (orgs.hasNext()) {
135                        domainField.addItem(orgs.next());
136                }
137                JLabel domainLabel = new JLabel("Organization: ");
138                domainLabel.setLabelFor(domainField);
139                JPanel domainPane = new JPanel(new FlowLayout());
140                domainPane.add(domainLabel);
141                domainPane.add(domainField);
142
143                // Lay out input fields and labels on inputPane
144                JPanel inputPane = new JPanel();
145                inputPane.setLayout(new BoxLayout(inputPane, BoxLayout.Y_AXIS));
146                inputPane.add(userNamePane);
147                inputPane.add(passwordPane);
148                inputPane.add(domainPane);
149
150                // Buttons and their buttonPane
151                JButton okButton = new JButton("OK");
152                JButton cancelButton = new JButton("Cancel");
153                JButton anonLoginButton = new JButton("Login Anonymously");
154                okButton.setActionCommand(OK);
155                okButton.addActionListener(this);
156                cancelButton.setActionCommand(CANCEL);
157                cancelButton.addActionListener(this);
158                anonLoginButton.setActionCommand(ANON);
159                anonLoginButton.addActionListener(this);
160                JPanel buttonPane = new JPanel(new FlowLayout());
161                buttonPane.add(okButton);
162                buttonPane.add(cancelButton);
163                buttonPane.add(anonLoginButton);
164
165                // Pane for the whole window
166                setLayout(new BorderLayout());
167                add(inputPane, BorderLayout.CENTER);
168                add(buttonPane, BorderLayout.PAGE_END);
169        }
170
171        /**
172         * Event listener
173         */
174        public void actionPerformed(ActionEvent e) {
175                String cmd = e.getActionCommand();
176
177                if (OK.equals(cmd)) { // User login and click OK button
178                        username = userNameField.getText();
179
180                        char[] passwd = passwordField.getPassword();
181                        password = new String(passwd);
182
183                        // Zero out the possible password, for security.
184                        for (int i = 0; i < passwd.length; i++) {
185                                passwd[i] = 0;
186                        }
187
188                        organization = (String) domainField.getSelectedItem();
189                } else if (CANCEL.equals(cmd)) {
190                        organization = DomainSelectionGUI.DOMAIN_BREAK;
191                } else if (ANON.equals(cmd)) {
192                        username = "anon";
193                        password = "anon";
194                        organization = "anon";
195                }
196                //System.out.println("LDAPLoginGUI actionPerformed calling controllingDialog.dispose()");
197                controllingDialog.dispose();
198                // controllingDialog.setVisible(false);
199                //System.out.println("LDAPLoginGUI actionPerformed done disposing of controllingDialog");
200        }
201
202        // Method to reset the focus of the login dialog
203        // Must be called from the event-dispatching thread.
204        protected void resetFocus() {
205                userNameField.requestFocusInWindow();
206        }
207
208        /**
209         * Create the GUI and show it. For thread safety, this method should be
210         * invoked from the event-dispatching thread.
211         */
212        public void createAndShowGUI() {
213                // Make sure we have nice window decorations.
214                System.out.println("LDAPLoginGUI createAndShowGUI creating gui");
215                try {
216                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
217                } catch (Exception e) {
218                        e.printStackTrace();
219                }
220
221                // Create and set up the window.
222                Configuration config = (Configuration) Configuration.configurations()
223                                .iterator().next();
224                // get the directory from the config
225                ModelDirectory directory = (ModelDirectory) config
226                                .getEntity("directory");
227
228                // find the parent tableau to use as the parent for this dialog
229                Iterator effigy = directory.entityList(Effigy.class).iterator();
230                Tableau t = null;
231                while (effigy.hasNext()) {
232                        Effigy e = (Effigy) effigy.next();
233                        t = (Tableau) e.getEntity("graphTableau");
234                        if (t != null) {
235                                break;
236                        }
237                }
238
239                controllingDialog = new JDialog(t.getFrame());
240                controllingDialog.setTitle("Authenticating for: " + domainName);
241                controllingDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
242                controllingDialog.setModal(true);
243
244                // Create and set up the content pane.
245                final LDAPLoginGUI contentPane = this;
246
247                this.setOpaque(true); // content panes must be opaque
248                controllingDialog.setContentPane(this);
249
250                // Make sure the focus goes to the right component
251                // whenever the frame is initially given the focus.
252                controllingDialog.addWindowListener(new WindowAdapter() {
253                        public void windowActivated(WindowEvent e) {
254                                contentPane.resetFocus();
255                        }
256
257                        // XXX the only thing that seems to cause windowClosing is the X 
258                        // close button so we can behave like Cancel button here and set
259                        // organization to magic string BREAK
260                        public void windowClosing(WindowEvent e) {
261                                organization = DomainSelectionGUI.DOMAIN_BREAK;
262                        }
263
264                });
265
266                // Display the window.
267                controllingDialog.pack();
268                controllingDialog.setLocationRelativeTo(null); // stay in the center
269                controllingDialog.setVisible(true);
270        }
271
272        /**
273         * Function to show the login dialog It adds the login dialog into the
274         * event-dispatching thread
275         */
276        public void fire() {
277                // creating and showing this application's GUI.
278                resetFields();
279                createAndShowGUI();
280        }
281
282        /**
283         * return the username
284         */
285        public String getUsername() {
286                return username;
287        }
288
289        /**
290         * return the password
291         */
292        public String getPassword() {
293                return password;
294        }
295
296        /**
297         * return the org
298         */
299        public String getOrganization() {
300                return organization;
301        }
302
303        public void setDomainName(String name) {
304                domainName = name;
305        }
306
307        /**
308         * resets the static fields to null
309         */
310        public void resetFields() {
311                username = null;
312                password = null;
313                organization = null;
314
315                // and the input fields
316                this.userNameField.setText(username);
317                this.passwordField.setText(password);
318                this.domainField.setSelectedIndex(0);
319
320        }
321}