Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.table.*;
- import javax.swing.event.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.io.*;
- import java.util.*;
- import java.sql.*;
- //import net.miginfocom.swt.*;
- // required dimensions 900x618
- // interfaces travel from page 30 to page 33
- // opened when the Feedback button in the sidebar is clicked
- public class Admin
- {
- JFrame container = new JFrame("Admin Dashboard");
- JPanel select_module = new JPanel(); // module selection screen
- JPanel select_assessment = new JPanel(); // assignment selection screen
- JPanel fb_sub_display = new JPanel(); // lecturers: submission screen students: display screen
- String username;
- String role; // roles: 1 = admin, 2 = student, 3 = module leader, 4 = teaching staff
- public Admin() throws Exception
- {
- container.setSize(900, 618);
- container.setLayout(new FlowLayout());
- container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- username = "Admin"; // default values for now; when integrating,
- role = "1"; // these'll be defined by the login procedure
- selectModuleScreen(); // load GUI
- /*
- username = "different";
- role = "1";
- selectModuleScreen();
- */
- container.setVisible(true);
- }
- public void selectModuleScreen() throws Exception
- {
- // construction of module selection GUI
- select_module.removeAll();
- select_module.revalidate(); // ensuring dynamically generated content is refreshed
- select_module.setVisible(true);
- select_assessment.setVisible(false); // ensuring correct window is displayed
- fb_sub_display.setVisible(false);
- select_module.setPreferredSize(new Dimension(875, 600));
- select_module.setLayout(new BorderLayout(20, 20));
- JLabel modlabel = null;
- JButton newuser = new JButton("<html>Add New User");
- //newuser.setMaximumSize(new Dimension(100, 60));
- if (role.equals("1")) // admin
- {
- modlabel = new JLabel("<html><body>Signed in as admin " + username
- + ".<br>Modules available:</body></html>");
- }
- else if (role.equals("2")) // student
- {
- modlabel = new JLabel("<html><body>Signed in as user " + username
- + ".<br>You are taking the following modules:</body></html>");
- }
- else if (role.equals("3") || role.equals("4")) // module leader or teacher
- {
- modlabel = new JLabel("<html><body>Signed in as user " + username
- + ".<br>You are delivering the following modules:</body></html>");
- }
- select_module.add(modlabel, BorderLayout.PAGE_START);
- select_module.add(Box.createRigidArea(new Dimension(30, 200)), BorderLayout.LINE_START);
- final JList modList = new JList(modulesTaken());
- modList.setSize(200, 500);
- MouseListener mouseListener = new MouseAdapter()
- {
- public void mouseClicked(MouseEvent e)
- {
- try
- {
- if (e.getClickCount() == 2) // double click
- {
- int index = modList.locationToIndex(e.getPoint());
- selectAssessmentScreen(modList.getSelectedValue().toString()); // passing moduleID
- }
- }
- catch(Exception exc)
- { System.out.println(exc); }
- }
- };
- modList.addMouseListener(mouseListener);
- JScrollPane scrollPane = new JScrollPane(modList);
- scrollPane.setSize(new Dimension(200, 500));
- select_module.add(scrollPane, BorderLayout.CENTER);
- JPanel side = new JPanel();
- JLabel explanation = new JLabel("<html><body>Welcome, " + userRole(role) + " " + username
- + ".<br><br>Feedback on homework assignments "
- + "may be<br>accessed through this interface.<br><br>"
- + "Select a module from the list by double-clicking it.</body></html>");
- side.setLayout(new BoxLayout(side, BoxLayout.Y_AXIS));
- side.add(explanation);side.add(newuser);
- //newuser.setAlignmentX(Component.LEFT_ALIGNMENT);
- select_module.add(side, BorderLayout.LINE_END);
- //select_module.add(explanation, BorderLayout.LINE_END);
- select_module.add(Box.createRigidArea(new Dimension(0, 50)), BorderLayout.PAGE_END);
- //select_module.add(newuser, BorderLayout.AFTER_LAST_LINE);
- container.add(select_module);
- }
- String userRole(String role){
- if (role.equals("1")) // admin
- {
- return "admin";
- }
- else if (role.equals("2")) // student
- {
- return "student";
- }
- else // module leader or teacher
- {
- return "Lecturer";
- }
- }
- public void selectAssessmentScreen(String username) throws Exception
- {
- // construction of assessment selection GUI
- final String user = username;
- select_assessment.removeAll();
- select_assessment.revalidate();
- select_module.setVisible(false);
- select_assessment.setVisible(true);
- fb_sub_display.setVisible(false);
- // Fetch SQL data
- Class.forName("org.sqlite.JDBC");
- Connection conn = DriverManager.getConnection("jdbc:sqlite:database.sqlite");
- Statement stat = conn.createStatement();
- ResultSet rs = stat.executeQuery(String.format("select * from users where username='%s'", username));
- JLabel assessmentLabel = null;
- assessmentLabel = new JLabel("<html><body>This is the edit users screen<br>"
- + "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> ");
- select_assessment.add(assessmentLabel, BorderLayout.PAGE_START);
- JButton submit = new JButton("Submit");
- JButton back = new JButton("Back");
- select_assessment.setLayout(new BoxLayout(select_assessment, BoxLayout.Y_AXIS));
- select_assessment.setPreferredSize(new Dimension(300,300));
- 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>");
- final JTextField emailf = new JTextField(rs.getString("email")), roleidf = new JTextField(rs.getString("role_id"));
- select_assessment.add(roleidf);
- select_assessment.add(userl);
- select_assessment.add(pagebreak);
- select_assessment.add(emaill);select_assessment.add(emailf);
- select_assessment.add(pagebreak);
- select_assessment.add(submit);select_assessment.add(back);
- rs.close();
- conn.close();
- back.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- try
- {
- selectModuleScreen();
- }
- catch (Exception exc)
- {
- System.out.println("Invalid");
- System.out.println(exc);
- }
- }
- });
- submit.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- try
- {
- UpdateUserDetails(emailf.getText(), roleidf.getText(), user);
- JOptionPane.showMessageDialog(container, "Details Have been saved","Success",JOptionPane.ERROR_MESSAGE);
- }
- catch (Exception exc)
- {
- System.out.println("Invalid");
- System.out.println(exc);
- }
- }
- });
- container.add(select_assessment);
- }
- public void UpdateUserDetails(String email, String roleid, String username) throws Exception
- {
- Class.forName("org.sqlite.JDBC");
- Connection conn = DriverManager.getConnection("jdbc:sqlite:database.sqlite");
- Statement stat = conn.createStatement();
- PreparedStatement prep = conn.prepareStatement(
- "UPDATE users SET email = ?, role_id = ? WHERE username=?;");
- prep.setString(1, email);
- prep.setString(2, roleid);
- prep.setString(3, username);
- prep.addBatch();
- conn.setAutoCommit(false);
- prep.executeBatch();
- conn.setAutoCommit(true);
- conn.close();
- }
- public String[] modulesTaken() throws Exception
- {
- Class.forName("org.sqlite.JDBC");
- Connection conn = DriverManager.getConnection("jdbc:sqlite:database.sqlite");
- Statement stat = conn.createStatement();
- ResultSet rs = stat.executeQuery("select * from users;");
- String[] rawModList = new String[200];
- int index = 0;
- while (rs.next())
- {
- rawModList[index] = rs.getString("username");
- index++;
- }
- rs.close();
- conn.close();
- int numElements = 0;
- for (int i = 0; i < 200; i++)
- {
- if (rawModList[i] != null)
- {
- numElements++;
- }
- }
- String[] modList = new String[numElements];
- System.arraycopy(rawModList, 0, modList, 0, numElements);
- return modList;
- }
- public static void main(String[] args) throws Exception
- {
- new Admin();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement