2010-02-03 03:29:47 +01:00
|
|
|
package com.metaweb.gridworks.history;
|
2010-01-27 02:48:42 +01:00
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.ArrayList;
|
2010-02-17 02:30:09 +01:00
|
|
|
import java.util.LinkedList;
|
2010-01-27 02:48:42 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
2010-02-01 20:16:09 +01:00
|
|
|
import org.json.JSONWriter;
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-02-03 03:29:47 +01:00
|
|
|
import com.metaweb.gridworks.Jsonizable;
|
|
|
|
import com.metaweb.gridworks.ProjectManager;
|
|
|
|
import com.metaweb.gridworks.model.Project;
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
public class History implements Serializable, Jsonizable {
|
2010-01-27 02:48:42 +01:00
|
|
|
private static final long serialVersionUID = -1529783362243627391L;
|
|
|
|
|
|
|
|
protected long _projectID;
|
|
|
|
protected List<HistoryEntry> _pastEntries;
|
|
|
|
protected List<HistoryEntry> _futureEntries;
|
|
|
|
|
|
|
|
public History(Project project) {
|
|
|
|
_projectID = project.id;
|
|
|
|
_pastEntries = new ArrayList<HistoryEntry>();
|
|
|
|
_futureEntries = new ArrayList<HistoryEntry>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addEntry(HistoryEntry entry) {
|
2010-02-01 09:31:50 +01:00
|
|
|
for (HistoryEntry entry2 : _futureEntries) {
|
|
|
|
entry2.delete();
|
|
|
|
}
|
2010-01-27 02:48:42 +01:00
|
|
|
_futureEntries.clear();
|
2010-02-01 09:31:50 +01:00
|
|
|
|
2010-01-27 02:48:42 +01:00
|
|
|
_pastEntries.add(entry);
|
2010-02-01 20:16:09 +01:00
|
|
|
|
2010-01-27 02:48:42 +01:00
|
|
|
entry.apply(ProjectManager.singleton.getProject(_projectID));
|
2010-02-01 20:16:09 +01:00
|
|
|
setModified();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setModified() {
|
2010-02-03 22:57:38 +01:00
|
|
|
ProjectManager.singleton.getProjectMetadata(_projectID).updateModified();
|
2010-01-27 02:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<HistoryEntry> getLastPastEntries(int count) {
|
2010-02-17 02:30:09 +01:00
|
|
|
if (count <= 0) {
|
|
|
|
return new LinkedList<HistoryEntry>(_pastEntries);
|
|
|
|
} else {
|
|
|
|
return _pastEntries.subList(Math.max(_pastEntries.size() - count, 0), _pastEntries.size());
|
|
|
|
}
|
2010-01-27 02:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void undoRedo(long lastDoneEntryID) {
|
|
|
|
if (lastDoneEntryID == 0) {
|
|
|
|
undo(_pastEntries.size());
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < _pastEntries.size(); i++) {
|
|
|
|
if (_pastEntries.get(i).id == lastDoneEntryID) {
|
|
|
|
undo(_pastEntries.size() - i - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < _futureEntries.size(); i++) {
|
|
|
|
if (_futureEntries.get(i).id == lastDoneEntryID) {
|
|
|
|
redo(i + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-01 09:31:50 +01:00
|
|
|
protected HistoryEntry getEntry(long entryID) {
|
|
|
|
for (int i = 0; i < _pastEntries.size(); i++) {
|
|
|
|
if (_pastEntries.get(i).id == entryID) {
|
|
|
|
return _pastEntries.get(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < _futureEntries.size(); i++) {
|
|
|
|
if (_futureEntries.get(i).id == entryID) {
|
|
|
|
return _futureEntries.get(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-01-27 02:48:42 +01:00
|
|
|
protected void undo(int times) {
|
|
|
|
Project project = ProjectManager.singleton.getProject(_projectID);
|
|
|
|
|
|
|
|
while (times > 0 && _pastEntries.size() > 0) {
|
|
|
|
HistoryEntry entry = _pastEntries.remove(_pastEntries.size() - 1);
|
|
|
|
times--;
|
|
|
|
|
|
|
|
entry.revert(project);
|
|
|
|
|
|
|
|
_futureEntries.add(0, entry);
|
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
setModified();
|
2010-01-27 02:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void redo(int times) {
|
|
|
|
Project project = ProjectManager.singleton.getProject(_projectID);
|
|
|
|
|
|
|
|
while (times > 0 && _futureEntries.size() > 0) {
|
|
|
|
HistoryEntry entry = _futureEntries.remove(0);
|
|
|
|
times--;
|
|
|
|
|
|
|
|
entry.apply(project);
|
|
|
|
|
|
|
|
_pastEntries.add(entry);
|
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
setModified();
|
2010-01-27 02:48:42 +01:00
|
|
|
}
|
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.key("past"); writer.array();
|
2010-01-27 02:48:42 +01:00
|
|
|
for (HistoryEntry entry : _pastEntries) {
|
2010-02-01 20:16:09 +01:00
|
|
|
entry.write(writer, options);
|
2010-01-27 02:48:42 +01:00
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.endArray();
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.key("future"); writer.array();
|
2010-01-27 02:48:42 +01:00
|
|
|
for (HistoryEntry entry : _futureEntries) {
|
2010-02-01 20:16:09 +01:00
|
|
|
entry.write(writer, options);
|
2010-01-27 02:48:42 +01:00
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.endArray();
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.endObject();
|
2010-01-27 02:48:42 +01:00
|
|
|
}
|
|
|
|
}
|