Search in sources :

Example 1 with AbstractUICommand

use of org.apache.myfaces.tobago.internal.component.AbstractUICommand in project myfaces-tobago by apache.

the class SortingUtils method sort.

private static boolean sort(final FacesContext facesContext, final AbstractUISheet sheet, final Object data, final UIColumn column, final SheetState sheetState, Comparator comparator) {
    final Comparator actualComparator;
    if (data instanceof List || data instanceof Object[]) {
        try {
            final UIComponent child = getFirstSortableChild(column.getChildren());
            if (child != null) {
                final boolean descending = !sheetState.isAscending();
                final String attribute = (child instanceof AbstractUICommand ? Attributes.label : Attributes.value).getName();
                final ValueExpression expression = child.getValueExpression(attribute);
                if (expression != null) {
                    final String var = sheet.getVar();
                    if (var == null) {
                        LOG.error("No sorting performed. Property var of sheet is not set!");
                        addNotSortableMessage(facesContext, column);
                        return false;
                    }
                    actualComparator = new ValueExpressionComparator(facesContext, var, expression, descending, comparator);
                } else {
                    LOG.error("No sorting performed, because no expression found for " + "attribute '{}' in component '{}' with id='{}'! You may check the type of the component!", attribute, child.getClass().getName(), child.getClientId());
                    addNotSortableMessage(facesContext, column);
                    return false;
                }
            } else {
                LOG.error("No sorting performed. Value is not instanceof List or Object[]!");
                addNotSortableMessage(facesContext, column);
                return false;
            }
        } catch (final Exception e) {
            LOG.error("Error while extracting sortMethod :" + e.getMessage(), e);
            addNotSortableMessage(facesContext, column);
            return false;
        }
        // memorize selected rows
        List<Object> selectedDataRows = null;
        if (sheetState.getSelectedRows().size() > 0) {
            selectedDataRows = new ArrayList<>(sheetState.getSelectedRows().size());
            Object dataRow;
            for (final Integer index : sheetState.getSelectedRows()) {
                if (data instanceof List) {
                    dataRow = ((List) data).get(index);
                } else {
                    dataRow = ((Object[]) data)[index];
                }
                selectedDataRows.add(dataRow);
            }
        }
        // do sorting
        if (data instanceof List) {
            Collections.sort((List) data, actualComparator);
        } else {
            // value is instanceof Object[]
            Arrays.sort((Object[]) data, actualComparator);
        }
        // restore selected rows
        if (selectedDataRows != null) {
            sheetState.getSelectedRows().clear();
            for (final Object dataRow : selectedDataRows) {
                int index = -1;
                if (data instanceof List) {
                    for (int i = 0; i < ((List) data).size() && index < 0; i++) {
                        if (dataRow == ((List) data).get(i)) {
                            index = i;
                        }
                    }
                } else {
                    for (int i = 0; i < ((Object[]) data).length && index < 0; i++) {
                        if (dataRow == ((Object[]) data)[i]) {
                            index = i;
                        }
                    }
                }
                if (index >= 0) {
                    sheetState.getSelectedRows().add(index);
                }
            }
        }
    } else {
        // DataModel?, ResultSet, Result or Object
        LOG.warn("Sorting not supported for type '{}'.", data != null ? data.getClass().toString() : "null");
        addNotSortableMessage(facesContext, column);
        return false;
    }
    return true;
}
Also used : AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) UIComponent(jakarta.faces.component.UIComponent) ValueExpressionComparator(org.apache.myfaces.tobago.util.ValueExpressionComparator) Comparator(java.util.Comparator) ValueExpressionComparator(org.apache.myfaces.tobago.util.ValueExpressionComparator) ValueExpression(jakarta.el.ValueExpression) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with AbstractUICommand

use of org.apache.myfaces.tobago.internal.component.AbstractUICommand in project myfaces-tobago by apache.

the class GroupController method sendToListener.

public void sendToListener(final AjaxBehaviorEvent event) {
    LOG.info("AjaxBehaviorEvent called.");
    if (event != null && event.getComponent() instanceof AbstractUICommand) {
        final AbstractUICommand command = (AbstractUICommand) event.getComponent();
        sendTo = command.getLabel();
        LOG.info("AjaxBehaviorEvent called. Current label: '{}'", sendTo);
    }
}
Also used : AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand)

Example 3 with AbstractUICommand

use of org.apache.myfaces.tobago.internal.component.AbstractUICommand in project myfaces-tobago by apache.

the class RendererBase method getBehaviorCommands.

protected CommandMap getBehaviorCommands(final FacesContext facesContext, final ClientBehaviorHolder clientBehaviorHolder) {
    CommandMap commandMap = null;
    for (final Map.Entry<String, List<ClientBehavior>> entry : clientBehaviorHolder.getClientBehaviors().entrySet()) {
        final String eventName = entry.getKey();
        final ClientBehaviorContext clientBehaviorContext = getClientBehaviorContext(facesContext, clientBehaviorHolder, eventName);
        for (final ClientBehavior clientBehavior : entry.getValue()) {
            if (clientBehavior instanceof EventBehavior) {
                final EventBehavior eventBehavior = (EventBehavior) clientBehavior;
                final AbstractUIEvent abstractUIEvent = RenderUtils.getAbstractUIEvent((UIComponent) clientBehaviorHolder, eventBehavior);
                if (abstractUIEvent != null && abstractUIEvent.isRendered() && !abstractUIEvent.isDisabled()) {
                    for (List<ClientBehavior> children : abstractUIEvent.getClientBehaviors().values()) {
                        for (ClientBehavior child : children) {
                            final CommandMap childMap = getCommandMap(facesContext, clientBehaviorContext, child);
                            commandMap = CommandMap.merge(commandMap, childMap);
                        }
                    }
                }
            }
            final CommandMap map = getCommandMap(facesContext, clientBehaviorContext, clientBehavior);
            commandMap = CommandMap.merge(commandMap, map);
        }
    }
    // if there is no explicit behavior (with f:ajax or tc:event), use the command properties as default.
    if ((commandMap == null || commandMap.isEmpty()) && clientBehaviorHolder instanceof AbstractUICommand) {
        if (commandMap == null) {
            commandMap = new CommandMap();
        }
        final AbstractUICommand holder = (AbstractUICommand) clientBehaviorHolder;
        commandMap.addCommand(ClientBehaviors.click, new Command(holder.getClientId(facesContext), holder.getFieldId(facesContext), holder.isTransition(), holder.getTarget(), null, null, ComponentUtils.getConfirmation(holder), null, TobagoClientBehaviorRenderer.createCollapsible(facesContext, holder), holder.isOmit()));
    }
    return commandMap;
}
Also used : AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) List(java.util.List) EventBehavior(org.apache.myfaces.tobago.internal.behavior.EventBehavior) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Map(java.util.Map) AbstractUIEvent(org.apache.myfaces.tobago.internal.component.AbstractUIEvent) ClientBehaviorContext(jakarta.faces.component.behavior.ClientBehaviorContext) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Example 4 with AbstractUICommand

use of org.apache.myfaces.tobago.internal.component.AbstractUICommand in project myfaces-tobago by apache.

the class RenderUtils method getBehaviorCommands.

/**
 * @deprecated since 5.0.0
 */
@Deprecated
public static CommandMap getBehaviorCommands(final FacesContext facesContext, final ClientBehaviorHolder clientBehaviorHolder) {
    CommandMap commandMap = null;
    for (final Map.Entry<String, List<ClientBehavior>> entry : clientBehaviorHolder.getClientBehaviors().entrySet()) {
        final String eventName = entry.getKey();
        final ClientBehaviorContext clientBehaviorContext = getClientBehaviorContext(facesContext, clientBehaviorHolder, eventName);
        for (final ClientBehavior clientBehavior : entry.getValue()) {
            if (clientBehavior instanceof EventBehavior) {
                final EventBehavior eventBehavior = (EventBehavior) clientBehavior;
                final AbstractUIEvent abstractUIEvent = getAbstractUIEvent((UIComponent) clientBehaviorHolder, eventBehavior);
                if (abstractUIEvent != null && abstractUIEvent.isRendered() && !abstractUIEvent.isDisabled()) {
                    for (List<ClientBehavior> children : abstractUIEvent.getClientBehaviors().values()) {
                        for (ClientBehavior child : children) {
                            final CommandMap childMap = getCommandMap(facesContext, clientBehaviorContext, child);
                            commandMap = CommandMap.merge(commandMap, childMap);
                        }
                    }
                }
            }
            final CommandMap map = getCommandMap(facesContext, clientBehaviorContext, clientBehavior);
            commandMap = CommandMap.merge(commandMap, map);
        }
    }
    // if there is no explicit behavior (with f:ajax or tc:event), use the command properties as default.
    if ((commandMap == null || commandMap.isEmpty()) && clientBehaviorHolder instanceof AbstractUICommand) {
        if (commandMap == null) {
            commandMap = new CommandMap();
        }
        final AbstractUICommand holder = (AbstractUICommand) clientBehaviorHolder;
        commandMap.addCommand(ClientBehaviors.click, new Command(holder.getClientId(facesContext), holder.getFieldId(facesContext), holder.isTransition(), holder.getTarget(), null, null, ComponentUtils.getConfirmation(holder), null, TobagoClientBehaviorRenderer.createCollapsible(facesContext, holder), holder.isOmit()));
    }
    return commandMap;
}
Also used : AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) List(java.util.List) EventBehavior(org.apache.myfaces.tobago.internal.behavior.EventBehavior) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Map(java.util.Map) AbstractUIEvent(org.apache.myfaces.tobago.internal.component.AbstractUIEvent) ClientBehaviorContext(jakarta.faces.component.behavior.ClientBehaviorContext) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Example 5 with AbstractUICommand

use of org.apache.myfaces.tobago.internal.component.AbstractUICommand in project myfaces-tobago by apache.

the class JsonUtilsUnitTest method more.

@Test
public void more() {
    final CommandMap map = new CommandMap();
    final AbstractUICommand command = (AbstractUICommand) ComponentUtils.createComponent(facesContext, Tags.button.componentType(), RendererTypes.Button, "command");
    ComponentUtils.setAttribute(command, Attributes.popupClose, "immediate");
    map.setClick(new Command("ns:actionId", null, false, "_blank", StringUtils.join(Arrays.asList("id1", "id2"), ' '), StringUtils.join(Arrays.asList("id1", "id2"), ' '), "Really?", 1000, new Collapse(Collapse.Operation.show, "myId"), true));
    final String expected = ("{" + "'click':{" + "'clientId':'ns:actionId'," + "'transition':false," + "'target':'_blank'," + "'execute':'id1 id2'," + "'render':'id1 id2'," + "'collapse':{" + "'transition':'show'," + "'forId':'myId'" + "}," + "'confirmation':'Really?'," + "'delay':1000," + "'omit':true" + "}" + "}").replaceAll("'", "\"");
    Assertions.assertEquals(expected, JsonUtils.encode(map));
}
Also used : Collapse(org.apache.myfaces.tobago.internal.renderkit.Collapse) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractUICommand (org.apache.myfaces.tobago.internal.component.AbstractUICommand)6 Command (org.apache.myfaces.tobago.internal.renderkit.Command)4 CommandMap (org.apache.myfaces.tobago.internal.renderkit.CommandMap)4 List (java.util.List)3 EventBehavior (org.apache.myfaces.tobago.internal.behavior.EventBehavior)3 AbstractUIEvent (org.apache.myfaces.tobago.internal.component.AbstractUIEvent)3 UIComponent (jakarta.faces.component.UIComponent)2 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)2 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)2 Map (java.util.Map)2 Collapse (org.apache.myfaces.tobago.internal.renderkit.Collapse)2 ValueExpression (jakarta.el.ValueExpression)1 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)1 FacesContext (jakarta.faces.context.FacesContext)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 ClientBehaviors (org.apache.myfaces.tobago.component.ClientBehaviors)1 SupportFieldId (org.apache.myfaces.tobago.component.SupportFieldId)1 ValueExpressionComparator (org.apache.myfaces.tobago.util.ValueExpressionComparator)1 Test (org.junit.jupiter.api.Test)1