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

org.wicketstuff.egrid.column.EditableGridActionsPanel Maven / Gradle / Ivy

package org.wicketstuff.egrid.column;

import org.apache.wicket.MetaDataKey;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxCallListener;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.wicketstuff.egrid.component.EditableDataTable;
import org.wicketstuff.egrid.component.EditableGridSubmitLink;
import org.wicketstuff.egrid.model.GridOperationData;
import org.wicketstuff.egrid.model.OperationType;

/**
 * 
 * @author Nadeem Mohammad
 * 
 */
public abstract class EditableGridActionsPanel extends Panel
{
	public final static MetaDataKey EDITING = new MetaDataKey()
	{
		private static final long serialVersionUID = 1L;
	};

	private static final long serialVersionUID = 1L;

	protected abstract void onSave(AjaxRequestTarget target);

	protected abstract void onError(AjaxRequestTarget target);

	protected abstract void onCancel(AjaxRequestTarget target);

	protected abstract void onDelete(AjaxRequestTarget target);

	public EditableGridActionsPanel(String id, final Item> cellItem)
	{
		super(id);

		@SuppressWarnings("unchecked")
		final Item rowItem = cellItem.findParent(Item.class);

		add(newEditLink(rowItem));
		add(newSaveLink(rowItem));
		add(newCancelLink(rowItem));
		add(newDeleteLink(rowItem));
	}

	private EditableGridSubmitLink newSaveLink(final Item rowItem)
	{
		return new EditableGridSubmitLink("save", rowItem)
		{

			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible()
			{
				return isThisRowBeingEdited(rowItem);
			}

			@Override
			protected void onSuccess(AjaxRequestTarget target)
			{
				rowItem.setMetaData(EDITING, Boolean.FALSE);
				send(getPage(), Broadcast.BREADTH, rowItem);
				target.add(rowItem);
				onSave(target);

			}

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

	private AjaxLink newDeleteLink(final Item rowItem)
	{
		return new AjaxLink("delete")
		{

			private static final long serialVersionUID = 1L;

			@Override
			protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
			{
				super.updateAjaxAttributes(attributes);
				AjaxCallListener listener = new AjaxCallListener();
				listener.onPrecondition("if(!confirm('Do you really want to delete?')){return false;}");
				attributes.getAjaxCallListeners().add(listener);
			}

			@SuppressWarnings("unchecked")
			@Override
			public void onClick(AjaxRequestTarget target)
			{
				EditableDataTable eventTarget = rowItem.findParent(EditableDataTable.class);
				send(getPage(), Broadcast.BREADTH, new GridOperationData(OperationType.DELETE,
					(T)rowItem.getDefaultModelObject(), eventTarget));
				target.add(eventTarget);
				onDelete(target);
			}

			@Override
			public boolean isVisible() {
				return EditableGridActionsPanel.this.allowDelete(rowItem);
			}
		};
	}

	private AjaxLink newCancelLink(final Item rowItem)
	{
		return new AjaxLink("cancel")
		{

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target)
			{
				rowItem.setMetaData(EDITING, Boolean.FALSE);
				send(getPage(), Broadcast.BREADTH, rowItem);
				target.add(rowItem);
				onCancel(target);
			}

			@Override
			public boolean isVisible()
			{
				return isThisRowBeingEdited(rowItem);
			}
		};
	}

	private AjaxLink newEditLink(final Item rowItem)
	{
		return new AjaxLink("edit")
		{

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target)
			{
				rowItem.setMetaData(EDITING, Boolean.TRUE);
				send(getPage(), Broadcast.BREADTH, rowItem);
				target.add(rowItem);
			}

			@Override
			public boolean isVisible()
			{
				return !isThisRowBeingEdited(rowItem);
			}
		};
	}

	private boolean isThisRowBeingEdited(Item rowItem)
	{
		return rowItem.getMetaData(EDITING);
	}
	
	protected boolean allowDelete(final Item rowItem) {
		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy