Warning: Can't use blame annotator:
svn blame failed on trunk/src/tmcsim/client/cadclientgui/screens/Login.java: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 532, 11.0 KB checked in by jdalbey, 6 years ago (diff)

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

RevLine 
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        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
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    }
103    private ActionListener newEnterActionListener()
104    {
105        return new ActionListener()
106        {
107            public void actionPerformed(ActionEvent e)
108            {
109                setVisible(false);
110                // Extract the selected student name from the combo box
111                String userEntry = (String) userNameCombo.getSelectedItem();
112                ScreenManager.setUserName(userEntry);
113                ScreenManager.openCADMenu();
114                ScreenManager.openAssignedIncidents();
115                ScreenManager.openUnitStatus();
116                ScreenManager.openPendingIncidents();
117                ScreenManager.openPowerlineUI();
118                // a secret password allows special permission
119                if (!passwordField.getText().equals("Dispatcher"))
120                {
121                    ScreenManager.setDispatcherAuthority(false);
122                }
123            }
124        };
125    }
126
127    private ActionListener newExitActionListener()
128    {
129        return new ActionListener()
130        {
131            public void actionPerformed(ActionEvent e)
132            {
133                System.exit(0);
134            }
135        };
136    }
137
138    private void initView()
139    {
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       
147        login = new Box(BoxLayout.Y_AXIS);
148        login.setAlignmentX(LEFT_ALIGNMENT);
149        JPanel whiteBackground = new JPanel();
150        whiteBackground.setLayout(new BorderLayout());
151        whiteBackground.setAlignmentX(LEFT_ALIGNMENT);
152        whiteBackground.setBackground(Color.WHITE);
153        whiteBackground.setMaximumSize(new Dimension(750, 150));
154        whiteBackground.setMinimumSize(new Dimension(750, 150));
155        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        }
164        login.add(whiteBackground);
165
166        JPanel blueBackground = new JPanel();
167        blueBackground.setAlignmentX(LEFT_ALIGNMENT);
168        blueBackground.setBackground(new Color(50, 150, 200));
169        blueBackground.setMaximumSize(new Dimension(750, 350));
170        blueBackground.setMinimumSize(new Dimension(750, 350));
171        blueBackground.setPreferredSize(new Dimension(750, 350));
172
173        JLabel title = new JLabel();
174        // Check if we are running from a jar to put message from manifest into title
175        Package clientpkg = Login.class.getPackage();
176        String builddate = clientpkg.getImplementationVersion();
177        String version = "";
178        if (builddate != null)
179        {
180            version = "          (build " + builddate + ")";
181        }
182        else // We're running from a dev environment
183        {
184            version = "           00-00-00 00:00";
185        }
186
187        title.setText("Inform CAD 5.3 Patch 5 ");
188        title.setHorizontalAlignment(SwingConstants.CENTER);
189        title.setFont(new Font("Arial", Font.PLAIN, 22));
190        title.setForeground(Color.WHITE);
191        title.setMaximumSize(new Dimension(750, 100));
192        title.setMinimumSize(new Dimension(750, 100));
193        title.setPreferredSize(new Dimension(750, 100));
194        blueBackground.add(title);
195
196        JLabel builddateLabel = new JLabel(version);
197        builddateLabel.setForeground(Color.WHITE);
198        builddateLabel.setHorizontalAlignment(SwingConstants.CENTER);
199        blueBackground.add(builddateLabel);
200
201        Box leftBox = new Box(BoxLayout.Y_AXIS);
202        leftBox.setMaximumSize(new Dimension(350, 90));
203        leftBox.setMinimumSize(new Dimension(350, 90));
204        leftBox.setPreferredSize(new Dimension(350, 90));
205        leftBox.setAlignmentX(LEFT_ALIGNMENT);
206
207        JLabel userNameLabel = new JLabel("User name ");
208        userNameLabel.setForeground(Color.WHITE);
209        Box userNameBox = new Box(BoxLayout.X_AXIS);
210        userNameBox.setAlignmentX(LEFT_ALIGNMENT);
211        userNameBox.add(Box.createHorizontalStrut(10));
212        userNameBox.add(userNameLabel);
213        userNameBox.add(userNameCombo);
214        leftBox.add(userNameBox);
215
216        JLabel passwordLabel = new JLabel("Password  ");
217        passwordLabel.setForeground(Color.WHITE);
218        passwordField = new JTextField("Not required");
219        passwordField.setEnabled(false);
220        Box passwordBox = new Box(BoxLayout.X_AXIS);
221        passwordBox.setAlignmentX(LEFT_ALIGNMENT);
222        passwordBox.add(Box.createHorizontalStrut(10));
223        passwordBox.add(passwordLabel);
224        passwordBox.add(passwordField);
225        leftBox.add(Box.createVerticalStrut(10));
226        leftBox.add(passwordBox);
227
228        JButton enter = new JButton("Login");
229        enter.addActionListener(newEnterActionListener());
230        JButton newPassword = new JButton("New Password");
231        newPassword.setEnabled(false);
232        JButton exit = new JButton("Exit");
233        exit.addActionListener(newExitActionListener());
234        JButton selectAll = new JButton("Select All");
235        selectAll.setEnabled(false);
236        JButton unselectAll = new JButton("Unselect all");
237        unselectAll.setEnabled(false);
238        Box buttonBox = new Box(BoxLayout.X_AXIS);
239        buttonBox.setAlignmentX(LEFT_ALIGNMENT);
240        buttonBox.add(Box.createHorizontalStrut(75));
241        buttonBox.add(enter);
242        buttonBox.add(Box.createHorizontalStrut(20));
243        buttonBox.add(newPassword);
244        buttonBox.add(Box.createHorizontalStrut(20));
245        buttonBox.add(exit);
246        buttonBox.add(Box.createHorizontalStrut(125));
247        buttonBox.add(selectAll);
248        buttonBox.add(Box.createHorizontalStrut(10));
249        buttonBox.add(unselectAll);
250
251        Box rightBox = new Box(BoxLayout.Y_AXIS);
252        rightBox.setAlignmentX(LEFT_ALIGNMENT);
253        rightBox.setMaximumSize(new Dimension(250, 90));
254        rightBox.setMinimumSize(new Dimension(250, 90));
255        rightBox.setPreferredSize(new Dimension(250, 90));
256        JLabel position = new JLabel("Positions:");
257        position.setPreferredSize(new Dimension(250, 20));
258        position.setMaximumSize(new Dimension(250, 20));
259        position.setMinimumSize(new Dimension(250, 20));
260        position.setBackground(Color.BLACK);
261        position.setHorizontalAlignment(SwingConstants.LEFT);
262        position.setForeground(Color.WHITE);
263        rightBox.add(position);
264        JPanel list = new JPanel();
265        rightBox.add(list);
266
267        Box centerBox = new Box(BoxLayout.X_AXIS);
268        centerBox.setAlignmentX(LEFT_ALIGNMENT);
269        centerBox.add(leftBox);
270        centerBox.add(Box.createHorizontalStrut(75));
271        centerBox.add(rightBox);
272        blueBackground.add(centerBox);
273        blueBackground.add(Box.createVerticalStrut(125));
274        blueBackground.add(buttonBox);
275        login.add(blueBackground);
276
277        getContentPane().add(login);
278        setUndecorated(true);
279        pack();
280        setLocationRelativeTo(null);
281        setDefaultCloseOperation(EXIT_ON_CLOSE);
282        setVisible(true);
283    }
284   
285    /** Local main for unit testing */
286    public static void main(String[] args) 
287    {
288        new Login("config/student_names.txt");
289    }
290   
291}
Note: See TracBrowser for help on using the repository browser.