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:
parent
49e7241d1d
commit
25fd5794cd
@ -17,6 +17,8 @@ public class ExpressionNominalRowGrouper implements RowVisitor {
|
||||
final protected int _cellIndex;
|
||||
|
||||
final public Map<Object, NominalFacetChoice> choices = new HashMap<Object, NominalFacetChoice>();
|
||||
public int blankCount = 0;
|
||||
public int errorCount = 0;
|
||||
|
||||
public ExpressionNominalRowGrouper(Evaluable evaluable, int cellIndex) {
|
||||
_evaluable = evaluable;
|
||||
@ -30,29 +32,33 @@ public class ExpressionNominalRowGrouper implements RowVisitor {
|
||||
ExpressionUtils.bind(bindings, row, rowIndex, cell);
|
||||
|
||||
Object value = _evaluable.evaluate(bindings);
|
||||
if (value != null) {
|
||||
if (value.getClass().isArray()) {
|
||||
Object[] a = (Object[]) value;
|
||||
for (Object v : a) {
|
||||
processValue(v);
|
||||
}
|
||||
} else {
|
||||
processValue(value);
|
||||
if (value != null && value.getClass().isArray()) {
|
||||
Object[] a = (Object[]) value;
|
||||
for (Object v : a) {
|
||||
processValue(v);
|
||||
}
|
||||
} else {
|
||||
processValue(value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void processValue(Object value) {
|
||||
DecoratedValue dValue = new DecoratedValue(value, value.toString());
|
||||
if (ExpressionUtils.isError(value)) {
|
||||
errorCount++;
|
||||
} else if (ExpressionUtils.isNonBlankData(value)) {
|
||||
DecoratedValue dValue = new DecoratedValue(value, value.toString());
|
||||
|
||||
if (choices.containsKey(value)) {
|
||||
choices.get(value).count++;
|
||||
} else {
|
||||
NominalFacetChoice choice = new NominalFacetChoice(dValue);
|
||||
choice.count = 1;
|
||||
if (choices.containsKey(value)) {
|
||||
choices.get(value).count++;
|
||||
} else {
|
||||
NominalFacetChoice choice = new NominalFacetChoice(dValue);
|
||||
choice.count = 1;
|
||||
|
||||
choices.put(value, choice);
|
||||
}
|
||||
choices.put(value, choice);
|
||||
}
|
||||
} else {
|
||||
blankCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ import com.metaweb.gridworks.model.Project;
|
||||
|
||||
public class ListFacet implements Facet {
|
||||
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 _expression;
|
||||
@ -27,6 +28,11 @@ public class ListFacet implements Facet {
|
||||
protected int _cellIndex;
|
||||
protected Evaluable _eval;
|
||||
|
||||
// computed
|
||||
protected List<NominalFacetChoice> _choices = new LinkedList<NominalFacetChoice>();
|
||||
protected int _blankCount;
|
||||
protected int _errorCount;
|
||||
|
||||
public ListFacet() {
|
||||
}
|
||||
|
||||
@ -43,6 +49,22 @@ public class ListFacet implements Facet {
|
||||
choice.write(writer, options);
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
@ -70,11 +92,18 @@ public class ListFacet implements Facet {
|
||||
|
||||
_selection.add(nominalFacetChoice);
|
||||
}
|
||||
|
||||
if (o.has("selectBlank")) {
|
||||
_selectBlank = o.getBoolean("selectBlank");
|
||||
}
|
||||
if (o.has("selectError")) {
|
||||
_selectError = o.getBoolean("selectError");
|
||||
}
|
||||
}
|
||||
|
||||
public RowFilter getRowFilter() {
|
||||
return _selection.size() == 0 ? null :
|
||||
new ExpressionEqualRowFilter(_eval, _cellIndex, createMatches());
|
||||
return _selection.size() == 0 && !_selectBlank && !_selectError ? null :
|
||||
new ExpressionEqualRowFilter(_eval, _cellIndex, createMatches(), _selectBlank, _selectError);
|
||||
}
|
||||
|
||||
public void computeChoices(Project project, FilteredRows filteredRows) {
|
||||
@ -94,6 +123,9 @@ public class ListFacet implements Facet {
|
||||
_choices.add(choice);
|
||||
}
|
||||
}
|
||||
|
||||
_blankCount = grouper.blankCount;
|
||||
_errorCount = grouper.errorCount;
|
||||
}
|
||||
|
||||
protected Object[] createMatches() {
|
||||
|
@ -12,11 +12,15 @@ public class ExpressionEqualRowFilter implements RowFilter {
|
||||
final protected Evaluable _evaluable;
|
||||
final protected int _cellIndex;
|
||||
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;
|
||||
_cellIndex = cellIndex;
|
||||
_matches = matches;
|
||||
_selectBlank = selectBlank;
|
||||
_selectError = selectError;
|
||||
}
|
||||
|
||||
public boolean filterRow(Project project, int rowIndex, Row row) {
|
||||
@ -25,24 +29,31 @@ public class ExpressionEqualRowFilter implements RowFilter {
|
||||
ExpressionUtils.bind(bindings, row, rowIndex, cell);
|
||||
|
||||
Object value = _evaluable.evaluate(bindings);
|
||||
if (value != null) {
|
||||
if (value.getClass().isArray()) {
|
||||
Object[] a = (Object[]) value;
|
||||
for (Object v : a) {
|
||||
for (Object match : _matches) {
|
||||
if (match.equals(v)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (Object match : _matches) {
|
||||
if (match.equals(value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (value != null && value.getClass().isArray()) {
|
||||
Object[] a = (Object[]) value;
|
||||
for (Object v : a) {
|
||||
if (testValue(v)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return testValue(value);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,11 +10,7 @@ public class VariableExpr implements Evaluable {
|
||||
}
|
||||
|
||||
public Object evaluate(Properties bindings) {
|
||||
if (bindings.containsKey(_name)) {
|
||||
return bindings.get(_name);
|
||||
} else {
|
||||
return new EvalError("No variable named " + _name);
|
||||
}
|
||||
return bindings.get(_name);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
@ -8,6 +8,9 @@ function ListFacet(div, config, options, selection) {
|
||||
}
|
||||
|
||||
this._selection = selection || [];
|
||||
this._blankChoice = null;
|
||||
this._errorChoice = null;
|
||||
|
||||
this._data = null;
|
||||
|
||||
this.render();
|
||||
@ -35,7 +38,9 @@ ListFacet.prototype.getJSON = function() {
|
||||
name: this._config.name,
|
||||
columnName: this._config.columnName,
|
||||
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++) {
|
||||
var choice = {
|
||||
@ -47,7 +52,9 @@ ListFacet.prototype.getJSON = 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) {
|
||||
@ -64,6 +71,9 @@ ListFacet.prototype.updateState = function(data) {
|
||||
this._selection = selection;
|
||||
this._reSortChoices();
|
||||
|
||||
this._blankChoice = data.blankChoice || null;
|
||||
this._errorChoice = data.errorChoice || null;
|
||||
|
||||
this.render();
|
||||
};
|
||||
|
||||
@ -107,7 +117,10 @@ ListFacet.prototype.render = function() {
|
||||
if (this._data == null) {
|
||||
bodyDiv.html("Loading...");
|
||||
} 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) {
|
||||
var reset = function() {
|
||||
self._reset();
|
||||
@ -117,8 +130,8 @@ ListFacet.prototype.render = function() {
|
||||
);
|
||||
}
|
||||
|
||||
var renderChoice = function(choice) {
|
||||
var label = choice.v.l;
|
||||
var renderChoice = function(choice, customLabel) {
|
||||
var label = customLabel || choice.v.l;
|
||||
var count = choice.c;
|
||||
|
||||
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++) {
|
||||
renderChoice(choices[i]);
|
||||
}
|
||||
if (this._blankChoice != null) {
|
||||
renderChoice(this._blankChoice, "(blank)");
|
||||
}
|
||||
if (this._errorChoice != null) {
|
||||
renderChoice(this._errorChoice, "(error)");
|
||||
}
|
||||
|
||||
bodyDiv[0].scrollTop = scrollTop;
|
||||
|
||||
@ -190,16 +209,31 @@ ListFacet.prototype.render = function() {
|
||||
ListFacet.prototype._select = function(choice, only) {
|
||||
if (only) {
|
||||
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();
|
||||
};
|
||||
|
||||
ListFacet.prototype._deselect = function(choice) {
|
||||
for (var i = this._selection.length - 1; i >= 0; i--) {
|
||||
if (this._selection[i] == choice) {
|
||||
this._selection.splice(i, 1);
|
||||
break;
|
||||
if (choice === this._errorChoice || choice === this._blankChoice) {
|
||||
choice.s = false;
|
||||
} else {
|
||||
for (var i = this._selection.length - 1; i >= 0; i--) {
|
||||
if (this._selection[i] === choice) {
|
||||
this._selection.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
this._updateRest();
|
||||
@ -207,6 +241,9 @@ ListFacet.prototype._deselect = function(choice) {
|
||||
|
||||
ListFacet.prototype._reset = function() {
|
||||
this._selection = [];
|
||||
this._blankChoice = null;
|
||||
this._errorChoice = null;
|
||||
|
||||
this._updateRest();
|
||||
};
|
||||
|
||||
@ -215,7 +252,10 @@ ListFacet.prototype._remove = function() {
|
||||
|
||||
this._div = null;
|
||||
this._config = null;
|
||||
|
||||
this._selection = null;
|
||||
this._blankChoice = null;
|
||||
this._errorChoice = null;
|
||||
this._data = null;
|
||||
};
|
||||
|
||||
|
@ -82,7 +82,7 @@ BrowsingEngine.prototype.removeFacet = function(facet) {
|
||||
}
|
||||
|
||||
if (update) {
|
||||
Gridworks.update({ engineChanged: true }, onFinallyDone);
|
||||
Gridworks.update({ engineChanged: true });
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user