Fixed issue 196: failure and error dialog attempting to remove columns
git-svn-id: http://google-refine.googlecode.com/svn/trunk@2077 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
42ea00d382
commit
f7c33fba45
main/src/com/google/refine/model/changes
@ -37,11 +37,13 @@ import java.io.IOException;
|
|||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.google.refine.history.Change;
|
import com.google.refine.history.Change;
|
||||||
import com.google.refine.model.Column;
|
import com.google.refine.model.Column;
|
||||||
|
import com.google.refine.model.ColumnGroup;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
import com.google.refine.model.Row;
|
import com.google.refine.model.Row;
|
||||||
import com.google.refine.util.Pool;
|
import com.google.refine.util.Pool;
|
||||||
@ -51,6 +53,7 @@ public class ColumnAdditionChange extends ColumnChange {
|
|||||||
final protected int _columnIndex;
|
final protected int _columnIndex;
|
||||||
final protected CellAtRow[] _newCells;
|
final protected CellAtRow[] _newCells;
|
||||||
protected int _newCellIndex = -1;
|
protected int _newCellIndex = -1;
|
||||||
|
protected List<ColumnGroup> _oldColumnGroups;
|
||||||
|
|
||||||
public ColumnAdditionChange(String columnName, int columnIndex, List<CellAtRow> newCells) {
|
public ColumnAdditionChange(String columnName, int columnIndex, List<CellAtRow> newCells) {
|
||||||
_columnName = columnName;
|
_columnName = columnName;
|
||||||
@ -64,9 +67,36 @@ public class ColumnAdditionChange extends ColumnChange {
|
|||||||
if (_newCellIndex < 0) {
|
if (_newCellIndex < 0) {
|
||||||
_newCellIndex = project.columnModel.allocateNewCellIndex();
|
_newCellIndex = project.columnModel.allocateNewCellIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int columnGroupCount = project.columnModel.columnGroups.size();
|
||||||
|
_oldColumnGroups = new ArrayList<ColumnGroup>(columnGroupCount);
|
||||||
|
for (int i = columnGroupCount - 1; i >= 0; i--) {
|
||||||
|
ColumnGroup columnGroup = project.columnModel.columnGroups.get(i);
|
||||||
|
|
||||||
|
_oldColumnGroups.add(columnGroup);
|
||||||
|
|
||||||
|
if (columnGroup.startColumnIndex <= _columnIndex) {
|
||||||
|
if (columnGroup.startColumnIndex + columnGroup.columnSpan > _columnIndex) {
|
||||||
|
// the new column is inserted right in the middle of the group
|
||||||
|
project.columnModel.columnGroups.set(i, new ColumnGroup(
|
||||||
|
columnGroup.startColumnIndex,
|
||||||
|
columnGroup.columnSpan + 1,
|
||||||
|
columnGroup.keyColumnIndex < _columnIndex ?
|
||||||
|
columnGroup.keyColumnIndex :
|
||||||
|
(columnGroup.keyColumnIndex + 1)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// the new column precedes this whole group
|
||||||
|
project.columnModel.columnGroups.set(i, new ColumnGroup(
|
||||||
|
columnGroup.startColumnIndex + 1,
|
||||||
|
columnGroup.columnSpan,
|
||||||
|
columnGroup.keyColumnIndex + 1
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Column column = new Column(_newCellIndex, _columnName);
|
Column column = new Column(_newCellIndex, _columnName);
|
||||||
|
|
||||||
project.columnModel.columns.add(_columnIndex, column);
|
project.columnModel.columns.add(_columnIndex, column);
|
||||||
try {
|
try {
|
||||||
for (CellAtRow cell : _newCells) {
|
for (CellAtRow cell : _newCells) {
|
||||||
@ -75,6 +105,7 @@ public class ColumnAdditionChange extends ColumnChange {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
project.update();
|
project.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,6 +119,9 @@ public class ColumnAdditionChange extends ColumnChange {
|
|||||||
|
|
||||||
project.columnModel.columns.remove(_columnIndex);
|
project.columnModel.columns.remove(_columnIndex);
|
||||||
|
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
project.columnModel.columnGroups.addAll(_oldColumnGroups);
|
||||||
|
|
||||||
project.update();
|
project.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,6 +135,7 @@ public class ColumnAdditionChange extends ColumnChange {
|
|||||||
c.save(writer, options);
|
c.save(writer, options);
|
||||||
writer.write('\n');
|
writer.write('\n');
|
||||||
}
|
}
|
||||||
|
writeOldColumnGroups(writer, options, _oldColumnGroups);
|
||||||
writer.write("/ec/\n"); // end of change marker
|
writer.write("/ec/\n"); // end of change marker
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,6 +144,7 @@ public class ColumnAdditionChange extends ColumnChange {
|
|||||||
int columnIndex = -1;
|
int columnIndex = -1;
|
||||||
int newCellIndex = -1;
|
int newCellIndex = -1;
|
||||||
List<CellAtRow> newCells = null;
|
List<CellAtRow> newCells = null;
|
||||||
|
List<ColumnGroup> oldColumnGroups = null;
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
||||||
@ -131,12 +167,18 @@ public class ColumnAdditionChange extends ColumnChange {
|
|||||||
newCells.add(CellAtRow.load(line, pool));
|
newCells.add(CellAtRow.load(line, pool));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if ("oldColumnGroupCount".equals(field)) {
|
||||||
|
int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
|
||||||
|
|
||||||
|
oldColumnGroups = readOldColumnGroups(reader, oldColumnGroupCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnAdditionChange change = new ColumnAdditionChange(columnName, columnIndex, newCells);
|
ColumnAdditionChange change = new ColumnAdditionChange(columnName, columnIndex, newCells);
|
||||||
change._newCellIndex = newCellIndex;
|
change._newCellIndex = newCellIndex;
|
||||||
|
change._oldColumnGroups = oldColumnGroups != null ?
|
||||||
|
oldColumnGroups : new LinkedList<ColumnGroup>();
|
||||||
|
|
||||||
return change;
|
return change;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,45 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
package com.google.refine.model.changes;
|
package com.google.refine.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 org.json.JSONException;
|
||||||
|
import org.json.JSONWriter;
|
||||||
|
|
||||||
import com.google.refine.history.Change;
|
import com.google.refine.history.Change;
|
||||||
|
import com.google.refine.model.ColumnGroup;
|
||||||
|
|
||||||
abstract public class ColumnChange implements Change {
|
abstract public class ColumnChange implements Change {
|
||||||
|
|
||||||
|
static public void writeOldColumnGroups(Writer writer, Properties options,
|
||||||
|
List<ColumnGroup> oldColumnGroups) throws IOException {
|
||||||
|
writer.write("oldColumnGroupCount=");
|
||||||
|
writer.write(Integer.toString(oldColumnGroups.size())); writer.write('\n');
|
||||||
|
for (ColumnGroup cg : oldColumnGroups) {
|
||||||
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
||||||
|
try {
|
||||||
|
cg.write(jsonWriter, options);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
writer.write('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static public List<ColumnGroup> readOldColumnGroups(
|
||||||
|
LineNumberReader reader, int oldColumnGroupCount) throws Exception {
|
||||||
|
List<ColumnGroup> oldColumnGroups = new ArrayList<ColumnGroup>(oldColumnGroupCount);
|
||||||
|
for (int i = 0; i < oldColumnGroupCount; i++) {
|
||||||
|
String line = reader.readLine();
|
||||||
|
if (line != null) {
|
||||||
|
oldColumnGroups.add(ColumnGroup.load(line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return oldColumnGroups;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,10 +36,14 @@ package com.google.refine.model.changes;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.google.refine.history.Change;
|
import com.google.refine.history.Change;
|
||||||
import com.google.refine.model.Column;
|
import com.google.refine.model.Column;
|
||||||
|
import com.google.refine.model.ColumnGroup;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
import com.google.refine.util.Pool;
|
import com.google.refine.util.Pool;
|
||||||
|
|
||||||
@ -47,6 +51,7 @@ public class ColumnMoveChange extends ColumnChange {
|
|||||||
final protected String _columnName;
|
final protected String _columnName;
|
||||||
final protected int _newColumnIndex;
|
final protected int _newColumnIndex;
|
||||||
protected int _oldColumnIndex;
|
protected int _oldColumnIndex;
|
||||||
|
protected List<ColumnGroup> _oldColumnGroups;
|
||||||
|
|
||||||
public ColumnMoveChange(String columnName, int index) {
|
public ColumnMoveChange(String columnName, int index) {
|
||||||
_columnName = columnName;
|
_columnName = columnName;
|
||||||
@ -57,8 +62,13 @@ public class ColumnMoveChange extends ColumnChange {
|
|||||||
synchronized (project) {
|
synchronized (project) {
|
||||||
_oldColumnIndex = project.columnModel.getColumnIndexByName(_columnName);
|
_oldColumnIndex = project.columnModel.getColumnIndexByName(_columnName);
|
||||||
|
|
||||||
|
if (_oldColumnGroups == null) {
|
||||||
|
_oldColumnGroups = new ArrayList<ColumnGroup>(project.columnModel.columnGroups);
|
||||||
|
}
|
||||||
|
|
||||||
Column column = project.columnModel.columns.remove(_oldColumnIndex);
|
Column column = project.columnModel.columns.remove(_oldColumnIndex);
|
||||||
project.columnModel.columns.add(_newColumnIndex, column);
|
project.columnModel.columns.add(_newColumnIndex, column);
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
|
||||||
project.update();
|
project.update();
|
||||||
}
|
}
|
||||||
@ -69,6 +79,9 @@ public class ColumnMoveChange extends ColumnChange {
|
|||||||
Column column = project.columnModel.columns.remove(_newColumnIndex);
|
Column column = project.columnModel.columns.remove(_newColumnIndex);
|
||||||
project.columnModel.columns.add(_oldColumnIndex, column);
|
project.columnModel.columns.add(_oldColumnIndex, column);
|
||||||
|
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
project.columnModel.columnGroups.addAll(_oldColumnGroups);
|
||||||
|
|
||||||
project.update();
|
project.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,6 +90,7 @@ public class ColumnMoveChange extends ColumnChange {
|
|||||||
writer.write("columnName="); writer.write(_columnName); writer.write('\n');
|
writer.write("columnName="); writer.write(_columnName); writer.write('\n');
|
||||||
writer.write("oldColumnIndex="); writer.write(Integer.toString(_oldColumnIndex)); writer.write('\n');
|
writer.write("oldColumnIndex="); writer.write(Integer.toString(_oldColumnIndex)); writer.write('\n');
|
||||||
writer.write("newColumnIndex="); writer.write(Integer.toString(_newColumnIndex)); writer.write('\n');
|
writer.write("newColumnIndex="); writer.write(Integer.toString(_newColumnIndex)); writer.write('\n');
|
||||||
|
writeOldColumnGroups(writer, options, _oldColumnGroups);
|
||||||
writer.write("/ec/\n"); // end of change marker
|
writer.write("/ec/\n"); // end of change marker
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +98,8 @@ public class ColumnMoveChange extends ColumnChange {
|
|||||||
String columnName = null;
|
String columnName = null;
|
||||||
int oldColumnIndex = -1;
|
int oldColumnIndex = -1;
|
||||||
int newColumnIndex = -1;
|
int newColumnIndex = -1;
|
||||||
|
List<ColumnGroup> oldColumnGroups = null;
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
||||||
int equal = line.indexOf('=');
|
int equal = line.indexOf('=');
|
||||||
@ -97,11 +112,17 @@ public class ColumnMoveChange extends ColumnChange {
|
|||||||
newColumnIndex = Integer.parseInt(value);
|
newColumnIndex = Integer.parseInt(value);
|
||||||
} else if ("columnName".equals(field)) {
|
} else if ("columnName".equals(field)) {
|
||||||
columnName = value;
|
columnName = value;
|
||||||
|
} else if ("oldColumnGroupCount".equals(field)) {
|
||||||
|
int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
|
||||||
|
|
||||||
|
oldColumnGroups = readOldColumnGroups(reader, oldColumnGroupCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnMoveChange change = new ColumnMoveChange(columnName, newColumnIndex);
|
ColumnMoveChange change = new ColumnMoveChange(columnName, newColumnIndex);
|
||||||
change._oldColumnIndex = oldColumnIndex;
|
change._oldColumnIndex = oldColumnIndex;
|
||||||
|
change._oldColumnGroups = oldColumnGroups != null ?
|
||||||
|
oldColumnGroups : new LinkedList<ColumnGroup>();
|
||||||
|
|
||||||
return change;
|
return change;
|
||||||
}
|
}
|
||||||
|
@ -36,19 +36,24 @@ package com.google.refine.model.changes;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.google.refine.history.Change;
|
import com.google.refine.history.Change;
|
||||||
import com.google.refine.model.Cell;
|
import com.google.refine.model.Cell;
|
||||||
import com.google.refine.model.Column;
|
import com.google.refine.model.Column;
|
||||||
|
import com.google.refine.model.ColumnGroup;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
import com.google.refine.model.Row;
|
import com.google.refine.model.Row;
|
||||||
import com.google.refine.util.Pool;
|
import com.google.refine.util.Pool;
|
||||||
|
|
||||||
public class ColumnRemovalChange extends ColumnChange {
|
public class ColumnRemovalChange extends ColumnChange {
|
||||||
final protected int _oldColumnIndex;
|
final protected int _oldColumnIndex;
|
||||||
protected Column _oldColumn;
|
protected Column _oldColumn;
|
||||||
protected CellAtRow[] _oldCells;
|
protected CellAtRow[] _oldCells;
|
||||||
|
protected List<ColumnGroup> _oldColumnGroups;
|
||||||
|
|
||||||
public ColumnRemovalChange(int index) {
|
public ColumnRemovalChange(int index) {
|
||||||
_oldColumnIndex = index;
|
_oldColumnIndex = index;
|
||||||
@ -56,9 +61,44 @@ public class ColumnRemovalChange extends ColumnChange {
|
|||||||
|
|
||||||
public void apply(Project project) {
|
public void apply(Project project) {
|
||||||
synchronized (project) {
|
synchronized (project) {
|
||||||
|
int columnGroupCount = project.columnModel.columnGroups.size();
|
||||||
|
_oldColumnGroups = new ArrayList<ColumnGroup>(columnGroupCount);
|
||||||
|
for (int i = columnGroupCount - 1; i >= 0; i--) {
|
||||||
|
ColumnGroup columnGroup = project.columnModel.columnGroups.get(i);
|
||||||
|
|
||||||
|
_oldColumnGroups.add(columnGroup);
|
||||||
|
|
||||||
|
if (columnGroup.startColumnIndex <= _oldColumnIndex) {
|
||||||
|
if (columnGroup.startColumnIndex + columnGroup.columnSpan > _oldColumnIndex) {
|
||||||
|
// the group starts before or at _oldColumnIndex
|
||||||
|
// but spans to include _oldColumnIndex
|
||||||
|
|
||||||
|
if (columnGroup.keyColumnIndex == _oldColumnIndex) {
|
||||||
|
// the key column is removed, so we remove the whole group
|
||||||
|
project.columnModel.columnGroups.remove(i);
|
||||||
|
} else {
|
||||||
|
// otherwise, the group's span has been reduced by 1
|
||||||
|
project.columnModel.columnGroups.set(i, new ColumnGroup(
|
||||||
|
columnGroup.startColumnIndex,
|
||||||
|
columnGroup.columnSpan - 1,
|
||||||
|
columnGroup.keyColumnIndex < _oldColumnIndex ?
|
||||||
|
columnGroup.keyColumnIndex :
|
||||||
|
(columnGroup.keyColumnIndex - 1)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// the column removed precedes this whole group
|
||||||
|
project.columnModel.columnGroups.set(i, new ColumnGroup(
|
||||||
|
columnGroup.startColumnIndex - 1,
|
||||||
|
columnGroup.columnSpan,
|
||||||
|
columnGroup.keyColumnIndex - 1
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_oldColumn = project.columnModel.columns.remove(_oldColumnIndex);
|
_oldColumn = project.columnModel.columns.remove(_oldColumnIndex);
|
||||||
_oldCells = new CellAtRow[project.rows.size()];
|
_oldCells = new CellAtRow[project.rows.size()];
|
||||||
|
|
||||||
int cellIndex = _oldColumn.getCellIndex();
|
int cellIndex = _oldColumn.getCellIndex();
|
||||||
for (int i = 0; i < _oldCells.length; i++) {
|
for (int i = 0; i < _oldCells.length; i++) {
|
||||||
Row row = project.rows.get(i);
|
Row row = project.rows.get(i);
|
||||||
@ -85,6 +125,9 @@ public class ColumnRemovalChange extends ColumnChange {
|
|||||||
project.rows.get(cell.row).cells.set(cellIndex, cell.cell);
|
project.rows.get(cell.row).cells.set(cellIndex, cell.cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
project.columnModel.columnGroups.addAll(_oldColumnGroups);
|
||||||
|
|
||||||
project.update();
|
project.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,6 +140,8 @@ public class ColumnRemovalChange extends ColumnChange {
|
|||||||
c.save(writer, options);
|
c.save(writer, options);
|
||||||
writer.write('\n');
|
writer.write('\n');
|
||||||
}
|
}
|
||||||
|
writeOldColumnGroups(writer, options, _oldColumnGroups);
|
||||||
|
|
||||||
writer.write("/ec/\n"); // end of change marker
|
writer.write("/ec/\n"); // end of change marker
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,6 +149,7 @@ public class ColumnRemovalChange extends ColumnChange {
|
|||||||
int oldColumnIndex = -1;
|
int oldColumnIndex = -1;
|
||||||
Column oldColumn = null;
|
Column oldColumn = null;
|
||||||
CellAtRow[] oldCells = null;
|
CellAtRow[] oldCells = null;
|
||||||
|
List<ColumnGroup> oldColumnGroups = null;
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
||||||
@ -124,12 +170,18 @@ public class ColumnRemovalChange extends ColumnChange {
|
|||||||
oldCells[i] = CellAtRow.load(line, pool);
|
oldCells[i] = CellAtRow.load(line, pool);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if ("oldColumnGroupCount".equals(field)) {
|
||||||
|
int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
|
||||||
|
|
||||||
|
oldColumnGroups = readOldColumnGroups(reader, oldColumnGroupCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnRemovalChange change = new ColumnRemovalChange(oldColumnIndex);
|
ColumnRemovalChange change = new ColumnRemovalChange(oldColumnIndex);
|
||||||
change._oldColumn = oldColumn;
|
change._oldColumn = oldColumn;
|
||||||
change._oldCells = oldCells;
|
change._oldCells = oldCells;
|
||||||
|
change._oldColumnGroups = oldColumnGroups != null ?
|
||||||
|
oldColumnGroups : new LinkedList<ColumnGroup>();
|
||||||
|
|
||||||
return change;
|
return change;
|
||||||
}
|
}
|
||||||
|
@ -37,11 +37,13 @@ import java.io.IOException;
|
|||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.google.refine.history.Change;
|
import com.google.refine.history.Change;
|
||||||
import com.google.refine.model.Column;
|
import com.google.refine.model.Column;
|
||||||
|
import com.google.refine.model.ColumnGroup;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
import com.google.refine.util.Pool;
|
import com.google.refine.util.Pool;
|
||||||
|
|
||||||
@ -49,6 +51,7 @@ public class ColumnReorderChange extends ColumnChange {
|
|||||||
final protected List<String> _columnNames;
|
final protected List<String> _columnNames;
|
||||||
protected List<Column> _oldColumns;
|
protected List<Column> _oldColumns;
|
||||||
protected List<Column> _newColumns;
|
protected List<Column> _newColumns;
|
||||||
|
protected List<ColumnGroup> _oldColumnGroups;
|
||||||
|
|
||||||
public ColumnReorderChange(List<String> columnNames) {
|
public ColumnReorderChange(List<String> columnNames) {
|
||||||
_columnNames = columnNames;
|
_columnNames = columnNames;
|
||||||
@ -66,10 +69,14 @@ public class ColumnReorderChange extends ColumnChange {
|
|||||||
_newColumns.add(column);
|
_newColumns.add(column);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_oldColumnGroups = new ArrayList<ColumnGroup>(project.columnModel.columnGroups);
|
||||||
}
|
}
|
||||||
|
|
||||||
project.columnModel.columns.clear();
|
project.columnModel.columns.clear();
|
||||||
project.columnModel.columns.addAll(_newColumns);
|
project.columnModel.columns.addAll(_newColumns);
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
|
||||||
project.update();
|
project.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,6 +85,10 @@ public class ColumnReorderChange extends ColumnChange {
|
|||||||
synchronized (project) {
|
synchronized (project) {
|
||||||
project.columnModel.columns.clear();
|
project.columnModel.columns.clear();
|
||||||
project.columnModel.columns.addAll(_oldColumns);
|
project.columnModel.columns.addAll(_oldColumns);
|
||||||
|
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
project.columnModel.columnGroups.addAll(_oldColumnGroups);
|
||||||
|
|
||||||
project.update();
|
project.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,6 +109,7 @@ public class ColumnReorderChange extends ColumnChange {
|
|||||||
c.save(writer);
|
c.save(writer);
|
||||||
writer.write('\n');
|
writer.write('\n');
|
||||||
}
|
}
|
||||||
|
writeOldColumnGroups(writer, options, _oldColumnGroups);
|
||||||
writer.write("/ec/\n"); // end of change marker
|
writer.write("/ec/\n"); // end of change marker
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,6 +117,7 @@ public class ColumnReorderChange extends ColumnChange {
|
|||||||
List<String> columnNames = new ArrayList<String>();
|
List<String> columnNames = new ArrayList<String>();
|
||||||
List<Column> oldColumns = new ArrayList<Column>();
|
List<Column> oldColumns = new ArrayList<Column>();
|
||||||
List<Column> newColumns = new ArrayList<Column>();
|
List<Column> newColumns = new ArrayList<Column>();
|
||||||
|
List<ColumnGroup> oldColumnGroups = null;
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
||||||
@ -135,12 +148,18 @@ public class ColumnReorderChange extends ColumnChange {
|
|||||||
newColumns.add(Column.load(line));
|
newColumns.add(Column.load(line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if ("oldColumnGroupCount".equals(field)) {
|
||||||
|
int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
|
||||||
|
|
||||||
|
oldColumnGroups = readOldColumnGroups(reader, oldColumnGroupCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnReorderChange change = new ColumnReorderChange(columnNames);
|
ColumnReorderChange change = new ColumnReorderChange(columnNames);
|
||||||
change._oldColumns = oldColumns;
|
change._oldColumns = oldColumns;
|
||||||
change._newColumns = newColumns;
|
change._newColumns = newColumns;
|
||||||
|
change._oldColumnGroups = oldColumnGroups != null ?
|
||||||
|
oldColumnGroups : new LinkedList<ColumnGroup>();
|
||||||
|
|
||||||
return change;
|
return change;
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ import java.io.LineNumberReader;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
@ -47,6 +48,7 @@ import org.json.JSONTokener;
|
|||||||
import com.google.refine.history.Change;
|
import com.google.refine.history.Change;
|
||||||
import com.google.refine.model.Cell;
|
import com.google.refine.model.Cell;
|
||||||
import com.google.refine.model.Column;
|
import com.google.refine.model.Column;
|
||||||
|
import com.google.refine.model.ColumnGroup;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
import com.google.refine.model.Row;
|
import com.google.refine.model.Row;
|
||||||
import com.google.refine.util.Pool;
|
import com.google.refine.util.Pool;
|
||||||
@ -67,6 +69,8 @@ public class ColumnSplitChange implements Change {
|
|||||||
protected List<Row> _oldRows;
|
protected List<Row> _oldRows;
|
||||||
protected List<Row> _newRows;
|
protected List<Row> _newRows;
|
||||||
|
|
||||||
|
protected List<ColumnGroup> _oldColumnGroups;
|
||||||
|
|
||||||
public ColumnSplitChange(
|
public ColumnSplitChange(
|
||||||
String columnName,
|
String columnName,
|
||||||
List<String> columnNames,
|
List<String> columnNames,
|
||||||
@ -152,6 +156,49 @@ public class ColumnSplitChange implements Change {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int columnGroupCount = project.columnModel.columnGroups.size();
|
||||||
|
int columnCountChange = _columnNames.size() - (_removeOriginalColumn ? 1 : 0);
|
||||||
|
_oldColumnGroups = new ArrayList<ColumnGroup>(columnGroupCount);
|
||||||
|
for (int i = columnGroupCount - 1; i >= 0; i--) {
|
||||||
|
ColumnGroup columnGroup = project.columnModel.columnGroups.get(i);
|
||||||
|
|
||||||
|
_oldColumnGroups.add(columnGroup);
|
||||||
|
|
||||||
|
if (columnGroup.startColumnIndex <= _columnIndex) {
|
||||||
|
if (columnGroup.startColumnIndex + columnGroup.columnSpan > _columnIndex) {
|
||||||
|
// the column being split is in the middle of the group
|
||||||
|
|
||||||
|
if (columnGroup.keyColumnIndex == _columnIndex) {
|
||||||
|
if (_removeOriginalColumn) {
|
||||||
|
// the key column is being split and removed
|
||||||
|
project.columnModel.columnGroups.remove(i);
|
||||||
|
} else {
|
||||||
|
project.columnModel.columnGroups.set(i, new ColumnGroup(
|
||||||
|
columnGroup.startColumnIndex,
|
||||||
|
columnGroup.columnSpan + columnCountChange,
|
||||||
|
columnGroup.keyColumnIndex
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
project.columnModel.columnGroups.set(i, new ColumnGroup(
|
||||||
|
columnGroup.startColumnIndex,
|
||||||
|
columnGroup.columnSpan + columnCountChange,
|
||||||
|
columnGroup.keyColumnIndex < _columnIndex ?
|
||||||
|
columnGroup.keyColumnIndex :
|
||||||
|
(columnGroup.keyColumnIndex + columnCountChange)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// the new column precedes this whole group
|
||||||
|
project.columnModel.columnGroups.set(i, new ColumnGroup(
|
||||||
|
columnGroup.startColumnIndex + columnCountChange,
|
||||||
|
columnGroup.columnSpan,
|
||||||
|
columnGroup.keyColumnIndex + columnCountChange
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < _rowIndices.size(); i++) {
|
for (int i = 0; i < _rowIndices.size(); i++) {
|
||||||
int r = _rowIndices.get(i);
|
int r = _rowIndices.get(i);
|
||||||
Row newRow = _newRows.get(i);
|
Row newRow = _newRows.get(i);
|
||||||
@ -193,6 +240,9 @@ public class ColumnSplitChange implements Change {
|
|||||||
project.columnModel.columns.remove(_columnIndex + 1);
|
project.columnModel.columns.remove(_columnIndex + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
project.columnModel.columnGroups.addAll(_oldColumnGroups);
|
||||||
|
|
||||||
project.update();
|
project.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -240,6 +290,7 @@ public class ColumnSplitChange implements Change {
|
|||||||
row.save(writer, options);
|
row.save(writer, options);
|
||||||
writer.write('\n');
|
writer.write('\n');
|
||||||
}
|
}
|
||||||
|
ColumnChange.writeOldColumnGroups(writer, options, _oldColumnGroups);
|
||||||
writer.write("/ec/\n"); // end of change marker
|
writer.write("/ec/\n"); // end of change marker
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,6 +308,8 @@ public class ColumnSplitChange implements Change {
|
|||||||
List<Row> oldRows = null;
|
List<Row> oldRows = null;
|
||||||
List<Row> newRows = null;
|
List<Row> newRows = null;
|
||||||
|
|
||||||
|
List<ColumnGroup> oldColumnGroups = null;
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
||||||
int equal = line.indexOf('=');
|
int equal = line.indexOf('=');
|
||||||
@ -337,8 +390,11 @@ public class ColumnSplitChange implements Change {
|
|||||||
newRows.add(Row.load(line, pool));
|
newRows.add(Row.load(line, pool));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if ("oldColumnGroupCount".equals(field)) {
|
||||||
|
int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
|
||||||
|
|
||||||
|
oldColumnGroups = ColumnChange.readOldColumnGroups(reader, oldColumnGroupCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnSplitChange change = new ColumnSplitChange(
|
ColumnSplitChange change = new ColumnSplitChange(
|
||||||
@ -355,7 +411,8 @@ public class ColumnSplitChange implements Change {
|
|||||||
oldRows,
|
oldRows,
|
||||||
newRows
|
newRows
|
||||||
);
|
);
|
||||||
|
change._oldColumnGroups = oldColumnGroups != null ?
|
||||||
|
oldColumnGroups : new LinkedList<ColumnGroup>();
|
||||||
|
|
||||||
return change;
|
return change;
|
||||||
}
|
}
|
||||||
|
@ -37,11 +37,13 @@ import java.io.IOException;
|
|||||||
import java.io.LineNumberReader;
|
import java.io.LineNumberReader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.google.refine.history.Change;
|
import com.google.refine.history.Change;
|
||||||
import com.google.refine.model.Column;
|
import com.google.refine.model.Column;
|
||||||
|
import com.google.refine.model.ColumnGroup;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
import com.google.refine.model.Row;
|
import com.google.refine.model.Row;
|
||||||
import com.google.refine.util.Pool;
|
import com.google.refine.util.Pool;
|
||||||
@ -51,6 +53,7 @@ public class MassRowColumnChange implements Change {
|
|||||||
final protected List<Row> _newRows;
|
final protected List<Row> _newRows;
|
||||||
protected List<Column> _oldColumns;
|
protected List<Column> _oldColumns;
|
||||||
protected List<Row> _oldRows;
|
protected List<Row> _oldRows;
|
||||||
|
protected List<ColumnGroup> _oldColumnGroups;
|
||||||
|
|
||||||
public MassRowColumnChange(List<Column> newColumns, List<Row> newRows) {
|
public MassRowColumnChange(List<Column> newColumns, List<Row> newRows) {
|
||||||
_newColumns = newColumns;
|
_newColumns = newColumns;
|
||||||
@ -59,11 +62,19 @@ public class MassRowColumnChange implements Change {
|
|||||||
|
|
||||||
public void apply(Project project) {
|
public void apply(Project project) {
|
||||||
synchronized (project) {
|
synchronized (project) {
|
||||||
_oldColumns = new ArrayList<Column>(project.columnModel.columns);
|
if (_oldColumnGroups == null) {
|
||||||
_oldRows = new ArrayList<Row>(project.rows);
|
_oldColumnGroups = new ArrayList<ColumnGroup>(project.columnModel.columnGroups);
|
||||||
|
}
|
||||||
|
if (_oldColumns == null) {
|
||||||
|
_oldColumns = new ArrayList<Column>(project.columnModel.columns);
|
||||||
|
}
|
||||||
|
if (_oldRows == null) {
|
||||||
|
_oldRows = new ArrayList<Row>(project.rows);
|
||||||
|
}
|
||||||
|
|
||||||
project.columnModel.columns.clear();
|
project.columnModel.columns.clear();
|
||||||
project.columnModel.columns.addAll(_newColumns);
|
project.columnModel.columns.addAll(_newColumns);
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
|
||||||
project.rows.clear();
|
project.rows.clear();
|
||||||
project.rows.addAll(_newRows);
|
project.rows.addAll(_newRows);
|
||||||
@ -77,6 +88,9 @@ public class MassRowColumnChange implements Change {
|
|||||||
project.columnModel.columns.clear();
|
project.columnModel.columns.clear();
|
||||||
project.columnModel.columns.addAll(_oldColumns);
|
project.columnModel.columns.addAll(_oldColumns);
|
||||||
|
|
||||||
|
project.columnModel.columnGroups.clear();
|
||||||
|
project.columnModel.columnGroups.addAll(_oldColumnGroups);
|
||||||
|
|
||||||
project.rows.clear();
|
project.rows.clear();
|
||||||
project.rows.addAll(_oldRows);
|
project.rows.addAll(_oldRows);
|
||||||
|
|
||||||
@ -105,13 +119,15 @@ public class MassRowColumnChange implements Change {
|
|||||||
row.save(writer, options);
|
row.save(writer, options);
|
||||||
writer.write('\n');
|
writer.write('\n');
|
||||||
}
|
}
|
||||||
|
ColumnChange.writeOldColumnGroups(writer, options, _oldColumnGroups);
|
||||||
writer.write("/ec/\n"); // end of change marker
|
writer.write("/ec/\n"); // end of change marker
|
||||||
}
|
}
|
||||||
|
|
||||||
static public Change load(LineNumberReader reader, Pool pool) throws Exception {
|
static public Change load(LineNumberReader reader, Pool pool) throws Exception {
|
||||||
List<Column> oldColumns = null;
|
List<Column> oldColumns = null;
|
||||||
List<Column> newColumns = null;
|
List<Column> newColumns = null;
|
||||||
|
List<ColumnGroup> oldColumnGroups = null;
|
||||||
|
|
||||||
List<Row> oldRows = null;
|
List<Row> oldRows = null;
|
||||||
List<Row> newRows = null;
|
List<Row> newRows = null;
|
||||||
|
|
||||||
@ -160,12 +176,18 @@ public class MassRowColumnChange implements Change {
|
|||||||
newColumns.add(Column.load(line));
|
newColumns.add(Column.load(line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if ("oldColumnGroupCount".equals(field)) {
|
||||||
|
int oldColumnGroupCount = Integer.parseInt(line.substring(equal + 1));
|
||||||
|
|
||||||
|
oldColumnGroups = ColumnChange.readOldColumnGroups(reader, oldColumnGroupCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MassRowColumnChange change = new MassRowColumnChange(newColumns, newRows);
|
MassRowColumnChange change = new MassRowColumnChange(newColumns, newRows);
|
||||||
change._oldColumns = oldColumns;
|
change._oldColumns = oldColumns;
|
||||||
change._oldRows = oldRows;
|
change._oldRows = oldRows;
|
||||||
|
change._oldColumnGroups = oldColumnGroups != null ?
|
||||||
|
oldColumnGroups : new LinkedList<ColumnGroup>();
|
||||||
|
|
||||||
return change;
|
return change;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user