001/* 002 * Copyright (c) 2007-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.Dimension; 034import java.awt.FlowLayout; 035import java.awt.event.ActionEvent; 036import java.awt.event.ActionListener; 037import java.awt.event.WindowAdapter; 038import java.awt.event.WindowEvent; 039import java.io.IOException; 040import java.util.Iterator; 041import java.util.Vector; 042 043import javax.swing.BoxLayout; 044import javax.swing.JButton; 045import javax.swing.JComboBox; 046import javax.swing.JFrame; 047import javax.swing.JLabel; 048import javax.swing.JPanel; 049import javax.swing.UIManager; 050 051import org.kepler.authentication.Domain; 052import org.kepler.authentication.DomainList; 053 054/** 055 * DomainSelectionGUI pops up the pre-login dialog to let user select the 056 * authentication domain 057 * 058 * @author Ben Leinfelder 059 * 060 */ 061 062public class DomainSelectionGUI extends JPanel implements ActionListener { 063 private static JFrame controllingFrame; // The Frame of login dialog 064 065 // Command button constants 066 private String OK = "ok"; 067 private String CANCEL = "cancel"; 068 069 // Input fields 070 private JComboBox domainField; 071 private static Vector domainList; 072 private static String domain; 073 074 // XXX magic string, get rid of this 075 public static String DOMAIN_BREAK = "BREAK"; 076 077 /** 078 * The constructor is used to build all the display components 079 * 080 */ 081 public DomainSelectionGUI() { 082 // set up the list 083 try { 084 domainList = DomainList.getInstance().getDomainList(); 085 086 } catch (IOException e) { 087 e.printStackTrace(); 088 } 089 090 // Domain field, label, and pane 091 domainField = new JComboBox(); 092 Dimension dim = new Dimension(200, 20); 093 domainField.setMinimumSize(dim); 094 domainField.setPreferredSize(dim); 095 096 // iterate through the list, add each domain name as option 097 Iterator dIter = domainList.iterator(); 098 while (dIter.hasNext()) { 099 Domain d = (Domain) dIter.next(); 100 String domainName = d.getDomain(); 101 domainField.addItem(domainName); 102 } 103 104 JLabel domainLabel = new JLabel("Authentication Domain: "); 105 domainLabel.setLabelFor(domainField); 106 JPanel domainPane = new JPanel(new FlowLayout()); 107 domainPane.add(domainLabel); 108 domainPane.add(domainField); 109 110 // Lay out input fields and labels on inputPane 111 JPanel inputPane = new JPanel(); 112 inputPane.setLayout(new BoxLayout(inputPane, BoxLayout.Y_AXIS)); 113 inputPane.add(domainPane); 114 115 // Buttons and their buttonPane 116 JButton okButton = new JButton("OK"); 117 JButton resetButton = new JButton("Cancel"); 118 okButton.setActionCommand(OK); 119 okButton.addActionListener(this); 120 resetButton.setActionCommand(CANCEL); 121 resetButton.addActionListener(this); 122 123 JPanel buttonPane = new JPanel(new FlowLayout()); 124 buttonPane.add(okButton); 125 buttonPane.add(resetButton); 126 127 // Pane for the whole window 128 setLayout(new BorderLayout()); 129 add(inputPane, BorderLayout.CENTER); 130 add(buttonPane, BorderLayout.PAGE_END); 131 } 132 133 /** 134 * Event listener 135 */ 136 public void actionPerformed(ActionEvent e) { 137 String cmd = e.getActionCommand(); 138 139 if (OK.equals(cmd)) { 140 // User selects domain and clicks OK button 141 domain = (String) domainField.getSelectedItem(); 142 System.out.println("OK - disposing"); 143 controllingFrame.dispose(); 144 } else if (CANCEL.equals(cmd)) { 145 domain = DOMAIN_BREAK; 146 System.out.println("user cancelled action - disposing"); 147 controllingFrame.dispose(); 148 } 149 } 150 151 // Method to reset the focus of the login dialog 152 // Must be called from the event-dispatching thread. 153 protected void resetFocus() { 154 domainField.requestFocusInWindow(); 155 } 156 157 /** 158 * Create the GUI and show it. For thread safety, this method should be 159 * invoked from the event-dispatching thread. 160 */ 161 public static void createAndShowGUI() { 162 resetFields(); 163 // Make sure we have nice window decorations. 164 System.out.println("creating gui"); 165 try { 166 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 171 // Create and set up the window. 172 controllingFrame = new JFrame("Domain Selection"); 173 //controllingFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 174 controllingFrame.addWindowListener(new WindowAdapter(){ 175 public void windowClosing(WindowEvent e) { 176 domain = DOMAIN_BREAK; 177 controllingFrame.dispose(); 178 } 179 } 180 ); 181 182 // Create and set up the content pane. 183 final DomainSelectionGUI contentPane = new DomainSelectionGUI(); 184 contentPane.setOpaque(true); // content panes must be opaque 185 controllingFrame.setContentPane(contentPane); 186 187 // Make sure the focus goes to the right component 188 // whenever the frame is initially given the focus. 189 controllingFrame.addWindowListener(new WindowAdapter() { 190 public void windowActivated(WindowEvent e) { 191 contentPane.resetFocus(); 192 } 193 }); 194 195 // Display the window. 196 controllingFrame.pack(); 197 controllingFrame.setLocationRelativeTo(null); // stay in the center 198 controllingFrame.setVisible(true); 199 } 200 201 /** 202 * Function to show the login dialog It adds the login dialog into the 203 * event-dispatching thread 204 */ 205 public static void fire() { 206 // Schedule a job for the event-dispatching thread: 207 // creating and showing this application's GUI. 208 System.out.println("Firing new thread"); 209 resetFields(); 210 javax.swing.SwingUtilities.invokeLater(new Runnable() { 211 public void run() { 212 System.out.println("Running in new thread"); 213 createAndShowGUI(); 214 System.out.println("exiting new thread"); 215 } 216 }); 217 } 218 219 /** 220 * return the domain 221 */ 222 public static String getDomain() { 223 return domain; 224 } 225 226 /** 227 * resets the static fields to null 228 */ 229 public static void resetFields() { 230 domain = null; 231 } 232}