source: tmcsimulator/trunk/src/tmcsim/client/cadclientgui/screens/Login.java @ 639

Revision 639, 12.0 KB checked in by jdalbey, 5 years ago (diff)

Login.java modified to fix defect #254. LoginTest?.java unit test added.

Line 
1package tmcsim.client.cadclientgui.screens;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import static java.awt.Component.LEFT_ALIGNMENT;
6import java.awt.Dimension;
7import java.awt.Font;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.awt.event.KeyEvent;
11import 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;
25import javax.swing.Box;
26import javax.swing.BoxLayout;
27import javax.swing.ImageIcon;
28import javax.swing.JButton;
29import javax.swing.JComboBox;
30import javax.swing.JFrame;
31import javax.swing.JLabel;
32import javax.swing.JPanel;
33import javax.swing.JTextField;
34import javax.swing.SwingConstants;
35
36/**
37 * This class contains the view and controller for the Login screen. The view
38 * was not built using a GUI builder plug-in (but may want to consider doing so
39 * in the future), and the controller uses listeners to control how the view and
40 * data act.
41 *
42 * @author Vincent
43 */
44public class Login extends JFrame
45{
46    private Box login;
47    private JComboBox<String> userNameCombo;
48    private JTextField passwordField;
49    public static String kNamePrompt = "Enter your name";
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        students = readNamesFile(studentNamesFile);
56        initView();
57    }
58
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, #254
65     */
66    public static Vector<String> readNamesFile(String studentNamesFile)
67    {
68        // Initialize the student name list
69        Vector<String> lines = new Vector<String>();
70        FileInputStream fis = null;
71        try {
72            // Prepare an input stream from the names file
73            fis = new FileInputStream(studentNamesFile);
74            // Scan the names into a string
75            Scanner s = new Scanner(fis).useDelimiter("\\A");
76            String out = s.next();
77            // split string into an array
78            String[] results = out.split("\\n");
79            // Put the names into a list (so it can be displayed in comboBox)
80            for (String name: results)
81            {
82                // Make sure to reverse name order before putting in list
83                String displayName = name.trim();
84                // Ignore blank lines
85                if (displayName.length() > 0)
86                {
87                    // Limit name length so it fits in the combo box
88                    if (displayName.length() > 26)
89                    {
90                        displayName = displayName.substring(0,25);
91                    }
92                    // Add the validated name to the list of students
93                    lines.add(displayName);
94                }
95            }
96        } catch (FileNotFoundException ex) {
97            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
98        } finally {
99            try {
100                fis.close();
101            } catch (IOException ex) {
102                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
103            }
104        }
105        return lines;
106    }
107   
108    /** Reverse order of lastname, firstname to firstname lastname
109     * @param formattedname fullname with lastname first, separated by comma
110     * @return fullname with firstname first and no comma
111     *  Note, the result is what is desired for the CAD comment log entries.
112     *  If formattedname lacks a comma, just return it unchanged.
113     */
114    public static String reverseLastFirst(String formattedname)
115    {
116        int commapos = formattedname.indexOf(",");
117        // Handle input name without comma - just return it
118        if (commapos < 0) return formattedname;
119        // switch the name order
120        String firstname = formattedname.substring(commapos+1).trim();
121        String lastname = formattedname.substring(0,commapos).trim();
122        return firstname + ' ' + lastname;
123    }
124   
125    private ActionListener newEnterActionListener()
126    {
127        return new ActionListener()
128        {
129            public void actionPerformed(ActionEvent e)
130            {
131                setVisible(false);
132                // Extract the selected student name from the combo box
133                String userEntry = (String) userNameCombo.getSelectedItem();
134                userEntry = reverseLastFirst(userEntry); // switch name order
135                ScreenManager.setUserName(userEntry);
136                ScreenManager.openCADMenu();
137                ScreenManager.openAssignedIncidents();
138                ScreenManager.openUnitStatus();
139                ScreenManager.openPendingIncidents();
140                ScreenManager.openPowerlineUI();
141                // a secret password allows special permission
142                if (!passwordField.getText().equals("Dispatcher"))
143                {
144                    ScreenManager.setDispatcherAuthority(false);
145                }
146            }
147        };
148    }
149
150    private ActionListener newExitActionListener()
151    {
152        return new ActionListener()
153        {
154            public void actionPerformed(ActionEvent e)
155            {
156                System.exit(0);
157            }
158        };
159    }
160
161    private void initView()
162    {
163        // create a combo box containing the student names
164        userNameCombo = new JComboBox<String>(students);
165        // Create a scrollbar after ten names
166        userNameCombo.setMaximumRowCount(10);
167        userNameCombo.setForeground(Color.BLUE);
168        userNameCombo.setFont(new Font("Arial", Font.BOLD, 14));
169       
170        login = new Box(BoxLayout.Y_AXIS);
171        login.setAlignmentX(LEFT_ALIGNMENT);
172        JPanel whiteBackground = new JPanel();
173        whiteBackground.setLayout(new BorderLayout());
174        whiteBackground.setAlignmentX(LEFT_ALIGNMENT);
175        whiteBackground.setBackground(Color.WHITE);
176        whiteBackground.setMaximumSize(new Dimension(750, 150));
177        whiteBackground.setMinimumSize(new Dimension(750, 150));
178        whiteBackground.setPreferredSize(new Dimension(750, 150));
179        // Add TriTech banner to top of screen (ticket #205)
180        try {
181            BufferedImage myPicture = ImageIO.read(new File("images/TritechLoginBanner.png"));
182            JLabel picLabel = new JLabel(new ImageIcon(myPicture));
183            whiteBackground.add(picLabel, BorderLayout.CENTER);
184        } catch (IOException ex) {
185            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
186        }
187        login.add(whiteBackground);
188
189        JPanel blueBackground = new JPanel();
190        blueBackground.setAlignmentX(LEFT_ALIGNMENT);
191        blueBackground.setBackground(new Color(50, 150, 200));
192        blueBackground.setMaximumSize(new Dimension(750, 350));
193        blueBackground.setMinimumSize(new Dimension(750, 350));
194        blueBackground.setPreferredSize(new Dimension(750, 350));
195
196        JLabel title = new JLabel();
197        // Check if we are running from a jar to put message from manifest into title
198        Package clientpkg = Login.class.getPackage();
199        String builddate = clientpkg.getImplementationVersion();
200        String version = "";
201        if (builddate != null)
202        {
203            version = "          (build " + builddate + ")";
204        }
205        else // We're running from a dev environment
206        {
207            version = "           00-00-00 00:00";
208        }
209
210        title.setText("Inform CAD 5.3 Patch 5 ");
211        title.setHorizontalAlignment(SwingConstants.CENTER);
212        title.setFont(new Font("Arial", Font.PLAIN, 22));
213        title.setForeground(Color.WHITE);
214        title.setMaximumSize(new Dimension(750, 100));
215        title.setMinimumSize(new Dimension(750, 100));
216        title.setPreferredSize(new Dimension(750, 100));
217        blueBackground.add(title);
218
219        JLabel builddateLabel = new JLabel(version);
220        builddateLabel.setForeground(Color.WHITE);
221        builddateLabel.setHorizontalAlignment(SwingConstants.CENTER);
222        blueBackground.add(builddateLabel);
223
224        Box leftBox = new Box(BoxLayout.Y_AXIS);
225        leftBox.setMaximumSize(new Dimension(350, 90));
226        leftBox.setMinimumSize(new Dimension(350, 90));
227        leftBox.setPreferredSize(new Dimension(350, 90));
228        leftBox.setAlignmentX(LEFT_ALIGNMENT);
229
230        JLabel userNameLabel = new JLabel("User name ");
231        userNameLabel.setForeground(Color.WHITE);
232        Box userNameBox = new Box(BoxLayout.X_AXIS);
233        userNameBox.setAlignmentX(LEFT_ALIGNMENT);
234        userNameBox.add(Box.createHorizontalStrut(10));
235        userNameBox.add(userNameLabel);
236        userNameBox.add(userNameCombo);
237        leftBox.add(userNameBox);
238
239        JLabel passwordLabel = new JLabel("Password  ");
240        passwordLabel.setForeground(Color.WHITE);
241        passwordField = new JTextField("");
242        passwordField.setEnabled(true);
243        Box passwordBox = new Box(BoxLayout.X_AXIS);
244        passwordBox.setAlignmentX(LEFT_ALIGNMENT);
245        passwordBox.add(Box.createHorizontalStrut(10));
246        passwordBox.add(passwordLabel);
247        passwordBox.add(passwordField);
248        leftBox.add(Box.createVerticalStrut(10));
249        leftBox.add(passwordBox);
250
251        JButton enter = new JButton("Login");
252        enter.addActionListener(newEnterActionListener());
253        JButton newPassword = new JButton("New Password");
254        newPassword.setEnabled(false);
255        JButton exit = new JButton("Exit");
256        exit.addActionListener(newExitActionListener());
257        JButton selectAll = new JButton("Select All");
258        selectAll.setEnabled(false);
259        JButton unselectAll = new JButton("Unselect all");
260        unselectAll.setEnabled(false);
261        Box buttonBox = new Box(BoxLayout.X_AXIS);
262        buttonBox.setAlignmentX(LEFT_ALIGNMENT);
263        buttonBox.add(Box.createHorizontalStrut(75));
264        buttonBox.add(enter);
265        buttonBox.add(Box.createHorizontalStrut(20));
266        buttonBox.add(newPassword);
267        buttonBox.add(Box.createHorizontalStrut(20));
268        buttonBox.add(exit);
269        buttonBox.add(Box.createHorizontalStrut(125));
270        buttonBox.add(selectAll);
271        buttonBox.add(Box.createHorizontalStrut(10));
272        buttonBox.add(unselectAll);
273
274        Box rightBox = new Box(BoxLayout.Y_AXIS);
275        rightBox.setAlignmentX(LEFT_ALIGNMENT);
276        rightBox.setMaximumSize(new Dimension(250, 90));
277        rightBox.setMinimumSize(new Dimension(250, 90));
278        rightBox.setPreferredSize(new Dimension(250, 90));
279        JLabel position = new JLabel("Positions:");
280        position.setPreferredSize(new Dimension(250, 20));
281        position.setMaximumSize(new Dimension(250, 20));
282        position.setMinimumSize(new Dimension(250, 20));
283        position.setBackground(Color.BLACK);
284        position.setHorizontalAlignment(SwingConstants.LEFT);
285        position.setForeground(Color.WHITE);
286        rightBox.add(position);
287        JPanel list = new JPanel();
288        rightBox.add(list);
289
290        Box centerBox = new Box(BoxLayout.X_AXIS);
291        centerBox.setAlignmentX(LEFT_ALIGNMENT);
292        centerBox.add(leftBox);
293        centerBox.add(Box.createHorizontalStrut(75));
294        centerBox.add(rightBox);
295        blueBackground.add(centerBox);
296        blueBackground.add(Box.createVerticalStrut(125));
297        blueBackground.add(buttonBox);
298        login.add(blueBackground);
299
300        getContentPane().add(login);
301        setUndecorated(true);
302        pack();
303        setLocationRelativeTo(null);
304        setDefaultCloseOperation(EXIT_ON_CLOSE);
305        setVisible(true);
306    }
307   
308    /** Local main for unit testing */
309    public static void main(String[] args) 
310    {
311        new Login("config/student_names.txt");
312    }
313   
314}
Note: See TracBrowser for help on using the repository browser.