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

com.inmethod.grid.column.editable.EditableCellPanel Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package com.inmethod.grid.column.editable;

import java.util.Optional;

import com.inmethod.grid.IGridColumn;
import com.inmethod.grid.column.AbstractColumn;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.cycle.RequestCycle;

public abstract class EditableCellPanel extends Panel
{

	private static final long serialVersionUID = 1L;
	private final AbstractColumn column;

	public EditableCellPanel(String id, AbstractColumn column, IModel rowModel)
	{
		super(id, rowModel);
		this.column = column;
	}

	public AbstractColumn getColumn()
	{
		return column;
	}

	@Override
	protected void onBeforeRender()
	{
		super.onBeforeRender();

		Optional targetOptional = RequestCycle.get().find(AjaxRequestTarget.class);

		Component textField = get("textfield");

		if (targetOptional.isPresent() && isFocusTextField())
		{
			targetOptional.get().focusComponent(textField);
		}
	}

	protected boolean isFocusTextField()
	{
		IGridColumn lastClickedColumn = getColumn().getGrid().getLastClickedColumn();
		if (lastClickedColumn == getColumn())
		{
			getColumn().getGrid().cleanLastClickedColumn();
			return true;
		}
		else
		{
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	protected IModel getDefaultRowModel()
	{
		return (IModel)getDefaultModel();
	}

	@Override
	public boolean isVisible()
	{
		return column.getGrid().isItemEdited(getDefaultRowModel());
	}

	protected abstract FormComponent

getEditComponent(); }