@Override
	public boolean applyTo(Program program, TaskMonitor monitor) {

		Data data = program.getListing().getDefinedDataContaining(addr);
		Data startData = data.getComponent(startPath);
		if (startData == null) {
			setStatusMsg("Component data not found");
			return false;
		}

		Data parent = startData.getParent();
		DataType parentDataType = parent.getBaseDataType();

		if (!(parentDataType instanceof Structure)) {
			setStatusMsg("Range based operation only supported within Structures");
			return false;
		}

		DataType existingDT = startData.getDataType();
		int startIndex = startData.getComponentIndex();
		Data lastComp = parent.getComponentContaining(
			(int) (startData.getMinAddress().subtract(parent.getMinAddress()) + length - 1));
		int endIndex = lastComp.getComponentIndex();

		Structure struct = (Structure) parentDataType;

		// TODO: Is looking at the first component data-type (existingDT) the right thing to do for a selection ??

		if (newDataType instanceof FactoryDataType) {
			setStatusMsg("Factory data-type not allowed in structure: " + newDataType.getName());
			return false;
		}

		newDataType = newDataType.clone(program.getDataTypeManager());
		newDataType =
			DataUtilities.reconcileAppliedDataType(existingDT, newDataType, stackPointers);

		if (newDataType instanceof Dynamic && !((Dynamic) newDataType).canSpecifyLength()) {
			setStatusMsg(
				"Non-sizable Dynamic data-type not allowed in structure: " + newDataType.getName());
			return false;
		}

		for (int i = endIndex; i >= startIndex; i--) {
			struct.clearComponent(i);
		}

		if (newDataType != DataType.DEFAULT) {
			int index = startIndex;
			int numCreated = 0;
			while (length > 0) {
				try {
//			        MemBuffer memBuf = new ProgramStructureProviderContext(program,addr, 
//    	    	    					struct, struct.getComponent(index).getOffset());
					DataTypeInstance dti =
						DataTypeInstance.getDataTypeInstance(newDataType, length, false);
					if (dti == null || dti.getLength() > length) {
						break;
					}
					DataTypeComponent dtc =
						struct.replace(index, dti.getDataType(), dti.getLength());
					length -= dtc.getLength();
					numCreated++;
					index++;
				}

				catch (Exception e) {
					setStatusMsg(e.getMessage());
					return false;
				}
			}
			if (numCreated == 0) {
				setStatusMsg("Not enough space");
				return false;
			}
		}
		return true;
	}
@see ghidra.framework.cmd.BackgroundCommand#applyTo(ghidra.framework.model.DomainObject, ghidra.util.task.TaskMonitor)
Ghidra/Features/Base/src/main/java/ghidra/app/cmd/data/CreateDataInStructureBackgroundCmd.java