import java.awt.Dimension; import java.awt.Color; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JTabbedPane; import javax.swing.JOptionPane; import java.io.File; import java.io.IOException; import java.net.URLConnection; import java.util.Properties; public class profileMakerGUI extends JPanel implements ActionListener { private final String strUploadScript = "http://www.demonisch.co.uk/profiles/cgi-bin/profileMakerUploadScript.pl"; public final int WIDTH = 500, HEIGHT = 300; private final Color clrBackground = new Color(75,141,221), clrForeground = new Color(255,255,255); private int newProfileFormCount = 1; private boolean isLoggedIn = false; private String strSessionID = ""; private JTabbedPane tabSystem; private profileMakerMenuSystem menu; private JFrame mainApp = null; public profileMakerGUI (JFrame mainApp) { super.setLayout(new BorderLayout()); this.setPreferredSize(new Dimension(WIDTH, HEIGHT)); this.setOpaque(true); menu = new profileMakerMenuSystem(this); tabSystem = new JTabbedPane(); this.add(tabSystem, BorderLayout.CENTER); } public JMenuBar getMenu () { return menu; } public void actionPerformed (ActionEvent e) { if (e.getActionCommand().equalsIgnoreCase("New")) { formProfile profileForm = new formProfile(mainApp); profileForm.setColours(clrBackground, clrForeground); tabSystem.addTab(("Untitled " + (newProfileFormCount++)), profileForm); if (tabSystem.getTabCount() == 1) //If this is the first tab opened, then change the menu to allow file operations { menu.setProfileOpen(true); } } else if (e.getActionCommand().equalsIgnoreCase("Open")) { pmReadXML read = new pmReadXML(mainApp); boolean success = false; try { success = read.open(); } catch (dialogueException ex) { ex.displayError(mainApp); } if (success) { formProfile profileForm = new formProfile(mainApp); profileForm.setColours(clrBackground, clrForeground); profileForm.openProfileFromFile(read.getProperties(), read.getFile()); tabSystem.addTab(profileForm.getTitle(), profileForm); tabSystem.setSelectedIndex(tabSystem.getTabCount() - 1); if (tabSystem.getTabCount() == 1) { menu.setProfileOpen(true); } } } else if (e.getActionCommand().equalsIgnoreCase("Save")) { saveFile(false, tabSystem.getSelectedIndex()); } else if (e.getActionCommand().equalsIgnoreCase("Save As")) { saveFile(true, tabSystem.getSelectedIndex()); } else if (e.getActionCommand().equalsIgnoreCase("Save All")) { for (int i = 0; i < tabSystem.getTabCount(); i++) { saveFile(true, i); } } else if (e.getActionCommand().equalsIgnoreCase("Close")) { int index = tabSystem.getSelectedIndex(); if (canCloseFile(index) == 1) { tabSystem.remove(index); } if (tabSystem.getTabCount() < 1) { menu.setProfileOpen(false); } } else if (e.getActionCommand().equalsIgnoreCase("Close All")) { closeAll(); if (tabSystem.getTabCount() < 1) { menu.setProfileOpen(false); } } else if (e.getActionCommand().equalsIgnoreCase("Exit")) { closeAll(); if (tabSystem.getTabCount() < 1) { System.exit(0); } } else if (e.getActionCommand().equalsIgnoreCase("Login")) { pmFormLogin loginForm = new pmFormLogin(mainApp); loginForm.setVisible(true); //If successful login if (loginForm.isLoggedIn()) { loginAccount(loginForm.getSessionID()); } } else if (e.getActionCommand().equalsIgnoreCase("Register")) { pmFormRegister registerForm = new pmFormRegister(mainApp); registerForm.setVisible(true); //Successful register if (registerForm.isLoggedIn()) { loginAccount(registerForm.getSessionID()); } } else if (e.getActionCommand().equalsIgnoreCase("Upload Profile")) { formProfile profileForm = (formProfile)tabSystem.getSelectedComponent(); Properties propReply = null; boolean uploadAttempted = false; try { propReply = profileForm.uploadProfileToWebsite(strSessionID, strUploadScript); } catch (dialogueException ex) { ex.displayError(mainApp); } if (propReply != null) { if (propReply.getProperty("uploadSuccess").equalsIgnoreCase("true")) { JOptionPane.showMessageDialog(mainApp, "The profile was successfully uploaded and can be viewed at\n" + propReply.getProperty("url"), "Upload Complete", JOptionPane.INFORMATION_MESSAGE); } else { //If an error occurs a flag is returned saying if should be logged out or not if (propReply.getProperty("logout").equalsIgnoreCase("true")) { setLoggedIn(false); strSessionID = ""; } JOptionPane.showMessageDialog(mainApp, (String)propReply.getProperty("errorMsg"), (String)propReply.getProperty("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); } } } else if (e.getActionCommand().equalsIgnoreCase("Logout")) { setLoggedIn(false); strSessionID = ""; } } private void closeAll () { int choice = 0; for (int i = (tabSystem.getTabCount() - 1); i >= 0; i--) { choice = canCloseFile(i); if (choice == 1) { tabSystem.remove(i); } else if (choice == -1) { break; } } } //1 = yes, 0 = no, -1 cancel private int canCloseFile (int index) { formProfile profileForm = (formProfile)tabSystem.getComponentAt(index); tabSystem.setSelectedIndex(index); String strTitle = profileForm.getTitle(); if (profileForm.needsSaving()) { int choice = JOptionPane.showConfirmDialog(mainApp, "Do you wish to save the changes made to profile\n" + strTitle, "Confirm Save", JOptionPane.YES_NO_CANCEL_OPTION); if (choice == JOptionPane.YES_OPTION) { if (!saveFile(false, index)) { return 0; } } else if (choice == JOptionPane.CANCEL_OPTION) { return -1; } } return 1; } //If saveAs is true it will ask for a new filename private boolean saveFile (boolean saveAs, int index) { formProfile profileForm = (formProfile)tabSystem.getComponentAt(index); try { profileForm.saveProfileToFile(saveAs); tabSystem.setTitleAt(index, profileForm.getTitle()); return true; } catch (dialogueException ex) { ex.displayError(mainApp); return false; } } private void loginAccount (String strSessionID) { setLoggedIn(true); this.strSessionID = strSessionID; } private void setLoggedIn (boolean isLoggedIn) { this.isLoggedIn = isLoggedIn; menu.setLoggedIn(isLoggedIn); } }