Guest User

Untitled

a guest
May 7th, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2. import javax.swing.*;
  3.  
  4. import org.junit.Ignore;
  5.  
  6. import java.awt.event.*;
  7. import java.io.IOException;
  8.  
  9.  
  10. public class Gui extends Guiback
  11. {
  12. String username;
  13. String password;
  14. JFrame gui = new JFrame("SoftwareHuset A/S");
  15. JPanel panel = new JPanel();
  16. JLabel logoLabel, oldPasswordLabel, newPasswordLabel;
  17. JTextField usernameText, startDatoText, slutDatoText, projectNameText;
  18. JPasswordField passwordText, newPasswordText;
  19. JButton loginButton, createUserButton, createProjectButton, editProjectButton, cancelButton, editUserButton;
  20.  
  21. String[] projectsString = {"p1", "p2"};
  22.  
  23.  
  24. public Gui()
  25. {
  26. try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
  27. catch (ClassNotFoundException e) {}
  28. catch (InstantiationException e) {}
  29. catch (IllegalAccessException e) {}
  30. catch (UnsupportedLookAndFeelException e) {}
  31.  
  32. setUpGui();
  33. }
  34.  
  35. public static void main(String[] args)
  36. {
  37. Gui gui = new Gui();
  38.  
  39. }
  40.  
  41. public void setUpGui() {
  42.  
  43.  
  44.  
  45. panel.setLayout(null);
  46.  
  47. editUserButton = new JButton();
  48. cancelButton = new JButton();
  49. slutDatoText = new JTextField();
  50. projectNameText = new JTextField();
  51. createUserButton = new JButton();
  52. startDatoText = new JTextField();
  53. createProjectButton = new JButton();
  54. editProjectButton = new JButton();
  55. usernameText = new JTextField();
  56. usernameText.setBounds(50, 220, 200, 30);
  57. passwordText = new JPasswordField();
  58. passwordText.setBounds(50, 250, 200, 30);
  59. passwordText.setEchoChar('*');
  60. newPasswordText = new JPasswordField();
  61. newPasswordText.setEchoChar('*');
  62. newPasswordLabel = new JLabel();
  63. oldPasswordLabel = new JLabel();
  64.  
  65. usernameText.setText("Username");
  66. passwordText.setText("password");
  67.  
  68. ImageIcon logo = new ImageIcon("image/logo.png");
  69. logoLabel = new JLabel("", logo, JLabel.CENTER);
  70. logoLabel.setBounds(50, 10, 200, 200);
  71.  
  72. cancelButton.setBounds(50, 190, 200, 30);
  73. cancelButton.setText("Cancel");
  74.  
  75. loginButton = new JButton();
  76. loginButton.setBounds(100, 280, 100, 30);
  77. loginButton.setText("Login");
  78. loginButton.addActionListener(new ActionListener() {
  79. @Override
  80. public void actionPerformed(ActionEvent e) {
  81. username = usernameText.getText();
  82. password = new String(passwordText.getPassword());
  83. //System.out.println(username + " " + password);
  84. try
  85. {
  86. if((Guiback.testLogin(username, password)) == true) {
  87. changeToLoggedIn();
  88. hideLogIn();
  89. }
  90. }
  91.  
  92. catch (IOException e1)
  93. {
  94. e1.printStackTrace();
  95. }
  96. }
  97. });
  98.  
  99. panel.add(logoLabel);
  100. panel.add(loginButton);
  101. panel.add(usernameText);
  102. panel.add(passwordText);
  103. panel.add(newPasswordText);
  104. panel.add(cancelButton);
  105. panel.add(editUserButton);
  106. panel.add(createUserButton);
  107. panel.add(startDatoText);
  108. panel.add(slutDatoText);
  109. panel.add(createProjectButton);
  110. panel.add(projectNameText);
  111. panel.add(editProjectButton);
  112. panel.add(oldPasswordLabel);
  113. panel.add(newPasswordLabel);
  114.  
  115. cancelButton.setVisible(false);
  116.  
  117. //add components
  118. gui.add(panel);
  119.  
  120. //set window settings
  121. gui.setSize(300, 350);
  122. gui.setLocationRelativeTo(null);
  123. gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  124. gui.setVisible(true);
  125. gui.setResizable(false);
  126. }
  127.  
  128. public void changeToEditProject()
  129. {
  130.  
  131. gui.setSize(300, 300);
  132. startDatoText.setBounds(50, 80, 200, 30);
  133. startDatoText.setText("Start Date day:month:year");
  134. slutDatoText.setBounds(50, 110, 200, 30);
  135. slutDatoText.setText("End date day:month:year");
  136. editProjectButton.setBounds(50, 150, 200,30);
  137. projectNameText.setBounds(50, 50, 200, 30);
  138. projectNameText.setText("Project name");
  139.  
  140. cancelButton.addActionListener(new ActionListener() {
  141. @Override
  142. public void actionPerformed(ActionEvent e) {
  143. hideEditProject();
  144. changeToLoggedIn();
  145. showLoggedIn();
  146. }
  147. });
  148.  
  149. editProjectButton.addActionListener(new ActionListener() {
  150. @Override
  151. public void actionPerformed(ActionEvent e) {
  152.  
  153. }
  154. });
  155. }
  156.  
  157. public void changeToEditUser(){
  158. gui.setSize(300, 300);
  159. hideLoggedIn();
  160. showEditUser();
  161. usernameText.setBounds(50, 50, 200, 30);
  162. usernameText.setText("Username");
  163. passwordText.setBounds(50, 80, 200, 30);
  164. newPasswordText.setBounds(50, 110, 200, 30);
  165. editUserButton.setBounds(50, 150, 200,30);
  166. newPasswordLabel.setBounds(10, 110, 200, 30);
  167. newPasswordLabel.setText("New");
  168. oldPasswordLabel.setBounds(10, 80, 200, 30);
  169. oldPasswordLabel.setText("Old");
  170.  
  171. cancelButton.addActionListener(new ActionListener() {
  172. @Override
  173. public void actionPerformed(ActionEvent e) {
  174. hideEditUser();
  175. changeToLoggedIn();
  176. showLoggedIn();
  177. }
  178. });
  179.  
  180. editUserButton.addActionListener(new ActionListener() {
  181. @Override
  182. public void actionPerformed(ActionEvent e) {
  183. username = usernameText.getText();
  184. password = new String(passwordText.getPassword());
  185. String newPassword = new String(newPasswordText.getPassword());
  186. try{
  187. Guiback.editUserPassword(username, newPassword, password);
  188. }
  189. catch (IOException e1) {
  190.  
  191. }
  192. }
  193. });
  194.  
  195. }
  196.  
  197.  
  198. public void changeToLoggedIn()
  199. {
  200.  
  201.  
  202. gui.setSize(600, 600);
  203. newPasswordText.setText("New password");
  204. usernameText.setText("New username");
  205. usernameText.setBounds(50, 430, 200, 30);
  206. passwordText.setText("New password");
  207. passwordText.setBounds(50, 460, 200, 30);
  208.  
  209. projectNameText.setBounds(350, 400, 200, 30);
  210. projectNameText.setText("Project name");
  211. startDatoText.setBounds(350, 430, 200, 30);
  212. startDatoText.setText("Start Date day:month:year");
  213. slutDatoText.setBounds(350, 460, 200, 30);
  214. slutDatoText.setText("End date day:month:year");
  215.  
  216. createUserButton.setBounds(50, 490, 200, 30);
  217. createUserButton.setText("Create user");
  218.  
  219. createProjectButton.setBounds(350, 490, 200, 30);
  220. createProjectButton.setText("Create project");
  221.  
  222. editProjectButton.setBounds(350, 530, 200, 30);
  223. editProjectButton.setText("Edit project");
  224.  
  225. editUserButton.setBounds(50, 530, 200, 30);
  226. editUserButton.setText("Edit user");
  227.  
  228.  
  229. createUserButton.addActionListener(new ActionListener() {
  230. @Override
  231. public void actionPerformed(ActionEvent e) {
  232. username = usernameText.getText();
  233. password = new String( passwordText.getPassword());
  234.  
  235. try
  236. {
  237. if (Guiback.userAvailable(username) == true)
  238. {
  239. Guiback.createUser(username, password);
  240. JOptionPane.showMessageDialog(null, "infoMessage", "InfoBox: " + "titleBar", JOptionPane.INFORMATION_MESSAGE);
  241. }
  242. }
  243. catch (IOException e1)
  244. {
  245. e1.printStackTrace();
  246. }
  247. /*
  248. else
  249. {
  250. //Fejl om brugernavnet allerede er taget.
  251. }
  252. */
  253. }
  254. });
  255.  
  256. createProjectButton.addActionListener(new ActionListener() {
  257. @Override
  258. public void actionPerformed(ActionEvent e) {
  259.  
  260. }
  261. });
  262.  
  263. editProjectButton.addActionListener(new ActionListener() {
  264. @Override
  265. public void actionPerformed(ActionEvent e) {
  266. changeToEditProject();
  267. hideLoggedIn();
  268. showEditProject();
  269.  
  270. }
  271. });
  272.  
  273. editUserButton.addActionListener(new ActionListener() {
  274. @Override
  275. public void actionPerformed(ActionEvent e) {
  276. changeToEditUser();
  277. hideLoggedIn();
  278. showEditUser();
  279.  
  280. }
  281. });
  282.  
  283.  
  284.  
  285. }
  286.  
  287. public void hideLoggedIn() {
  288. projectNameText.setVisible(false);
  289. passwordText.setVisible(false);
  290. usernameText.setVisible(false);
  291. createUserButton.setVisible(false);
  292. createProjectButton.setVisible(false);
  293. startDatoText.setVisible(false);
  294. slutDatoText.setVisible(false);
  295. editUserButton.setVisible(false);
  296. editProjectButton.setVisible(false);
  297.  
  298. }
  299.  
  300. public void hideLogIn(){
  301. logoLabel.setVisible(false);
  302. loginButton.setVisible(false);
  303. }
  304.  
  305. public void hideEditProject(){
  306. cancelButton.setVisible(false);
  307. projectNameText.setVisible(false);
  308. }
  309.  
  310. public void hideEditUser() {
  311. cancelButton.setVisible(false);
  312. newPasswordText.setVisible(false);
  313. newPasswordLabel.setVisible(false);
  314. oldPasswordLabel.setVisible(false);
  315. }
  316. public void showLoggedIn() {
  317. projectNameText.setVisible(true);
  318. passwordText.setVisible(true);
  319. usernameText.setVisible(true);
  320. createUserButton.setVisible(true);
  321. createProjectButton.setVisible(true);
  322. startDatoText.setVisible(true);
  323. slutDatoText.setVisible(true);
  324. editUserButton.setVisible(true);
  325. editProjectButton.setVisible(true);
  326. }
  327.  
  328. public void showEditProject() {
  329. startDatoText.setVisible(true);
  330. slutDatoText.setVisible(true);
  331. cancelButton.setVisible(true);
  332. editProjectButton.setVisible(true);
  333. projectNameText.setVisible(true);
  334. }
  335.  
  336. public void showEditUser() {
  337. passwordText.setVisible(true);
  338. cancelButton.setVisible(true);
  339. editUserButton.setVisible(true);
  340. newPasswordText.setVisible(true);
  341. usernameText.setVisible(true);
  342. newPasswordLabel.setVisible(true);
  343. oldPasswordLabel.setVisible(true);
  344. }
  345. }
Add Comment
Please, Sign In to add comment