Skip to content

Commit

Permalink
Added row flagging support. Fixed bug in row star change: starring or…
Browse files Browse the repository at this point in the history
… unstarring one row wasn't undo-able previously.

git-svn-id: http:https://google-refine.googlecode.com/svn/trunk@547 7d457c2a-affb-35e4-300a-418c747d4874
  • Loading branch information
dfhuynh committed Apr 26, 2010
1 parent 1b16ef0 commit fed3c87
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 13 deletions.
6 changes: 6 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ licenses/icu4j.LICENSE.txt

licenses/json.LICENSE.txt
json

Others
------

Flag icon
http:https://pixel-mixer.com/category/free-icons/
Binary file modified src/graphics/star-flag-map.psd
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,27 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
);

performProcessAndRespond(request, response, project, process);
} else {
respond(response, "{ \"code\" : \"error\", \"message\" : \"invalid command parameters\" }");
return;
}

String flaggedString = request.getParameter("flagged");
if (flaggedString != null) {
boolean flagged = "true".endsWith(flaggedString);
String description = (flagged ? "Flag row " : "Unflag row ") + (rowIndex + 1);

FlagOneRowProcess process = new FlagOneRowProcess(
project,
description,
rowIndex,
flagged
);

performProcessAndRespond(request, response, project, process);
return;
}

respond(response, "{ \"code\" : \"error\", \"message\" : \"invalid command parameters\" }");

} catch (Exception e) {
respondException(response, e);
}
Expand Down Expand Up @@ -71,4 +89,29 @@ protected HistoryEntry createHistoryEntry() throws Exception {
);
}
}
protected static class FlagOneRowProcess extends QuickHistoryEntryProcess {
final int rowIndex;
final boolean flagged;

FlagOneRowProcess(
Project project,
String briefDescription,
int rowIndex,
boolean flagged
) {
super(project, briefDescription);

this.rowIndex = rowIndex;
this.flagged = flagged;
}

protected HistoryEntry createHistoryEntry() throws Exception {
return new HistoryEntry(
_project,
(flagged ? "Flag row " : "Unflag row ") + (rowIndex + 1),
null,
new RowStarChange(rowIndex, flagged)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.RowFlagOperation;
import com.metaweb.gridworks.operations.RowStarOperation;

public class AnnotateRowsCommand extends EngineDependentCommand {
Expand All @@ -21,6 +22,13 @@ protected AbstractOperation createOperation(Project project,

return new RowStarOperation(engineConfig, starred);
}

String flaggedString = request.getParameter("flagged");
if (flaggedString != null) {
boolean flagged = "true".endsWith(flaggedString);

return new RowFlagOperation(engineConfig, flagged);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.metaweb.gridworks.model.changes;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Writer;
import java.util.Properties;

import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.util.Pool;

public class RowFlagChange implements Change {
final int rowIndex;
final boolean newFlagged;
Boolean oldFlagged = null;

public RowFlagChange(int rowIndex, boolean newFlagged) {
this.rowIndex = rowIndex;
this.newFlagged = newFlagged;
}

public void apply(Project project) {
Row row = project.rows.get(rowIndex);
if (oldFlagged == null) {
oldFlagged = row.flagged;
}
row.flagged = newFlagged;
}

public void revert(Project project) {
Row row = project.rows.get(rowIndex);

row.flagged = oldFlagged;
}

public void save(Writer writer, Properties options) throws IOException {
writer.write("row="); writer.write(Integer.toString(rowIndex)); writer.write('\n');
writer.write("newFlagged="); writer.write(Boolean.toString(newFlagged)); writer.write('\n');
writer.write("oldFlagged="); writer.write(Boolean.toString(oldFlagged)); writer.write('\n');
writer.write("/ec/\n"); // end of change marker
}

static public RowFlagChange load(LineNumberReader reader, Pool pool) throws Exception {
int row = -1;
boolean oldStarred = false;
boolean newStarred = false;

String line;
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
int equal = line.indexOf('=');
CharSequence field = line.subSequence(0, equal);
String value = line.substring(equal + 1);

if ("row".equals(field)) {
row = Integer.parseInt(value);
} else if ("oldFlagged".equals(field)) {
oldStarred = Boolean.parseBoolean(value);
} else if ("newFlagged".equals(field)) {
oldStarred = Boolean.parseBoolean(value);
}
}

RowFlagChange change = new RowFlagChange(row, newStarred);
change.oldFlagged = oldStarred;

return change;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class RowStarChange implements Change {
final int rowIndex;
final boolean newStarred;
boolean oldStarred;
Boolean oldStarred = null;

public RowStarChange(int rowIndex, boolean newStarred) {
this.rowIndex = rowIndex;
Expand All @@ -22,8 +22,9 @@ public RowStarChange(int rowIndex, boolean newStarred) {

public void apply(Project project) {
Row row = project.rows.get(rowIndex);

oldStarred = row.starred;
if (oldStarred == null) {
oldStarred = row.starred;
}
row.starred = newStarred;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static protected void register(String name, Class<? extends AbstractOperation> k

register("row-removal", RowRemovalOperation.class);
register("row-star", RowStarOperation.class);
register("row-flag", RowFlagOperation.class);

register("save-protograph", SaveProtographOperation.class);
register("text-transform", TextTransformOperation.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.metaweb.gridworks.operations;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;

import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
import com.metaweb.gridworks.model.changes.MassChange;
import com.metaweb.gridworks.model.changes.RowFlagChange;

public class RowFlagOperation extends EngineDependentOperation {
final protected boolean _flagged;

static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
JSONObject engineConfig = obj.getJSONObject("engineConfig");
boolean flagged = obj.getBoolean("flagged");

return new RowFlagOperation(
engineConfig,
flagged
);
}

public RowFlagOperation(JSONObject engineConfig, boolean flagged) {
super(engineConfig);
_flagged = flagged;
}

public void write(JSONWriter writer, Properties options)
throws JSONException {

writer.object();
writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass()));
writer.key("description"); writer.value(getBriefDescription(null));
writer.key("engineConfig"); writer.value(getEngineConfig());
writer.key("flagged"); writer.value(_flagged);
writer.endObject();
}

protected String getBriefDescription(Project project) {
return (_flagged ? "Flag rows" : "Unflag rows");
}

protected HistoryEntry createHistoryEntry(Project project) throws Exception {
Engine engine = createEngine(project);

List<Change> changes = new ArrayList<Change>(project.rows.size());

FilteredRows filteredRows = engine.getAllFilteredRows(false);
filteredRows.accept(project, createRowVisitor(project, changes));

return new HistoryEntry(
project,
(_flagged ? "Flag" : "Unflag") + " " + changes.size() + " rows",
this,
new MassChange(changes, false)
);
}

protected RowVisitor createRowVisitor(Project project, List<Change> changes) throws Exception {
return new RowVisitor() {
List<Change> changes;

public RowVisitor init(List<Change> changes) {
this.changes = changes;
return this;
}

public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) {
if (row.starred != _flagged) {
RowFlagChange change = new RowFlagChange(rowIndex, _flagged);

changes.add(change);
}
return false;
}
}.init(changes);
}
}
Loading

0 comments on commit fed3c87

Please sign in to comment.