Implemented row remove command.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@391 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-04-06 07:47:44 +00:00
parent 1fd85c62bf
commit 9d9329ca96
6 changed files with 219 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import com.metaweb.gridworks.commands.edit.DeleteProjectCommand;
import com.metaweb.gridworks.commands.edit.ExportProjectCommand;
import com.metaweb.gridworks.commands.edit.ExtendDataCommand;
import com.metaweb.gridworks.commands.edit.ImportProjectCommand;
import com.metaweb.gridworks.commands.edit.RemoveRowsCommand;
import com.metaweb.gridworks.commands.edit.RenameColumnCommand;
import com.metaweb.gridworks.commands.edit.TextTransformCommand;
import com.metaweb.gridworks.commands.edit.EditOneCellCommand;
@ -110,6 +111,7 @@ public class GridworksServlet extends HttpServlet {
_commands.put("annotate-one-row", new AnnotateOneRowCommand());
_commands.put("annotate-rows", new AnnotateRowsCommand());
_commands.put("remove-rows", new RemoveRowsCommand());
_commands.put("save-protograph", new SaveProtographCommand());

View File

@ -0,0 +1,20 @@
package com.metaweb.gridworks.commands.edit;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import com.metaweb.gridworks.commands.EngineDependentCommand;
import com.metaweb.gridworks.model.AbstractOperation;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.operations.RowRemovalOperation;
public class RemoveRowsCommand extends EngineDependentCommand {
@Override
protected AbstractOperation createOperation(Project project,
HttpServletRequest request, JSONObject engineConfig) throws Exception {
return new RowRemovalOperation(engineConfig);
}
}

View File

@ -0,0 +1,108 @@
package com.metaweb.gridworks.model.changes;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.metaweb.gridworks.history.Change;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
public class RowRemovalChange implements Change {
final protected List<Integer> _rowIndices;
protected List<Row> _rows;
public RowRemovalChange(List<Integer> rowIndices) {
_rowIndices = rowIndices;
}
public void apply(Project project) {
synchronized (project) {
int count = _rowIndices.size();
_rows = new ArrayList<Row>(count);
int offset = 0;
for (int i = 0; i < count; i++) {
int index = _rowIndices.get(i);
Row row = project.rows.remove(index + offset);
_rows.add(row);
offset--;
}
project.recomputeRowContextDependencies();
}
}
public void revert(Project project) {
synchronized (project) {
int count = _rowIndices.size();
for (int i = 0; i < count; i++) {
int index = _rowIndices.get(i);
Row row = _rows.get(i);
project.rows.add(index, row);
}
project.recomputeRowContextDependencies();
}
}
public void save(Writer writer, Properties options) throws IOException {
writer.write("rowIndexCount="); writer.write(Integer.toString(_rowIndices.size())); writer.write('\n');
for (Integer index : _rowIndices) {
writer.write(index.toString());
writer.write('\n');
}
writer.write("rowCount="); writer.write(Integer.toString(_rows.size())); writer.write('\n');
for (Row row : _rows) {
row.save(writer, options);
writer.write('\n');
}
writer.write("/ec/\n"); // end of change marker
}
static public Change load(LineNumberReader reader) throws Exception {
List<Integer> rowIndices = null;
List<Row> rows = null;
String line;
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
int equal = line.indexOf('=');
CharSequence field = line.subSequence(0, equal);
if ("rowIndexCount".equals(field)) {
int count = Integer.parseInt(line.substring(equal + 1));
rowIndices = new ArrayList<Integer>(count);
for (int i = 0; i < count; i++) {
line = reader.readLine();
if (line != null) {
rowIndices.add(Integer.parseInt(line));
}
}
} else if ("rowCount".equals(field)) {
int count = Integer.parseInt(line.substring(equal + 1));
rows = new ArrayList<Row>(count);
for (int i = 0; i < count; i++) {
line = reader.readLine();
if (line != null) {
rows.add(Row.load(line));
}
}
}
}
RowRemovalChange change = new RowRemovalChange(rowIndices);
change._rows = rows;
return change;
}
}

View File

@ -32,8 +32,10 @@ public abstract class OperationRegistry {
register("column-addition", ColumnAdditionOperation.class);
register("column-removal", ColumnRemovalOperation.class);
register("column-rename", ColumnRenameOperation.class);
register("extend-data", ExtendDataOperation.class);
register("row-removal", RowRemovalOperation.class);
register("row-star", RowStarOperation.class);
register("save-protograph", SaveProtographOperation.class);

View File

@ -0,0 +1,80 @@
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.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.RowRemovalChange;
public class RowRemovalOperation extends EngineDependentOperation {
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
JSONObject engineConfig = obj.getJSONObject("engineConfig");
return new RowRemovalOperation(
engineConfig
);
}
public RowRemovalOperation(JSONObject engineConfig) {
super(engineConfig);
}
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.endObject();
}
protected String getBriefDescription(Project project) {
return "Remove rows";
}
protected HistoryEntry createHistoryEntry(Project project) throws Exception {
Engine engine = createEngine(project);
List<Integer> rowIndices = new ArrayList<Integer>();
FilteredRows filteredRows = engine.getAllFilteredRows(false);
filteredRows.accept(project, createRowVisitor(project, rowIndices));
return new HistoryEntry(
project,
"Remove " + rowIndices.size() + " rows",
this,
new RowRemovalChange(rowIndices)
);
}
protected RowVisitor createRowVisitor(Project project, List<Integer> rowIndices) throws Exception {
return new RowVisitor() {
List<Integer> rowIndices;
public RowVisitor init(List<Integer> rowIndices) {
this.rowIndices = rowIndices;
return this;
}
public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) {
if (!includeContextual) {
rowIndices.add(rowIndex);
}
return false;
}
}.init(rowIndices);
}
}

View File

@ -333,6 +333,13 @@ DataTableView.prototype._createMenuForAllColumns = function(elmt) {
click: function() {
Gridworks.postProcess("annotate-rows", { "starred" : "false" }, null, { rowMetadataChanged: true });
}
},
{},
{
label: "Remove Visible Rows",
click: function() {
Gridworks.postProcess("remove-rows", {}, null, { rowMetadataChanged: true });
}
}
]
},