/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada 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.ProcessFlow;

import java.util.ArrayList;

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.Graphics.OverlayEntity;
import com.jaamsim.Graphics.TextBasics;
import com.jaamsim.Samples.SampleConstant;
import com.jaamsim.Samples.SampleInput;
import com.jaamsim.basicsim.Entity;
import com.jaamsim.input.EntityInput;
import com.jaamsim.input.EntityListInput;
import com.jaamsim.input.Keyword;
import com.jaamsim.units.DimensionlessUnit;

public class SetGraphics extends LinkedComponent {

	@Keyword(description = "The entity whose graphics are to be changed. Defaults to the entity "
	                     + "that was received.",
	         exampleList = {"Server1", "this.target"})
	private final EntityInput<DisplayEntity> targetEntity;

	@Keyword(description = "List of entities whose graphics can chosen for assignment to the "
	                     + "target entity.",
	         exampleList = {"DisplayEntity1 DisplayEntity2"})
	private final EntityListInput<DisplayEntity> graphicsList;

	@Keyword(description = "A number that determines the choice of entities from the "
	                     + "GraphicsList:\n"
	                     + "   1 = first entity's graphics, 2 = second entity's graphics, etc.",
	         exampleList = {"2", "DiscreteDistribution1", "'1 + [TimeSeries1].PresentValue'"})
	private final SampleInput choice;

	{
		targetEntity = new EntityInput<>( DisplayEntity.class, "TargetEntity", "Key Inputs", null);
		targetEntity.setDefaultText("This Entity");
		this.addInput(targetEntity);

		graphicsList = new EntityListInput<>(DisplayEntity.class, "GraphicsList", "Key Inputs", null);
		graphicsList.setRequired(true);
		ArrayList<Class<? extends Entity>> list = new ArrayList<>();
		list.add(TextBasics.class);
		list.add(OverlayEntity.class);
		graphicsList.setInvalidClasses(list);
		this.addInput(graphicsList);

		choice = new SampleInput("Choice", "Key Inputs", new SampleConstant(DimensionlessUnit.class, 1.0));
		choice.setUnitType(DimensionlessUnit.class);
		choice.setEntity(this);
		choice.setValidRange(1, Double.POSITIVE_INFINITY);
		this.addInput(choice);
	}

	public SetGraphics() {}

	@Override
	public void addEntity(DisplayEntity ent) {
		super.addEntity(ent);

		// Identify the entity whose graphics are to be changed
		DisplayEntity target = targetEntity.getValue();
		if (target == null)
			target = ent;

		// Choose the new graphics for this entity
		int i = (int) choice.getValue().getNextSample(this.getSimTime());
		if (i<1 || i>graphicsList.getValue().size())
			error("Chosen index i=%s is out of range for GraphicList: %s.", i, graphicsList.getValue());
		DisplayEntity chosen = graphicsList.getValue().get(i-1);

		target.setDisplayModelList(chosen.getDisplayModelList());
		target.setSize(chosen.getSize());
		target.setOrientation(chosen.getOrientation());
		target.setAlignment(chosen.getAlignment());

		// Send the entity to the next component in the chain
		this.sendToNextComponent(ent);
	}

}

@@ -87,19 +87,63 @@
 da Inc.%0A
+ * Copyright (C) 2017 JaamSim Software Inc.%0A
  *%0A
-
  * Licen
@@ -756,16 +756,68 @@
 yList;%0A%0A
+import com.jaamsim.EntityProviders.EntityProvInput;%0A
 import c
@@ -1057,46 +1057,8 @@
 ty;%0A
-import com.jaamsim.input.EntityInput;%0A
 impo
@@ -1436,24 +1436,28 @@
 final Entity
+Prov
 Input%3CDispla
@@ -2112,24 +2112,28 @@
 w Entity
+Prov
 Input%3C%3E(
  Display
@@ -2124,17 +2124,16 @@
 Input%3C%3E(
- 
 DisplayE
@@ -2217,20 +2217,152 @@
 xt(%22
-This Entity%22
+this.obj%22);%0A%09%09targetEntity.setEntity(this);%0A%09%09targetEntity.addInvalidClass(TextBasics.class);%0A%09%09targetEntity.addInvalidClass(OverlayEntity.class
 );%0A%09
@@ -3082,16 +3082,54 @@
 ty(ent);
+%0A%09%09double simTime = this.getSimTime();
 %0A%0A%09%09// I
@@ -3228,16 +3228,39 @@
 tValue()
+.getNextEntity(simTime)
 ;%0A%09%09if (
@@ -3386,33 +3386,23 @@
 tSample(
-this.getS
+s
 imTime
-()
 );%0A%09%09if 

allow SetGraphic keyword TargetEntity to accept an expression