<commit_before>package org.minimalj.frontend.editor;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.minimalj.application.DevMode;
import org.minimalj.frontend.Frontend;
import org.minimalj.frontend.Frontend.SwitchContent;
import org.minimalj.frontend.action.Action;
import org.minimalj.frontend.form.Form;
import org.minimalj.frontend.page.IDialog;
import org.minimalj.model.properties.PropertyInterface;
import org.minimalj.model.validation.Validatable;
import org.minimalj.model.validation.Validation;
import org.minimalj.model.validation.ValidationMessage;
import org.minimalj.util.ExceptionUtils;
import org.minimalj.util.mock.Mocking;

public abstract class Wizard<RESULT> extends Action {

	private static final Logger logger = Logger.getLogger(Wizard.class.getName());

	private Object stepObject;
	private WizardStep step;
	private Form form;
	private final EditorChangeListener changeListener = new EditorChangeListener();
	private final List<ValidationMessage> validationMessages = new ArrayList<>();
	private FinishAction finishAction;
	private NextWizardStepAction nextAction;
	private IDialog dialog;
	private SwitchContent switchContent;
	private int stepIndex;
	
	public Wizard() {
		super();
	}

	public Wizard(String actionName) {
		super(actionName);
	}

	public String getTitle() {
		return getName();
	}

	@Override
	public void action() {
		switchContent = Frontend.getInstance().createSwitchContent();

		CancelAction cancelAction = new CancelAction();
		PreviousWizardStepAction previousAction = new PreviousWizardStepAction();
		nextAction = new NextWizardStepAction();
		finishAction = new FinishAction();

		stepIndex = 0;
		step = getFirstStep();
		switchStep();
		
		if (DevMode.isActive()) {
			FillWithDemoDataAction demoAction = new FillWithDemoDataAction();
			dialog = Frontend.showDialog(getTitle(), switchContent, nextAction, cancelAction, demoAction, cancelAction, previousAction, nextAction, finishAction);
		} else {
			dialog = Frontend.showDialog(getTitle(), switchContent, nextAction, cancelAction, cancelAction, previousAction, nextAction, finishAction);
		}
	}

	private void switchStep() {
		stepObject = step.createObject();

		form = step.createForm();
		form.setChangeListener(changeListener);
		form.setObject(stepObject);
		
		validate(stepObject);
		
		switchContent.show(form.getContent());
	}
	
	protected abstract WizardStep<?> getFirstStep();
	
	private void validate(Object stepObject) {
		validationMessages.clear();
		if (stepObject instanceof Validation) {
			((Validation) stepObject).validate(validationMessages);
		}
		ObjectValidator.validateForEmpty(stepObject, validationMessages, form.getProperties());
		ObjectValidator.validateForInvalid(stepObject, validationMessages, form.getProperties());
		ObjectValidator.validatePropertyValues(stepObject, validationMessages, form.getProperties());
		if (step instanceof Validatable) {
			((Validatable) step).validate();
		}
		form.indicate(validationMessages);
		nextAction.setValidationMessages(validationMessages);<commit_msg>Wizard: don't allow "previous" on the first wizard step<commit_after>package org.minimalj.frontend.editor;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.minimalj.application.DevMode;
import org.minimalj.frontend.Frontend;
import org.minimalj.frontend.Frontend.SwitchContent;
import org.minimalj.frontend.action.Action;
import org.minimalj.frontend.form.Form;
import org.minimalj.frontend.page.IDialog;
import org.minimalj.model.properties.PropertyInterface;
import org.minimalj.model.validation.Validatable;
import org.minimalj.model.validation.Validation;
import org.minimalj.model.validation.ValidationMessage;
import org.minimalj.util.ExceptionUtils;
import org.minimalj.util.mock.Mocking;

public abstract class Wizard<RESULT> extends Action {

	private static final Logger logger = Logger.getLogger(Wizard.class.getName());

	private Object stepObject;
	private WizardStep step;
	private Form form;
	private final EditorChangeListener changeListener = new EditorChangeListener();
	private final List<ValidationMessage> validationMessages = new ArrayList<>();
	private FinishAction finishAction;
	private NextWizardStepAction nextAction;
	private PreviousWizardStepAction previousAction;
	private IDialog dialog;
	private SwitchContent switchContent;
	private int stepIndex;
	
	public Wizard() {
		super();
	}

	public Wizard(String actionName) {
		super(actionName);
	}

	public String getTitle() {
		return getName();
	}

	@Override
	public void action() {
		switchContent = Frontend.getInstance().createSwitchContent();

		CancelAction cancelAction = new CancelAction();
		previousAction = new PreviousWizardStepAction();
		nextAction = new NextWizardStepAction();
		finishAction = new FinishAction();

		stepIndex = 0;
		step = getFirstStep();
		switchStep();
		
		if (DevMode.isActive()) {
			FillWithDemoDataAction demoAction = new FillWithDemoDataAction();
			dialog = Frontend.showDialog(getTitle(), switchContent, nextAction, cancelAction, demoAction, cancelAction, previousAction, nextAction, finishAction);
		} else {
			dialog = Frontend.showDialog(getTitle(), switchContent, nextAction, cancelAction, cancelAction, previousAction, nextAction, finishAction);
		}
	}

	private void switchStep() {
		stepObject = step.createObject();

		form = step.createForm();
		form.setChangeListener(changeListener);
		form.setObject(stepObject);
		
		validate(stepObject);
		
		switchContent.show(form.getContent());
		previousAction.setEnabled(stepIndex > 0);
	}
	
	protected abstract WizardStep<?> getFirstStep();
	
	private void validate(Object stepObject) {
		validationMessages.clear();
		if (stepObject instanceof Validation) {
			((Validation) stepObject).validate(validationMessages);
		}
		ObjectValidator.validateForEmpty(stepObject, validationMessages, form.getProperties());
		ObjectValidator.validateForInvalid(stepObject, validationMessages, form.getProperties());
		ObjectValidator.validatePropertyValues(stepObject, validationMessages, form.getProperties());
		if (step instanceof Validatable) {
			((Validatable) step).validate();
		}
		form.indicate(validationMessages);
		nextAction.setValidationMessages(validationMessages);
{'fraction_non_alphanumeric': 0.1005568814638027, 'fraction_numerical': 0.00047732696897374703, 'mean_word_length': 6.225287356321839, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, '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': '526982', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1678, 'n_words': 383}