package Application;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import Assingment.User;
import Assingment.NormalUser;
import Assingment.AdminUser;
import Assingment.UserList;
import Assingment.BusinessManager;
import Assingment.UserHandler;
/**
*
* @author Tristan
*/
public class UserManagement extends javax.swing.JFrame {
private BusinessManager theBM;
private UserList users;
private AdminUser adminUsers;
private NormalUser normalUsers;
private DefaultComboBoxModel userModel;
private File fileName;
private String[] userNames;
/** Creates new form UserManagement */
public UserManagement() {
createDM();
createUserCmb();
initComponents();
// Create the File object to hold the file name
fileName = new File("C:\\Users\\Tristan\\Desktop\\edited\\Assingment 2010-01-16 at 16.11.38", "UserData.xml");
// Create the data in a Branch object, then get the
// account numbers and place them in the combo box.
getData();
userNames = users.getUserNames();
userModel = new DefaultComboBoxModel(userNames);
// NetBeans method to initialise GUI components
// Show the data for the first Users
cmbUsers.setSelectedIndex(0);
showUsersDetails();
}
private void getData() {
// Set up the file details and create the parser, then start it.
// On completion, retrieve the accounts and display them.
DefaultHandler handler = new UserHandler();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(fileName, handler);
} catch (Exception err) {
System.out.println("Exception: " + err.getMessage());
err.printStackTrace();
}
// On completion of the parsing process, get the branch object
// created by the account handler
users = ((UserHandler) handler).getUserlist();
}
private void createDM() {
// Creates the model through test data
generateTD();
}
private void createUserCmb() {
String[] userNames = users.getUserNames();
userModel = new DefaultComboBoxModel(userNames);
}
private void generateTD() {
// This method creates the data model from hard-coded test data.
NormalUser nUser;
AdminUser aUser;
// Create the object that controls the data model
theBM = new BusinessManager();
// allows the GUI to access the objects they contain.
users = theBM.getUsers();
aUser = new AdminUser("Tristan Parker", "taparker", "tristan@parker.com", 2);
users.addUser(aUser);
nUser = new NormalUser("Caroline Williams", "cmwillaims", "caroline@williams.com", 2);
users.addUser(nUser);
}
// Event Handlers
private void showUsersDetails() {
User selected = users.getUserAt(cmbUsers.getSelectedIndex());
txtEmailAddress.setText(selected.getEmail());
txtUsername.setText(selected.getUserID());
if (selected instanceof AdminUser) {
radAdmin.setSelected(true);
} else {
radNormal.setSelected(true);
}
}
private void addUserDetails() {
AddUserGUI createUser;
User user;
user =new AdminUser();
user =new NormalUser();
createUser =new AddUserGUI(this, true, user);
createUser.setVisible(true);
if (createUser.getCloseButton().equals("OK")) {
users.addUser(user);
userModel.addElement(user.getName());
cmbUsers.setSelectedIndex(users.size() - 1);
}
}
private void updateUser() {
int selectedIndex = cmbUsers.getSelectedIndex();
if (selectedIndex == -1) {
JOptionPane.showMessageDialog(null,
"User not selected!", "Error",
JOptionPane.ERROR_MESSAGE);
} else {
AddUserGUI augUpdate;
User selected = users.getUserAt(selectedIndex);
augUpdate =
new AddUserGUI(this, true, selected);
augUpdate.setVisible(true);
// On return from the dialog, rebuild model in case
// the name has changed
if (augUpdate.getCloseButton().equals("OK")) {
userModel = new DefaultComboBoxModel(users.getUserNames());
cmbUsers.setModel(userModel);
cmbUsers.setSelectedIndex(selectedIndex);
}
}
}
private void deleteUser() {
int confirmed = JOptionPane.showConfirmDialog(null,
"Are you sure you want to delete this user ",
"Confirm closure!", JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
users.removeUserAt(cmbUsers.getSelectedIndex());
userModel.removeElementAt(cmbUsers.getSelectedIndex());
}
}
// Save and exit
private void saveAndExit() {
FileOutputStream outStream;
PrintWriter pw;
User user;
// Create stream and write the DTD
try {
outStream = new FileOutputStream(fileName);
pw = new PrintWriter(outStream);
// write out DTD
pw.println("<?xml version = \"1.0\" standalone = \"yes\"?>");
pw.println("<!DOCTYPE Users [");
pw.println(" <!ELEMENT Users (aUser, nUser)+ >");
pw.println(" <!ELEMENT aUser (name, userID, emailAddress) >");
pw.println(" <!ELEMENT nUser (name, userID, emailAddress) >");
pw.println(" <!ELEMENT name (#PCDATA) >");
pw.println(" <!ELEMENT userID (#PCDATA) >");
pw.println(" <!ELEMENT emailAddress (#PCDATA) >");
pw.println("] >");
// Write out the data
pw.println("<Users>");
// Write out the xml representation of each account object.
// It doesn't matter which type of account it is - because
// BankAccount has a getXML() method as well as the sub-classes
// having the same signature methods, it will use whichever is
// appropriate to the current object.
for (int ct = 0; ct < users.size(); ct++) {
user = users.getUserAt(ct);
pw.println(user.getXML());
}
pw.println("</Users>");
// Close the file
pw.close();
} catch (IOException err) {
err.printStackTrace();
}
// Finally, close the program.
System.exit(0);
}