Author: pgolawski
Date: 2008-11-12 06:50:19 -0500 (Wed, 12 Nov 2008)
New Revision: 11106
Modified:
trunk/ui/extendedDataTable/src/main/java/org/richfaces/component/ExtendedDataTableState.java
Log:
use JSON format to save and restore state
Modified:
trunk/ui/extendedDataTable/src/main/java/org/richfaces/component/ExtendedDataTableState.java
===================================================================
---
trunk/ui/extendedDataTable/src/main/java/org/richfaces/component/ExtendedDataTableState.java 2008-11-12
11:49:44 UTC (rev 11105)
+++
trunk/ui/extendedDataTable/src/main/java/org/richfaces/component/ExtendedDataTableState.java 2008-11-12
11:50:19 UTC (rev 11106)
@@ -5,7 +5,6 @@
import java.io.Serializable;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
@@ -17,11 +16,17 @@
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
-import org.ajax4jsf.util.HtmlDimensions;
+import org.richfaces.json.JSONArray;
+import org.richfaces.json.JSONCollection;
+import org.richfaces.json.JSONException;
+import org.richfaces.json.JSONMap;
+import org.richfaces.json.JSONObject;
+import org.richfaces.json.JSONStringer;
+import org.richfaces.json.JSONWriter;
import org.richfaces.model.Ordering;
/**
- * @author pawelgo
+ * @author pgolawski
*
*/
public class ExtendedDataTableState implements Serializable {
@@ -30,8 +35,10 @@
public static final String TABLE_STATE_ATTR_NAME = "tableState";
- protected static final String SEP = ":";
+ public static final String NONE_COLUMN_ID = "none";
+ //protected static final String SEP = ":";
+
protected ColumnsOrder columnsOrder;
protected ColumnsVisibility columnsVisibility;
protected ColumnsSizeState columnsSizeState;
@@ -47,22 +54,24 @@
* Converts its state based on table attribute value or create default state if it is
not set.
*/
protected void init(UIExtendedDataTable extendedDataTable){
- //get state value from components attributes
+ //get state value from components attribute
String value = (String)extendedDataTable.getAttributes().get(TABLE_STATE_ATTR_NAME);
- //split state value into parts
- String[] values = fromString(value);
+ JSONMap stateMap = null;
+ if ((value != null) && (value.length() > 0)){
+ try {
+ stateMap = new JSONMap(value);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
//initialize columns order part
- String val = (values != null && values.length>0) ? values[0] : null;
- columnsOrder = ColumnsOrder.getColumnsOrder(extendedDataTable, val);
+ columnsOrder = ColumnsOrder.getColumnsOrder(extendedDataTable, (stateMap == null ? null
: (JSONCollection)stateMap.get("columnsOrder")));
//initialize columns visibility part
- val = (values != null && values.length>1) ? values[1] : null;
- columnsVisibility = ColumnsVisibility.getColumnsVisibility(extendedDataTable, val);
+ columnsVisibility = ColumnsVisibility.getColumnsVisibility(extendedDataTable, (stateMap
== null ? null : (JSONCollection)stateMap.get("columnsVisibility")));
//initialize columns size part
- val = (values != null && values.length>2) ? values[2] : null;
- columnsSizeState = ColumnsSizeState.getColumnsSize(extendedDataTable, val);
+ columnsSizeState = ColumnsSizeState.getColumnsSize(extendedDataTable, (stateMap == null
? null : (JSONMap)stateMap.get("columnsSizeState")));
//initialize column grouping part
- val = (values != null && values.length>3) ? values[3] : null;
- columnGroupingState = ColumnGroupingState.getColumnGropingState(extendedDataTable,
val);
+ columnGroupingState = ColumnGroupingState.getColumnGropingState(extendedDataTable,
(stateMap == null ? null : (JSONMap)stateMap.get("columnGroupingState")));
}//init
/**
@@ -76,24 +85,24 @@
}//publishChanges
/**
- * Converts its state to String representation.
+ * Converts its state to String representation in JSON format.
*/
public String toString(){
- String[] values = new String[4];
- values[0] = columnsOrder.toString();
- values[1] = columnsVisibility.toString();
- values[2] = columnsSizeState.toString();
- values[3] = columnGroupingState.toString();
- StringBuilder builder = new StringBuilder();
- for (String str : values){
- builder.append(str).append(SEP);
- }//for
- return builder.toString();
+ return toJSON().toString();
}//toString
- public String[] fromString(String value){
- return (value == null) ? null : value.split(SEP);
- }//fromString
+ public JSONObject toJSON(){
+ JSONObject result = new JSONObject();
+ try {
+ result.put("columnsOrder", columnsOrder.toJSON());
+ result.put("columnsVisibility", columnsVisibility.toJSON());
+ result.put("columnsSizeState", columnsSizeState.toJSON());
+ result.put("columnGroupingState", columnGroupingState.toJSON());
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
/*
* (non-Javadoc)
@@ -151,19 +160,10 @@
public String getColumnSize(UIComponent column) {
return columnsSizeState.getColumnSize(column);
}
-
+
/*
* (non-Javadoc)
*
- * @see ColumnGroupingState#isGroupingOn()
- */
- public boolean isGroupingOn(){
- return columnGroupingState.isGroupingOn();
- }
-
- /*
- * (non-Javadoc)
- *
* @see ColumnGroupingState#getGroupingColumnId()
*/
public String getGroupingColumnId(){
@@ -222,35 +222,55 @@
private static final long serialVersionUID = 8724163192351491340L;
- private static final String SEP = ";";
-
private static final String DEFAULT_WIDTH = "100";
- private String value;
+ private JSONMap value;
private ColumnsSizeState() {
super();
}
- static ColumnsSizeState getColumnsSize(UIExtendedDataTable extendedDataTable, String
val){
+ static ColumnsSizeState getColumnsSize(UIExtendedDataTable extendedDataTable, JSONMap
map){
ColumnsSizeState columnsSize = new ColumnsSizeState();
- columnsSize.init(extendedDataTable, val);
+ columnsSize.init(extendedDataTable, map);
return columnsSize;
}
/**
* Converts its state from String representation or create default state if it is not
set.
*/
- private void init(UIExtendedDataTable extendedDataTable, String val){
- value = val;
- if ((value == null) || (value.length() == 0))
+ private void init(UIExtendedDataTable extendedDataTable, JSONMap map){
+ value = null;
+ if ((map != null) && (map.size()>0)){
+// //try to restore state from string
+ value = map;
+// try {
+// value = new JSONMap(val);
+// } catch (JSONException e) {
+// e.printStackTrace();
+// }
+ }
+
+ if (value == null){
createDefaultColumnsSizeState(extendedDataTable);
+ }
}
/**
- * Converts its state to String representation.
+ * Converts its state to String representation in JSON format.
*/
public String toString(){
+ if (value == null){
+ return "";
+ }
+ return value.toString();
+ }
+
+ /**
+ * Get state in JSON format.
+ * @return JSON object contains state
+ */
+ public JSONMap toJSON(){
return value;
}
@@ -258,13 +278,16 @@
* Create default column order based on component children.
*/
private void createDefaultColumnsSizeState(UIExtendedDataTable extendedDataTable){
- StringBuilder builder = new StringBuilder();
-
- for (Iterator<UIColumn> iter = extendedDataTable.getChildColumns();
iter.hasNext();) {
- UIColumn col = iter.next();
- builder.append(col.getId().toUpperCase()).append("-").append(getDefaultColumnSize(col)).append(SEP);
+ try {
+ JSONWriter writer = new JSONStringer().object();
+ for (Iterator<UIColumn> iter = extendedDataTable.getChildColumns();
iter.hasNext();) {
+ UIColumn col = iter.next();
+ writer.key(col.getId()).value(getDefaultColumnSize(col));
+ }
+ value = new JSONMap(writer.endObject().toString());
+ } catch (JSONException e) {
+ e.printStackTrace();
}
- value = builder.toString();
}
private String getDefaultColumnSize(UIComponent column){
@@ -275,52 +298,29 @@
public String getColumnSize(UIComponent column){
if (value == null)
return getDefaultColumnSize(column);
- String[] widths = value.split(SEP);
- if (widths != null){
- String colId = column.getId().toUpperCase();
- for (String val : widths){
- if (val.toUpperCase().startsWith(colId+"-")){
- return val.split("-")[1];
- }
- }//for
+ String res = value.get(column.getId()).toString();
+ if (res == null){
+ res = getDefaultColumnSize(column);
}
- return getDefaultColumnSize(column);
+ return res;
}
- private String formatWidth(String value){
- return String.valueOf(HtmlDimensions.decode(value).intValue());
- }
+// private String formatWidth(String value){
+// return String.valueOf(HtmlDimensions.decode(value).intValue());
+// }
public void changeColumnSize(UIExtendedDataTable extendedDataTable, String newValue){
if (value == null)
return;
- Set<String> widths = new
HashSet<String>(Arrays.asList(value.toUpperCase().split(SEP)));
String[] newWidths = newValue.split(";");
int index = 0;
for (Iterator<UIColumn> iter = extendedDataTable.getSortedColumns();
iter.hasNext();) {
UIComponent col = (UIComponent) iter.next();
if (col.isRendered()){
- String colId = col.getId().toUpperCase();
- //remove existing item
- Set<String> toDel = new HashSet<String>();
- for (String val : widths){
- if (val.toUpperCase().startsWith(colId+"-")){
- toDel.add(val);
- }
- }//for
- widths.removeAll(toDel);
- //create new item
- String newWidth = newWidths[index++];
- String item = colId + "-" + newWidth;;
- widths.add(item);
+ String colId = col.getId();
+ value.put(colId, newWidths[index++]);
}//if
}//for
- //build new value
- StringBuilder builder = new StringBuilder();
- for (String val : widths){
- builder.append(val).append(SEP);
- }
- value = builder.toString();
}//changeColumnSize
}//ColumnsSizeState
@@ -329,33 +329,61 @@
private static final long serialVersionUID = 907700564445889954L;
- private static final String SEP = ";";
+ private JSONArray value;
- private String value;
-
private ColumnsOrder() {
super();
}
- static ColumnsOrder getColumnsOrder(UIExtendedDataTable extendedDataTable, String val){
+ static ColumnsOrder getColumnsOrder(UIExtendedDataTable extendedDataTable,
JSONCollection collection){
ColumnsOrder columnsOrder = new ColumnsOrder();
- columnsOrder.init(extendedDataTable, val);
+ columnsOrder.init(extendedDataTable, collection);
return columnsOrder;
}
+ static ColumnsOrder getColumnsOrder(UIExtendedDataTable extendedDataTable, String val)
throws JSONException{
+ ColumnsOrder columnsOrder = new ColumnsOrder();
+ columnsOrder.init(extendedDataTable, new JSONCollection(val));
+ return columnsOrder;
+ }
+
/**
* Converts its state from String representation or create default state if it is not
set.
*/
- private void init(UIExtendedDataTable extendedDataTable, String val){
- value = val;
- if ((value == null) || (value.length() == 0))
+ private void init(UIExtendedDataTable extendedDataTable, JSONCollection collection){
+ value = null;
+ if ((collection != null) && (collection.size()>0)){
+ //try to restore state from string
+ value = new JSONArray(collection);
+// try {
+// value = new JSONArray(val);
+// } catch (JSONException e) {
+// e.printStackTrace();
+// }
+ }
+
+ if (value == null){
createDefaultColumnsOrder(extendedDataTable);
+ }
}
+
+
/**
- * Converts its state to String representation.
+ * Converts its state to String representation in JSON format.
*/
public String toString(){
+ if (value == null){
+ return "";
+ }
+ return value.toString();
+ }
+
+ /**
+ * Get state in JSON format.
+ * @return JSON object contains state
+ */
+ public JSONArray toJSON(){
return value;
}
@@ -363,12 +391,11 @@
* Create default column order based on component children.
*/
private void createDefaultColumnsOrder(UIExtendedDataTable extendedDataTable){
- StringBuilder builder = new StringBuilder();
+ value = new JSONArray();
for (Iterator<UIColumn> iter = extendedDataTable.getChildColumns();
iter.hasNext();) {
- UIColumn child = iter.next();
- builder.append(child.getId().toUpperCase()).append(SEP);
+ UIColumn col = iter.next();
+ value.put(col.getId());
}
- value = builder.toString();
}
/**
@@ -376,13 +403,16 @@
* @param columnId column id to be found
* @return column index or null if not found
*/
- private Integer getColumnIndex(String columnId){
+ private int getColumnIndex(String columnId){
if (value == null)
- return null;
- List<String> list = Arrays.asList(value.toUpperCase().split(SEP));
- if (list.contains(columnId.toUpperCase()))
- return list.indexOf(columnId.toUpperCase());
- return null;
+ return -1;
+ for (int i=0;i<value.length(); i++){
+ Object val = value.opt(i);
+ if (columnId.equals(val)){
+ return i;
+ }
+ }
+ return -1;
}//getColumnIndex
/**
@@ -396,26 +426,26 @@
return;
if (sourceColumnId.equals(targetColumnId))
return;
- List<String> list = new
ArrayList<String>(Arrays.asList(value.toUpperCase().split(SEP)));
+ List<String> list = new ArrayList<String>(value.length());
+ for (int i=0;i<value.length(); i++){
+ list.add(value.optString(i));
+ }
//get index of source column
- int sourceIndex = list.indexOf(sourceColumnId.toUpperCase());
+ int sourceIndex = list.indexOf(sourceColumnId);
//remove from order if exist
if (sourceIndex != -1)
list.remove(sourceIndex);
//get index of target column
- int targetIndex = list.indexOf(targetColumnId.toUpperCase());
+ int targetIndex = list.indexOf(targetColumnId);
//add source column after or before target column
if (targetIndex == -1)//add to end
- list.add(sourceColumnId.toUpperCase());
+ list.add(sourceColumnId);
else{
//add at proper position
- list.add((targetIndex + (dropBefore ? 0 : 1)), sourceColumnId.toUpperCase());
+ list.add((targetIndex + (dropBefore ? 0 : 1)), sourceColumnId);
}
- //convert from List to String
- StringBuilder builder = new StringBuilder();
- for (String str : list)
- builder.append(str).append(SEP);
- value = builder.toString();
+ //convert from list to JSON
+ value = new JSONArray(list);
}
/**
@@ -430,10 +460,10 @@
public int compare(UIComponent o1, UIComponent o2) {
Integer index1 = getColumnIndex(o1.getId());
Integer index2 = getColumnIndex(o2.getId());
- if (index1 == null) {
- return ((index2 == null) ? 0 : 1);
+ if (index1 == -1) {
+ return ((index2 == -1) ? 0 : 1);
}
- return ((index2 == null) ? -1 : index1.compareTo(index2));
+ return ((index2 == -1) ? -1 : index1.compareTo(index2));
}
});
return childs;
@@ -445,27 +475,44 @@
private static final long serialVersionUID = -3923409650272094713L;
- private static final String SEP = ";";
+ //private static final String SEP = ";";
- private String value;
+ private JSONArray value;
private ColumnsVisibility() {
super();
}
- static ColumnsVisibility getColumnsVisibility(UIExtendedDataTable extendedDataTable,
String val){
+ static ColumnsVisibility getColumnsVisibility(UIExtendedDataTable extendedDataTable,
String val) throws JSONException{
ColumnsVisibility columnsVisibility = new ColumnsVisibility();
- columnsVisibility.init(extendedDataTable, val);
+ columnsVisibility.init(extendedDataTable, new JSONCollection(val));
return columnsVisibility;
}
+ static ColumnsVisibility getColumnsVisibility(UIExtendedDataTable extendedDataTable,
JSONCollection collection){
+ ColumnsVisibility columnsVisibility = new ColumnsVisibility();
+ columnsVisibility.init(extendedDataTable, collection);
+ return columnsVisibility;
+ }
+
/**
* Converts its state from String representation or create default state if it is not
set.
*/
- private void init(UIExtendedDataTable extendedDataTable, String val){
- value = val;
- if ((value == null) || (value.length() == 0))
+ private void init(UIExtendedDataTable extendedDataTable, JSONCollection collection){
+ value = null;
+ if ((collection != null) && (collection.size()>0)){
+ //try to restore state from string
+ value = new JSONArray(collection);
+// try {
+// value = new JSONArray(val);
+// } catch (JSONException e) {
+// e.printStackTrace();
+// }
+ }
+
+ if (value == null){
createDefaultColumnsVisibility(extendedDataTable);
+ }
//set visibility flag for all columns
for (Iterator<UIColumn> iter = extendedDataTable.getChildColumns();
iter.hasNext();) {
UIColumn child = iter.next();
@@ -477,9 +524,20 @@
}//init
/**
- * Converts its state to String representation.
+ * Converts its state to String representation in JSON format.
*/
public String toString(){
+ if (value == null){
+ return "";
+ }
+ return value.toString();
+ }
+
+ /**
+ * Get state in JSON format.
+ * @return JSON object contains state
+ */
+ public JSONArray toJSON(){
return value;
}
@@ -487,15 +545,31 @@
* Create default column visibility based on component children.
*/
private void createDefaultColumnsVisibility(UIExtendedDataTable extendedDataTable){
- StringBuilder builder = new StringBuilder();
+ value = new JSONArray();
for (Iterator<UIColumn> iter = extendedDataTable.getChildColumns();
iter.hasNext();) {
- UIColumn kid = iter.next();
- builder.append(kid.getId().toUpperCase()).append(SEP);
+ UIColumn col = iter.next();
+ value.put(col.getId());
}
- value = builder.toString();
}//createDefaultColumnsVisibility
/**
+ * Get column position.
+ * @param columnId column id to be found
+ * @return column position or -1 if not found
+ */
+ private int getColumnPosition(String columnId){
+ if (value == null)
+ return -1;
+ for (int i=0;i<value.length(); i++){
+ Object val = value.opt(i);
+ if (columnId.equals(val)){
+ return i;
+ }
+ }
+ return -1;
+ }//getColumnIndex
+
+ /**
* Get column visibility.
* @param columnId column id to be found
* @return true if column is visible, otherwise false
@@ -503,8 +577,7 @@
boolean isVisible(String columnId){
if (value == null)
return true;
- Set<String> visibleIds = new
HashSet<String>(Arrays.asList(value.toUpperCase().split(SEP)));
- return visibleIds.contains(columnId.toUpperCase());
+ return (value.opt(getColumnPosition(columnId)) != null);
}//isVisible
/**
@@ -517,36 +590,36 @@
return;
UIColumn column = null;
//find column by id
- for (Iterator<UIColumn> iter = extendedDataTable.getChildColumns();
iter.hasNext();) {
- UIColumn col = iter.next();
- if (col.getId().equalsIgnoreCase(columnId)){
- if (col instanceof UIColumn){
- column = (UIColumn) col;
- }
- break;
- }//if
- }//for
- if (column == null)
- return;
+// for (Iterator<UIColumn> iter = extendedDataTable.getChildColumns();
iter.hasNext();) {
+// UIColumn col = iter.next();
+// if (col.getId().equalsIgnoreCase(columnId)){
+// if (col instanceof UIColumn){
+// column = (UIColumn) col;
+// }
+// break;
+// }//if
+// }//for
+// if (column == null)
+// return;
boolean visible = column.isVisible();
//toggle visibility
visible = !visible;
//set visibility flag for column
column.setVisible(visible);
- Set<String> visibleIds = new
HashSet<String>(Arrays.asList(value.toUpperCase().split(SEP)));
+ Set<String> visibleIds = new HashSet<String>(value.length());
+ for (int i=0;i<value.length(); i++){
+ visibleIds.add(value.optString(i));
+ }
if (visible){
//add id to set
- visibleIds.add(columnId.toUpperCase());
+ visibleIds.add(columnId);
}
else{
//remove id from list
- visibleIds.remove(columnId.toUpperCase());
+ visibleIds.remove(columnId);
}
- //convert from Set to String
- StringBuilder builder = new StringBuilder();
- for (String str : visibleIds)
- builder.append(str).append(SEP);
- value = builder.toString();
+ //convert from set to JSON
+ value = new JSONArray(visibleIds);
}//changeVisibility
}//ColumnsVisibility
@@ -555,7 +628,6 @@
private static final long serialVersionUID = -3923409650272094713L;
- private static final String SEP = ";";
//private static final String TRUE = "1";
//private static final String FALSE = "0";
private static final Boolean DEF = Boolean.TRUE;//expanded
@@ -563,64 +635,71 @@
private String columnId;
private List<Boolean> groupExpanded;
private Ordering ordering;
- //private String value;
private ColumnGroupingState() {
super();
}
- static ColumnGroupingState getColumnGropingState(UIExtendedDataTable extendedDataTable,
String val){
+ static ColumnGroupingState getColumnGropingState(UIExtendedDataTable extendedDataTable,
JSONMap map){
ColumnGroupingState groupingState = new ColumnGroupingState();
- groupingState.init(extendedDataTable, val);
+ groupingState.init(extendedDataTable, map);
return groupingState;
}
/**
* Converts its state from String representation or create default state if it is not
set.
*/
- private void init(UIExtendedDataTable extendedDataTable, String val){
+ private void init(UIExtendedDataTable extendedDataTable, JSONMap map){
columnId = null;
ordering = Ordering.UNSORTED;
groupExpanded = new ArrayList<Boolean>();
- if ((val == null) || (val.length() == 0))
- return;
- List<String> tmp = Arrays.asList(val.split(SEP));
- if (!tmp.isEmpty()){
- columnId = tmp.get(0); //column id
- ordering = Ordering.valueOf(tmp.get(1)); //sort order
- if (ordering == null)
- ordering = Ordering.UNSORTED;
-// tmp = tmp.subList(2, tmp.size());//remove fist and second item
-// for (String s : tmp){
-// groupExpanded.add(Boolean.valueOf(s.equals(TRUE)));
-// }//for
+ if ((map != null) && (map.size() > 0)){
+ //try to restore state from string
+// try {
+ columnId = (String)map.get("columnId");
+ ordering = Ordering.valueOf((String)map.get("order"));
+// } catch (JSONException e) {
+// e.printStackTrace();
+// }
}
- //get column by id and set sort order
- for (Iterator<UIColumn> columns = extendedDataTable.getChildColumns();
columns.hasNext(); ){
- UIColumn child = columns.next();
- if (columnId.equalsIgnoreCase(child.getId())) {
- child.setSortOrder(ordering);
- break;
+
+ if (columnId != null){
+ //get column by id and set sort order
+ for (Iterator<UIColumn> columns = extendedDataTable.getChildColumns();
columns.hasNext(); ){
+ UIColumn child = columns.next();
+ if (columnId.equalsIgnoreCase(child.getId())) {
+ child.setSortOrder(ordering);
+ break;
+ }
}
}
}//init
/**
- * Converts its state to String representation.
+ * Converts its state to String representation in JSON format.
*/
public String toString(){
- if (columnId == null)
- return "";
- StringBuilder builder = new StringBuilder();
- builder.append(columnId).append(SEP); //add column id
- builder.append(ordering).append(SEP); //add sort order
-// for (Boolean b : groupExpanded){
-// builder.append(b ? TRUE : FALSE).append(SEP);
-// }
- return builder.toString();
+ return toJSON().toString();
}
/**
+ * Get state in JSON format.
+ * @return JSON object contains state
+ */
+ public JSONObject toJSON(){
+ JSONObject result = new JSONObject();
+ if (columnId != null){
+ try {
+ result.put("columnId", columnId);
+ result.put("order", ordering);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+ return result;
+ }
+
+ /**
* Gets grouped column id.
* @return grouped column id if grouping is on, otherwise false
*/
@@ -629,14 +708,6 @@
}
/**
- * Checks if grouping is on.
- * @return true if grouping is on, otherwise false
- */
- boolean isGroupingOn(){
- return (columnId != null);
- }
-
- /**
* Turn on grouping for column.
* @param colId id of column to be grouped
* @param ordering sort order
@@ -659,7 +730,7 @@
* Turn off grouping.
*/
void disableGrouping(){
- columnId = null;
+ columnId = ExtendedDataTableState.NONE_COLUMN_ID;
ordering = Ordering.UNSORTED;
resetGroupVisibilityState();
}
@@ -696,4 +767,4 @@
return groupExpanded.get(index).booleanValue();
}
-}//ColumnGroupingState
\ No newline at end of file
+}//ColumnGroupingState