/** * Sets the slot at the specified unitIndex to the specified Unit. * * @param unitIndex the index of the slot to put the Unit in * @param unit the Unit to set */ public void newSetUnit(int unitIndex, Unit unit) { if (unit != null) { // System.out.println("\nnewSetUnit " + unitIndex + " to " + unit); } // checkConsistency("start of newSetUnit"); if (unitIndex >= size || unitIndex < 0) { return; // shouldn't happen, but can? } if (unit == null) { delUnit(unitIndex); return; } else { } // Pre-conditioning: If unit is too long for part, truncate it first: int unitDuration = unit.getRhythmValue(); int nextUnitStart = unitIndex + unitDuration; if (nextUnitStart >= size) { unit = unit.copy(); unit.setRhythmValue(size - unitIndex); nextUnitStart = size; } // If this unit overlays one or more units, set them to null. // Let makeConsistent fix things up. for (int index = unitIndex; index < nextUnitStart; index++) { slots.set(index, null); } // If the new unit overlays part of another unit, replace the latter // with a rest. if (nextUnitStart < size && slots.get(nextUnitStart) == null) { int nextUnitEnd = getNextIndex(nextUnitStart); if (this instanceof MelodyPart) { slots.set(nextUnitStart, new Rest(nextUnitEnd - nextUnitStart)); } else { // HB - Disabled as of 7-8-13, not sure this is needed when pasting // chords back into leadsheet. // slots.set(nextUnitStart, new Chord(nextUnitEnd - nextUnitStart)); } } // Place the new unit setSlot(unitIndex, unit, "leaving newSetUnit"); // Re-eneable this!! checkConsistency("end of newSetUnit"); }