-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#666 TableForm redesign, ListField compiles (but not integrated yet)
- Loading branch information
1 parent
9f7a92f
commit 7222fd2
Showing
12 changed files
with
175 additions
and
61 deletions.
There are no files selected for viewing
14 changes: 9 additions & 5 deletions
14
elements/src/main/java/com/manydesigns/elements/KeyValueAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
package com.manydesigns.elements; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Abstraction over key-value data for Elements forms (e.g. JSON objects, HTTP requests, etc.) | ||
* | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Emanuele Poggi - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public interface KeyValueAccessor { | ||
public static final String copyright = | ||
"Copyright (C) 2005-2020 ManyDesigns srl"; | ||
String copyright = "Copyright (C) 2005-2024 ManyDesigns srl"; | ||
|
||
Object get(String name); | ||
void set(String name, Object value); | ||
boolean has(String name); | ||
|
||
KeyValueAccessor object(String name); | ||
List<KeyValueAccessor> list(String name); | ||
|
||
KeyValueAccessor list(String name); | ||
|
||
KeyValueAccessor atIndex(int index); | ||
|
||
int length(); | ||
} |
36 changes: 36 additions & 0 deletions
36
elements/src/main/java/com/manydesigns/elements/KeyValueListAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.manydesigns.elements; | ||
|
||
import com.manydesigns.elements.json.JSONObjectAccessor; | ||
|
||
public abstract class KeyValueListAccessor implements KeyValueAccessor { | ||
protected KeyValueAccessor currentObjectAccessor; | ||
|
||
@Override | ||
public Object get(String name) { | ||
return currentObjectAccessor != null ? currentObjectAccessor.get(name) : null; | ||
} | ||
|
||
@Override | ||
public void set(String name, Object value) { | ||
if (currentObjectAccessor != null) { | ||
currentObjectAccessor.set(name, value); | ||
} else { | ||
throw new IllegalStateException("No element in the list"); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean has(String name) { | ||
return currentObjectAccessor != null && currentObjectAccessor.has(name); | ||
} | ||
|
||
@Override | ||
public KeyValueAccessor object(String name) { | ||
return currentObjectAccessor != null ? currentObjectAccessor.object(name) : null; | ||
} | ||
|
||
@Override | ||
public KeyValueAccessor list(String name) { | ||
return currentObjectAccessor != null ? currentObjectAccessor.list(name) : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,8 @@ | |
import javax.servlet.http.HttpServletRequest; | ||
import java.lang.reflect.Array; | ||
import java.util.*; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Supplier; | ||
|
||
/** A form with multiple rows, where a row is a collection of fields. Every row has the same fields. | ||
* @author Paolo Predonzani - [email protected] | ||
|
@@ -63,23 +65,18 @@ public class TableForm implements Element { | |
protected boolean condensed = false; | ||
protected boolean striped = true; | ||
|
||
private static final String SELECTION_CELL_CLASS = "selection-cell"; | ||
protected final BiFunction<TableForm, Integer, Row> rowBuilder; | ||
|
||
//************************************************************************** | ||
// Costruttori | ||
//************************************************************************** | ||
private static final String SELECTION_CELL_CLASS = "selection-cell"; | ||
|
||
public TableForm(PropertyAccessor... propertyAccessors) { | ||
public TableForm(BiFunction<TableForm, Integer, Row> rowBuilder, PropertyAccessor... propertyAccessors) { | ||
this.rowBuilder = rowBuilder; | ||
columns = new Column[propertyAccessors.length]; | ||
for (int i = 0; i < columns.length; i++) { | ||
columns[i] = new Column(propertyAccessors[i]); | ||
} | ||
} | ||
|
||
//************************************************************************** | ||
// Implementazione di Element | ||
//************************************************************************** | ||
|
||
public void toXhtml(@NotNull XhtmlBuffer xb) { | ||
xb.openElement("table"); | ||
xb.addAttribute("class", | ||
|
@@ -156,14 +153,12 @@ public void readFromRequest(HttpServletRequest req) { | |
|
||
@Override | ||
public void readFrom(KeyValueAccessor keyValueAccessor) { | ||
Object object = keyValueAccessor.get(); | ||
if (object instanceof Iterable) { | ||
Iterator<?> iterator = ((Iterable<?>) object).iterator(); | ||
for (Row row : rows) { | ||
if (iterator.hasNext()) { | ||
row.readFrom(keyValueAccessor.inner(iterator.next())); | ||
} | ||
} | ||
rows.clear(); | ||
for (int i = 0; i < keyValueAccessor.length(); i++) { | ||
KeyValueAccessor inner = keyValueAccessor.atIndex(i); | ||
Row row = rowBuilder.apply(this, i); | ||
row.readFrom(inner); | ||
rows.add(row); | ||
} | ||
} | ||
|
||
|
@@ -407,6 +402,10 @@ public int getIndex() { | |
} | ||
} | ||
|
||
public Row newRow(int index) { | ||
return new Row(index); | ||
} | ||
|
||
//************************************************************************** | ||
// Inner class: Column | ||
//************************************************************************** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,13 +36,18 @@ | |
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/* | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
/** | ||
* Builder for {@link TableForm}s. | ||
* | ||
* @author Paolo Predonzani - [email protected] | ||
* @author Angelo Lupo - [email protected] | ||
* @author Giampiero Granatella - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public class TableFormBuilder extends AbstractFormBuilder { | ||
public static final String copyright = | ||
"Copyright (C) 2005-2020 ManyDesigns srl"; | ||
|
@@ -63,10 +68,6 @@ public class TableFormBuilder extends AbstractFormBuilder { | |
LoggerFactory.getLogger(TableFormBuilder.class); | ||
|
||
|
||
//************************************************************************** | ||
// Constructors | ||
//************************************************************************** | ||
|
||
public TableFormBuilder(Class aClass) { | ||
this(JavaClassAccessor.getClassAccessor(aClass)); | ||
} | ||
|
@@ -85,11 +86,10 @@ public TableFormBuilder(ClassAccessor classAccessor) { | |
//************************************************************************** | ||
|
||
public TableFormBuilder configFields(String... fieldNames) { | ||
propertyAccessors = new ArrayList<PropertyAccessor>(); | ||
propertyAccessors = new ArrayList<>(); | ||
for (String currentField : fieldNames) { | ||
try { | ||
PropertyAccessor accessor = | ||
classAccessor.getProperty(currentField); | ||
PropertyAccessor accessor = classAccessor.getProperty(currentField); | ||
propertyAccessors.add(accessor); | ||
} catch (NoSuchFieldException e) { | ||
logger.warn("Field not found: {}", currentField); | ||
|
@@ -176,7 +176,7 @@ public TableForm build() { | |
new PropertyAccessor[propertyAccessors.size()]; | ||
propertyAccessors.toArray(propertyAccessorsArray); | ||
|
||
TableForm tableForm = new TableForm(propertyAccessorsArray); | ||
TableForm tableForm = new TableForm(rowBuilder(), propertyAccessorsArray); | ||
|
||
if (StringUtils.isNotBlank(prefix)) { | ||
tableForm.setPrefix(prefix); | ||
|
@@ -185,9 +185,6 @@ public TableForm build() { | |
// set up the columns | ||
setupColumns(tableForm); | ||
|
||
// set up the rows | ||
setupRows(tableForm); | ||
|
||
return tableForm; | ||
} | ||
|
||
|
@@ -202,9 +199,10 @@ protected void setupColumns(TableForm tableForm) { | |
} | ||
} | ||
|
||
protected void setupRows(TableForm tableForm) { | ||
int index = 0; | ||
for (TableForm.Row row : tableForm.getRows()) { | ||
protected BiFunction<TableForm, Integer, TableForm.Row> rowBuilder() { | ||
return (tableForm, index) -> { | ||
TableForm.Row row = tableForm.newRow(index); | ||
|
||
String rowPrefix = | ||
StringUtils.join(new Object[]{prefix, "row", index, "_"}); | ||
|
||
|
@@ -224,8 +222,8 @@ protected void setupRows(TableForm tableForm) { | |
setupSelectionProvidersForRow(tableForm, row, current); | ||
} | ||
|
||
index++; | ||
} | ||
return row; | ||
}; | ||
} | ||
|
||
protected void setupSelectionProvidersForRow(TableForm tableForm, TableForm.Row row, | ||
|
31 changes: 31 additions & 0 deletions
31
elements/src/main/java/com/manydesigns/elements/json/JSONArrayAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.manydesigns.elements.json; | ||
|
||
import com.manydesigns.elements.KeyValueListAccessor; | ||
import org.json.JSONArray; | ||
|
||
public class JSONArrayAccessor extends KeyValueListAccessor { | ||
|
||
private final JSONArray jsonArray; | ||
|
||
public JSONArrayAccessor(JSONArray jsonArray) { | ||
this.jsonArray = jsonArray; | ||
if (!jsonArray.isEmpty()) { | ||
currentObjectAccessor = new JSONObjectAccessor(jsonArray.getJSONObject(0)); | ||
} | ||
} | ||
|
||
protected JSONArrayAccessor(JSONArray jsonArray, JSONObjectAccessor currentAccessor) { | ||
this.jsonArray = jsonArray; | ||
this.currentObjectAccessor = currentAccessor; | ||
} | ||
|
||
@Override | ||
public KeyValueListAccessor atIndex(int index) { | ||
return new JSONArrayAccessor(jsonArray, new JSONObjectAccessor(jsonArray.getJSONObject(index))); | ||
} | ||
|
||
@Override | ||
public int length() { | ||
return jsonArray.length(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ | |
* @author Emanuele Poggi - [email protected] | ||
* @author Alessio Stalla - [email protected] | ||
*/ | ||
public class JsonKeyValueAccessor implements KeyValueAccessor { | ||
public class JSONObjectAccessor implements KeyValueAccessor { | ||
|
||
private final JSONObject jsonObject; | ||
|
||
public JsonKeyValueAccessor(JSONObject jsonObject) { | ||
public JSONObjectAccessor(JSONObject jsonObject) { | ||
this.jsonObject = jsonObject; | ||
} | ||
|
||
|
@@ -42,6 +42,24 @@ public KeyValueAccessor object(String name) { | |
if (jsonObject.isNull(name)) { | ||
return null; | ||
} | ||
return new JsonKeyValueAccessor(jsonObject.getJSONObject(name)); | ||
return new JSONObjectAccessor(jsonObject.getJSONObject(name)); | ||
} | ||
|
||
@Override | ||
public KeyValueAccessor list(String name) { | ||
if (jsonObject.isNull(name)) { | ||
return null; | ||
} | ||
return new JSONArrayAccessor(jsonObject.getJSONArray(name)); | ||
} | ||
|
||
@Override | ||
public KeyValueAccessor atIndex(int index) { | ||
throw new UnsupportedOperationException("Not a list"); | ||
} | ||
|
||
@Override | ||
public int length() { | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.