Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WICKET-6941 Add visible attribute to AbstractColumn to hide/show columns in DataTable/DataGridView #491

Open
wants to merge 12 commits into
base: wicket-9.x
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,23 @@ protected final void populateItem(final Item<T> item)
for (int i = 0; i < populatorsNumber; i++)
{
ICellPopulator<T> populator = populators.get(i);
IModel<ICellPopulator<T>> populatorModel = new Model<>(populator);
Item<ICellPopulator<T>> 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<ICellPopulator<T>> populatorModel = new Model<>(populator);
Item<ICellPopulator<T>> 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) )");
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Example
* <p>
*
* <pre>
* 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()+&quot; &quot;+user.getLastName();
* cellItem.add(new Label(componentId, name);
* }}
* </pre>
*
* 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 <T>
* Model object type
*/
public interface ICellPopulator<T> extends IClusterable, IDetachable
{
/**
* Method used to populate a cell in the {@link DataGridView}
*
* <b>Implementation MUST add a component to the cellItem using the component id provided by
* componentId argument, otherwise a WicketRuntimeException will be thrown</b>
*
* @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<ICellPopulator<T>> cellItem, final String componentId,
final IModel<T> 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.
* <p>
* Example
* <p>
*
* <pre>
* 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()+&quot; &quot;+user.getLastName();
* cellItem.add(new Label(componentId, name);
* }}
* </pre>
*
* 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 <T>
* Model object type
*/
public interface ICellPopulator<T> extends IClusterable, IDetachable
{
/**
* Method used to populate a cell in the {@link DataGridView}
*
* <b>Implementation MUST add a component to the cellItem using the component id provided by
* componentId argument, otherwise a WicketRuntimeException will be thrown</b>
*
* @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<ICellPopulator<T>> cellItem, final String componentId,
final IModel<T> rowModel);

/**
* Gets whether this column is visible in a {@link DataGridView}
*
* @return true if column is visible
*/
default boolean isVisible()
{
return true;
};
}
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Example
*
* <pre>
* ICellPopulator cityPopulator = new PropertyPopulator(&quot;address.city&quot;);
* </pre>
*
* @param <T>
* @author Igor Vaynberg (ivaynberg)
*/
public class PropertyPopulator<T> implements ICellPopulator<T>
{
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<ICellPopulator<T>> cellItem, final String componentId,
final IModel<T> 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.
* <p>
* Example
*
* <pre>
* ICellPopulator cityPopulator = new PropertyPopulator(&quot;address.city&quot;);
* </pre>
*
* @param <T>
* @author Igor Vaynberg (ivaynberg)
*/
public class PropertyPopulator<T> implements ICellPopulator<T>
{
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<ICellPopulator<T>> cellItem, final String componentId,
final IModel<T> rowModel)
{
cellItem.add(new Label(componentId, new PropertyModel<>(rowModel, property)));
}
}
Loading