Ignore:
Timestamp:
11/27/2019 07:56:32 AM (6 years ago)
Author:
jdalbey
Message:

Login.java, CADClient.java: Implement #206, login screen has drop down box of student names read from a config file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/client/cadclientgui/screens/Login.java

    r289 r532  
    11package tmcsim.client.cadclientgui.screens; 
    22 
     3import java.awt.BorderLayout; 
    34import java.awt.Color; 
    45import static java.awt.Component.LEFT_ALIGNMENT; 
     
    910import java.awt.event.KeyEvent; 
    1011import java.awt.event.KeyListener; 
     12import java.awt.image.BufferedImage; 
     13import java.io.File; 
     14import java.io.FileInputStream; 
     15import java.io.FileNotFoundException; 
     16import java.io.IOException; 
     17import java.util.ArrayList; 
     18import java.util.Arrays; 
     19import java.util.List; 
     20import java.util.Scanner; 
     21import java.util.Vector; 
     22import java.util.logging.Level; 
     23import java.util.logging.Logger; 
     24import javax.imageio.ImageIO; 
    1125import javax.swing.Box; 
    1226import javax.swing.BoxLayout; 
     27import javax.swing.ImageIcon; 
    1328import javax.swing.JButton; 
     29import javax.swing.JComboBox; 
    1430import javax.swing.JFrame; 
    1531import javax.swing.JLabel; 
     
    2642 * @author Vincent 
    2743 */ 
    28 public class Login extends JFrame implements KeyListener 
     44public class Login extends JFrame 
    2945{ 
    3046    private Box login; 
    31     private JTextField userNameField; 
     47    private JComboBox<String> userNameCombo; 
    3248    private JTextField passwordField; 
    3349    public static String kNamePrompt = "Enter your name"; 
    34  
    35     public Login() 
    36     { 
     50    /** a list of student names that will be displayed on the login screen */ 
     51    private Vector<String> students; 
     52     
     53    public Login(String studentNamesFile) 
     54    { 
     55        readNamesFile(studentNamesFile); 
    3756        initView(); 
    3857    } 
    3958 
     59    /**  
     60     * A list of student names is read from a file.  The intent is to allow the 
     61     * system operators to customize the login display for each training session 
     62     * with the names of participants in that training.  
     63     * @param studentNamesFile the full path to a text file of student names 
     64     * @author jdalbey  ticket #206 
     65     */ 
     66    private void readNamesFile(String studentNamesFile) 
     67    { 
     68        FileInputStream fis = null; 
     69        try { 
     70            // Prepare an input stream from the names file 
     71            fis = new FileInputStream(studentNamesFile); 
     72            // Scan the names into a string 
     73            Scanner s = new Scanner(fis).useDelimiter("\\A"); 
     74            String out = s.next(); 
     75            // split string into an array 
     76            String[] results = out.split("\\n"); 
     77            // Initialize the student name list 
     78            students = new Vector(); 
     79            // Make sure to truncate names before putting in list 
     80            for (String name: results) 
     81            { 
     82                String truncName = name.trim(); 
     83                // Limit name length so it fits in the combo box 
     84                if (truncName.length() > 26) 
     85                { 
     86                    truncName = truncName.substring(0,25); 
     87                } 
     88                // Add the validated name to the list of students 
     89                students.add(truncName); 
     90            } 
     91            // Append the default name  
     92            students.add("Anonymous Trainee"); 
     93        } catch (FileNotFoundException ex) { 
     94            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); 
     95        } finally { 
     96            try { 
     97                fis.close(); 
     98            } catch (IOException ex) { 
     99                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); 
     100            } 
     101        } 
     102    } 
    40103    private ActionListener newEnterActionListener() 
    41104    { 
     
    45108            { 
    46109                setVisible(false); 
    47                 // Check if user provided a username JD 
    48                 String userEntry = userNameField.getText(); 
    49                 if (userEntry.equals(kNamePrompt)) 
    50                 { 
    51                     ScreenManager.setUserName("Anonymous Trainee"); 
    52                 } 
    53                 else 
    54                 { 
    55                     ScreenManager.setUserName(userEntry); 
    56                 } 
     110                // Extract the selected student name from the combo box 
     111                String userEntry = (String) userNameCombo.getSelectedItem(); 
     112                ScreenManager.setUserName(userEntry); 
    57113                ScreenManager.openCADMenu(); 
    58114                ScreenManager.openAssignedIncidents(); 
     
    60116                ScreenManager.openPendingIncidents(); 
    61117                ScreenManager.openPowerlineUI(); 
     118                // a secret password allows special permission 
    62119                if (!passwordField.getText().equals("Dispatcher")) 
    63120                { 
     
    81138    private void initView() 
    82139    { 
     140        // create a combo box containing the student names 
     141        userNameCombo = new JComboBox<String>(students); 
     142        // Create a scrollbar after ten names 
     143        userNameCombo.setMaximumRowCount(10); 
     144        userNameCombo.setForeground(Color.BLUE); 
     145        userNameCombo.setFont(new Font("Arial", Font.BOLD, 14)); 
     146         
    83147        login = new Box(BoxLayout.Y_AXIS); 
    84148        login.setAlignmentX(LEFT_ALIGNMENT); 
    85149        JPanel whiteBackground = new JPanel(); 
     150        whiteBackground.setLayout(new BorderLayout()); 
    86151        whiteBackground.setAlignmentX(LEFT_ALIGNMENT); 
    87152        whiteBackground.setBackground(Color.WHITE); 
     
    89154        whiteBackground.setMinimumSize(new Dimension(750, 150)); 
    90155        whiteBackground.setPreferredSize(new Dimension(750, 150)); 
     156        // Add TriTech banner to top of screen (ticket #205) 
     157        try { 
     158            BufferedImage myPicture = ImageIO.read(new File("images/TritechLoginBanner.png")); 
     159            JLabel picLabel = new JLabel(new ImageIcon(myPicture)); 
     160            whiteBackground.add(picLabel, BorderLayout.CENTER); 
     161        } catch (IOException ex) { 
     162            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex); 
     163        } 
    91164        login.add(whiteBackground); 
    92165 
     
    134207        JLabel userNameLabel = new JLabel("User name "); 
    135208        userNameLabel.setForeground(Color.WHITE); 
    136         userNameField = new JTextField(kNamePrompt); 
    137         userNameField.addKeyListener(this); 
    138         userNameField.setForeground(Color.GRAY); 
    139209        Box userNameBox = new Box(BoxLayout.X_AXIS); 
    140210        userNameBox.setAlignmentX(LEFT_ALIGNMENT); 
    141211        userNameBox.add(Box.createHorizontalStrut(10)); 
    142212        userNameBox.add(userNameLabel); 
    143         userNameBox.add(userNameField); 
     213        userNameBox.add(userNameCombo); 
    144214        leftBox.add(userNameBox); 
    145215 
     
    212282        setVisible(true); 
    213283    } 
    214  
    215     /**  
    216      * This keylistener removes the prompt displayed in the username text box 
    217      * the first time the user presses a key.  Fixes #96 
    218      * @author jdalbey 
    219      * @param evt  
    220      */ 
    221     public void keyPressed(KeyEvent evt)  
    222     { 
    223         // See if the text in the box is the initial prompt 
    224         // If so, the user hasn't typed anything yet, this is the 1st keypress 
    225         // Even the shift key will be caught here. 
    226         if (userNameField.getText().equals(kNamePrompt)) 
    227         { 
    228             userNameField.setText(""); 
    229             // Change the text color from gray to black 
    230             userNameField.setForeground(Color.BLACK); 
    231         } 
    232     } 
    233  
    234     public void keyReleased(KeyEvent e) {} 
    235     public void keyTyped(KeyEvent e) {} 
     284     
     285    /** Local main for unit testing */ 
     286    public static void main(String[] args)  
     287    { 
     288        new Login("config/student_names.txt"); 
     289    } 
     290     
    236291} 
Note: See TracChangeset for help on using the changeset viewer.