ImgScroll/src/main/java/com/rarchives/ripme/ui/History.java

147 lines
3.8 KiB
Java
Raw Normal View History

2014-07-30 16:48:52 +02:00
package com.rarchives.ripme.ui;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
2014-07-30 16:48:52 +02:00
import java.util.ArrayList;
import java.util.Date;
2014-07-30 16:48:52 +02:00
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
2014-07-30 16:48:52 +02:00
import org.json.JSONObject;
public class History {
private final List<HistoryEntry> list;
private static final String[] COLUMNS = new String[] {
"URL",
"created",
"modified",
"#",
""
};
2014-07-30 16:48:52 +02:00
public History() {
this.list = new ArrayList<HistoryEntry>();
}
public void add(HistoryEntry entry) {
2014-07-30 16:48:52 +02:00
list.add(entry);
}
public void remove(HistoryEntry entry) {
2014-07-30 16:48:52 +02:00
list.remove(entry);
}
public void remove(int index) {
list.remove(index);
}
2014-07-30 16:48:52 +02:00
public void clear() {
list.clear();
}
public HistoryEntry get(int index) {
return list.get(index);
}
public String getColumnName(int index) {
return COLUMNS[index];
}
public int getColumnCount() {
return COLUMNS.length;
}
public Object getValueAt(int row, int col) {
HistoryEntry entry = this.list.get(row);
switch (col) {
case 0:
return entry.url;
case 1:
return dateToHumanReadable(entry.startDate);
case 2:
return dateToHumanReadable(entry.modifiedDate);
case 3:
return entry.count;
case 4:
return entry.selected;
default:
return null;
}
}
private String dateToHumanReadable(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.format(date);
}
public boolean containsURL(String url) {
for (HistoryEntry entry : this.list) {
if (entry.url.equals(url)) {
return true;
}
}
return false;
}
public HistoryEntry getEntryByURL(String url) {
for (HistoryEntry entry : this.list) {
if (entry.url.equals(url)) {
return entry;
}
}
throw new RuntimeException("Could not find URL " + url + " in History");
}
2014-07-30 16:48:52 +02:00
public void fromJSON(JSONArray jsonArray) {
JSONObject json;
for (int i = 0; i < jsonArray.length(); i++) {
json = jsonArray.getJSONObject(i);
list.add(new HistoryEntry().fromJSON(json));
2014-07-30 16:48:52 +02:00
}
}
2017-05-10 00:03:12 +02:00
2014-07-30 16:48:52 +02:00
public void fromFile(String filename) throws IOException {
InputStream is = new FileInputStream(filename);
try {
String jsonString = IOUtils.toString(is);
JSONArray jsonArray = new JSONArray(jsonString);
fromJSON(jsonArray);
} catch (JSONException e) {
throw new IOException("Failed to load JSON file " + filename + ": " + e.getMessage(), e);
2014-07-30 16:48:52 +02:00
} finally {
is.close();
}
}
2017-05-10 00:03:12 +02:00
2014-07-30 16:48:52 +02:00
public void fromList(List<String> stringList) {
for (String item : stringList) {
HistoryEntry entry = new HistoryEntry();
2014-07-30 16:48:52 +02:00
entry.url = item;
list.add(entry);
}
}
public JSONArray toJSON() {
JSONArray jsonArray = new JSONArray();
for (HistoryEntry entry : list) {
2014-07-30 16:48:52 +02:00
jsonArray.put(entry.toJSON());
}
return jsonArray;
}
2017-05-10 00:03:12 +02:00
public List<HistoryEntry> toList() {
2014-07-30 16:48:52 +02:00
return list;
}
2017-05-10 00:03:12 +02:00
public boolean isEmpty() {
return list.isEmpty();
}
2017-05-10 00:03:12 +02:00
2014-07-30 16:48:52 +02:00
public void toFile(String filename) throws IOException {
OutputStream os = new FileOutputStream(filename);
try {
2015-01-11 09:23:43 +01:00
IOUtils.write(toJSON().toString(2), os);
2014-07-30 16:48:52 +02:00
} finally {
os.close();
}
}
}