Search in sources :

Example 1 with ContainerMacroParameters

use of org.xwiki.rendering.macro.container.ContainerMacroParameters in project xwiki-platform by xwiki.

the class ColumnsDashboardRenderer method renderGadgets.

@Override
public List<Block> renderGadgets(List<Gadget> gadgets, GadgetRenderer gadgetsRenderer, MacroTransformationContext context) throws MacroExecutionException {
    // transform the passed gagdets in a list of column gadgets
    List<ColumnGadget> columnGadgets = new ArrayList<ColumnGadget>();
    List<Gadget> invalidGadgets = new ArrayList<Gadget>();
    for (Gadget gadget : gadgets) {
        ColumnGadget cGadget = new ColumnGadget(gadget);
        if (cGadget.getColumn() != null && cGadget.getIndex() != null) {
            columnGadgets.add(cGadget);
        } else {
            invalidGadgets.add(gadget);
        }
    }
    // sort the column gadgets by first the column number then by index in column, for all those which have a valid
    // position
    Collections.sort(columnGadgets, new Comparator<ColumnGadget>() {

        public int compare(ColumnGadget g1, ColumnGadget g2) {
            return g1.getColumn().equals(g2.getColumn()) ? g1.getIndex() - g2.getIndex() : g1.getColumn() - g2.getColumn();
        }
    });
    // get the maximmum column number in the gadgets list and create that number of columns. This is the column
    // number of the last gadget in the ordered list. Default is 1 column, the empty dashboard is made of one empty
    // column
    int columns = 1;
    if (!columnGadgets.isEmpty()) {
        // prevent bad configurations to mess up the dashboard layout
        int lastGadgetsColumn = columnGadgets.get(columnGadgets.size() - 1).getColumn();
        if (lastGadgetsColumn > 1) {
            columns = lastGadgetsColumn;
        }
    }
    // create the list of gadget containers
    List<Block> gadgetContainers = new ArrayList<Block>();
    for (int i = 0; i < columns; i++) {
        GroupBlock gContainer = new GroupBlock();
        gContainer.setParameter(CLASS, DashboardMacro.GADGET_CONTAINER);
        // and generate the ids of the gadget containers, which are column numbers, 1 based
        gContainer.setParameter(ID, DashboardMacro.GADGET_CONTAINER_PREFIX + (i + 1));
        gadgetContainers.add(gContainer);
    }
    // render them as columns using the container macro and appropriate parameters
    ContainerMacroParameters containerParams = new ContainerMacroParameters();
    containerParams.setLayoutStyle("columns");
    BlocksContainerMacro containerMacro = new BlocksContainerMacro();
    containerMacro.setComponentManager(this.componentManager);
    containerMacro.setContent(gadgetContainers);
    List<Block> layoutedResult = containerMacro.execute(containerParams, null, context);
    for (ColumnGadget gadget : columnGadgets) {
        int columnIndex = gadget.getColumn() - 1;
        gadgetContainers.get(columnIndex).addChildren(gadgetsRenderer.decorateGadget(gadget));
    }
    // and return the result
    return layoutedResult;
}
Also used : Gadget(org.xwiki.rendering.macro.dashboard.Gadget) ArrayList(java.util.ArrayList) GroupBlock(org.xwiki.rendering.block.GroupBlock) Block(org.xwiki.rendering.block.Block) GroupBlock(org.xwiki.rendering.block.GroupBlock) ContainerMacroParameters(org.xwiki.rendering.macro.container.ContainerMacroParameters)

Aggregations

ArrayList (java.util.ArrayList)1 Block (org.xwiki.rendering.block.Block)1 GroupBlock (org.xwiki.rendering.block.GroupBlock)1 ContainerMacroParameters (org.xwiki.rendering.macro.container.ContainerMacroParameters)1 Gadget (org.xwiki.rendering.macro.dashboard.Gadget)1