I'll first give you a pseudo version so you can see what the code is doing.
A user opens up the email screen. Each email is assigned a number. When the user attaches files, the files are renamed according to the email number. When the user sends the message, the attachment files are renamed in the same location to reflect the email number and the attachment number within that email. (I have come up with a different way to approach this. I'm going to try renaming the files to the correct name from the start. But I still would like to know why this gives the java.exe error)
Code for when attachments are added to the email:
private void handleAttach(){
fileChooser = new LFileChooser();
fileChooser.setApproveButtonText(Utils.getLabel
("Buttons_FileChooser_Attach"));
int option = fileChooser.showOpenDialog(this);
if(option == JFileChooser.APPROVE_OPTION){
if(fileChooser.getSelectedFile()!=null){
File destinationFile = new File(FolderLocations.MailAttachmentsLOCATION, "out"+thisEmail.getEmailNumber()+"_"+fileChooser.getSelectedFile().getName());
File sourceFile = fileChooser.getSelectedFile();
moveFiles(destinationFile, sourceFile);
}
}
}
//moveFiles moves the files from the source to the destination.
Code when the email gets sent:
public void save(){
//big date thing is for calculating the sting of the datetime the email was sent
Calendar timeNow = Calendar.getInstance();
int day = timeNow.get(Calendar.DAY_OF_MONTH);
int month = timeNow.get(Calendar.MONTH) +1;
int year = timeNow.get(Calendar.YEAR);
int hour = timeNow.get(Calendar.HOUR_OF_DAY);
int min = timeNow.get(Calendar.MINUTE);
int second = timeNow.get(Calendar.SECOND);
String days;if(day<10) days = "0"+day;else days = day+"";
String months;if(month<10) months = "0"+month;else months = month+"";
String hours;if(hour<10) hours = "0"+hour;else hours = hour+"";
String mins;if(min<10) mins = "0"+min;else mins = min+"";
String seconds;if(second<10) seconds = "0"+second;else seconds = second+"";
dateTime = days+"/"+months+"/"+year+" "+hours+":"+mins+":"+seconds;
finalizeAttachments();
EmailHandler eh = new EmailHandler(this);
eh.sendToOutbox();
//sendToOutbox - writes a line in a text file and saves the next email number to a properties file (two different files).
}
public void finalizeAttachments(){
int attachmentNumber = 0;
String attachNumber = "";
for(int i=0; i<emailAttachmentsVector.size(); i++){
File tempFile = (File)emailAttachmentsVector.get(i);
String tempFileNameOrig = tempFile.getName();
String tempFileNameNew = tempFileNameOrig.substring(tempFileNameOrig.indexOf("_")+1, tempFileNameOrig.length());
String tempFileName = "out"+thisEmailNumber+"l"+i+"_"+tempFileNameNew;
File newFile = new File(tempFile.getParent(), tempFileName);
boolean success = tempFile.renameTo(newFile);
System.out.println(success+" "+newFile.getPath());
attachmentNumber = i+1;
if(attachmentNumber < 10) attachNumber = "0"+attachmentNumber;
else attachNumber = attachmentNumber+"";
attachments += tempFileNameNew+":"+attachNumber;
if(attachmentNumber<emailAttachmentsVector.size()) attachments += "|";
}
}
If there's more that you would like, just ask.
Cheers.
Rachel