class CreateArrayInStructureCmd implements Command {

	public boolean applyTo(DomainObject obj) {
		Program program = (Program)obj;
		Listing listing = program.getListing();
		
		if (dataType instanceof FactoryDataType) {
			msg = "Array not allowed on a Factory data-type: " + dataType.getName();
			return false;
		}
		if (dataType instanceof Dynamic && !((Dynamic)dataType).canSpecifyLength()) {
			msg = "Array not allowed on a non-sizable Dynamic data-type: " + dataType.getName();
			return false;
		}
		if (numElements <= 0) {
			msg = "Number of elements must be positive, not "+numElements;
			return false;
		}

		Data data = listing.getDataContaining(addr);
		Data compData = data.getComponent(compPath);
		
		int elementLength;
		if (dataType instanceof Dynamic) {
			elementLength = compData.getLength();
		}
		else {
			elementLength = dataType.getLength();
		}
		if (elementLength <=0) {
			msg = "DataType must have fixed size > 0, not "+elementLength;
			return false;
		}
		int length = numElements*elementLength;

        int index = compData.getComponentIndex();
        int offset = compData.getParentOffset();
        DataType parentDataType = compData.getParent().getBaseDataType();

        if (!(parentDataType instanceof Structure)) {
            msg = "Data not in a structure";
            return false;
        }
        Structure struct = (Structure)parentDataType;
       	if (offset+length > struct.getLength()) {
       		msg = "Array too big for structure";
       		return false;
       	}

		try {
        	ArrayDataType adt = new ArrayDataType(dataType, numElements, dataType.getLength());
			clearStruct(struct, compData.getParentOffset(), length);
			if (struct.isInternallyAligned()) {
				struct.insert(index, adt, adt.getLength());
			}
			else {
				struct.replace(index, adt, adt.getLength());
			}
		}
		catch(Exception e) {
			msg = e.getMessage();
			return false;
		}	
		return true;
	}

	public  CreateArrayInStructureCmd(Address addr, int numElements, DataType dt,
									  int[] compPath);

}