Skip to content

Commit

Permalink
ViewColumn/ViewLayout usability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Dec 5, 2017
1 parent e83e490 commit 3d8dd2b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/main/java/de/metas/ui/web/view/descriptor/ViewLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,15 @@ public <T extends IViewRow> Builder addElementsFromViewRowClass(final Class<T> v
return this;
}

public <T extends IViewRow> Builder addElementsFromViewRowClassAndFieldNames(final Class<T> viewRowClass, final String... fieldNames)
{
final List<DocumentLayoutElementDescriptor.Builder> elements = ViewColumnHelper.createLayoutElementsForClassAndFieldNames(viewRowClass, fieldNames);
Check.assumeNotEmpty(elements, "elements is not empty"); // shall never happen

addElements(elements);
return this;
}

public boolean hasElements()
{
return !elementBuilders.isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* If empty, the column won't be displayed in any of {@link JSONViewDataType} profiles.
*/
ViewColumnLayout[] layouts() default {};

ViewEditorRenderMode editor() default ViewEditorRenderMode.NEVER;

@Target({ ElementType.FIELD })
Expand All @@ -62,6 +62,12 @@
{
JSONViewDataType when();

/**
* If <code>true</code> if the column shall be displayed by default.
* If <code>false</code> the column will be displayed only on demand, when it was explicitly specified.
*/
boolean displayed() default true;

/** Display sequence number */
int seqNo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;
Expand Down Expand Up @@ -104,6 +103,17 @@ public static List<DocumentLayoutElementDescriptor.Builder> createLayoutElements
.collect(ImmutableList.toImmutableList());
}

public static List<DocumentLayoutElementDescriptor.Builder> createLayoutElementsForClassAndFieldNames(@NonNull final Class<?> dataType, @NonNull final String... fieldNames)
{
Check.assumeNotEmpty(fieldNames, "fieldNames is not empty");

final ClassViewDescriptor descriptor = getDescriptor(dataType);
return Stream.of(fieldNames)
.map(descriptor::getColumnByName)
.map(column -> createLayoutElement(column))
.collect(ImmutableList.toImmutableList());
}

private static ClassViewDescriptor createClassViewDescriptor(final Class<?> dataType)
{
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -133,6 +143,7 @@ private static ClassViewColumnDescriptor createClassViewColumnDescriptor(final F
final ImmutableMap<JSONViewDataType, ClassViewColumnLayoutDescriptor> layoutsByViewType = Stream.of(viewColumnAnn.layouts())
.map(layoutAnn -> ClassViewColumnLayoutDescriptor.builder()
.viewType(layoutAnn.when())
.displayed(layoutAnn.displayed())
.seqNo(layoutAnn.seqNo())
.build())
.collect(GuavaCollectors.toImmutableMapByKey(ClassViewColumnLayoutDescriptor::getViewType));
Expand Down Expand Up @@ -160,7 +171,7 @@ private static DocumentLayoutElementDescriptor.Builder createLayoutElement(final
public static <T extends IViewRow> ImmutableMap<String, Object> extractJsonMap(final T row)
{
final Class<? extends IViewRow> rowClass = row.getClass();
final Map<String, Object> result = new LinkedHashMap<>();
final LinkedHashMap<String, Object> result = new LinkedHashMap<>();
getDescriptor(rowClass)
.getColumns()
.forEach(column -> {
Expand Down Expand Up @@ -215,6 +226,14 @@ private ClassViewDescriptor(@Singular ImmutableList<ClassViewColumnDescriptor> c
this.widgetTypesByFieldName = columns.stream()
.collect(ImmutableMap.toImmutableMap(ClassViewColumnDescriptor::getFieldName, ClassViewColumnDescriptor::getWidgetType));
}

public ClassViewColumnDescriptor getColumnByName(@NonNull final String fieldName)
{
return columns.stream()
.filter(column -> fieldName.equals(column.getFieldName()))
.findFirst()
.orElseThrow(() -> new AdempiereException("No column found for " + fieldName + " in " + this));
}
}

@Value
Expand All @@ -238,7 +257,7 @@ private static final class ClassViewColumnDescriptor
public boolean isDisplayed(final JSONViewDataType viewType)
{
final ClassViewColumnLayoutDescriptor layout = layoutsByViewType.get(viewType);
return layout != null;
return layout != null && layout.isDisplayed();
}

public int getSeqNo(final JSONViewDataType viewType)
Expand All @@ -260,6 +279,7 @@ private static final class ClassViewColumnLayoutDescriptor
{
@NonNull
private JSONViewDataType viewType;
private final boolean displayed;
private final int seqNo;
}
}

0 comments on commit 3d8dd2b

Please sign in to comment.