diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/AbstractDataGridView.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/AbstractDataGridView.java index 0ff6d5a3a0b..0cd9d5ef51c 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/AbstractDataGridView.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/AbstractDataGridView.java @@ -149,21 +149,23 @@ protected final void populateItem(final Item item) for (int i = 0; i < populatorsNumber; i++) { ICellPopulator populator = populators.get(i); - IModel> populatorModel = new Model<>(populator); - Item> cellItem = newCellItem(cells.newChildId(), i, populatorModel); - cells.add(cellItem); - - populator.populateItem(cellItem, CELL_ITEM_ID, item.getModel()); - - if (cellItem.get("cell") == null) + if (populator.isVisible()) { - throw new WicketRuntimeException( - populator.getClass().getName() + - ".populateItem() failed to add a component with id [" + - CELL_ITEM_ID + - "] to the provided [cellItem] object. Make sure you call add() on cellItem and make sure you gave the added component passed in 'componentId' id. ( *cellItem*.add(new MyComponent(*componentId*, rowModel) )"); + IModel> populatorModel = new Model<>(populator); + Item> cellItem = newCellItem(cells.newChildId(), i, populatorModel); + cells.add(cellItem); + + populator.populateItem(cellItem, CELL_ITEM_ID, item.getModel()); + + if (cellItem.get("cell") == null) + { + throw new WicketRuntimeException( + populator.getClass().getName() + + ".populateItem() failed to add a component with id [" + + CELL_ITEM_ID + + "] to the provided [cellItem] object. Make sure you call add() on cellItem and make sure you gave the added component passed in 'componentId' id. ( *cellItem*.add(new MyComponent(*componentId*, rowModel) )"); + } } } - } } diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/ICellPopulator.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/ICellPopulator.java index 4fea7bcc260..849a5afadb8 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/ICellPopulator.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/ICellPopulator.java @@ -1,74 +1,84 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.extensions.markup.html.repeater.data.grid; - -import org.apache.wicket.markup.repeater.Item; -import org.apache.wicket.model.IDetachable; -import org.apache.wicket.model.IModel; -import org.apache.wicket.util.io.IClusterable; - -/** - * Represents an object that is capable of populating an {@link Item} container representing a cell - * in a {@link DataGridView} with components. - *

- * Example - *

- * - *

- * class NamePopulator implements ICellPopulator
- * {
- * 	void populateItem(final Item cellItem, final String componentId, final IModel rowModel) {
- *       User user=(User)rowModel.getObject(cellItem);
- *       String name=user.getFirstName()+" "+user.getLastName();
- *       cellItem.add(new Label(componentId, name);
- *     }}
- * 
- * - * In this example the IDataProvider assigned to the DataGridView retrieves User objects from the - * database. The cell populator adds a label to the cell that will display the full name of the - * user. - * - * @see DataGridView - * @see Item - * - * @author Igor Vaynberg (ivaynberg) - * - * @param - * Model object type - */ -public interface ICellPopulator extends IClusterable, IDetachable -{ - /** - * Method used to populate a cell in the {@link DataGridView} - * - * Implementation MUST add a component to the cellItem using the component id provided by - * componentId argument, otherwise a WicketRuntimeException will be thrown - * - * @param cellItem - * the item representing the current table cell being rendered - * @param componentId - * the id of the component used to render the cell (only one component should be - * added to the cell) - * @param rowModel - * the model of the row item being rendered. this model usually contains the model - * provided by the data provider. - * - * @see Item - */ - void populateItem(final Item> cellItem, final String componentId, - final IModel rowModel); -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.extensions.markup.html.repeater.data.grid; + +import org.apache.wicket.markup.repeater.Item; +import org.apache.wicket.model.IDetachable; +import org.apache.wicket.model.IModel; +import org.apache.wicket.util.io.IClusterable; + +/** + * Represents an object that is capable of populating an {@link Item} container representing a cell + * in a {@link DataGridView} with components. + *

+ * Example + *

+ * + *

+ * class NamePopulator implements ICellPopulator
+ * {
+ * 	void populateItem(final Item cellItem, final String componentId, final IModel rowModel) {
+ *       User user=(User)rowModel.getObject(cellItem);
+ *       String name=user.getFirstName()+" "+user.getLastName();
+ *       cellItem.add(new Label(componentId, name);
+ *     }}
+ * 
+ * + * In this example the IDataProvider assigned to the DataGridView retrieves User objects from the + * database. The cell populator adds a label to the cell that will display the full name of the + * user. + * + * @see DataGridView + * @see Item + * + * @author Igor Vaynberg (ivaynberg) + * + * @param + * Model object type + */ +public interface ICellPopulator extends IClusterable, IDetachable +{ + /** + * Method used to populate a cell in the {@link DataGridView} + * + * Implementation MUST add a component to the cellItem using the component id provided by + * componentId argument, otherwise a WicketRuntimeException will be thrown + * + * @param cellItem + * the item representing the current table cell being rendered + * @param componentId + * the id of the component used to render the cell (only one component should be + * added to the cell) + * @param rowModel + * the model of the row item being rendered. this model usually contains the model + * provided by the data provider. + * + * @see Item + */ + void populateItem(final Item> cellItem, final String componentId, + final IModel rowModel); + + /** + * Gets whether this column is visible in a {@link DataGridView} + * + * @return true if column is visible + */ + default boolean isVisible() + { + return true; + }; +} diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/PropertyPopulator.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/PropertyPopulator.java index 145eb69531e..1412cf9ae8a 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/PropertyPopulator.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/grid/PropertyPopulator.java @@ -1,77 +1,77 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.extensions.markup.html.repeater.data.grid; - -import org.apache.wicket.markup.html.basic.Label; -import org.apache.wicket.markup.repeater.Item; -import org.apache.wicket.model.IModel; -import org.apache.wicket.model.PropertyModel; - -/** - * A convenience implementation of {@link ICellPopulator} that adds a label that will display the - * value of the specified property. Non-string properties will be converted to a string before - * display. - *

- * Example - * - *

- * ICellPopulator cityPopulator = new PropertyPopulator("address.city");
- * 
- * - * @param - * @author Igor Vaynberg (ivaynberg) - */ -public class PropertyPopulator implements ICellPopulator -{ - private static final long serialVersionUID = 1L; - private final String property; - - /** - * Constructor - * - * @param property - * property whose value will be displayed in the cell. uses wicket's - * {@link PropertyModel} notation. - */ - public PropertyPopulator(final String property) - { - if (property == null) - { - throw new IllegalArgumentException("argument [property] cannot be null"); - } - this.property = property; - } - - /** - * @see org.apache.wicket.model.IDetachable#detach() - */ - @Override - public void detach() - { - } - - /** - * @see org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator#populateItem(org.apache.wicket.markup.repeater.Item, - * java.lang.String, org.apache.wicket.model.IModel) - */ - @Override - public void populateItem(final Item> cellItem, final String componentId, - final IModel rowModel) - { - cellItem.add(new Label(componentId, new PropertyModel<>(rowModel, property))); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.extensions.markup.html.repeater.data.grid; + +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.repeater.Item; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.PropertyModel; + +/** + * A convenience implementation of {@link ICellPopulator} that adds a label that will display the + * value of the specified property. Non-string properties will be converted to a string before + * display. + *

+ * Example + * + *

+ * ICellPopulator cityPopulator = new PropertyPopulator("address.city");
+ * 
+ * + * @param + * @author Igor Vaynberg (ivaynberg) + */ +public class PropertyPopulator implements ICellPopulator +{ + private static final long serialVersionUID = 1L; + private final String property; + + /** + * Constructor + * + * @param property + * property whose value will be displayed in the cell. uses wicket's + * {@link PropertyModel} notation. + */ + public PropertyPopulator(final String property) + { + if (property == null) + { + throw new IllegalArgumentException("argument [property] cannot be null"); + } + this.property = property; + } + + /** + * @see org.apache.wicket.model.IDetachable#detach() + */ + @Override + public void detach() + { + } + + /** + * @see org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator#populateItem(org.apache.wicket.markup.repeater.Item, + * java.lang.String, org.apache.wicket.model.IModel) + */ + @Override + public void populateItem(final Item> cellItem, final String componentId, + final IModel rowModel) + { + cellItem.add(new Label(componentId, new PropertyModel<>(rowModel, property))); + } +} diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/AbstractColumn.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/AbstractColumn.java index 3ef1f61693d..097c6b6531e 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/AbstractColumn.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/AbstractColumn.java @@ -1,95 +1,95 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.extensions.markup.html.repeater.data.table; - -import org.apache.wicket.Component; -import org.apache.wicket.markup.html.basic.Label; -import org.apache.wicket.model.IModel; - -/** - * A helper implementation for the IColumn interface - * - * @author Igor Vaynberg ( ivaynberg ) - * @param - * the type of the object that will be rendered in this column's cells - * @param - * the type of the sort property - */ -public abstract class AbstractColumn implements IStyledColumn -{ - private static final long serialVersionUID = 1L; - - private final IModel displayModel; - - private final S sortProperty; - - /** - * @param displayModel - * model used to generate header text - * @param sortProperty - * sort property this column represents - */ - public AbstractColumn(final IModel displayModel, final S sortProperty) - { - this.displayModel = displayModel; - this.sortProperty = sortProperty; - } - - /** - * @param displayModel - * model used to generate header text - */ - public AbstractColumn(final IModel displayModel) - { - this(displayModel, null); - } - - /** - * @return returns display model to be used for the header component - */ - public IModel getDisplayModel() - { - return displayModel; - } - - @Override - public S getSortProperty() - { - return sortProperty; - } - - @Override - public Component getHeader(final String componentId) - { - return new Label(componentId, getDisplayModel()); - } - - @Override - public void detach() - { - if (displayModel != null) - { - displayModel.detach(); - } - } - - @Override - public String getCssClass() - { - return null; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.extensions.markup.html.repeater.data.table; + +import org.apache.wicket.Component; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.model.IModel; + +/** + * A helper implementation for the IColumn interface + * + * @author Igor Vaynberg ( ivaynberg ) + * @param + * the type of the object that will be rendered in this column's cells + * @param + * the type of the sort property + */ +public abstract class AbstractColumn implements IStyledColumn +{ + private static final long serialVersionUID = 1L; + + private final IModel displayModel; + + private final S sortProperty; + + /** + * @param displayModel + * model used to generate header text + * @param sortProperty + * sort property this column represents + */ + public AbstractColumn(final IModel displayModel, final S sortProperty) + { + this.displayModel = displayModel; + this.sortProperty = sortProperty; + } + + /** + * @param displayModel + * model used to generate header text + */ + public AbstractColumn(final IModel displayModel) + { + this(displayModel, null); + } + + /** + * @return returns display model to be used for the header component + */ + public IModel getDisplayModel() + { + return displayModel; + } + + @Override + public S getSortProperty() + { + return sortProperty; + } + + @Override + public Component getHeader(final String componentId) + { + return new Label(componentId, getDisplayModel()); + } + + @Override + public void detach() + { + if (displayModel != null) + { + displayModel.detach(); + } + } + + @Override + public String getCssClass() + { + return null; + } +} diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/HeadersToolbar.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/HeadersToolbar.java index c7a39710b9e..41b9a26a89c 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/HeadersToolbar.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/HeadersToolbar.java @@ -71,7 +71,10 @@ protected Iterator>> getItemModels() for (IColumn column : table.getColumns()) { - columnsModels.add(Model.of(column)); + if (column.isVisible()) + { + columnsModels.add(Model.of(column)); + } } return columnsModels.iterator(); diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/filter/FilterToolbar.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/filter/FilterToolbar.java index 74d30ef495a..a8824085425 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/filter/FilterToolbar.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/repeater/data/table/filter/FilterToolbar.java @@ -1,152 +1,163 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.wicket.extensions.markup.html.repeater.data.table.filter; - -import java.util.LinkedList; -import java.util.List; - -import org.apache.wicket.Component; -import org.apache.wicket.behavior.Behavior; -import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractToolbar; -import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable; -import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn; -import org.apache.wicket.extensions.markup.html.repeater.data.table.IStyledColumn; -import org.apache.wicket.markup.ComponentTag; -import org.apache.wicket.markup.html.list.ListItem; -import org.apache.wicket.markup.html.list.ListView; -import org.apache.wicket.model.IModel; -import org.apache.wicket.util.lang.Args; -import org.apache.wicket.util.string.Strings; - - -/** - * Toolbar that creates a form to hold form components used to filter data in the data table. Form - * components are provided by columns that implement IFilteredColumn. - * - * @author Igor Vaynberg (ivaynberg) - */ -public class FilterToolbar extends AbstractToolbar -{ - private static final String FILTER_ID = "filter"; - private static final long serialVersionUID = 1L; - - /** - * Constructor - * - * @param table - * data table this toolbar will be added to - * @param form - * the filter form - * @param - * the type of the DataTable's model object - * @param - * the type of the DataTable's sorting parameter - * @param - * the type of filter state object - * - */ - public FilterToolbar(final DataTable table, final FilterForm form) - { - super(table); - - Args.notNull(table, "table"); - - IModel>> model = new IModel<>() { - private static final long serialVersionUID = 1L; - - @Override - public List> getObject() { - return new LinkedList<>(table.getColumns()); - } - }; - - // populate the toolbar with components provided by filtered columns - ListView> filters = new ListView<>("filters", model) - { - private static final long serialVersionUID = 1L; - - @Override - protected void populateItem(ListItem> item) - { - final IColumn col = item.getModelObject(); - item.setRenderBodyOnly(true); - - Component filter = null; - - if (col instanceof IFilteredColumn) - { - IFilteredColumn filteredCol = (IFilteredColumn)col; - filter = filteredCol.getFilter(FILTER_ID, form); - } - - if (filter == null) - { - filter = new NoFilter(FILTER_ID); - } - else - { - if (!filter.getId().equals(FILTER_ID)) - { - throw new IllegalStateException( - "filter component returned with an invalid component id. invalid component id [" + - filter.getId() + - "] required component id [" + - getId() + - "] generating column [" + col + "] "); - } - } - - if (col instanceof IStyledColumn) - { - filter.add(new Behavior() - { - private static final long serialVersionUID = 1L; - - /** - * @see Behavior#onComponentTag(Component, ComponentTag) - */ - @Override - public void onComponentTag(final Component component, final ComponentTag tag) - { - String className = ((IStyledColumn)col).getCssClass(); - if (!Strings.isEmpty(className)) - { - tag.append("class", className, " "); - } - } - }); - } - - item.add(filter); - } - }; - filters.setReuseItems(true); - - add(filters); - } - - @Override - protected void onBeforeRender() - { - if (findParent(FilterForm.class) == null) - { - throw new IllegalStateException("FilterToolbar must be contained within a FilterForm"); - } - super.onBeforeRender(); - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.extensions.markup.html.repeater.data.table.filter; + +import java.util.LinkedList; +import java.util.List; + +import org.apache.wicket.Component; +import org.apache.wicket.behavior.Behavior; +import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractToolbar; +import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable; +import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn; +import org.apache.wicket.extensions.markup.html.repeater.data.table.IStyledColumn; +import org.apache.wicket.markup.ComponentTag; +import org.apache.wicket.markup.html.list.ListItem; +import org.apache.wicket.markup.html.list.ListView; +import org.apache.wicket.model.IModel; +import org.apache.wicket.util.lang.Args; +import org.apache.wicket.util.string.Strings; + + +/** + * Toolbar that creates a form to hold form components used to filter data in the data table. Form + * components are provided by columns that implement IFilteredColumn. + * + * @author Igor Vaynberg (ivaynberg) + */ +public class FilterToolbar extends AbstractToolbar +{ + private static final String FILTER_ID = "filter"; + private static final long serialVersionUID = 1L; + + /** + * Constructor + * + * @param table + * data table this toolbar will be added to + * @param form + * the filter form + * @param + * the type of the DataTable's model object + * @param + * the type of the DataTable's sorting parameter + * @param + * the type of filter state object + * + */ + public FilterToolbar(final DataTable table, final FilterForm form) + { + super(table); + + Args.notNull(table, "table"); + + IModel>> model = new IModel<>() { + private static final long serialVersionUID = 1L; + + @Override + public List> getObject() { + + List> columnsModels = new LinkedList<>(); + + for (IColumn column : table.getColumns()) + { + if (column.isVisible()) + { + columnsModels.add(column); + } + } + + return columnsModels; + } + }; + + // populate the toolbar with components provided by filtered columns + ListView> filters = new ListView<>("filters", model) + { + private static final long serialVersionUID = 1L; + + @Override + protected void populateItem(ListItem> item) + { + final IColumn col = item.getModelObject(); + item.setRenderBodyOnly(true); + + Component filter = null; + + if (col instanceof IFilteredColumn) + { + IFilteredColumn filteredCol = (IFilteredColumn)col; + filter = filteredCol.getFilter(FILTER_ID, form); + } + + if (filter == null) + { + filter = new NoFilter(FILTER_ID); + } + else + { + if (!filter.getId().equals(FILTER_ID)) + { + throw new IllegalStateException( + "filter component returned with an invalid component id. invalid component id [" + + filter.getId() + + "] required component id [" + + getId() + + "] generating column [" + col + "] "); + } + } + + if (col instanceof IStyledColumn) + { + filter.add(new Behavior() + { + private static final long serialVersionUID = 1L; + + /** + * @see Behavior#onComponentTag(Component, ComponentTag) + */ + @Override + public void onComponentTag(final Component component, final ComponentTag tag) + { + String className = ((IStyledColumn)col).getCssClass(); + if (!Strings.isEmpty(className)) + { + tag.append("class", className, " "); + } + } + }); + } + + item.add(filter); + } + }; + filters.setReuseItems(true); + + add(filters); + } + + @Override + protected void onBeforeRender() + { + if (findParent(FilterForm.class) == null) + { + throw new IllegalStateException("FilterToolbar must be contained within a FilterForm"); + } + super.onBeforeRender(); + } + +} diff --git a/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/Contact.java b/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/Contact.java index 7a0d480b87a..52a83bf13c8 100644 --- a/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/Contact.java +++ b/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/Contact.java @@ -37,6 +37,8 @@ public class Contact implements IClusterable private String cellPhone; + private String age; + /** * Constructor */ @@ -51,7 +53,7 @@ public Contact() public String toString() { return "[Contact id=" + id + " firstName=" + firstName + " lastName=" + lastName + - " homePhone=" + homePhone + " cellPhone=" + cellPhone + "]"; + " homePhone=" + homePhone + " cellPhone=" + cellPhone + " age=" + age + "]"; } /** @@ -74,7 +76,8 @@ public boolean equals(final Object obj) return other.getFirstName().equals(getFirstName()) && other.getLastName().equals(getLastName()) && other.getHomePhone().equals(getHomePhone()) && - other.getCellPhone().equals(getCellPhone()); + other.getCellPhone().equals(getCellPhone()) && + other.getAge().equals(getAge()); } else @@ -174,4 +177,20 @@ public void setLastName(final String lastName) { this.lastName = lastName; } + + /** + * @return age + */ + public String getAge() + { + return age; + } + + /** + * @param age + */ + public void setAge(String age) + { + this.age = age; + } } diff --git a/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/ContactGenerator.java b/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/ContactGenerator.java index ecf422f9363..ef6bcc436c4 100644 --- a/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/ContactGenerator.java +++ b/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/ContactGenerator.java @@ -70,6 +70,7 @@ public Contact generate() contact.setId(generateId()); contact.setHomePhone(generatePhoneNumber()); contact.setCellPhone(generatePhoneNumber()); + contact.setAge(generateAge()); return contact; } @@ -101,6 +102,11 @@ private String generatePhoneNumber() .toString(); } + private String generateAge() + { + return new StringBuilder().append(rint(1, 999)).toString(); + } + private int rint(final int min, final int max) { return (int)(Math.random() * (max - min) + min); diff --git a/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/DataTablePage.java b/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/DataTablePage.java index 44eb1ed55fa..7ce1c6fad37 100644 --- a/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/DataTablePage.java +++ b/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/DataTablePage.java @@ -85,12 +85,29 @@ public String getCssClass() columns.add(new PropertyColumn(new Model<>("Home Phone"), "homePhone")); columns.add(new PropertyColumn(new Model<>("Cell Phone"), "cellPhone")); + IColumn ageColumn = new PropertyColumn(new Model<>("Age"), "age") + { + private static final long serialVersionUID = 1L; + public boolean isVisible() + { + return false; + } + }; + columns.add(ageColumn); + @SuppressWarnings({ "rawtypes", "unchecked" }) DefaultDataTable defaultDataTable = new DefaultDataTable("table", columns, new SortableContactDataProvider(), 8) { + private static final long serialVersionUID = 1L; + @Override + protected void onConfigure() + { + super.onConfigure(); + } + @Override protected IModel getCaptionModel() { diff --git a/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/DataTableTest.java b/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/DataTableTest.java index 21b293bc662..fb668f0b179 100644 --- a/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/DataTableTest.java +++ b/wicket-extensions/src/test/java/org/apache/wicket/extensions/markup/html/repeater/data/table/DataTableTest.java @@ -186,4 +186,16 @@ public IResourceStream getMarkupResourceStream(MarkupContainer container, } } + /** + * Tests that a {@link DataTable} with non-visible column will not be rendered. + */ + @Test + public void testWicket6941() + { + DataTablePage page = new DataTablePage(); + tester.startPage(page); + tester.assertRenderedPage(DataTablePage.class); + assertTrue(tester.getLastResponseAsString().contains("ID")); + assertFalse(tester.getLastResponseAsString().contains("Age")); + } }