2010-02-03 21:55:48 +01:00
|
|
|
package com.metaweb.gridworks.model;
|
|
|
|
|
2010-03-07 23:37:26 +01:00
|
|
|
import java.io.Writer;
|
2010-02-03 21:55:48 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
2010-03-07 23:37:26 +01:00
|
|
|
import org.json.JSONObject;
|
2010-02-03 21:55:48 +01:00
|
|
|
import org.json.JSONWriter;
|
|
|
|
|
|
|
|
import com.metaweb.gridworks.Jsonizable;
|
2010-03-07 23:37:26 +01:00
|
|
|
import com.metaweb.gridworks.util.ParsingUtilities;
|
2010-02-03 21:55:48 +01:00
|
|
|
|
2010-03-07 23:37:26 +01:00
|
|
|
public class ColumnGroup implements Jsonizable {
|
|
|
|
final public int startColumnIndex;
|
2010-03-03 05:19:58 +01:00
|
|
|
final public int columnSpan;
|
|
|
|
final public int keyColumnIndex; // could be -1 if there is no key cell
|
|
|
|
|
|
|
|
transient public ColumnGroup parentGroup;
|
2010-03-07 23:37:26 +01:00
|
|
|
transient public List<ColumnGroup> subgroups;
|
2010-03-03 05:19:58 +01:00
|
|
|
|
|
|
|
public ColumnGroup(int startColumnIndex, int columnSpan, int keyColumnIndex) {
|
|
|
|
this.startColumnIndex = startColumnIndex;
|
|
|
|
this.columnSpan = columnSpan;
|
|
|
|
this.keyColumnIndex = keyColumnIndex;
|
|
|
|
internalInitialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
|
|
|
|
|
|
|
writer.key("startColumnIndex"); writer.value(startColumnIndex);
|
|
|
|
writer.key("columnSpan"); writer.value(columnSpan);
|
|
|
|
writer.key("keyColumnIndex"); writer.value(keyColumnIndex);
|
|
|
|
|
2010-03-07 23:37:26 +01:00
|
|
|
if (!"save".equals(options.get("mode"))) {
|
|
|
|
if (subgroups != null && subgroups.size() > 0) {
|
|
|
|
writer.key("subgroups"); writer.array();
|
|
|
|
for (ColumnGroup g : subgroups) {
|
|
|
|
g.write(writer, options);
|
|
|
|
}
|
|
|
|
writer.endArray();
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.endObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean contains(ColumnGroup g) {
|
|
|
|
return (g.startColumnIndex >= startColumnIndex &&
|
|
|
|
g.startColumnIndex < startColumnIndex + columnSpan);
|
|
|
|
}
|
|
|
|
|
2010-03-07 23:37:26 +01:00
|
|
|
public void save(Writer writer) {
|
|
|
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
|
|
|
try {
|
|
|
|
write(jsonWriter, new Properties());
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static public ColumnGroup load(String s) throws Exception {
|
|
|
|
JSONObject obj = ParsingUtilities.evaluateJsonStringToObject(s);
|
|
|
|
|
|
|
|
return new ColumnGroup(
|
|
|
|
obj.getInt("startColumnIndex"),
|
|
|
|
obj.getInt("columnSpan"),
|
|
|
|
obj.getInt("keyColumnIndex")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
protected void internalInitialize() {
|
|
|
|
subgroups = new LinkedList<ColumnGroup>();
|
|
|
|
}
|
2010-02-03 21:55:48 +01:00
|
|
|
}
|