PageRenderTime 136ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wicketwebbeans/src/main/java/com/googlecode/wicketwebbeans/containers/BeanGridPanel.java

http://wicket-web-beans.googlecode.com/
Java | 228 lines | 141 code | 32 blank | 55 comment | 16 complexity | fb9039c0d81dda54d0925ecd33c6a262 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*---
  2. Copyright 2006-2007 Visual Systems Corporation.
  3. http://www.vscorp.com
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. ---*/
  14. package com.googlecode.wicketwebbeans.containers;
  15. import java.io.Serializable;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.wicket.AttributeModifier;
  19. import org.apache.wicket.Component;
  20. import org.apache.wicket.behavior.SimpleAttributeModifier;
  21. import org.apache.wicket.markup.ComponentTag;
  22. import org.apache.wicket.markup.html.form.Form;
  23. import org.apache.wicket.markup.html.list.ListItem;
  24. import org.apache.wicket.markup.html.list.ListView;
  25. import org.apache.wicket.markup.html.panel.Panel;
  26. import org.apache.wicket.model.IModel;
  27. import org.apache.wicket.model.Model;
  28. import com.googlecode.wicketwebbeans.actions.BeanActionButton;
  29. import com.googlecode.wicketwebbeans.fields.LabeledField;
  30. import com.googlecode.wicketwebbeans.fields.UnlabeledField;
  31. import com.googlecode.wicketwebbeans.model.BeanMetaData;
  32. import com.googlecode.wicketwebbeans.model.ElementMetaData;
  33. import com.googlecode.wicketwebbeans.model.TabMetaData;
  34. /**
  35. * A panel for generically displaying Java Beans in a grid-style layout.
  36. * The Bean config may specify the number of columns as "cols". The default is 3.
  37. * Elements within the grid may specify a config parameter of "colspan" which indicates the
  38. * number of columns to span in the grid.
  39. * These parameters, along with EMPTY fields, allow for flexible layout.
  40. *
  41. * @author Dan Syrstad
  42. */
  43. public class BeanGridPanel extends Panel
  44. {
  45. public static final String PARAM_COLSPAN = "colspan";
  46. public static final String PARAM_COLS = "cols";
  47. private static final long serialVersionUID = -2149828837634944417L;
  48. private Object bean;
  49. private BeanMetaData beanMetaData;
  50. private TabMetaData tabMetaData;
  51. private boolean showLabels;
  52. private int columns;
  53. /**
  54. * Construct a new BeanGridPanel.
  55. *
  56. * @param id the Wicket id for the panel.
  57. * @param bean the bean to be displayed. This may be an IModel or regular bean object.
  58. * @param beanMetaData the meta data for the bean
  59. */
  60. public BeanGridPanel(String id, final Object bean, final BeanMetaData beanMetaData)
  61. {
  62. this(id, bean, beanMetaData, null);
  63. }
  64. /**
  65. * Construct a new BeanGridPanel.
  66. *
  67. * @param id the Wicket id for the panel.
  68. * @param bean the bean to be displayed. This may be an IModel or regular bean object.
  69. * @param beanMetaData the meta data for the bean
  70. * @param groupMetaData the tab to be displayed. If this is null, all displayed properties
  71. * for the bean will be displayed.
  72. */
  73. public BeanGridPanel(String id, final Object bean, final BeanMetaData beanMetaData, TabMetaData groupMetaData)
  74. {
  75. this(id, bean, beanMetaData, groupMetaData, true);
  76. }
  77. /**
  78. * Construct a new BeanGridPanel.
  79. *
  80. * @param id the Wicket id for the panel.
  81. * @param bean the bean to be displayed. This may be an IModel or regular bean object.
  82. * @param beanMetaData the meta data for the bean
  83. * @param tabMetaData the tab to be displayed. If this is null, all displayed properties
  84. * for the bean will be displayed.
  85. * @param showLabels if true, property labels will be displayed, otherwise they won't.
  86. */
  87. public BeanGridPanel(String id, final Object bean, final BeanMetaData beanMetaData, TabMetaData tabMetaData,
  88. final boolean showLabels)
  89. {
  90. super(id);
  91. this.bean = bean;
  92. this.beanMetaData = beanMetaData;
  93. this.tabMetaData = tabMetaData;
  94. this.showLabels = showLabels;
  95. beanMetaData.applyCss(bean, beanMetaData, this);
  96. List<ElementMetaData> displayedProperties;
  97. if (tabMetaData == null) {
  98. displayedProperties = beanMetaData.getDisplayedElements();
  99. }
  100. else {
  101. displayedProperties = beanMetaData.getTabElements(tabMetaData);
  102. }
  103. // Get Number of rows from config
  104. //Properties config = beanMetaData.getParameters();
  105. columns = beanMetaData.getIntParameter(PARAM_COLS, 3);
  106. if (columns < 1) {
  107. throw new RuntimeException("Invalid columns config value: " + columns);
  108. }
  109. // Break out the rows and columns ahead of time.
  110. List<List<ElementMetaData>> rowsAndCols = new ArrayList<List<ElementMetaData>>();
  111. int colPos = 0;
  112. List<ElementMetaData> currRow = null;
  113. for (ElementMetaData element : displayedProperties) {
  114. int colspan = element.getIntParameter(PARAM_COLSPAN, 1);
  115. if (colspan < 1 || colspan > columns) {
  116. throw new RuntimeException("Invalid colspan parameter value: " + colspan);
  117. }
  118. // If colspan > number of columns left, start a new row.
  119. if ((colPos + colspan) > columns) {
  120. colPos = 0;
  121. }
  122. if (colPos == 0) {
  123. currRow = new ArrayList<ElementMetaData>();
  124. rowsAndCols.add(currRow);
  125. }
  126. currRow.add(element);
  127. colPos += colspan;
  128. if (colPos >= columns) {
  129. colPos = 0;
  130. }
  131. }
  132. Model propModel = new Model<Serializable>((Serializable) rowsAndCols);
  133. add( new RowListView("r", propModel) );
  134. }
  135. @Override
  136. public void detachModels()
  137. {
  138. super.detachModels();
  139. if (bean instanceof IModel) {
  140. ((IModel)bean).detach();
  141. }
  142. }
  143. @Override
  144. protected void onComponentTag(ComponentTag tag)
  145. {
  146. super.onComponentTag(tag);
  147. beanMetaData.warnIfAnyParameterNotConsumed(tabMetaData);
  148. }
  149. private final class RowListView extends ListView
  150. {
  151. private static final long serialVersionUID = 1L;
  152. RowListView(String id, IModel model)
  153. {
  154. super(id, model);
  155. }
  156. protected void populateItem(ListItem item)
  157. {
  158. List<ElementMetaData> columns = (List<ElementMetaData>)item.getModelObject();
  159. item.add( new ColListView("c", new Model((Serializable)columns)));
  160. }
  161. }
  162. private final class ColListView extends ListView
  163. {
  164. private static final long serialVersionUID = 1L;
  165. ColListView(String id, IModel model)
  166. {
  167. super(id, model);
  168. }
  169. protected void populateItem(ListItem item)
  170. {
  171. ElementMetaData element = (ElementMetaData) item.getModelObject();
  172. int colspan = element.getIntParameter(PARAM_COLSPAN, 1);
  173. Component component;
  174. if (element.isAction()) {
  175. Form form = findParent(Form.class);
  176. component = new BeanActionButton("c", element, form, bean);
  177. item.add( new SimpleAttributeModifier("class", "beanActionButtonCell") );
  178. }
  179. else {
  180. component = beanMetaData.getComponentRegistry().getComponent(bean, "c", element);
  181. if (!(component instanceof UnlabeledField) && showLabels) {
  182. component = new LabeledField("c", element.getLabelComponent("l"), component);
  183. }
  184. }
  185. beanMetaData.applyCss(bean, element, component);
  186. item.add( new AttributeModifier(PARAM_COLSPAN, true, new Model<String>(String.valueOf(colspan))) );
  187. int pct100 = (colspan * 10000) / columns;
  188. String width = "width: " + (pct100 / 100) + "." + (pct100 % 100) + "%;";
  189. item.add( new AttributeModifier("style", true, new Model<String>(width)) );
  190. item.add(component);
  191. }
  192. }
  193. }