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;
}
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);
}
}
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;
}
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;
}
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));
}
Aggregations