Search in sources :

Example 1 with ListDataModel

use of jakarta.faces.model.ListDataModel in project myfaces by apache.

the class UIDataRowStateTest method testAddRowAfterSetRowIndex.

@Test
public void testAddRowAfterSetRowIndex() {
    List<Item> list = new ArrayList<Item>();
    int rowCount = 10;
    facesContext.getExternalContext().getRequestMap().put("items", list);
    UIViewRoot root = facesContext.getViewRoot();
    UIData data = new HtmlDataTable();
    data.setId("table");
    root.getChildren().add(data);
    data.setValue(new ListDataModel(list));
    data.setVar("item");
    data.setRows(rowCount);
    UIColumn col = new HtmlColumn();
    data.getChildren().add(col);
    UIOutput text = new HtmlOutputText();
    text.setId("text");
    text.setValue(facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(), "#{item.name}", String.class));
    col.getChildren().add(text);
    data.setRowIndex(-1);
    data.processDecodes(facesContext);
    for (int i = 0; i < rowCount; i++) {
        list.add(new Item(i, "name" + i, "lastName" + i));
    }
    data.processDecodes(facesContext);
}
Also used : HtmlDataTable(jakarta.faces.component.html.HtmlDataTable) UIColumn(jakarta.faces.component.UIColumn) ArrayList(java.util.ArrayList) ListDataModel(jakarta.faces.model.ListDataModel) UIData(jakarta.faces.component.UIData) HtmlColumn(jakarta.faces.component.html.HtmlColumn) UIOutput(jakarta.faces.component.UIOutput) HtmlOutputText(jakarta.faces.component.html.HtmlOutputText) UIViewRoot(jakarta.faces.component.UIViewRoot) Test(org.junit.Test)

Example 2 with ListDataModel

use of jakarta.faces.model.ListDataModel in project myfaces by apache.

the class UIDataRowStateTest method testChangeIdsAfterSetRowIndex2.

@Test
public void testChangeIdsAfterSetRowIndex2() {
    List<Item> list = new ArrayList<Item>();
    int rowCount = 10;
    for (int i = 0; i < rowCount; i++) {
        list.add(new Item(i, "name" + i, "lastName" + i));
    }
    facesContext.getExternalContext().getRequestMap().put("items", list);
    UIViewRoot root = facesContext.getViewRoot();
    UIData data = new HtmlDataTable();
    data.setId("table");
    root.getChildren().add(data);
    data.setValue(new ListDataModel(list));
    data.setVar("item");
    data.setRows(rowCount);
    UIColumn col = new HtmlColumn();
    data.getChildren().add(col);
    UIOutput text = new HtmlOutputText();
    text.setId("text");
    text.setValue(facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(), "#{item.name}", String.class));
    col.getChildren().add(text);
    UIInput inputText = new HtmlInputText();
    inputText.setId("text");
    inputText.setValue(facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(), "#{item.lastName}", String.class));
    col.getChildren().add(inputText);
    for (int i = 0; i < rowCount; i++) {
        data.setRowIndex(i);
        Assert.assertEquals(data.getId() + ":" + i + ":" + text.getId(), text.getClientId());
        Assert.assertEquals(data.getId() + ":" + i + ":" + inputText.getId(), inputText.getClientId());
    }
    data.setRowIndex(-1);
    Assert.assertEquals(data.getId() + ":" + text.getId(), text.getClientId());
    Assert.assertEquals(data.getId() + ":" + inputText.getId(), inputText.getClientId());
}
Also used : HtmlDataTable(jakarta.faces.component.html.HtmlDataTable) UIColumn(jakarta.faces.component.UIColumn) ArrayList(java.util.ArrayList) HtmlInputText(jakarta.faces.component.html.HtmlInputText) UIInput(jakarta.faces.component.UIInput) ListDataModel(jakarta.faces.model.ListDataModel) UIData(jakarta.faces.component.UIData) HtmlColumn(jakarta.faces.component.html.HtmlColumn) UIOutput(jakarta.faces.component.UIOutput) HtmlOutputText(jakarta.faces.component.html.HtmlOutputText) UIViewRoot(jakarta.faces.component.UIViewRoot) Test(org.junit.Test)

Example 3 with ListDataModel

use of jakarta.faces.model.ListDataModel in project myfaces by apache.

the class InvokeOnComponentTest method testInvokeOnCompOnUIDataChildren.

public void testInvokeOnCompOnUIDataChildren() throws Exception {
    // column1
    UIColumn c1 = new UIColumn();
    c1.setId("col1");
    UIOutput headerFacet = new UIOutput();
    headerFacet.setValue("HEADER");
    headerFacet.setId("header");
    c1.setHeader(headerFacet);
    UIOutput name = new UIOutput();
    name.setValue("#{data.username}");
    c1.getChildren().add(name);
    // column2
    UIColumn c2 = new UIColumn();
    c2.setId("col2");
    UIOutput secondheaderFacet = new UIOutput();
    secondheaderFacet.setValue("New HEADER");
    secondheaderFacet.setId("header2");
    c2.setHeader(secondheaderFacet);
    UIOutput passwd = new UIOutput();
    passwd.setValue("#{data.password}");
    c2.getChildren().add(passwd);
    // main table
    UIData table = new UIData();
    table.setId("table");
    table.setVar("data");
    table.getChildren().add(c1);
    table.getChildren().add(c2);
    DataModel model = new ListDataModel(createTestData());
    table.setValue(model);
    this.facesContext.getViewRoot().getChildren().add(table);
    System.out.println("RC; " + table.getRowCount());
    table.encodeBegin(facesContext);
    System.out.println("RC; " + table.getRowCount());
    this.facesContext.getViewRoot().invokeOnComponent(facesContext, passwd.getClientId(facesContext), cc);
    Mockito.verify(cc, Mockito.never()).invokeContextCallback(facesContext, table);
    Mockito.verify(cc, Mockito.never()).invokeContextCallback(facesContext, passwd);
    Mockito.verify(cc, Mockito.never()).invokeContextCallback(facesContext, c1);
    Mockito.verify(cc, Mockito.never()).invokeContextCallback(facesContext, name);
}
Also used : UIColumn(jakarta.faces.component.UIColumn) UIOutput(jakarta.faces.component.UIOutput) ListDataModel(jakarta.faces.model.ListDataModel) DataModel(jakarta.faces.model.DataModel) ListDataModel(jakarta.faces.model.ListDataModel) UIData(jakarta.faces.component.UIData)

Example 4 with ListDataModel

use of jakarta.faces.model.ListDataModel in project mojarra by eclipse-ee4j.

the class UIRepeat method getDataModel.

private DataModel getDataModel() {
    if (model == null) {
        Object val = getValue();
        if (val == null) {
            Integer begin = getBegin();
            Integer end = getEnd();
            if (end == null) {
                if (begin == null) {
                    model = EMPTY_MODEL;
                } else {
                    throw new IllegalArgumentException("end");
                }
            } else {
                int b = begin == null ? 0 : begin;
                int e = end;
                int d = b < e ? 1 : b > e ? -1 : 0;
                int s = Math.abs(e - b) + 1;
                Integer[] array = new Integer[s];
                for (int i = 0; i < s; i++) {
                    array[i] = b + i * d;
                }
                model = new ArrayDataModel<>(array);
                setBegin(0);
                setEnd(s);
            }
        } else if (val instanceof DataModel) {
            // noinspection unchecked
            model = (DataModel<Object>) val;
        } else if (val instanceof List) {
            // noinspection unchecked
            model = new ListDataModel<>((List<Object>) val);
        } else if (Object[].class.isAssignableFrom(val.getClass())) {
            model = new ArrayDataModel<>((Object[]) val);
        } else if (val instanceof ResultSet) {
            model = new ResultSetDataModel((ResultSet) val);
        } else if (val instanceof Iterable) {
            model = new IterableDataModel<>((Iterable<?>) val);
        } else if (val instanceof Map) {
            model = new IterableDataModel<>(((Map<?, ?>) val).entrySet());
        } else {
            DataModel<?> dataModel = createDataModel(val.getClass());
            if (dataModel != null) {
                dataModel.setWrappedData(val);
                model = dataModel;
            } else {
                model = new ScalarDataModel<>(val);
            }
        }
    }
    return model;
}
Also used : ArrayDataModel(jakarta.faces.model.ArrayDataModel) VisitHint(jakarta.faces.component.visit.VisitHint) IterableDataModel(jakarta.faces.model.IterableDataModel) ResultSetDataModel(jakarta.faces.model.ResultSetDataModel) CdiUtils.createDataModel(com.sun.faces.cdi.CdiUtils.createDataModel) IterableDataModel(jakarta.faces.model.IterableDataModel) ArrayDataModel(jakarta.faces.model.ArrayDataModel) DataModel(jakarta.faces.model.DataModel) ScalarDataModel(jakarta.faces.model.ScalarDataModel) ListDataModel(jakarta.faces.model.ListDataModel) ResultSet(java.sql.ResultSet) List(java.util.List) ResultSetDataModel(jakarta.faces.model.ResultSetDataModel) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ListDataModel

use of jakarta.faces.model.ListDataModel in project myfaces by apache.

the class UIDataRowStateTest method testChangeIdsAfterSetRowIndex.

/**
 */
@Test
public void testChangeIdsAfterSetRowIndex() {
    List<Item> list = new ArrayList<Item>();
    int rowCount = 10;
    for (int i = 0; i < rowCount; i++) {
        list.add(new Item(i, "name" + i, "lastName" + i));
    }
    facesContext.getExternalContext().getRequestMap().put("items", list);
    UIViewRoot root = facesContext.getViewRoot();
    UIData data = new HtmlDataTable();
    data.setId("table");
    root.getChildren().add(data);
    data.setValue(new ListDataModel(list));
    data.setVar("item");
    data.setRows(rowCount);
    UIColumn col = new HtmlColumn();
    data.getChildren().add(col);
    UIOutput text = new HtmlOutputText();
    text.setId("text");
    text.setValue(facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(), "#{item.name}", String.class));
    col.getChildren().add(text);
    for (int i = 0; i < rowCount; i++) {
        data.setRowIndex(i);
        Assert.assertEquals(data.getId() + ":" + i + ":" + text.getId(), text.getClientId());
    }
    data.setRowIndex(-1);
    Assert.assertEquals(data.getId() + ":" + text.getId(), text.getClientId());
}
Also used : HtmlDataTable(jakarta.faces.component.html.HtmlDataTable) UIColumn(jakarta.faces.component.UIColumn) ArrayList(java.util.ArrayList) ListDataModel(jakarta.faces.model.ListDataModel) UIData(jakarta.faces.component.UIData) HtmlColumn(jakarta.faces.component.html.HtmlColumn) UIOutput(jakarta.faces.component.UIOutput) HtmlOutputText(jakarta.faces.component.html.HtmlOutputText) UIViewRoot(jakarta.faces.component.UIViewRoot) Test(org.junit.Test)

Aggregations

ListDataModel (jakarta.faces.model.ListDataModel)9 UIColumn (jakarta.faces.component.UIColumn)6 UIData (jakarta.faces.component.UIData)6 UIOutput (jakarta.faces.component.UIOutput)6 UIViewRoot (jakarta.faces.component.UIViewRoot)4 HtmlColumn (jakarta.faces.component.html.HtmlColumn)4 HtmlDataTable (jakarta.faces.component.html.HtmlDataTable)4 HtmlOutputText (jakarta.faces.component.html.HtmlOutputText)4 DataModel (jakarta.faces.model.DataModel)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 UIInput (jakarta.faces.component.UIInput)2 HtmlInputText (jakarta.faces.component.html.HtmlInputText)2 CdiUtils.createDataModel (com.sun.faces.cdi.CdiUtils.createDataModel)1 VisitHint (jakarta.faces.component.visit.VisitHint)1 ArrayDataModel (jakarta.faces.model.ArrayDataModel)1 IterableDataModel (jakarta.faces.model.IterableDataModel)1 ResultSetDataModel (jakarta.faces.model.ResultSetDataModel)1 ScalarDataModel (jakarta.faces.model.ScalarDataModel)1 PrintWriter (java.io.PrintWriter)1