/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2017-2019 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.Commands;

import com.jaamsim.basicsim.Entity;
import com.jaamsim.basicsim.ErrorException;
import com.jaamsim.basicsim.JaamSimModel;
import com.jaamsim.input.InputAgent;
import com.jaamsim.ui.GUIFrame;

public class DefineCommand implements Command {

	private final Class<? extends Entity> klass;
	private Entity entity;
	private final String entityName;

	public DefineCommand(Class<? extends Entity> cls, String name) {
		klass = cls;
		entity = null;
		entityName = name;
	}

	@Override
	public void execute() {

		// If the entity has been killed by the undo method, then simply restore it to life
		if (entity != null) {
			entity.restore(entityName);
			return;
		}

		// Create the entity
		JaamSimModel simModel = GUIFrame.getJaamSimModel();
		if (simModel.getNamedEntity(entityName) != null) {
			throw new ErrorException("Name is already in use. Should never happen.");
		}
		entity = InputAgent.defineEntityWithUniqueName(simModel, klass, entityName, "", true);
	}

	@Override
	public void undo() {
		entity.kill();
	}

	@Override
	public Command tryMerge(Command cmd) {
		return null;
	}

	@Override
	public String toString() {
		return String.format("Create: '%s'", entityName);
	}

}

@@ -916,16 +916,54 @@
 mand %7B%0A%0A
+%09private final JaamSimModel simModel;%0A
 %09private
@@ -1000,16 +1000,16 @@
  klass;%0A
-
 %09private
@@ -1125,16 +1125,169 @@
 name) %7B%0A
+%09%09this(GUIFrame.getJaamSimModel(), cls, name);%0A%09%7D%0A%0A%09public DefineCommand(JaamSimModel sim, Class%3C? extends Entity%3E cls, String name) %7B%0A%09%09simModel = sim;%0A
 %09%09klass 
@@ -1529,16 +1529,16 @@
 n;%0A%09%09%7D%0A%0A
+
 %09%09// Cre
@@ -1556,62 +1556,8 @@
 ity%0A
-%09%09JaamSimModel simModel = GUIFrame.getJaamSimModel();%0A
 %09%09if

add property 'simModel' to DefineCommand