All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wicketstuff.egrid.EditableGrid Maven / Gradle / Ivy

package org.wicketstuff.egrid;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IFormSubmitter;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.wicketstuff.egrid.column.EditableGridActionsColumn;
import org.wicketstuff.egrid.component.EditableDataTable;
import org.wicketstuff.egrid.component.EditableDataTable.RowItem;
import org.wicketstuff.egrid.provider.IEditableDataProvider;
import org.wicketstuff.egrid.toolbar.EditableGridBottomToolbar;
import org.wicketstuff.egrid.toolbar.EditableGridHeadersToolbar;
import org.wicketstuff.egrid.toolbar.EditableGridNavigationToolbar;

/**
 * 
 * @author Nadeem Mohammad
 * 
 */
public class EditableGrid extends Panel
{

	private static final long serialVersionUID = 1L;
	
	private EditableDataTable dataTable;

	public EditableGrid(final String id, final List> columns,
		final IEditableDataProvider dataProvider, final long rowsPerPage, Class clazz)
	{
		super(id);
		List> newCols = new ArrayList>();
		newCols.addAll(columns);
		newCols.add(newActionsColumn());

		add(buildForm(newCols, dataProvider, rowsPerPage, clazz));
	}

	private Component buildForm(final List> columns,
		final IEditableDataProvider dataProvider, long rowsPerPage, Class clazz)
	{
		Form form = new NonValidatingForm("form");
		form.setOutputMarkupId(true);
		this.dataTable = newDataTable(columns, dataProvider, rowsPerPage, clazz);
		form.add(this.dataTable);
		return form;
	}

	public final EditableGrid setTableBodyCss(final String cssStyle)
	{
		this.dataTable.setTableBodyCss(cssStyle);
		return this;
	}
	
	public final EditableGrid setTableCss(final String cssStyle)
	{
		this.dataTable.add(AttributeModifier.replace("class", cssStyle));
		return this;
	}

	private static class NonValidatingForm extends Form
	{
		private static final long serialVersionUID = 1L;
		public NonValidatingForm(String id)
		{
			super(id);
		}
		@Override
		public void process(IFormSubmitter submittingComponent)
		{
			delegateSubmit(submittingComponent);
		}
		
	}

	private EditableDataTable newDataTable(final List> columns,
		final IEditableDataProvider dataProvider, long rowsPerPage, Class clazz)
	{
		final EditableDataTable dataTable = new EditableDataTable("dataTable", columns,
			dataProvider, rowsPerPage, clazz)
		{

			private static final long serialVersionUID = 1L;

			@Override
			protected void onError(AjaxRequestTarget target)
			{
				EditableGrid.this.onError(target);
			}
			@Override
			protected Item newRowItem(String id, int index, IModel model) {
				return super.newRowItem(id, index, model);
			}
		};

		dataTable.setOutputMarkupId(true);

		dataTable.addTopToolbar(new EditableGridNavigationToolbar(dataTable));
		dataTable.addTopToolbar(new EditableGridHeadersToolbar(dataTable, dataProvider));		
		if (displayAddFeature())
		{			
			dataTable.addBottomToolbar(newAddBottomToolbar(dataProvider, clazz, dataTable));
		}

		return dataTable;
	}

	protected RowItem newRowItem(String id, int index, IModel model) {
		return new RowItem(id, index, model);
	}

	private EditableGridBottomToolbar newAddBottomToolbar(
			final IEditableDataProvider dataProvider, Class clazz,
			final EditableDataTable dataTable)
			{
		return new EditableGridBottomToolbar(dataTable, clazz)
				{

					private static final long serialVersionUID = 1L;

					@Override
					protected void onAdd(AjaxRequestTarget target, T newRow)
					{
						dataProvider.add(newRow);
						target.add(dataTable);
						EditableGrid.this.onAdd(target, newRow);
					}

					@Override
					protected void onError(AjaxRequestTarget target)
					{
						super.onError(target);
						EditableGrid.this.onError(target);
					}

				};
	}

	private EditableGridActionsColumn newActionsColumn()
	{
		return new EditableGridActionsColumn(new Model("Actions"))
		{

			private static final long serialVersionUID = 1L;

			@Override
			protected void onError(AjaxRequestTarget target, IModel rowModel)
			{
				EditableGrid.this.onError(target);
			}

			@Override
			protected void onSave(AjaxRequestTarget target, IModel rowModel)
			{
				EditableGrid.this.onSave(target, rowModel);
			}

			@Override
			protected void onDelete(AjaxRequestTarget target, IModel rowModel)
			{
				EditableGrid.this.onDelete(target, rowModel);
			}

			@Override
			protected void onCancel(AjaxRequestTarget target)
			{
				EditableGrid.this.onCancel(target);
			}

			@Override
			protected boolean allowDelete(Item rowItem) {
				return EditableGrid.this.allowDelete(rowItem);
			}
		};
	}

	protected boolean allowDelete(Item rowItem) {
		return true;
	}

	protected void onCancel(AjaxRequestTarget target)
	{

	}


	protected void onDelete(AjaxRequestTarget target, IModel rowModel)
	{

	}

	protected void onSave(AjaxRequestTarget target, IModel rowModel)
	{

	}

	protected void onError(AjaxRequestTarget target)
	{

	}

	protected void onAdd(AjaxRequestTarget target, T newRow)
	{

	}
	
	protected boolean displayAddFeature() {
		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy