package jtodo.app;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Locale;
import jtodo.ui.DefaultUITodoEntry;
import jtodo.ui.Severity;
/**
* Contains the methods loadfile and savefile to load and save Todo entries in
* and from a file.
*/
public class TodoEntryIO {
/**
* Loads to-do entries from the file that is specified by the file name
* given. Reading the file line for line, splitting the lines up and saving
* the splits in an array, after that creates a new object of the type
* "DefaultTodoEntry with the saved informations.
*
* @param fileName
* To load to-do entries from
* @throws IOException
* if loading the file fails.
*/
public static void loadfile(String fileName) throws ApplicationException {
System.out.println("load: NIY");
try {
BufferedReader input = new BufferedReader(new FileReader(fileName));
String zeile = null;
// reads the single information and saving it into an array as long
// as the line isn't empty (end of file)
while ((zeile = input.readLine()) != null) {
String[] strings = zeile.split("\t");
// englische dateformat "MM'/'d'/'yyyy',' h:mm a"
// string[0] ist immer die locale des eintrags.
if (strings[0].contains("en")
&& (Locale.getDefault().toString().contains("de"))) {
// strings[1] sollte das datum sein gesplittet mit /
String duedate[] = strings[1].split("/");
// duedate[2] sollten die jahre sein + das anhängsel der
// uhrzeit
String datetime[] = duedate[2].split(",");
// datetime[1] sollte logischerweise die zeit im format h:mm
// a sein
String time[] = datetime[1].split(":");
// time[1] sind die minuten + das am/pm anhängsel
String ampm[] = time[1].split("");
// +12 stunden rechnen falls ampm[1] pm ist
if (ampm[1] == "pm") {
time[0] = time[0] + 12;
}
// dd.MM.yyyy, hh:mm sollte rauskommen.
strings[2] = duedate[1] + "." + duedate[0] + "."
+ datetime[0] + "," + time[0] + ":" + ampm[0];
}
// creating the new entry with the informations saved in the
// array
DefaultUITodoEntry tempentry = new DefaultUITodoEntry(
strings[1], strings[2], Severity.valueOf(strings[3]));
Application.instance.validateEntry(tempentry);
Application.instance.liste.add(tempentry);
}
} catch (IOException exp) {
throw new ApplicationException("Error loading file!");
}
}
/**
* Saves to-do entries to the file that is specified by the file name given.
* Writes for each entry, the single informations in a line and splits them
* with a tab
*
* @param fileName
* To save to-do entries in
* @throws IOException
* if creating the file fails.
*/
public static void savefile(String fileName) throws ApplicationException {
PrintWriter pw = null;
Locale log = Locale.getDefault();
try {
Writer fw = null;
// specifies the filename and adding the ending .cvs if the file
// doesn't have it
if (fileName.contains(".cvs")) {
fw = new BufferedWriter(new FileWriter(fileName));
} else {
fw = new BufferedWriter(new FileWriter(fileName + ".cvs"));
}
pw = new PrintWriter(fw);
// writing the single informations of each entry in a single line
for (int i = 0; i < Application.instance.liste.size(); i++) {
pw.print(log.toString());
pw.print("\t");
pw.print(Application.instance.liste.get(i).getDueDate());
pw.print("\t");
pw.print(Application.instance.liste.get(i).getDescription());
pw.print("\t");
pw.println(Application.instance.liste.get(i).getSeverity()
.toString());
}
} catch (IOException e) {
System.err.println("Error creating file!");
} finally {
if (pw != null) {
pw.close();
}
}
}
}