Added events to OverlayModel interface, so overlay models can react to saving events and to disposing event from the project.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1191 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-08-22 05:06:36 +00:00
parent 4ea765b689
commit 95e2e30c8a
5 changed files with 60 additions and 6 deletions

View File

@ -182,7 +182,7 @@ public class GridworksServlet extends Butterfly {
_timer = null;
}
if (ProjectManager.singleton != null) {
ProjectManager.singleton.save(true); // complete save
ProjectManager.singleton.dispose();
ProjectManager.singleton = null;
}

View File

@ -61,6 +61,17 @@ public abstract class ProjectManager {
preparePreferenceStore(_preferenceStore);
}
public void dispose() {
save(true); // complete save
for (Project project : _projects.values()) {
project.dispose();
}
_projects.clear();
_projectsMetadata.clear();
}
/**
* Registers the project in the memory of the current session
@ -211,7 +222,7 @@ public abstract class ProjectManager {
* It's been a while since the project was last saved and it hasn't been
* modified. We can safely remove it from the cache to save some memory.
*/
_projects.remove(id);
_projects.remove(id).dispose();
}
}
}
@ -374,12 +385,12 @@ public abstract class ProjectManager {
* @param projectID
*/
protected void removeProject(long projectID){
if (_projects.containsKey(projectID)) {
_projects.remove(projectID).dispose();
}
if (_projectsMetadata.containsKey(projectID)) {
_projectsMetadata.remove(projectID);
}
if (_projects.containsKey(projectID)) {
_projects.remove(projectID);
}
}
//--------------Miscellaneous-----------

View File

@ -3,5 +3,9 @@ package com.google.gridworks.model;
import com.google.gridworks.Jsonizable;
public interface OverlayModel extends Jsonizable {
public void onBeforeSave();
public void onAfterSave();
public void dispose();
}

View File

@ -67,6 +67,16 @@ public class Project {
this.id = id;
this.history = new History(this);
}
public void dispose() {
for (OverlayModel overlayModel : overlayModels.values()) {
try {
overlayModel.dispose();
} catch (Exception e) {
logger.warn("Error signaling overlay model before disposing", e);
}
}
}
public Date getLastSave(){
return this._lastSave;
@ -83,6 +93,14 @@ public class Project {
}
public void saveToOutputStream(OutputStream out, Pool pool) throws IOException {
for (OverlayModel overlayModel : overlayModels.values()) {
try {
overlayModel.onBeforeSave();
} catch (Exception e) {
logger.warn("Error signaling overlay model before saving", e);
}
}
Writer writer = new OutputStreamWriter(out);
try {
Properties options = new Properties();
@ -93,6 +111,14 @@ public class Project {
} finally {
writer.flush();
}
for (OverlayModel overlayModel : overlayModels.values()) {
try {
overlayModel.onAfterSave();
} catch (Exception e) {
logger.warn("Error signaling overlay model after saving", e);
}
}
}
protected void saveToWriter(Writer writer, Properties options) throws IOException {

View File

@ -23,6 +23,19 @@ public class Protograph implements OverlayModel {
return _rootNodes.get(index);
}
@Override
public void onBeforeSave() {
}
@Override
public void onAfterSave() {
}
@Override
public void dispose() {
}
static public Protograph reconstruct(JSONObject o) throws JSONException {
Protograph g = new Protograph();