<commit_before>package sophena.rcp.wizards;

import java.util.List;
import java.util.UUID;

import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sophena.db.daos.BuildingStateDao;
import sophena.db.daos.ProjectDao;
import sophena.model.BuildingState;
import sophena.model.BuildingType;
import sophena.model.Consumer;
import sophena.model.Project;
import sophena.model.descriptors.ProjectDescriptor;
import sophena.rcp.App;
import sophena.rcp.Labels;
import sophena.rcp.M;
import sophena.rcp.editors.consumers.ConsumerEditor;
import sophena.rcp.help.H;
import sophena.rcp.help.HelpLink;
import sophena.rcp.navigation.Navigator;
import sophena.rcp.utils.Colors;
import sophena.rcp.utils.Controls;
import sophena.rcp.utils.EntityCombo;
import sophena.rcp.utils.Strings;
import sophena.rcp.utils.UI;
import sophena.utils.Num;

public class ConsumerWizard extends Wizard {

	private Logger log = LoggerFactory.getLogger(getClass());
	private Page page;
	private Project project;

	public static void open(ProjectDescriptor d) {
		if (d == null)
			return;
		ProjectDao dao = new ProjectDao(App.getDb());
		open(dao.get(d.id));
	}

	public static void open(Project project) {
		if (project == null)
			return;
		ConsumerWizard wiz = new ConsumerWizard();
		wiz.setWindowTitle(M.CreateNewConsumer);
		wiz.project = project;
		WizardDialog dialog = new WizardDialog(UI.shell(), wiz);
		dialog.setPageSize(150, 400);
		if (dialog.open() == Window.OK)
			Navigator.refresh();
	}

	@Override
	public boolean performFinish() {
		try {
			Consumer consumer = page.consumer;
			setDefaults(consumer);
			project.consumers.add(consumer);
			ProjectDao dao = new ProjectDao(App.getDb());
			dao.update(project);
			Navigator.refresh();
			ConsumerEditor.open(project.toDescriptor(),
					consumer.toDescriptor());
			return true;
		} catch (Exception e) {
			log.error("failed to save consumer", e);
			return false;
		}
	}

	private void setDefaults(Consumer c) {
		BuildingState bs = c.buildingState;
		if (bs != null) {
			c.heatingLimit = bs.heatingLimit;
			c.waterFraction = bs.waterFraction;
			c.loadHours = bs.loadHours;
		} else {
			c.heatingLimit = 15;
			c.waterFraction = 10;
			c.loadHours = 1800;
		}
	}

	@Override
	public void addPages() {
		page = new Page();
		addPage(page);
	}

	private class Page extends WizardPage {

		private Consumer consumer;
		private EntityCombo<BuildingState> stateCombo;

		private Page() {
			super("ConsumerWizardPage", M.CreateNewConsumer, null);
			consumer = new Consumer();
			consumer.id = UUID.randomUUID().toString();
			consumer.name = M.NewConsumer;
			consumer.demandBased = false;
		}

		@Override
		public void createControl(Composite parent) {
			Composite composite = UI.formComposite(parent);
			setControl(composite);
			Text nt = UI.formText(composite, M.Name);
			nt.setBackground(Colors.forRequiredField());
			nt.setText(consumer.name);
			nt.addModifyListener(e -> {
				consumer.name = nt.getText();
				validate();
			});
			Text dt = UI.formMultiText(composite, M.Description);
			dt.addModifyListener((e) -> consumer.description = dt.getText());
			createTypeCombo(composite);
			createStateCombo(composite);
			createCalculationRadios(composite);
		}

		private void createTypeCombo(Composite composite) {
			Combo combo = UI.formCombo(composite, M.BuildingType);
			BuildingType[] types = BuildingType.values();
			String[] items = new String[types.length];
			for (int i = 0; i < types.length; i++) {
				items[i] = Labels.get(types[i]);
			}
			combo.setItems(items);
			combo.select(0);
			Controls.onSelect(combo, e -> {
				int idx = combo.getSelectionIndex();
				initBuildingState(types[idx]);
			});
		}

		private void createStateCombo(Composite composite) {
			stateCombo = new EntityCombo<>();
			stateCombo.create(M.BuildingState, composite);
			initBuildingState(BuildingType.values()[0]);
			stateCombo.onSelect(s -> consumer.buildingState = s);
		}

		private void initBuildingState(BuildingType type) {
			BuildingStateDao dao = new BuildingStateDao(App.getDb());
			List<BuildingState> states = dao.getAllWith(type);
			BuildingState selected = BuildingStateDao.getDefault(states);
			stateCombo.setInput(states);
			stateCombo.select(selected);
			consumer.buildingState = selected;
		}

		private void createCalculationRadios(Composite composite) {
			UI.formLabel(composite, "");
			Button consumption = new Button(composite, SWT.RADIO);
			consumption.setText(M.ConsumptionBasedCalculation);
			consumption.setSelection(true);<commit_msg>consumer wizard: only provide building types that have at least on state<commit_after>package sophena.rcp.wizards;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sophena.db.daos.BuildingStateDao;
import sophena.db.daos.ProjectDao;
import sophena.model.BuildingState;
import sophena.model.BuildingType;
import sophena.model.Consumer;
import sophena.model.Project;
import sophena.model.descriptors.ProjectDescriptor;
import sophena.rcp.App;
import sophena.rcp.Labels;
import sophena.rcp.M;
import sophena.rcp.editors.consumers.ConsumerEditor;
import sophena.rcp.help.H;
import sophena.rcp.help.HelpLink;
import sophena.rcp.navigation.Navigator;
import sophena.rcp.utils.Colors;
import sophena.rcp.utils.Controls;
import sophena.rcp.utils.EntityCombo;
import sophena.rcp.utils.Strings;
import sophena.rcp.utils.UI;
import sophena.utils.Num;

public class ConsumerWizard extends Wizard {

	private Logger log = LoggerFactory.getLogger(getClass());
	private Page page;
	private Project project;

	public static void open(ProjectDescriptor d) {
		if (d == null)
			return;
		ProjectDao dao = new ProjectDao(App.getDb());
		open(dao.get(d.id));
	}

	public static void open(Project project) {
		if (project == null)
			return;
		ConsumerWizard wiz = new ConsumerWizard();
		wiz.setWindowTitle(M.CreateNewConsumer);
		wiz.project = project;
		WizardDialog dialog = new WizardDialog(UI.shell(), wiz);
		dialog.setPageSize(150, 400);
		if (dialog.open() == Window.OK)
			Navigator.refresh();
	}

	@Override
	public boolean performFinish() {
		try {
			Consumer consumer = page.consumer;
			setDefaults(consumer);
			project.consumers.add(consumer);
			ProjectDao dao = new ProjectDao(App.getDb());
			dao.update(project);
			Navigator.refresh();
			ConsumerEditor.open(project.toDescriptor(),
					consumer.toDescriptor());
			return true;
		} catch (Exception e) {
			log.error("failed to save consumer", e);
			return false;
		}
	}

	private void setDefaults(Consumer c) {
		BuildingState bs = c.buildingState;
		if (bs != null) {
			c.heatingLimit = bs.heatingLimit;
			c.waterFraction = bs.waterFraction;
			c.loadHours = bs.loadHours;
		} else {
			c.heatingLimit = 15;
			c.waterFraction = 10;
			c.loadHours = 1800;
		}
	}

	@Override
	public void addPages() {
		page = new Page();
		addPage(page);
	}

	private class Page extends WizardPage {

		private Consumer consumer;
		private EntityCombo<BuildingState> stateCombo;

		private Page() {
			super("ConsumerWizardPage", M.CreateNewConsumer, null);
			consumer = new Consumer();
			consumer.id = UUID.randomUUID().toString();
			consumer.name = M.NewConsumer;
			consumer.demandBased = false;
		}

		@Override
		public void createControl(Composite parent) {
			Composite composite = UI.formComposite(parent);
			setControl(composite);
			Text nt = UI.formText(composite, M.Name);
			nt.setBackground(Colors.forRequiredField());
			nt.setText(consumer.name);
			nt.addModifyListener(e -> {
				consumer.name = nt.getText();
				validate();
			});
			Text dt = UI.formMultiText(composite, M.Description);
			dt.addModifyListener((e) -> consumer.description = dt.getText());
			createTypeCombo(composite);
			createStateCombo(composite);
			createCalculationRadios(composite);
		}

		private void createTypeCombo(Composite composite) {
			Combo combo = UI.formCombo(composite, M.BuildingType);
			BuildingType[] types = buildingTypes();
			String[] items = new String[types.length];
			for (int i = 0; i < types.length; i++) {
				items[i] = Labels.get(types[i]);
			}
			combo.setItems(items);
			combo.select(0);
			Controls.onSelect(combo, e -> {
				int idx = combo.getSelectionIndex();
				initBuildingState(types[idx]);
			});
		}

		/** Get the building types that have at least one building state. */
		private BuildingType[] buildingTypes() {
			BuildingStateDao dao = new BuildingStateDao(App.getDb());
			BuildingType[] types = BuildingType.values();
			Set<BuildingType> used = new HashSet<>();
			for (BuildingState s : dao.getAll()) {
				if (s.type == null)
					continue;
				used.add(s.type);
			}
			BuildingType[] selected = new BuildingType[used.size()];
			int i = 0;
			for (BuildingType type : types) {
				if (!used.contains(type) || i >= selected.length)
					continue;
				selected[i] = type;
				i++;
			}
			return selected;
		}

		private void createStateCombo(Composite composite) {
			stateCombo = new EntityCombo<>();
			stateCombo.create(M.BuildingState, composite);
			initBuildingState(BuildingType.values()[0]);
			stateCombo.onSelect(s -> consumer.buildingState = s);
		}

		private void initBuildingState(BuildingType type) {
			BuildingStateDao dao = new BuildingStateDao(App.getDb());
			List<BuildingState> states = dao.getAllWith(type);
			BuildingState selected = BuildingStateDao.getDefault(states);
			stateCombo.setInput(states);
			stateCombo.select(selected);
			consumer.buildingState = selected;
		}

		private void createCalculationRadios(Composite composite) {
			UI.formLabel(composite, "");
			Button consumption = new Button(composite, SWT.RADIO);
			consumption.setText(M.ConsumptionBasedCalculation);
			consumption.setSelection(true);
{'fraction_non_alphanumeric': 0.12284380062899075, 'fraction_numerical': 0.003716763556656819, 'mean_word_length': 4.426059979317476, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, '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': '3013128', 'n_tokens_mistral': 3922, 'n_tokens_neox': 3425, 'n_words': 697}