import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Color; import java.awt.Image; import java.awt.Dimension; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.BorderFactory; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JFrame; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JTextArea; import javax.swing.JScrollPane; import java.io.IOException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.BufferedOutputStream; import java.net.URLConnection; import java.util.Properties; public class formProfile extends JPanel implements ActionListener { //Keeps a reference to where this file is saved on the disk private File file = null; private JFrame mainApp; private String strSavedName = ""; private String strSavedEmail = ""; private String strSavedAge = ""; private String strSavedDes = ""; //Used to check if the file needs saving or not private String strSavedImageLocation = ""; private String strImageLocation = ""; //The location of the last valid image private String strNewImageLocation = ""; //The location of the current picture being browsed for private JLabel lblName, lblEmail, lblAge, lblImage, lblDes; private JTextField txtName, txtEmail, txtAge; private JTextArea txtDes; private JButton cmdBrowse, cmdRemove; private JPanel panRoot, panProfileText, panGrid, panNorthEast, panNorthWest, panImage, panBrowse, panDes; public formProfile (JFrame mainApp) { this.mainApp = mainApp; lblName = new JLabel("Name: ", JLabel.RIGHT); lblName.setToolTipText("Name must only consist of letters and spaces"); lblEmail = new JLabel("E-mail: ", JLabel.RIGHT); lblEmail.setToolTipText("Must be in the format someone@somewhere.extension"); lblAge = new JLabel("Age: ", JLabel.RIGHT); lblAge.setToolTipText("Enter an age between 10 and 120"); lblDes = new JLabel("description", JLabel.CENTER); lblDes.setToolTipText("Describe yourself in under 1,000 characters"); lblImage = new JLabel(""); lblImage.setToolTipText("This is how the image will look on the webpage"); lblImage.setPreferredSize(new Dimension(200, 200)); txtName = new JTextField("", 10); txtEmail = new JTextField("", 10); txtAge = new JTextField("", 2); txtDes = new JTextArea("", 6, 20); txtDes.setLineWrap(true); txtDes.setWrapStyleWord(true); JScrollPane containerDes = new JScrollPane(txtDes, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); cmdBrowse = new JButton("Browse..."); cmdRemove = new JButton("Remove"); cmdBrowse.setToolTipText("Find an image from disk to upload to the website"); cmdRemove.setToolTipText("Remove image from your profile"); cmdRemove.setEnabled(false); cmdBrowse.addActionListener(this); cmdRemove.addActionListener(this); panRoot = new JPanel(new BorderLayout()); panProfileText = new JPanel(); panNorthWest = new JPanel(new BorderLayout()); panNorthEast = new JPanel(new BorderLayout()); panImage = new JPanel(); panDes = new JPanel(new BorderLayout()); panBrowse = new JPanel(); panGrid = new JPanel(new GridLayout(3, 2, 2, 2)); this.add(panRoot, BorderLayout.CENTER); panRoot.add(panProfileText, BorderLayout.WEST); panProfileText.add(panNorthWest); panNorthWest.add(panGrid, BorderLayout.NORTH); panGrid.add(lblName); panGrid.add(txtName); panGrid.add(lblEmail); panGrid.add(txtEmail); panGrid.add(lblAge); panGrid.add(txtAge); panNorthWest.add(panDes, BorderLayout.SOUTH); panDes.add(lblDes, BorderLayout.NORTH); panDes.add(containerDes, BorderLayout.SOUTH); panRoot.add(panNorthEast, BorderLayout.EAST); panNorthEast.add(panImage, BorderLayout.NORTH); panImage.add(lblImage); panNorthEast.add(panBrowse, BorderLayout.SOUTH); panBrowse.add(cmdBrowse); panBrowse.add(cmdRemove); panRoot.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); panGrid.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 4)); panDes.setBorder(BorderFactory.createEmptyBorder(12, 1, 1, 0)); lblImage.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 1)); } public void setColours (Color clrBackground, Color clrForeground) { this.setBackground(clrBackground); panRoot.setBackground(clrBackground); panProfileText.setBackground(clrBackground); panNorthWest.setBackground(clrBackground); panNorthEast.setBackground(clrBackground); panImage.setBackground(clrBackground); panBrowse.setBackground(clrBackground); panGrid.setBackground(clrBackground); panDes.setBackground(clrBackground); lblName.setForeground(clrForeground); lblAge.setForeground(clrForeground); lblEmail.setForeground(clrForeground); lblDes.setForeground(clrForeground); } public void openProfileFromFile (Properties props, File file) { this.file = file; setupOpenedProfile(props); if (!strNewImageLocation.equals("")) { setupImage(); } } private void setupOpenedProfile (Properties props) { strSavedName = props.getProperty("name"); strSavedEmail = props.getProperty("email"); strSavedAge = props.getProperty("age"); strSavedDes = props.getProperty("des"); strNewImageLocation = props.getProperty("pic"); strSavedImageLocation = strNewImageLocation; txtName.setText(strSavedName); txtEmail.setText(strSavedEmail); txtAge.setText(strSavedAge); txtDes.setText(strSavedDes); isDataValid(); } private void setupImage () { byte[] imageData = getImageFromFile(); if (imageData != null) { Image imgOrg = dataManager.createImage(imageData); lblImage.setIcon(new ImageIcon(imgOrg.getScaledInstance(200, 200, Image.SCALE_AREA_AVERAGING)));//Creates a scaled image filter and applies it cmdRemove.setEnabled(true); strImageLocation = strNewImageLocation; } } private byte[] getImageFromFile () { try {//Make sure the image is under 100kb big, is an image etc return pmValidData.getValidImageBytes(new File(strNewImageLocation), 102400); } catch (dialogueException ex) { ex.displayError(); } return null; } public String getTitle () { if (strSavedName.equals("")) { return "Unnamed profile"; } return strSavedName; } public boolean saveProfileToFile (boolean saveAs) throws dialogueException { File saveFile = null; if (!isDataValid()) { throw new dialogueException("Can not save this profile because it contains errors", "Invalid Data", JOptionPane.WARNING_MESSAGE); } if (!saveAs) { saveFile = this.file; } saveFile = pmWriteXML.save(mainApp, saveFile, getProfileAsProperties()); if (saveFile != null) { this.file = saveFile; strSavedName = txtName.getText(); strSavedEmail = txtEmail.getText(); strSavedAge = txtAge.getText(); strSavedDes = txtDes.getText(); strSavedImageLocation = strImageLocation; return true; } return false; } public Properties uploadProfileToWebsite (String strSessionID, String strUploadScript) throws dialogueException { Properties props = getProfileAsProperties(); props.setProperty("sessionID", strSessionID); String strImageUpload = ""; if (!isDataValid()) { throw new dialogueException("Can not upload this profile because it contains errors", "Invalid Data", JOptionPane.WARNING_MESSAGE); } if (strImageLocation.equals("")) { int choice = JOptionPane.showConfirmDialog(mainApp, "You have not selected an image to upload\nDo you wish to keep the existing picture in your profile?\n\nNo will delete the current picture from your profile", "Keep profile picture", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if (choice == JOptionPane.CANCEL_OPTION) { return null; } else if (choice == JOptionPane.YES_OPTION) { strImageUpload = "leave"; } else { strImageUpload = "delete"; } } else if (!strImageLocation.equals("")) { strImageUpload = "upload"; } URLConnection connection = null; try { connection = pmConnectionManager.getConnection(strUploadScript, true, true); } catch (Exception ex) { throw new dialogueException(ex.getMessage(), "Could not connect", JOptionPane.ERROR_MESSAGE); } props.setProperty("imageUpload", strImageUpload); try { //Not uploading an image if (strImageUpload.equals("leave") || strImageUpload.equals("delete")) { return pmConnectionManager.sendAndRecTextData(connection, props); } else { //getImageFromFile() uses strNewImageLocation, but for the upload want to use the valid image String strTemp = strNewImageLocation; //So save the current new location, then use the valid image strNewImageLocation = strImageLocation; byte[] imageData = getImageFromFile(); strNewImageLocation = strTemp; //Then restore the old value if (imageData == null) { throw new dialogueException("The image could not be found, possibly because it has been deleted, renamed or moved", "Broken Image", JOptionPane.ERROR_MESSAGE); } scriptUploadData uploadData = new scriptUploadData(1); uploadData.put(imageData, "file", strImageLocation, "image/" + pmValidData.getFileExt(strImageLocation)); return pmConnectionManager.sendAndRecMultiData(connection, props, uploadData); } } catch (IOException ex) { throw new dialogueException("An error occured when trying to upload the profile\n" + ex.getMessage(), "Upload Error", JOptionPane.ERROR_MESSAGE); } } private Properties getProfileAsProperties () { Properties props = new Properties(); props.setProperty("name", txtName.getText()); props.setProperty("email", txtEmail.getText()); props.setProperty("age", txtAge.getText()); props.setProperty("des", txtDes.getText()); props.setProperty("pic", strImageLocation); return props; } public boolean isDataValid () { boolean nameValid = false; boolean emailValid = false; boolean desValid = false; boolean ageValid = false; if (txtAge.getText().equals("") || pmValidData.getValidAge(txtAge.getText(), 10, 120) != -1) { ageValid = true; } else { JOptionPane.showMessageDialog(mainApp, "The age is not valid\nAge must be a numeric value between 10 and 120", "Invalid Age", JOptionPane.WARNING_MESSAGE); } if (txtEmail.getText().equals("") || (pmValidData.isValidEmail(txtEmail.getText()) && txtEmail.getText().length() >= 6 && txtEmail.getText().length() <= 40)) { emailValid = true; } else { JOptionPane.showMessageDialog(mainApp, "E-mail addresses must follow the format someone@somewhere.extension\nIt must be between 6 and 40 characters long", "Invalid E-mail", JOptionPane.WARNING_MESSAGE); } if (txtName.getText().equals("") || pmValidData.isValidName(txtName.getText())) { nameValid = true; } else { JOptionPane.showMessageDialog(mainApp, "The name must contain letters and spaces\nand be between 2 to 20 characters long", "Invalid Name", JOptionPane.WARNING_MESSAGE); } if (txtDes.getText().length() <= 1000) { desValid = true; } else { JOptionPane.showMessageDialog(mainApp, "The profile description must be 1,000 characters or less", "Invalid Des", JOptionPane.WARNING_MESSAGE); } //Returns true only if all of the variables are true return (nameValid && emailValid && desValid && ageValid); } public boolean needsSaving () { String strTempAge = ""; if (!(txtAge.getText().equals("") && strSavedAge.equals(""))) { strTempAge = Integer.toString(pmValidData.getValidAge(txtAge.getText(), -1, -1)); } return (!txtName.getText().equals(strSavedName) || !txtEmail.getText().equals(strSavedEmail) || !txtDes.getText().equals(strSavedDes) || !strTempAge.equals(strSavedAge) || !strSavedImageLocation.equals(strImageLocation)); } private boolean openImageFromDisk () { File fileDir = null; if (strNewImageLocation.equals("")) { fileDir = new File(System.getProperty("user.dir")); } else { fileDir = new File(strNewImageLocation); if (!fileDir.exists()) //If the location does not exist { fileDir = new File(System.getProperty("user.dir")); } } JFileChooser fileOpen = new JFileChooser(); fileOpen.addChoosableFileFilter(new fileTypeImage()); fileOpen.setAcceptAllFileFilterUsed(false); fileOpen.setCurrentDirectory(fileDir); fileOpen.setApproveButtonToolTipText("Select this picture"); fileOpen.setDialogType(JFileChooser.OPEN_DIALOG); fileOpen.setDialogTitle("Select Picture"); int returnValue = fileOpen.showDialog(mainApp, "Select"); if (returnValue == JFileChooser.APPROVE_OPTION) { strNewImageLocation = fileOpen.getSelectedFile().getAbsolutePath(); return true; } return false; } public void actionPerformed (ActionEvent e) { if (e.getSource() == cmdRemove) { strImageLocation = ""; lblImage.setIcon(null); cmdRemove.setEnabled(false); } else if (e.getSource() == cmdBrowse) { if (openImageFromDisk()) { setupImage(); } } } }