2010-02-03 03:29:47 +01:00
|
|
|
package com.metaweb.gridworks.model;
|
2010-01-24 22:09:50 +01:00
|
|
|
|
2010-02-03 21:55:48 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.ObjectInputStream;
|
2010-01-25 23:51:25 +01:00
|
|
|
import java.io.Serializable;
|
2010-02-03 21:55:48 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.HashMap;
|
2010-01-24 22:09:50 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2010-02-01 00:15:50 +01:00
|
|
|
import java.util.Map;
|
2010-01-27 02:48:42 +01:00
|
|
|
import java.util.Properties;
|
2010-01-24 22:09:50 +01:00
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
import org.json.JSONException;
|
2010-02-01 20:16:09 +01:00
|
|
|
import org.json.JSONWriter;
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-02-03 03:29:47 +01:00
|
|
|
import com.metaweb.gridworks.Jsonizable;
|
2010-02-01 20:16:09 +01:00
|
|
|
|
|
|
|
public class ColumnModel implements Serializable, Jsonizable {
|
2010-01-25 23:51:25 +01:00
|
|
|
private static final long serialVersionUID = 7679639795211544511L;
|
|
|
|
|
2010-02-03 22:57:38 +01:00
|
|
|
final public List<Column> columns = new LinkedList<Column>();
|
|
|
|
final public List<ColumnGroup> columnGroups = new LinkedList<ColumnGroup>();
|
2010-01-25 23:51:25 +01:00
|
|
|
|
2010-02-03 22:57:38 +01:00
|
|
|
private int _maxCellIndex;
|
|
|
|
private int _keyCellIndex;
|
2010-02-03 21:55:48 +01:00
|
|
|
|
|
|
|
transient protected Map<String, Column> _nameToColumn;
|
|
|
|
transient protected Map<Integer, Column> _cellIndexToColumn;
|
|
|
|
transient protected List<ColumnGroup> _rootColumnGroups;
|
|
|
|
|
|
|
|
public ColumnModel() {
|
|
|
|
internalInitialize();
|
|
|
|
}
|
|
|
|
|
2010-02-03 22:57:38 +01:00
|
|
|
public void setMaxCellIndex(int maxCellIndex) {
|
|
|
|
this._maxCellIndex = Math.max(this._maxCellIndex, maxCellIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxCellIndex() {
|
|
|
|
return _maxCellIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setKeyCellIndex(int keyCellIndex) {
|
|
|
|
// TODO: check validity of new cell index, e.g., it's not in any group
|
|
|
|
this._keyCellIndex = keyCellIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyCellIndex() {
|
|
|
|
return _keyCellIndex;
|
|
|
|
}
|
|
|
|
|
2010-02-03 21:55:48 +01:00
|
|
|
public void update() {
|
|
|
|
generateMaps();
|
|
|
|
}
|
2010-02-01 00:15:50 +01:00
|
|
|
|
|
|
|
public Column getColumnByName(String name) {
|
|
|
|
return _nameToColumn.get(name);
|
|
|
|
}
|
2010-02-01 04:22:35 +01:00
|
|
|
|
|
|
|
public Column getColumnByCellIndex(int cellIndex) {
|
2010-02-03 21:55:48 +01:00
|
|
|
return _cellIndexToColumn.get(cellIndex);
|
2010-02-01 04:22:35 +01:00
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
2010-02-03 21:55:48 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.key("columns");
|
|
|
|
writer.array();
|
|
|
|
for (Column column : columns) {
|
|
|
|
column.write(writer, options);
|
|
|
|
}
|
|
|
|
writer.endArray();
|
2010-02-03 21:55:48 +01:00
|
|
|
|
2010-02-03 22:57:38 +01:00
|
|
|
writer.key("keyCellIndex"); writer.value(getKeyCellIndex());
|
2010-02-03 21:55:48 +01:00
|
|
|
writer.key("columnGroups");
|
|
|
|
writer.array();
|
|
|
|
for (ColumnGroup g : _rootColumnGroups) {
|
|
|
|
g.write(writer, options);
|
|
|
|
}
|
|
|
|
writer.endArray();
|
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.endObject();
|
|
|
|
}
|
2010-02-03 21:55:48 +01:00
|
|
|
|
|
|
|
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
|
|
|
in.defaultReadObject();
|
|
|
|
internalInitialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void internalInitialize() {
|
|
|
|
generateMaps();
|
|
|
|
|
|
|
|
// Turn the flat list of column groups into a tree
|
|
|
|
|
|
|
|
_rootColumnGroups = new LinkedList<ColumnGroup>(columnGroups);
|
|
|
|
Collections.sort(_rootColumnGroups, new Comparator<ColumnGroup>() {
|
|
|
|
@Override
|
|
|
|
public int compare(ColumnGroup o1, ColumnGroup o2) {
|
|
|
|
int firstDiff = o1.cellIndices[0] - o2.cellIndices[0];
|
|
|
|
return firstDiff != 0 ?
|
|
|
|
firstDiff : // whichever group that starts first goes first
|
|
|
|
(o2.cellIndices.length - o1.cellIndices.length); // otherwise, the larger group goes first
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (int i = _rootColumnGroups.size() - 1; i >= 0; i--) {
|
|
|
|
ColumnGroup g = _rootColumnGroups.get(i);
|
|
|
|
|
|
|
|
for (int j = i + 1; j < _rootColumnGroups.size(); j++) {
|
|
|
|
ColumnGroup g2 = _rootColumnGroups.get(j);
|
|
|
|
if (g2.parentGroup == null && g.contains(g2)) {
|
|
|
|
g2.parentGroup = g;
|
|
|
|
g.subgroups.add(g2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = _rootColumnGroups.size() - 1; i >= 0; i--) {
|
|
|
|
if (_rootColumnGroups.get(i).parentGroup != null) {
|
|
|
|
_rootColumnGroups.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void generateMaps() {
|
|
|
|
_nameToColumn = new HashMap<String, Column>();
|
|
|
|
_cellIndexToColumn = new HashMap<Integer, Column>();
|
|
|
|
|
|
|
|
for (Column column : columns) {
|
2010-02-03 22:57:38 +01:00
|
|
|
_nameToColumn.put(column.getHeaderLabel(), column);
|
|
|
|
_cellIndexToColumn.put(column.getCellIndex(), column);
|
2010-02-03 21:55:48 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|