<commit_before>/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2015 Ausenco Engineering Canada Inc.
 * Copyright (C) 2018-2021 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.BasicObjects;

import com.jaamsim.Commands.KeywordCommand;
import com.jaamsim.Graphics.TextBasics;
import com.jaamsim.Samples.SampleProvider;
import com.jaamsim.basicsim.GUIListener;
import com.jaamsim.input.Input;
import com.jaamsim.input.InputAgent;
import com.jaamsim.input.InputErrorException;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.KeywordIndex;
import com.jaamsim.input.Output;
import com.jaamsim.input.UnitTypeInput;
import com.jaamsim.input.ValueInput;
import com.jaamsim.units.Unit;
import com.jaamsim.units.UserSpecifiedUnit;

public class InputValue extends TextBasics implements SampleProvider {

	@Keyword(description = "The unit type for the input value.",
	         exampleList = {"DistanceUnit"})
	protected final UnitTypeInput unitType;

	@Keyword(description = "The numerical value for the input.",
	         exampleList = {"1.5 km"})
	protected final ValueInput valInput;

	private boolean suppressUpdate = false; // prevents the white space in the edited text from changing

	{
		unitType = new UnitTypeInput("UnitType", KEY_INPUTS, UserSpecifiedUnit.class);
		unitType.setRequired(true);
		this.addInput(unitType);

		valInput = new ValueInput("Value", KEY_INPUTS, 0.0d);
		valInput.setUnitType(UserSpecifiedUnit.class);
		this.addInput(valInput);
	}

	public InputValue() {
		setText(valInput.getDefaultString(getJaamSimModel()));
	}

	@Override
	public void updateForInput(Input<?> in) {
		super.updateForInput(in);

		if (in == unitType) {
			setUnitType(unitType.getUnitType());
			if (valInput.isDefault())
				setText(valInput.getDefaultString(getJaamSimModel()));
			return;
		}

		if (in == valInput) {
			if (!suppressUpdate)
				setText(valInput.getValueString());
			if (valInput.isDefault())
				setText(valInput.getDefaultString(getJaamSimModel()));
			suppressUpdate = false;
			return;
		}
	}

	private void setUnitType(Class<? extends Unit> ut) {
		valInput.setUnitType(ut);
	}

	@Override
	public void acceptEdits() {
		try {
			suppressUpdate = true;
			KeywordIndex kw = InputAgent.formatInput(valInput.getKeyword(), getText());
			InputAgent.storeAndExecute(new KeywordCommand(this, kw));
			super.acceptEdits();
		}<commit_msg>JS: update ValInput input callbacks to callback objects<commit_after>/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2015 Ausenco Engineering Canada Inc.
 * Copyright (C) 2018-2021 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.BasicObjects;

import com.jaamsim.Commands.KeywordCommand;
import com.jaamsim.Graphics.TextBasics;
import com.jaamsim.Samples.SampleProvider;
import com.jaamsim.basicsim.Entity;
import com.jaamsim.basicsim.GUIListener;
import com.jaamsim.input.Input;
import com.jaamsim.input.InputAgent;
import com.jaamsim.input.InputCallback;
import com.jaamsim.input.InputErrorException;
import com.jaamsim.input.Keyword;
import com.jaamsim.input.KeywordIndex;
import com.jaamsim.input.Output;
import com.jaamsim.input.UnitTypeInput;
import com.jaamsim.input.ValueInput;
import com.jaamsim.units.Unit;
import com.jaamsim.units.UserSpecifiedUnit;

public class InputValue extends TextBasics implements SampleProvider {

	@Keyword(description = "The unit type for the input value.",
	         exampleList = {"DistanceUnit"})
	protected final UnitTypeInput unitType;

	@Keyword(description = "The numerical value for the input.",
	         exampleList = {"1.5 km"})
	protected final ValueInput valInput;

	private boolean suppressUpdate = false; // prevents the white space in the edited text from changing

	{
		unitType = new UnitTypeInput("UnitType", KEY_INPUTS, UserSpecifiedUnit.class);
		unitType.setRequired(true);
		unitType.setCallback(unitTypeInputCallback);
		this.addInput(unitType);

		valInput = new ValueInput("Value", KEY_INPUTS, 0.0d);
		valInput.setUnitType(UserSpecifiedUnit.class);
		valInput.setCallback(valInputCallback);
		this.addInput(valInput);
	}

	public InputValue() {
		setText(valInput.getDefaultString(getJaamSimModel()));
	}

	static final InputCallback unitTypeInputCallback = new InputCallback() {
		@Override
		public void callback(Entity ent, Input<?> inp) {
			((InputValue)ent).updateUnitType();
		}
	};

	void updateUnitType() {
		setUnitType(unitType.getUnitType());
		if (valInput.isDefault())
			setText(valInput.getDefaultString(getJaamSimModel()));
	}

	static final InputCallback valInputCallback = new InputCallback() {
		@Override
		public void callback(Entity ent, Input<?> inp) {
			((InputValue)ent).updateValInput();
		}
	};

	void updateValInput() {
		if (!suppressUpdate)
			setText(valInput.getValueString());
		if (valInput.isDefault())
			setText(valInput.getDefaultString(getJaamSimModel()));
		suppressUpdate = false;
	}

	private void setUnitType(Class<? extends Unit> ut) {
		valInput.setUnitType(ut);
	}

	@Override
	public void acceptEdits() {
		try {
			suppressUpdate = true;
			KeywordIndex kw = InputAgent.formatInput(valInput.getKeyword(), getText());
			InputAgent.storeAndExecute(new KeywordCommand(this, kw));
			super.acceptEdits();
		}
{'fraction_non_alphanumeric': 0.1037377256889452, 'fraction_numerical': 0.0063351282863477985, 'mean_word_length': 4.852641334569046, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5932326', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1883, 'n_words': 511}