Made fixed width column guessing slightly better.

Made sure fixed width parser UI take into account the File column.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2202 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2011-08-14 02:05:18 +00:00
parent 41e4e1cd70
commit 33d99186ea
2 changed files with 26 additions and 23 deletions

View File

@ -184,16 +184,16 @@ public class FixedWidthImporter extends TabularImportingParserBase {
int startIndex = 0; int startIndex = 0;
for (int c = 0; c < counts.length; c++) { for (int c = 0; c < counts.length; c++) {
int count = counts[c]; int count = counts[c];
if (count == lineCount && c > startIndex) { if (count == lineCount) {
widths.add(c - startIndex + 1); widths.add(c - startIndex + 1);
startIndex = c + 1; startIndex = c + 1;
} }
} }
for (int i = widths.size() - 1; i > 0; i--) { for (int i = widths.size() - 2; i >= 0; i--) {
if (widths.get(i) == 1) { if (widths.get(i) == 1) {
widths.set(i + 1, widths.get(i + 1) + 1);
widths.remove(i); widths.remove(i);
widths.set(i - 1, widths.get(i - 1) + 1);
} }
} }

View File

@ -75,7 +75,7 @@ Refine.FixedWidthParserUI.prototype.confirmReadyToCreateProject = function() {
Refine.FixedWidthParserUI.prototype.getOptions = function() { Refine.FixedWidthParserUI.prototype.getOptions = function() {
var options = { var options = {
columnWidths: [].concat(this._config.columnWidths) columnWidths: this._getColumnWidths()
}; };
var columnNames = $.trim(this._optionContainerElmts.columnNamesInput[0].value).replace(/,\s+/g, ',').split(','); var columnNames = $.trim(this._optionContainerElmts.columnNamesInput[0].value).replace(/,\s+/g, ',').split(',');
@ -188,22 +188,18 @@ Refine.FixedWidthParserUI.prototype._initialize = function() {
}; };
this._optionContainer.find("input").bind("change", onChange); this._optionContainer.find("input").bind("change", onChange);
this._optionContainer.find("select").bind("change", onChange); this._optionContainer.find("select").bind("change", onChange);
};
this._optionContainerElmts.columnWidthsInput.bind("change", function() { Refine.FixedWidthParserUI.prototype._getColumnWidths = function() {
var newColumnWidths = []; var newColumnWidths = [];
var a = $.trim(this.value).replace(/,\s+/g, ',').split(','); var a = $.trim(this._optionContainerElmts.columnWidthsInput[0].value).replace(/,\s+/g, ',').split(',');
for (var i = 0; i < a.length; i++) { for (var i = 0; i < a.length; i++) {
var n = parseInt(a[i]); var n = parseInt(a[i]);
if (isNaN(n)) { if (!isNaN(n)) {
return;
}
newColumnWidths.push(n); newColumnWidths.push(n);
} }
self._config.columnWidths = newColumnWidths; }
onChange(); return newColumnWidths;
});
this._optionContainerElmts.columnNamesInput.bind("change", onChange);
}; };
Refine.FixedWidthParserUI.prototype._scheduleUpdatePreview = function() { Refine.FixedWidthParserUI.prototype._scheduleUpdatePreview = function() {
@ -233,7 +229,7 @@ Refine.FixedWidthParserUI.prototype.updatePreview = function() {
self._controller.getPreviewData(function(projectData) { self._controller.getPreviewData(function(projectData) {
new Refine.FixedWidthPreviewTable( new Refine.FixedWidthPreviewTable(
self, self,
self._config, options,
projectData, projectData,
self._dataContainer self._dataContainer
); );
@ -268,6 +264,7 @@ Refine.FixedWidthPreviewTable.prototype._render = function() {
var columns = this._projectData.columnModel.columns; var columns = this._projectData.columnModel.columns;
var columnWidths = [].concat(this._config.columnWidths); var columnWidths = [].concat(this._config.columnWidths);
var includeFileSources = this._config.includeFileSources;
var addCell = function(tr) { var addCell = function(tr) {
var index = tr.cells.length; var index = tr.cells.length;
@ -286,8 +283,9 @@ Refine.FixedWidthPreviewTable.prototype._render = function() {
var createColumnHeader = function(column, index) { var createColumnHeader = function(column, index) {
var name = column.name; var name = column.name;
if (index < columnWidths.length) { var index2 = index - (includeFileSources ? 1 : 0);
name = name.slice(0, columnWidths[index]); if (index2 >= 0 && index2 < columnWidths.length) {
name = name.slice(0, columnWidths[index2]);
} }
$(addCell(trHead)) $(addCell(trHead))
.addClass("column-header") .addClass("column-header")
@ -299,7 +297,7 @@ Refine.FixedWidthPreviewTable.prototype._render = function() {
} }
/*------------------------------------------------------------ /*------------------------------------------------------------
* Data Cells * Data Rows of Cells
*------------------------------------------------------------ *------------------------------------------------------------
*/ */
@ -346,7 +344,10 @@ Refine.FixedWidthPreviewTable.prototype._render = function() {
renderRow(table.insertRow(table.rows.length), r, row); renderRow(table.insertRow(table.rows.length), r, row);
} }
var pixelOffset = $(trHead.cells[1]).position().left; /*
* Measure font metrics.
*/
var pixelOffset = $(trHead.cells[includeFileSources ? 2 : 1]).position().left;
var testString = '01234567890123456789012345678901234567890123456789'; var testString = '01234567890123456789012345678901234567890123456789';
var testDiv = $('<div>') var testDiv = $('<div>')
.css('position', 'absolute') .css('position', 'absolute')
@ -356,6 +357,9 @@ Refine.FixedWidthPreviewTable.prototype._render = function() {
var pixelsPerChar = testDiv.width() / testString.length; var pixelsPerChar = testDiv.width() / testString.length;
testDiv.remove(); testDiv.remove();
/*
* Render column separators
*/
var columnSeparators = []; var columnSeparators = [];
var columnCharIndexes = []; var columnCharIndexes = [];
var positionColumnSeparator = function(outer, charIndex) { var positionColumnSeparator = function(outer, charIndex) {
@ -378,7 +382,6 @@ Refine.FixedWidthPreviewTable.prototype._render = function() {
} }
} }
self._config.columnWidths = newColumnWidths;
self._parserUI._optionContainerElmts.columnWidthsInput[0].value = newColumnWidths.join(','); self._parserUI._optionContainerElmts.columnWidthsInput[0].value = newColumnWidths.join(',');
self._parserUI.updatePreview(); self._parserUI.updatePreview();
}; };