1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.dashboard

File ColumnsDashboardRenderer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
39
6
2
201
106
14
0.36
6.5
3
2.33

Classes

Class Line # Actions
ColumnGadget 49 9 0% 6 0
1.0100%
ColumnsDashboardRenderer 118 30 0% 8 3
0.928571492.9%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.macro.dashboard;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.Comparator;
25    import java.util.List;
26   
27    import javax.inject.Inject;
28    import javax.inject.Named;
29    import javax.inject.Singleton;
30   
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.rendering.block.Block;
34    import org.xwiki.rendering.block.GroupBlock;
35    import org.xwiki.rendering.macro.MacroExecutionException;
36    import org.xwiki.rendering.macro.container.ContainerMacroParameters;
37    import org.xwiki.rendering.macro.dashboard.DashboardRenderer;
38    import org.xwiki.rendering.macro.dashboard.Gadget;
39    import org.xwiki.rendering.macro.dashboard.GadgetRenderer;
40    import org.xwiki.rendering.transformation.MacroTransformationContext;
41   
42    /**
43    * Specialized gadget that interprets its position as a pair of numbers, the first being the index of the column, and
44    * the second being the index of the gadget inside the column.
45    *
46    * @version $Id: 06ad96f024ddccaad8149a7f42323aa8971c6c83 $
47    * @since 3.0M3
48    */
 
49    class ColumnGadget extends Gadget
50    {
51    /**
52    * The index of the column of this gadget.
53    */
54    private Integer column;
55   
56    /**
57    * The index of this gadget inside its column.
58    */
59    private Integer index;
60   
61    /**
62    * Creates a column gadget which is a copy of the passed gadget.
63    *
64    * @param gadget the gadget to copy into a column gadget.
65    */
 
66  13 toggle ColumnGadget(Gadget gadget)
67    {
68  13 super(gadget.getId(), gadget.getTitle(), gadget.getContent(), gadget.getPosition());
69  13 this.setTitleSource(gadget.getTitleSource());
70    }
71   
72    /**
73    * @return the column
74    */
 
75  95 toggle public Integer getColumn()
76    {
77  95 return this.column;
78    }
79   
80    /**
81    * @return the index
82    */
 
83  47 toggle public Integer getIndex()
84    {
85  47 return this.index;
86    }
87   
 
88  13 toggle @Override
89    public void setPosition(String position)
90    {
91  13 super.setPosition(position);
92   
93    // parse the position as a "container, index" pair and store the container number and index. <br>
94    // TODO: move this code in an API class since the comma separated format is more generic than the columns layout
95    // split by comma, first position is column, second position is index
96  13 String[] split = position.split(",");
97  13 try {
98  13 this.column = Integer.valueOf(split[0].trim());
99  13 this.index = Integer.valueOf(split[1].trim());
100    } catch (ArrayIndexOutOfBoundsException e) {
101    // nothing, just leave column and index null. Not layoutable in columns
102    } catch (NumberFormatException e) {
103    // same, nothing, just leave column and index null. Not layoutable in columns
104    }
105    }
106    }
107   
108    /**
109    * The columns dashboard renderer, that renders the list of passed gadgets in columns, and interprets the positions as
110    * pairs of column number and gadget index.
111    *
112    * @version $Id: 06ad96f024ddccaad8149a7f42323aa8971c6c83 $
113    * @since 3.0M3
114    */
115    @Component
116    @Named("columns")
117    @Singleton
 
118    public class ColumnsDashboardRenderer implements DashboardRenderer
119    {
120    /**
121    * The HTML class attribute name.
122    */
123    protected static final String CLASS = "class";
124   
125    /**
126    * The HTML id attribute name.
127    */
128    protected static final String ID = "id";
129   
130    /**
131    * The component manager, to inject to the {@link BlocksContainerMacro}.
132    */
133    @Inject
134    private ComponentManager componentManager;
135   
 
136  3 toggle @Override
137    public List<Block> renderGadgets(List<Gadget> gadgets, GadgetRenderer gadgetsRenderer,
138    MacroTransformationContext context) throws MacroExecutionException
139    {
140    // transform the passed gagdets in a list of column gadgets
141  3 List<ColumnGadget> columnGadgets = new ArrayList<ColumnGadget>();
142  3 List<Gadget> invalidGadgets = new ArrayList<Gadget>();
143  3 for (Gadget gadget : gadgets) {
144  13 ColumnGadget cGadget = new ColumnGadget(gadget);
145  13 if (cGadget.getColumn() != null && cGadget.getIndex() != null) {
146  13 columnGadgets.add(cGadget);
147    } else {
148  0 invalidGadgets.add(gadget);
149    }
150    }
151   
152    // sort the column gadgets by first the column number then by index in column, for all those which have a valid
153    // position
154  3 Collections.sort(columnGadgets, new Comparator<ColumnGadget>()
155    {
 
156  25 toggle public int compare(ColumnGadget g1, ColumnGadget g2)
157    {
158  25 return g1.getColumn().equals(g2.getColumn()) ? g1.getIndex() - g2.getIndex() : g1.getColumn()
159    - g2.getColumn();
160    }
161    });
162   
163    // get the maximmum column number in the gadgets list and create that number of columns. This is the column
164    // number of the last gadget in the ordered list. Default is 1 column, the empty dashboard is made of one empty
165    // column
166  3 int columns = 1;
167  3 if (!columnGadgets.isEmpty()) {
168    // prevent bad configurations to mess up the dashboard layout
169  3 int lastGadgetsColumn = columnGadgets.get(columnGadgets.size() - 1).getColumn();
170  3 if (lastGadgetsColumn > 1) {
171  1 columns = lastGadgetsColumn;
172    }
173    }
174   
175    // create the list of gadget containers
176  3 List<Block> gadgetContainers = new ArrayList<Block>();
177  7 for (int i = 0; i < columns; i++) {
178  4 GroupBlock gContainer = new GroupBlock();
179  4 gContainer.setParameter(CLASS, DashboardMacro.GADGET_CONTAINER);
180    // and generate the ids of the gadget containers, which are column numbers, 1 based
181  4 gContainer.setParameter(ID, DashboardMacro.GADGET_CONTAINER_PREFIX + (i + 1));
182  4 gadgetContainers.add(gContainer);
183    }
184   
185    // render them as columns using the container macro and appropriate parameters
186  3 ContainerMacroParameters containerParams = new ContainerMacroParameters();
187  3 containerParams.setLayoutStyle("columns");
188  3 BlocksContainerMacro containerMacro = new BlocksContainerMacro();
189  3 containerMacro.setComponentManager(this.componentManager);
190  3 containerMacro.setContent(gadgetContainers);
191  3 List<Block> layoutedResult = containerMacro.execute(containerParams, null, context);
192   
193  3 for (ColumnGadget gadget : columnGadgets) {
194  13 int columnIndex = gadget.getColumn() - 1;
195  13 gadgetContainers.get(columnIndex).addChildren(gadgetsRenderer.decorateGadget(gadget));
196    }
197   
198    // and return the result
199  3 return layoutedResult;
200    }
201    }