Added choices blank and error to list facets.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@151 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-27 06:59:55 +00:00
parent 49e7241d1d
commit 25fd5794cd
6 changed files with 139 additions and 54 deletions

View File

@ -17,6 +17,8 @@ public class ExpressionNominalRowGrouper implements RowVisitor {
final protected int _cellIndex; final protected int _cellIndex;
final public Map<Object, NominalFacetChoice> choices = new HashMap<Object, NominalFacetChoice>(); final public Map<Object, NominalFacetChoice> choices = new HashMap<Object, NominalFacetChoice>();
public int blankCount = 0;
public int errorCount = 0;
public ExpressionNominalRowGrouper(Evaluable evaluable, int cellIndex) { public ExpressionNominalRowGrouper(Evaluable evaluable, int cellIndex) {
_evaluable = evaluable; _evaluable = evaluable;
@ -30,29 +32,33 @@ public class ExpressionNominalRowGrouper implements RowVisitor {
ExpressionUtils.bind(bindings, row, rowIndex, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object value = _evaluable.evaluate(bindings); Object value = _evaluable.evaluate(bindings);
if (value != null) { if (value != null && value.getClass().isArray()) {
if (value.getClass().isArray()) { Object[] a = (Object[]) value;
Object[] a = (Object[]) value; for (Object v : a) {
for (Object v : a) { processValue(v);
processValue(v);
}
} else {
processValue(value);
} }
} else {
processValue(value);
} }
return false; return false;
} }
protected void processValue(Object value) { protected void processValue(Object value) {
DecoratedValue dValue = new DecoratedValue(value, value.toString()); if (ExpressionUtils.isError(value)) {
errorCount++;
if (choices.containsKey(value)) { } else if (ExpressionUtils.isNonBlankData(value)) {
choices.get(value).count++; DecoratedValue dValue = new DecoratedValue(value, value.toString());
} else {
NominalFacetChoice choice = new NominalFacetChoice(dValue); if (choices.containsKey(value)) {
choice.count = 1; choices.get(value).count++;
} else {
choices.put(value, choice); NominalFacetChoice choice = new NominalFacetChoice(dValue);
} choice.count = 1;
choices.put(value, choice);
}
} else {
blankCount++;
}
} }
} }

View File

@ -19,7 +19,8 @@ import com.metaweb.gridworks.model.Project;
public class ListFacet implements Facet { public class ListFacet implements Facet {
protected List<NominalFacetChoice> _selection = new LinkedList<NominalFacetChoice>(); protected List<NominalFacetChoice> _selection = new LinkedList<NominalFacetChoice>();
protected List<NominalFacetChoice> _choices = new LinkedList<NominalFacetChoice>(); protected boolean _selectBlank;
protected boolean _selectError;
protected String _name; protected String _name;
protected String _expression; protected String _expression;
@ -27,6 +28,11 @@ public class ListFacet implements Facet {
protected int _cellIndex; protected int _cellIndex;
protected Evaluable _eval; protected Evaluable _eval;
// computed
protected List<NominalFacetChoice> _choices = new LinkedList<NominalFacetChoice>();
protected int _blankCount;
protected int _errorCount;
public ListFacet() { public ListFacet() {
} }
@ -43,6 +49,22 @@ public class ListFacet implements Facet {
choice.write(writer, options); choice.write(writer, options);
} }
writer.endArray(); writer.endArray();
if (_selectBlank || _blankCount > 0) {
writer.key("blankChoice");
writer.object();
writer.key("s"); writer.value(_selectBlank);
writer.key("c"); writer.value(_blankCount);
writer.endObject();
}
if (_selectError || _errorCount > 0) {
writer.key("errorChoice");
writer.object();
writer.key("s"); writer.value(_selectError);
writer.key("c"); writer.value(_errorCount);
writer.endObject();
}
writer.endObject(); writer.endObject();
} }
@ -70,11 +92,18 @@ public class ListFacet implements Facet {
_selection.add(nominalFacetChoice); _selection.add(nominalFacetChoice);
} }
if (o.has("selectBlank")) {
_selectBlank = o.getBoolean("selectBlank");
}
if (o.has("selectError")) {
_selectError = o.getBoolean("selectError");
}
} }
public RowFilter getRowFilter() { public RowFilter getRowFilter() {
return _selection.size() == 0 ? null : return _selection.size() == 0 && !_selectBlank && !_selectError ? null :
new ExpressionEqualRowFilter(_eval, _cellIndex, createMatches()); new ExpressionEqualRowFilter(_eval, _cellIndex, createMatches(), _selectBlank, _selectError);
} }
public void computeChoices(Project project, FilteredRows filteredRows) { public void computeChoices(Project project, FilteredRows filteredRows) {
@ -94,6 +123,9 @@ public class ListFacet implements Facet {
_choices.add(choice); _choices.add(choice);
} }
} }
_blankCount = grouper.blankCount;
_errorCount = grouper.errorCount;
} }
protected Object[] createMatches() { protected Object[] createMatches() {

View File

@ -12,11 +12,15 @@ public class ExpressionEqualRowFilter implements RowFilter {
final protected Evaluable _evaluable; final protected Evaluable _evaluable;
final protected int _cellIndex; final protected int _cellIndex;
final protected Object[] _matches; final protected Object[] _matches;
final protected boolean _selectBlank;
final protected boolean _selectError;
public ExpressionEqualRowFilter(Evaluable evaluable, int cellIndex, Object[] matches) { public ExpressionEqualRowFilter(Evaluable evaluable, int cellIndex, Object[] matches, boolean selectBlank, boolean selectError) {
_evaluable = evaluable; _evaluable = evaluable;
_cellIndex = cellIndex; _cellIndex = cellIndex;
_matches = matches; _matches = matches;
_selectBlank = selectBlank;
_selectError = selectError;
} }
public boolean filterRow(Project project, int rowIndex, Row row) { public boolean filterRow(Project project, int rowIndex, Row row) {
@ -25,24 +29,31 @@ public class ExpressionEqualRowFilter implements RowFilter {
ExpressionUtils.bind(bindings, row, rowIndex, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object value = _evaluable.evaluate(bindings); Object value = _evaluable.evaluate(bindings);
if (value != null) { if (value != null && value.getClass().isArray()) {
if (value.getClass().isArray()) { Object[] a = (Object[]) value;
Object[] a = (Object[]) value; for (Object v : a) {
for (Object v : a) { if (testValue(v)) {
for (Object match : _matches) { return true;
if (match.equals(v)) { }
return true;
}
}
}
} else {
for (Object match : _matches) {
if (match.equals(value)) {
return true;
}
}
} }
} else {
return testValue(value);
} }
return false; return false;
} }
protected boolean testValue(Object v) {
if (ExpressionUtils.isError(v)) {
return _selectError;
} else if (ExpressionUtils.isNonBlankData(v)) {
for (Object match : _matches) {
if (match.equals(v)) {
return true;
}
}
return false;
} else {
return _selectBlank;
}
}
} }

View File

@ -10,11 +10,7 @@ public class VariableExpr implements Evaluable {
} }
public Object evaluate(Properties bindings) { public Object evaluate(Properties bindings) {
if (bindings.containsKey(_name)) { return bindings.get(_name);
return bindings.get(_name);
} else {
return new EvalError("No variable named " + _name);
}
} }
public String toString() { public String toString() {

View File

@ -8,6 +8,9 @@ function ListFacet(div, config, options, selection) {
} }
this._selection = selection || []; this._selection = selection || [];
this._blankChoice = null;
this._errorChoice = null;
this._data = null; this._data = null;
this.render(); this.render();
@ -35,7 +38,9 @@ ListFacet.prototype.getJSON = function() {
name: this._config.name, name: this._config.name,
columnName: this._config.columnName, columnName: this._config.columnName,
expression: this._config.expression, expression: this._config.expression,
selection: [] selection: [],
selectBlank: this._blankChoice != null && this._blankChoice.s,
selectError: this._errorChoice != null && this._errorChoice.s
} }
for (var i = 0; i < this._selection.length; i++) { for (var i = 0; i < this._selection.length; i++) {
var choice = { var choice = {
@ -47,7 +52,9 @@ ListFacet.prototype.getJSON = function() {
}; };
ListFacet.prototype.hasSelection = function() { ListFacet.prototype.hasSelection = function() {
return this._selection.length > 0; return this._selection.length > 0 ||
(this._blankChoice != null && this._blankChoice.s) ||
(this._errorChoice != null && this._errorChoice.s);
}; };
ListFacet.prototype.updateState = function(data) { ListFacet.prototype.updateState = function(data) {
@ -64,6 +71,9 @@ ListFacet.prototype.updateState = function(data) {
this._selection = selection; this._selection = selection;
this._reSortChoices(); this._reSortChoices();
this._blankChoice = data.blankChoice || null;
this._errorChoice = data.errorChoice || null;
this.render(); this.render();
}; };
@ -107,7 +117,10 @@ ListFacet.prototype.render = function() {
if (this._data == null) { if (this._data == null) {
bodyDiv.html("Loading..."); bodyDiv.html("Loading...");
} else { } else {
var selectionCount = this._selection.length; var selectionCount = this._selection.length
+ (this._blankChoice != null && this._blankChoice.s ? 1 : 0)
+ (this._errorChoice != null && this._errorChoice.s ? 1 : 0);
if (selectionCount > 0) { if (selectionCount > 0) {
var reset = function() { var reset = function() {
self._reset(); self._reset();
@ -117,8 +130,8 @@ ListFacet.prototype.render = function() {
); );
} }
var renderChoice = function(choice) { var renderChoice = function(choice, customLabel) {
var label = choice.v.l; var label = customLabel || choice.v.l;
var count = choice.c; var count = choice.c;
var choiceDiv = $('<div></div>').addClass("facet-choice").appendTo(bodyDiv); var choiceDiv = $('<div></div>').addClass("facet-choice").appendTo(bodyDiv);
@ -165,6 +178,12 @@ ListFacet.prototype.render = function() {
for (var i = 0; i < choices.length; i++) { for (var i = 0; i < choices.length; i++) {
renderChoice(choices[i]); renderChoice(choices[i]);
} }
if (this._blankChoice != null) {
renderChoice(this._blankChoice, "(blank)");
}
if (this._errorChoice != null) {
renderChoice(this._errorChoice, "(error)");
}
bodyDiv[0].scrollTop = scrollTop; bodyDiv[0].scrollTop = scrollTop;
@ -190,16 +209,31 @@ ListFacet.prototype.render = function() {
ListFacet.prototype._select = function(choice, only) { ListFacet.prototype._select = function(choice, only) {
if (only) { if (only) {
this._selection = []; this._selection = [];
if (this._blankChoice != null) {
this._blankChoice.s = false;
}
if (this._errorChoice != null) {
this._errorChoice.s = false;
}
} }
this._selection.push(choice);
choice.s = true;
if (choice !== this._errorChoice && choice !== this._blankChoice) {
this._selection.push(choice);
}
this._updateRest(); this._updateRest();
}; };
ListFacet.prototype._deselect = function(choice) { ListFacet.prototype._deselect = function(choice) {
for (var i = this._selection.length - 1; i >= 0; i--) { if (choice === this._errorChoice || choice === this._blankChoice) {
if (this._selection[i] == choice) { choice.s = false;
this._selection.splice(i, 1); } else {
break; for (var i = this._selection.length - 1; i >= 0; i--) {
if (this._selection[i] === choice) {
this._selection.splice(i, 1);
break;
}
} }
} }
this._updateRest(); this._updateRest();
@ -207,6 +241,9 @@ ListFacet.prototype._deselect = function(choice) {
ListFacet.prototype._reset = function() { ListFacet.prototype._reset = function() {
this._selection = []; this._selection = [];
this._blankChoice = null;
this._errorChoice = null;
this._updateRest(); this._updateRest();
}; };
@ -215,7 +252,10 @@ ListFacet.prototype._remove = function() {
this._div = null; this._div = null;
this._config = null; this._config = null;
this._selection = null; this._selection = null;
this._blankChoice = null;
this._errorChoice = null;
this._data = null; this._data = null;
}; };

View File

@ -82,7 +82,7 @@ BrowsingEngine.prototype.removeFacet = function(facet) {
} }
if (update) { if (update) {
Gridworks.update({ engineChanged: true }, onFinallyDone); Gridworks.update({ engineChanged: true });
} }
}; };