int minY = 0;
int maxY = 0;
int minSameTimeY = -1;
long lastStartTime = 0;
Program minProgram;
ProgramPanel minPanel;
do {
// Find out the program with the lowest start time
minProgram = null;
minPanel = null;
int programCol = 0;
long minStartTime = Long.MAX_VALUE;
for (int col = 0; col < columnCount; col++) {
ProgramPanel panel = model.getProgramPanel(col, rowIdxArr[col]);
if (panel != null) {
Program program = panel.getProgram();
if (program != null) {
long startTime = (program.getDate().getValue()) * 10000
+ program.getStartTime();
if (startTime < minStartTime) {
// found earliest by now
minStartTime = startTime;
minProgram = program;
minPanel = panel;
programCol = col;
}
}
}
}
// Layout the program
if (minProgram != null) {
int programRow = rowIdxArr[programCol];
// Get the y position for the program
int y = Math.max(minY, colYArr[programCol]);
if (minStartTime == lastStartTime) {
y = Math.max(minSameTimeY, colYArr[programCol]);
}
// Ensure that the start of the program is at the specified y
if (programRow == 0) {
// This is the first program of this column -> Set columnStartArr
columnStartArr[programCol] = y;
} else {
// Adjust the last program of this column to reach the y position
ProgramPanel lastPanel = model.getProgramPanel(programCol, programRow - 1);
int height = lastPanel.getPreferredHeight();
int heightDiff = y - colYArr[programCol];
height += heightDiff;
lastPanel.setHeight(height);
}
// Get the height for the program
int preferredHeight = minPanel.getPreferredHeight();
// Set the height for the program if it is the last of the row
if (programRow + 1 == model.getRowCount(programCol)) {
// It is the last row
minPanel.setHeight(preferredHeight);
}
// Prepare the next iteration
if (minStartTime != lastStartTime) {
//minimum y for programs with same start time
minSameTimeY = y;
}
minY = Math.max(y, minY);
lastStartTime = minStartTime;
colYArr[programCol] = y + preferredHeight;
maxY = Math.max(maxY,colYArr[programCol]);
rowIdxArr[programCol]++;
}
} while (minProgram != null);
// expand last program of each column up to end of day (if it reaches end of day)
for (int col = 0; col < columnCount; col++) {
int count = model.getRowCount(col);
if(count > 0) {
ProgramPanel panel = model.getProgramPanel(col, count-1);
Program program = panel.getProgram();
if (program.getStartTime() <= Settings.propProgramTableEndOfDay.getInt() && program.getStartTime() + program.getLength() >= Settings.propProgramTableEndOfDay.getInt()) {
panel.setHeight(maxY - colYArr[col] + panel.getHeight());
}
}
}