| 1 | package tmcsim.client.cadclientgui.screens; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Color; |
|---|
| 4 | import static java.awt.Component.LEFT_ALIGNMENT; |
|---|
| 5 | import java.awt.Dimension; |
|---|
| 6 | import java.awt.Font; |
|---|
| 7 | import java.awt.event.ActionEvent; |
|---|
| 8 | import java.awt.event.ActionListener; |
|---|
| 9 | import java.awt.event.KeyEvent; |
|---|
| 10 | import java.awt.event.KeyListener; |
|---|
| 11 | import javax.swing.Box; |
|---|
| 12 | import javax.swing.BoxLayout; |
|---|
| 13 | import javax.swing.JButton; |
|---|
| 14 | import javax.swing.JFrame; |
|---|
| 15 | import javax.swing.JLabel; |
|---|
| 16 | import javax.swing.JPanel; |
|---|
| 17 | import javax.swing.JTextField; |
|---|
| 18 | import javax.swing.SwingConstants; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * This class contains the view and controller for the Login screen. The view |
|---|
| 22 | * was not built using a GUI builder plug-in (but may want to consider doing so |
|---|
| 23 | * in the future), and the controller uses listeners to control how the view and |
|---|
| 24 | * data act. |
|---|
| 25 | * |
|---|
| 26 | * @author Vincent |
|---|
| 27 | */ |
|---|
| 28 | public class Login extends JFrame implements KeyListener |
|---|
| 29 | { |
|---|
| 30 | private Box login; |
|---|
| 31 | private JTextField userNameField; |
|---|
| 32 | private JTextField passwordField; |
|---|
| 33 | public static String kNamePrompt = "Enter your name"; |
|---|
| 34 | |
|---|
| 35 | public Login() |
|---|
| 36 | { |
|---|
| 37 | initView(); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | private ActionListener newEnterActionListener() |
|---|
| 41 | { |
|---|
| 42 | return new ActionListener() |
|---|
| 43 | { |
|---|
| 44 | public void actionPerformed(ActionEvent e) |
|---|
| 45 | { |
|---|
| 46 | 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 | } |
|---|
| 57 | ScreenManager.openCADMenu(); |
|---|
| 58 | ScreenManager.openAssignedIncidents(); |
|---|
| 59 | ScreenManager.openUnitStatus(); |
|---|
| 60 | ScreenManager.openPendingIncidents(); |
|---|
| 61 | ScreenManager.openPowerlineUI(); |
|---|
| 62 | if (!passwordField.getText().equals("Dispatcher")) |
|---|
| 63 | { |
|---|
| 64 | ScreenManager.setDispatcherAuthority(false); |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | }; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | private ActionListener newExitActionListener() |
|---|
| 71 | { |
|---|
| 72 | return new ActionListener() |
|---|
| 73 | { |
|---|
| 74 | public void actionPerformed(ActionEvent e) |
|---|
| 75 | { |
|---|
| 76 | System.exit(0); |
|---|
| 77 | } |
|---|
| 78 | }; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | private void initView() |
|---|
| 82 | { |
|---|
| 83 | login = new Box(BoxLayout.Y_AXIS); |
|---|
| 84 | login.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 85 | JPanel whiteBackground = new JPanel(); |
|---|
| 86 | whiteBackground.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 87 | whiteBackground.setBackground(Color.WHITE); |
|---|
| 88 | whiteBackground.setMaximumSize(new Dimension(750, 150)); |
|---|
| 89 | whiteBackground.setMinimumSize(new Dimension(750, 150)); |
|---|
| 90 | whiteBackground.setPreferredSize(new Dimension(750, 150)); |
|---|
| 91 | login.add(whiteBackground); |
|---|
| 92 | |
|---|
| 93 | JPanel blueBackground = new JPanel(); |
|---|
| 94 | blueBackground.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 95 | blueBackground.setBackground(new Color(50, 150, 200)); |
|---|
| 96 | blueBackground.setMaximumSize(new Dimension(750, 350)); |
|---|
| 97 | blueBackground.setMinimumSize(new Dimension(750, 350)); |
|---|
| 98 | blueBackground.setPreferredSize(new Dimension(750, 350)); |
|---|
| 99 | |
|---|
| 100 | JLabel title = new JLabel(); |
|---|
| 101 | // Check if we are running from a jar to put message from manifest into title |
|---|
| 102 | Package clientpkg = Login.class.getPackage(); |
|---|
| 103 | String builddate = clientpkg.getImplementationVersion(); |
|---|
| 104 | String version = ""; |
|---|
| 105 | if (builddate != null) |
|---|
| 106 | { |
|---|
| 107 | version = " (build " + builddate + ")"; |
|---|
| 108 | } |
|---|
| 109 | else // We're running from a dev environment |
|---|
| 110 | { |
|---|
| 111 | version = " 00-00-00 00:00"; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | title.setText("Inform CAD 5.3 Patch 5 "); |
|---|
| 115 | title.setHorizontalAlignment(SwingConstants.CENTER); |
|---|
| 116 | title.setFont(new Font("Arial", Font.PLAIN, 22)); |
|---|
| 117 | title.setForeground(Color.WHITE); |
|---|
| 118 | title.setMaximumSize(new Dimension(750, 100)); |
|---|
| 119 | title.setMinimumSize(new Dimension(750, 100)); |
|---|
| 120 | title.setPreferredSize(new Dimension(750, 100)); |
|---|
| 121 | blueBackground.add(title); |
|---|
| 122 | |
|---|
| 123 | JLabel builddateLabel = new JLabel(version); |
|---|
| 124 | builddateLabel.setForeground(Color.WHITE); |
|---|
| 125 | builddateLabel.setHorizontalAlignment(SwingConstants.CENTER); |
|---|
| 126 | blueBackground.add(builddateLabel); |
|---|
| 127 | |
|---|
| 128 | Box leftBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 129 | leftBox.setMaximumSize(new Dimension(350, 90)); |
|---|
| 130 | leftBox.setMinimumSize(new Dimension(350, 90)); |
|---|
| 131 | leftBox.setPreferredSize(new Dimension(350, 90)); |
|---|
| 132 | leftBox.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 133 | |
|---|
| 134 | JLabel userNameLabel = new JLabel("User name "); |
|---|
| 135 | userNameLabel.setForeground(Color.WHITE); |
|---|
| 136 | userNameField = new JTextField(kNamePrompt); |
|---|
| 137 | userNameField.addKeyListener(this); |
|---|
| 138 | userNameField.setForeground(Color.GRAY); |
|---|
| 139 | Box userNameBox = new Box(BoxLayout.X_AXIS); |
|---|
| 140 | userNameBox.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 141 | userNameBox.add(Box.createHorizontalStrut(10)); |
|---|
| 142 | userNameBox.add(userNameLabel); |
|---|
| 143 | userNameBox.add(userNameField); |
|---|
| 144 | leftBox.add(userNameBox); |
|---|
| 145 | |
|---|
| 146 | JLabel passwordLabel = new JLabel("Password "); |
|---|
| 147 | passwordLabel.setForeground(Color.WHITE); |
|---|
| 148 | passwordField = new JTextField("Not required"); |
|---|
| 149 | passwordField.setEnabled(false); |
|---|
| 150 | Box passwordBox = new Box(BoxLayout.X_AXIS); |
|---|
| 151 | passwordBox.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 152 | passwordBox.add(Box.createHorizontalStrut(10)); |
|---|
| 153 | passwordBox.add(passwordLabel); |
|---|
| 154 | passwordBox.add(passwordField); |
|---|
| 155 | leftBox.add(Box.createVerticalStrut(10)); |
|---|
| 156 | leftBox.add(passwordBox); |
|---|
| 157 | |
|---|
| 158 | JButton enter = new JButton("Login"); |
|---|
| 159 | enter.addActionListener(newEnterActionListener()); |
|---|
| 160 | JButton newPassword = new JButton("New Password"); |
|---|
| 161 | newPassword.setEnabled(false); |
|---|
| 162 | JButton exit = new JButton("Exit"); |
|---|
| 163 | exit.addActionListener(newExitActionListener()); |
|---|
| 164 | JButton selectAll = new JButton("Select All"); |
|---|
| 165 | selectAll.setEnabled(false); |
|---|
| 166 | JButton unselectAll = new JButton("Unselect all"); |
|---|
| 167 | unselectAll.setEnabled(false); |
|---|
| 168 | Box buttonBox = new Box(BoxLayout.X_AXIS); |
|---|
| 169 | buttonBox.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 170 | buttonBox.add(Box.createHorizontalStrut(75)); |
|---|
| 171 | buttonBox.add(enter); |
|---|
| 172 | buttonBox.add(Box.createHorizontalStrut(20)); |
|---|
| 173 | buttonBox.add(newPassword); |
|---|
| 174 | buttonBox.add(Box.createHorizontalStrut(20)); |
|---|
| 175 | buttonBox.add(exit); |
|---|
| 176 | buttonBox.add(Box.createHorizontalStrut(125)); |
|---|
| 177 | buttonBox.add(selectAll); |
|---|
| 178 | buttonBox.add(Box.createHorizontalStrut(10)); |
|---|
| 179 | buttonBox.add(unselectAll); |
|---|
| 180 | |
|---|
| 181 | Box rightBox = new Box(BoxLayout.Y_AXIS); |
|---|
| 182 | rightBox.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 183 | rightBox.setMaximumSize(new Dimension(250, 90)); |
|---|
| 184 | rightBox.setMinimumSize(new Dimension(250, 90)); |
|---|
| 185 | rightBox.setPreferredSize(new Dimension(250, 90)); |
|---|
| 186 | JLabel position = new JLabel("Positions:"); |
|---|
| 187 | position.setPreferredSize(new Dimension(250, 20)); |
|---|
| 188 | position.setMaximumSize(new Dimension(250, 20)); |
|---|
| 189 | position.setMinimumSize(new Dimension(250, 20)); |
|---|
| 190 | position.setBackground(Color.BLACK); |
|---|
| 191 | position.setHorizontalAlignment(SwingConstants.LEFT); |
|---|
| 192 | position.setForeground(Color.WHITE); |
|---|
| 193 | rightBox.add(position); |
|---|
| 194 | JPanel list = new JPanel(); |
|---|
| 195 | rightBox.add(list); |
|---|
| 196 | |
|---|
| 197 | Box centerBox = new Box(BoxLayout.X_AXIS); |
|---|
| 198 | centerBox.setAlignmentX(LEFT_ALIGNMENT); |
|---|
| 199 | centerBox.add(leftBox); |
|---|
| 200 | centerBox.add(Box.createHorizontalStrut(75)); |
|---|
| 201 | centerBox.add(rightBox); |
|---|
| 202 | blueBackground.add(centerBox); |
|---|
| 203 | blueBackground.add(Box.createVerticalStrut(125)); |
|---|
| 204 | blueBackground.add(buttonBox); |
|---|
| 205 | login.add(blueBackground); |
|---|
| 206 | |
|---|
| 207 | getContentPane().add(login); |
|---|
| 208 | setUndecorated(true); |
|---|
| 209 | pack(); |
|---|
| 210 | setLocationRelativeTo(null); |
|---|
| 211 | setDefaultCloseOperation(EXIT_ON_CLOSE); |
|---|
| 212 | setVisible(true); |
|---|
| 213 | } |
|---|
| 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) {} |
|---|
| 236 | } |
|---|