Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import javax.swing.table.*;
  4. import javax.swing.event.*;
  5. import java.awt.event.*;
  6. import java.awt.*;
  7. import java.io.*;
  8. import java.util.*;
  9. import java.sql.*;
  10.  
  11. //import net.miginfocom.swt.*;
  12. // required dimensions 900x618
  13. // interfaces travel from page 30 to page 33
  14. // opened when the Feedback button in the sidebar is clicked
  15.  
  16. public class Admin
  17. {
  18. JFrame container = new JFrame("Admin Dashboard");
  19. JPanel select_module = new JPanel(); // module selection screen
  20. JPanel select_assessment = new JPanel(); // assignment selection screen
  21. JPanel fb_sub_display = new JPanel(); // lecturers: submission screen students: display screen
  22. String username;
  23. String role; // roles: 1 = admin, 2 = student, 3 = module leader, 4 = teaching staff
  24.  
  25.  
  26. public Admin() throws Exception
  27. {
  28.  
  29. container.setSize(900, 618);
  30. container.setLayout(new FlowLayout());
  31. container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.  
  33. username = "Admin"; // default values for now; when integrating,
  34. role = "1"; // these'll be defined by the login procedure
  35.  
  36. selectModuleScreen(); // load GUI
  37.  
  38. /*
  39. username = "different";
  40. role = "1";
  41. selectModuleScreen();
  42. */
  43.  
  44. container.setVisible(true);
  45. }
  46.  
  47.  
  48. public void selectModuleScreen() throws Exception
  49. {
  50. // construction of module selection GUI
  51.  
  52. select_module.removeAll();
  53. select_module.revalidate(); // ensuring dynamically generated content is refreshed
  54.  
  55. select_module.setVisible(true);
  56. select_assessment.setVisible(false); // ensuring correct window is displayed
  57. fb_sub_display.setVisible(false);
  58.  
  59. select_module.setPreferredSize(new Dimension(875, 600));
  60. select_module.setLayout(new BorderLayout(20, 20));
  61.  
  62. JLabel modlabel = null;
  63. JButton newuser = new JButton("<html>Add New User");
  64. //newuser.setMaximumSize(new Dimension(100, 60));
  65.  
  66. if (role.equals("1")) // admin
  67. {
  68. modlabel = new JLabel("<html><body>Signed in as admin " + username
  69. + ".<br>Modules available:</body></html>");
  70. }
  71. else if (role.equals("2")) // student
  72. {
  73. modlabel = new JLabel("<html><body>Signed in as user " + username
  74. + ".<br>You are taking the following modules:</body></html>");
  75. }
  76. else if (role.equals("3") || role.equals("4")) // module leader or teacher
  77. {
  78. modlabel = new JLabel("<html><body>Signed in as user " + username
  79. + ".<br>You are delivering the following modules:</body></html>");
  80. }
  81.  
  82.  
  83. select_module.add(modlabel, BorderLayout.PAGE_START);
  84.  
  85. select_module.add(Box.createRigidArea(new Dimension(30, 200)), BorderLayout.LINE_START);
  86.  
  87. final JList modList = new JList(modulesTaken());
  88. modList.setSize(200, 500);
  89.  
  90. MouseListener mouseListener = new MouseAdapter()
  91. {
  92. public void mouseClicked(MouseEvent e)
  93. {
  94. try
  95. {
  96. if (e.getClickCount() == 2) // double click
  97. {
  98. int index = modList.locationToIndex(e.getPoint());
  99. selectAssessmentScreen(modList.getSelectedValue().toString()); // passing moduleID
  100. }
  101. }
  102. catch(Exception exc)
  103. { System.out.println(exc); }
  104. }
  105. };
  106. modList.addMouseListener(mouseListener);
  107.  
  108. JScrollPane scrollPane = new JScrollPane(modList);
  109. scrollPane.setSize(new Dimension(200, 500));
  110.  
  111. select_module.add(scrollPane, BorderLayout.CENTER);
  112. JPanel side = new JPanel();
  113. JLabel explanation = new JLabel("<html><body>Welcome, " + userRole(role) + " " + username
  114. + ".<br><br>Feedback on homework assignments "
  115. + "may be<br>accessed through this interface.<br><br>"
  116. + "Select a module from the list by double-clicking it.</body></html>");
  117.  
  118. side.setLayout(new BoxLayout(side, BoxLayout.Y_AXIS));
  119. side.add(explanation);side.add(newuser);
  120. //newuser.setAlignmentX(Component.LEFT_ALIGNMENT);
  121. select_module.add(side, BorderLayout.LINE_END);
  122.  
  123. //select_module.add(explanation, BorderLayout.LINE_END);
  124. select_module.add(Box.createRigidArea(new Dimension(0, 50)), BorderLayout.PAGE_END);
  125. //select_module.add(newuser, BorderLayout.AFTER_LAST_LINE);
  126. container.add(select_module);
  127. }
  128.  
  129.  
  130. String userRole(String role){
  131. if (role.equals("1")) // admin
  132. {
  133. return "admin";
  134. }
  135. else if (role.equals("2")) // student
  136. {
  137. return "student";
  138. }
  139. else // module leader or teacher
  140. {
  141. return "Lecturer";
  142. }
  143. }
  144. public void selectAssessmentScreen(String username) throws Exception
  145. {
  146. // construction of assessment selection GUI
  147. final String user = username;
  148. select_assessment.removeAll();
  149. select_assessment.revalidate();
  150.  
  151. select_module.setVisible(false);
  152. select_assessment.setVisible(true);
  153. fb_sub_display.setVisible(false);
  154.  
  155. // Fetch SQL data
  156. Class.forName("org.sqlite.JDBC");
  157. Connection conn = DriverManager.getConnection("jdbc:sqlite:database.sqlite");
  158. Statement stat = conn.createStatement();
  159.  
  160. ResultSet rs = stat.executeQuery(String.format("select * from users where username='%s'", username));
  161. JLabel assessmentLabel = null;
  162. assessmentLabel = new JLabel("<html><body>This is the edit users screen<br>"
  163. + "You reached here by selecting:" + username + "<br><br> RoleID: <br> <b>Admin: 1<br> Student: 2<br> Module Leader: 3 <br> Teaching Staff: 4 </b><br> ");
  164.  
  165. select_assessment.add(assessmentLabel, BorderLayout.PAGE_START);
  166.  
  167. JButton submit = new JButton("Submit");
  168. JButton back = new JButton("Back");
  169. select_assessment.setLayout(new BoxLayout(select_assessment, BoxLayout.Y_AXIS));
  170. select_assessment.setPreferredSize(new Dimension(300,300));
  171. JLabel userl = new JLabel("Username: " + rs.getString("username")), emaill = new JLabel("Password:"), welcome = new JLabel("<html><body><br>Edit User Details below<br></body></html>"), roleidl = new JLabel(""), pagebreak = new JLabel("<html><body><br></body></html>");
  172. final JTextField emailf = new JTextField(rs.getString("email")), roleidf = new JTextField(rs.getString("role_id"));
  173. select_assessment.add(roleidf);
  174. select_assessment.add(userl);
  175. select_assessment.add(pagebreak);
  176. select_assessment.add(emaill);select_assessment.add(emailf);
  177. select_assessment.add(pagebreak);
  178. select_assessment.add(submit);select_assessment.add(back);
  179. rs.close();
  180. conn.close();
  181. back.addActionListener(new ActionListener()
  182. {
  183. public void actionPerformed(ActionEvent e)
  184. {
  185. try
  186. {
  187. selectModuleScreen();
  188. }
  189. catch (Exception exc)
  190. {
  191. System.out.println("Invalid");
  192. System.out.println(exc);
  193. }
  194. }
  195. });
  196. submit.addActionListener(new ActionListener()
  197. {
  198. public void actionPerformed(ActionEvent e)
  199. {
  200. try
  201. {
  202. UpdateUserDetails(emailf.getText(), roleidf.getText(), user);
  203. JOptionPane.showMessageDialog(container, "Details Have been saved","Success",JOptionPane.ERROR_MESSAGE);
  204. }
  205. catch (Exception exc)
  206. {
  207. System.out.println("Invalid");
  208. System.out.println(exc);
  209. }
  210. }
  211. });
  212. container.add(select_assessment);
  213. }
  214. public void UpdateUserDetails(String email, String roleid, String username) throws Exception
  215. {
  216. Class.forName("org.sqlite.JDBC");
  217. Connection conn = DriverManager.getConnection("jdbc:sqlite:database.sqlite");
  218. Statement stat = conn.createStatement();
  219. PreparedStatement prep = conn.prepareStatement(
  220. "UPDATE users SET email = ?, role_id = ? WHERE username=?;");
  221. prep.setString(1, email);
  222. prep.setString(2, roleid);
  223. prep.setString(3, username);
  224. prep.addBatch();
  225. conn.setAutoCommit(false);
  226. prep.executeBatch();
  227. conn.setAutoCommit(true);
  228. conn.close();
  229. }
  230.  
  231. public String[] modulesTaken() throws Exception
  232. {
  233. Class.forName("org.sqlite.JDBC");
  234. Connection conn = DriverManager.getConnection("jdbc:sqlite:database.sqlite");
  235. Statement stat = conn.createStatement();
  236.  
  237. ResultSet rs = stat.executeQuery("select * from users;");
  238.  
  239. String[] rawModList = new String[200];
  240. int index = 0;
  241.  
  242. while (rs.next())
  243. {
  244. rawModList[index] = rs.getString("username");
  245. index++;
  246. }
  247.  
  248. rs.close();
  249. conn.close();
  250.  
  251. int numElements = 0;
  252.  
  253. for (int i = 0; i < 200; i++)
  254. {
  255. if (rawModList[i] != null)
  256. {
  257. numElements++;
  258. }
  259. }
  260.  
  261. String[] modList = new String[numElements];
  262. System.arraycopy(rawModList, 0, modList, 0, numElements);
  263.  
  264. return modList;
  265. }
  266.  
  267. public static void main(String[] args) throws Exception
  268. {
  269.  
  270. new Admin();
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement