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

File ColumnsLayoutManager.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
23
3
1
118
65
9
0.39
7.67
3
3

Classes

Class Line # Actions
ColumnsLayoutManager 47 23 0% 9 7
0.8157894681.6%
 

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.container;
21   
22    import java.util.HashMap;
23    import java.util.List;
24    import java.util.Map;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.block.GroupBlock;
34    import org.xwiki.rendering.block.match.ClassBlockMatcher;
35    import org.xwiki.rendering.macro.container.LayoutManager;
36    import org.xwiki.skinx.SkinExtension;
37   
38    /**
39    * Layout manager implementation to layout the group blocks inside a container as columns.
40    *
41    * @version $Id: 1bfb4a54caebbe27f216e295dc7fd7a654e3805a $
42    * @since 2.5M2
43    */
44    @Component
45    @Named("columns")
46    @Singleton
 
47    public class ColumnsLayoutManager implements LayoutManager
48    {
49    /**
50    * The name of the parameter to convey style information to the HTML (html style attribute).
51    */
52    private static final String CLASS_ATTRIBUTE = "class";
53   
54    /**
55    * The javascript file skin extension, to fetch the columns layout css.
56    */
57    @Inject
58    @Named("ssfx")
59    private SkinExtension ssfx;
60   
 
61  3 toggle @Override
62    public void layoutContainer(Block container)
63    {
64  3 List<GroupBlock> innerGroups =
65    container.getBlocks(new ClassBlockMatcher(GroupBlock.class), Block.Axes.CHILD);
66    // FIXME Should we cry and throw an exception if ever we meet something else than a group right under
67    // the container macro, or automatically put it in a group maybe ?
68   
69  3 int count = innerGroups.size();
70   
71  3 if (innerGroups.size() == 0) {
72    // nothing inside, nothing to layout
73  0 return;
74    }
75   
76  3 Map<String, Object> skinxParams = new HashMap<String, Object>();
77  3 skinxParams.put("forceSkinAction", true);
78   
79  3 ssfx.use("uicomponents/container/columns.css", skinxParams);
80   
81    // add styles to all columns inside
82  7 for (int i = 0; i < count; i++) {
83  4 GroupBlock column = innerGroups.get(i);
84  4 String classValue = "column";
85  4 if (i == 0) {
86    // we're at the first element in the list, put a marker. Don't need it to do standard columns layout,
87    // but maybe somebody needs it for customization...
88  3 classValue = classValue + " first-column";
89    }
90  4 if (i == count - 1) {
91    // we're at the last element in the list, put a marker
92  3 classValue = classValue + " last-column";
93    }
94  4 String oldClass = column.getParameter(CLASS_ATTRIBUTE);
95  4 column.setParameter(CLASS_ATTRIBUTE, (StringUtils.isEmpty(oldClass) ? classValue : oldClass + " "
96    + classValue));
97    }
98   
99    // finally, clear the floats introduced by the columns
100  3 Map<String, String> clearFloatsParams = new HashMap<String, String>();
101  3 clearFloatsParams.put(CLASS_ATTRIBUTE, "clearfloats");
102  3 String oldClass = container.getParameter(CLASS_ATTRIBUTE);
103  3 String newClass = "container-columns container-columns-" + count;
104  3 container.setParameter(CLASS_ATTRIBUTE, StringUtils.isEmpty(oldClass) ? newClass : oldClass + " " + newClass);
105  3 container.addChild(new GroupBlock(clearFloatsParams));
106    }
107   
 
108  0 toggle @Override
109    public Object getParameter(String parameterName)
110    {
111  0 return null;
112    }
113   
 
114  0 toggle @Override
115    public void setParameter(String parameterName, Object parameterValue)
116    {
117    }
118    }