import java.awt.Dimension; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JFrame; import javax.swing.BorderFactory; import java.net.URLConnection; import java.net.URL; import java.net.MalformedURLException; import java.io.IOException; import java.util.Properties; import java.util.Calendar; public class pmAbstractFormGetAccount extends JDialog implements ActionListener { protected final String strBase = "http://www.demonisch.co.uk/profiles/cgi-bin/"; protected final Color clrTitledBorder = new Color(0,0,0); protected String strSessionID = ""; protected boolean isLoggedIn = false; protected JFrame mainApp = null; protected JLabel lblAlias, lblPass; protected JTextField txtAlias; protected JPasswordField txtPass; protected JButton cmdLogin, cmdCancel; protected JPanel panRoot, panGrid, panSouth; protected boolean isLoggedIn () { return isLoggedIn; } protected String getSessionID () { return strSessionID; } protected boolean testpmValidData (String strAlias, String strPass) { /*if(true) //for debug { return true; }*/ if (!pmValidData.containsValidChars(strAlias, true, false, "")) { JOptionPane.showMessageDialog(mainApp, "The alias may only contain letters", "Invalid Alias", JOptionPane.WARNING_MESSAGE); } else if (strAlias.length() < 4 || strAlias.length() > 15) { JOptionPane.showMessageDialog(mainApp, "The alias must be between 4 and 15 characters long", "Invalid Alias", JOptionPane.WARNING_MESSAGE); } else if (!pmValidData.containsValidChars(strPass, true, true, "")) { JOptionPane.showMessageDialog(mainApp, "The password may only contain letters and numbers", "Invalid Password", JOptionPane.WARNING_MESSAGE); } else if (strPass.length() < 6 || strPass.length() > 20) { JOptionPane.showMessageDialog(mainApp, "The password must be between 6 and 20 characters long", "Invalid Password", JOptionPane.WARNING_MESSAGE); } else { return true; } return false; } public pmAbstractFormGetAccount (JFrame mainApp, String strTitle, Dimension dimSize) { super(mainApp, true); this.mainApp = mainApp; this.setTitle(strTitle); this.setSize(dimSize); this.setResizable(false); this.setLayout(new BorderLayout()); lblAlias = new JLabel("Alias: ", JLabel.RIGHT); lblPass = new JLabel("Pass: ", JLabel.RIGHT); txtAlias = new JTextField("", 5); txtPass = new JPasswordField("", 5); cmdLogin = new JButton(""); cmdCancel = new JButton("Cancel"); cmdLogin.addActionListener(this); cmdCancel.addActionListener(this); panRoot = new JPanel(new BorderLayout()); panSouth = new JPanel(); this.add(panRoot, BorderLayout.CENTER); panRoot.add(panSouth, BorderLayout.SOUTH); panSouth.add(cmdLogin); panSouth.add(cmdCancel); panRoot.setBorder(BorderFactory.createEmptyBorder(2,2,2,2)); } public void actionPerformed (ActionEvent e) { } private String createSessionID () { Calendar calNow = Calendar.getInstance(); return "" + calNow.getTimeInMillis() + getRandomString(); } private int getRandomNum (int length) { return ((1 + (int)(Math.random() * length)) - 1); } private String getRandomString () { String strRandom = ""; String strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; int wordLength = 10; for (int i = 0; i < wordLength; i++) { strRandom += strLetters.charAt(getRandomNum(strLetters.length())); } return strRandom; } protected Properties performConnection (String strScriptHandler, String strAlias, String strPass) throws dialogueException { strSessionID = createSessionID(); Properties props = new Properties(); props.setProperty("sessionID", strSessionID); props.setProperty("alias", strAlias); props.setProperty("pass", strPass); URLConnection connection = null; try { connection = pmConnectionManager.getConnection(strScriptHandler, true, true); } catch (Exception ex) { throw new dialogueException(ex.getMessage(), "Connection could not be made", JOptionPane.ERROR_MESSAGE); } try { return pmConnectionManager.sendAndRecTextData(connection, props); } catch (IOException ex) { throw new dialogueException("An error occured when trying to connect to the accounts script\n" + ex.getMessage(), "Could not get account details", JOptionPane.ERROR_MESSAGE); } } }