<commit_before>package fr.openwide.core.wicket.more.markup.html.template.js.jquery.plugins.fancybox;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.odlabs.wiquery.core.events.Event;
import org.odlabs.wiquery.core.events.MouseEvent;
import org.odlabs.wiquery.core.events.WiQueryEventBehavior;
import org.odlabs.wiquery.core.javascript.JsScope;

import fr.openwide.core.wicket.markup.html.panel.GenericPanel;
import fr.openwide.core.wicket.more.markup.html.feedback.ContainerFeedbackPanel;

public abstract class AbstractGenericPopupFormPanel<T> extends GenericPanel<T> implements IPopupContentAwareComponent {

	private static final long serialVersionUID = 1772065899149334374L;
	
	private FormPanelMode formPanelMode;
	
	protected Form<T> itemForm;

	protected ContainerFeedbackPanel feedback;

	private boolean bindCancel;
	
	public AbstractGenericPopupFormPanel(String id, IModel<T> model, FormPanelMode formPanelMode) {
		this(id, model, formPanelMode, false);
	}

	public AbstractGenericPopupFormPanel(String id, IModel<T> model, FormPanelMode formPanelMode, boolean bindCancel) {
		super(id, model);
		this.bindCancel = bindCancel;
		this.setOutputMarkupId(true);
		
		if (formPanelMode == null) {
			throw new IllegalStateException("GenericFormPanel must have a FormPanelMode");
		}
		this.formPanelMode = formPanelMode;
		
		feedback = new ContainerFeedbackPanel("feedback", this);
		feedback.setOutputMarkupId(true);
		
		itemForm = new Form<T>("itemForm", model);
		
		AjaxButton submitButton = new AjaxButton("submitButton", itemForm) {
			
			private static final long serialVersionUID = -3721476446722988109L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				AbstractGenericPopupFormPanel.this.onSubmitInternal(target, form);
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				AbstractGenericPopupFormPanel.this.reloadPanel(target);
			}
			
		};
		itemForm.add(submitButton);
		
		Component cancelButton;
		if (this.bindCancel) {
			cancelButton = new AjaxButton("cancelButton", itemForm) {
				
				private static final long serialVersionUID = -3721476446722988109L;

				@Override
				protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
					AbstractGenericPopupFormPanel.this.onSubmitInternal(target, form);
				}
				
				@Override
				protected void onError(AjaxRequestTarget target, Form<?> form) {
					AbstractGenericPopupFormPanel.this.reloadPanel(target);
				}
				
			};
		} else {
			// simple implementation, no ajax, whuch close popup
			cancelButton = new WebMarkupContainer("cancelButton");
			cancelButton.add(new WiQueryEventBehavior(new Event(MouseEvent.CLICK) {
				
				private static final long serialVersionUID = 1L;

				@Override
				public JsScope callback() {
					return JsScope.quickScope(FancyboxHelper.getClose());
				}
			}));
		}
		itemForm.add(cancelButton);
		
		add(itemForm, feedback);
	}

	public boolean isAddMode() {
		return formPanelMode == FormPanelMode.ADD;
	}

	public boolean isEditMode() {
		return formPanelMode == FormPanelMode.EDIT;
	}

	protected void reloadPanel(AjaxRequestTarget target) {
		if (target != null) {
			target.addComponent(this);
			target.appendJavascript(FancyboxHelper.getResize());
		}
	}
	
	@Override
	public void hide(AjaxRequestTarget arg0) {
		// Nothing
	}
	
	@Override
	public void show(AjaxRequestTarget arg0) {
		if (isAddMode()) {
			itemForm.setModelObject(getEmptyModelObject());
		}
		itemForm.clearInput();
	}
	
	protected void onCancelInternal(AjaxRequestTarget target, Form<?> form) {
		// dummy implementation
		target.appendJavascript(FancyboxHelper.getClose());
	}
	
	protected abstract T getEmptyModelObject();
	
	protected abstract void onSubmitInternal(AjaxRequestTarget target, Form<?> form);

}<commit_msg>évolution du widget de manière à pouvoir binder si nécessairre le bouton cancel<commit_after>package fr.openwide.core.wicket.more.markup.html.template.js.jquery.plugins.fancybox;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.odlabs.wiquery.core.events.Event;
import org.odlabs.wiquery.core.events.MouseEvent;
import org.odlabs.wiquery.core.events.WiQueryAjaxEventBehavior;
import org.odlabs.wiquery.core.events.WiQueryEventBehavior;
import org.odlabs.wiquery.core.javascript.JsScope;

import fr.openwide.core.wicket.markup.html.panel.GenericPanel;
import fr.openwide.core.wicket.more.markup.html.feedback.ContainerFeedbackPanel;

public abstract class AbstractGenericPopupFormPanel<T> extends GenericPanel<T> implements IPopupContentAwareComponent {

	private static final long serialVersionUID = 1772065899149334374L;
	
	private FormPanelMode formPanelMode;
	
	protected Form<T> itemForm;

	protected ContainerFeedbackPanel feedback;

	private boolean bindCancel;
	
	public AbstractGenericPopupFormPanel(String id, IModel<T> model, FormPanelMode formPanelMode) {
		this(id, model, formPanelMode, false);
	}

	public AbstractGenericPopupFormPanel(String id, IModel<T> model, FormPanelMode formPanelMode, boolean bindCancel) {
		super(id, model);
		this.bindCancel = bindCancel;
		this.setOutputMarkupId(true);
		
		if (formPanelMode == null) {
			throw new IllegalStateException("GenericFormPanel must have a FormPanelMode");
		}
		this.formPanelMode = formPanelMode;
		
		feedback = new ContainerFeedbackPanel("feedback", this);
		feedback.setOutputMarkupId(true);
		
		itemForm = new Form<T>("itemForm", model);
		
		AjaxButton submitButton = new AjaxButton("submitButton", itemForm) {
			
			private static final long serialVersionUID = -3721476446722988109L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				AbstractGenericPopupFormPanel.this.onSubmitInternal(target, form);
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				AbstractGenericPopupFormPanel.this.reloadPanel(target);
			}
			
		};
		itemForm.add(submitButton);
		
		Component cancelButton;
		if (this.bindCancel) {
			cancelButton = new WebMarkupContainer("cancelButton");
			cancelButton.add(new WiQueryAjaxEventBehavior(MouseEvent.CLICK) {
				
				private static final long serialVersionUID = -3721476446722988109L;

				@Override
				protected void onEvent(AjaxRequestTarget target) {
					onCancelInternal(target);
				}
				
			});
		} else {
			// simple implementation, no ajax, whuch close popup
			cancelButton = new WebMarkupContainer("cancelButton");
			cancelButton.add(new WiQueryEventBehavior(new Event(MouseEvent.CLICK) {
				
				private static final long serialVersionUID = 1L;

				@Override
				public JsScope callback() {
					return JsScope.quickScope(FancyboxHelper.getClose());
				}
			}));
		}
		itemForm.add(cancelButton);
		
		add(itemForm, feedback);
	}

	public boolean isAddMode() {
		return formPanelMode == FormPanelMode.ADD;
	}

	public boolean isEditMode() {
		return formPanelMode == FormPanelMode.EDIT;
	}

	protected void reloadPanel(AjaxRequestTarget target) {
		if (target != null) {
			target.addComponent(this);
			target.appendJavascript(FancyboxHelper.getResize());
		}
	}
	
	@Override
	public void hide(AjaxRequestTarget arg0) {
		// Nothing
	}
	
	@Override
	public void show(AjaxRequestTarget arg0) {
		if (isAddMode()) {
			itemForm.setModelObject(getEmptyModelObject());
		}
		itemForm.clearInput();
	}
	
	/**
	 * if bindCancel is true, enable server-side processing. Be aware that form is not binded so modelObject is not updated
	 * with last information passed in by user.
	 */
	protected void onCancelInternal(AjaxRequestTarget target) {
		// dummy implementation
		target.appendJavascript(FancyboxHelper.getClose());
	}
	
	protected abstract T getEmptyModelObject();
	
	protected abstract void onSubmitInternal(AjaxRequestTarget target, Form<?> form);

}
{'fraction_non_alphanumeric': 0.09693014261542181, 'fraction_numerical': 0.014503263234227702, 'mean_word_length': 4.831571529245948, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4448096', 'n_tokens_mistral': 2917, 'n_tokens_neox': 2489, 'n_words': 514}