JComponent subMenu;
// Put the programs in a sub menu?
if (Settings.propTrayNowProgramsInSubMenu.getBoolean() && Settings.propTrayNowProgramsEnabled.getBoolean()) {
subMenu = new ScrollableMenu(mLocalizer.msg("menu.programsNow", "Now running programs"));
} else {
subMenu = mTrayMenu;
}
ArrayList<ProgramMenuItem> programs = new ArrayList<ProgramMenuItem>();
ArrayList<ProgramMenuItem> additional = new ArrayList<ProgramMenuItem>();
ArrayList<ProgramMenuItem> nextPrograms = new ArrayList<ProgramMenuItem>();
ArrayList<ProgramMenuItem> nextAdditionalPrograms = new ArrayList<ProgramMenuItem>();
/*
* Search through all channels.
*/
Date currentDate = Date.getCurrentDate();
for (Channel channel : channels) {
ChannelDayProgram today = TvDataBase.getInstance().getDayProgram(currentDate, channel);
if (today != null && today.getProgramCount() > 0) {
final int programCount = today.getProgramCount();
for (int j = 0; j < programCount; j++) {
if (j == 0 && today.getProgramAt(j).getStartTime() > IOUtilities.getMinutesAfterMidnight()) {
ChannelDayProgram yesterday = TvDataBase.getInstance().getDayProgram(currentDate.addDays(-1), channel);
if (yesterday != null && yesterday.getProgramCount() > 0) {
Program p = yesterday.getProgramAt(yesterday.getProgramCount() - 1);
if (p.isOnAir()) {
addToNowRunning(p, programs, additional);
Program p1 = today.getProgramAt(0);
addToNext(p1, nextPrograms, nextAdditionalPrograms);
break;
}
}
}
Program p = today.getProgramAt(j);
if (p.isOnAir()) {
addToNowRunning(p, programs, additional);
Program nextProgram = null;
if (j < programCount - 1) {
nextProgram = today.getProgramAt(j + 1);
} else {
ChannelDayProgram tomorrow = TvDataBase.getInstance().getDayProgram(currentDate.addDays(1), channel);
if (tomorrow != null && tomorrow.getProgramCount() > 0) {
nextProgram = tomorrow.getProgramAt(0);
}
}
if (nextProgram != null) {
addToNext(nextProgram, nextPrograms, nextAdditionalPrograms);
}
break;
}
}
}
}
// Show important programs?
if (Settings.propTrayImportantProgramsEnabled.getBoolean()) {
if (Settings.propTrayImportantProgramsInSubMenu.getBoolean()) {
mTrayMenu.add(addToImportantMenu(new ScrollableMenu(mLocalizer.msg("menu.programsImportant",
"Important programs")), programs));
} else {
addToImportantMenu(mTrayMenu, programs);
}
}
/*
* if there are running programs and they should be displayed add them to
* the menu.
*/
if (Settings.propTrayImportantProgramsEnabled.getBoolean()) {
mTrayMenu.addSeparator();
}
boolean now = false;
// filter duplicates from additional programs
for (ProgramMenuItem addItem : additional) {
boolean equal = false;
for (ProgramMenuItem item : programs) {
if (item.getProgram().equals(addItem.getProgram())) {
equal = true;
break;
}
}
if (!equal) {
programs.add(addItem);
}
}
if (Settings.propTrayNowProgramsEnabled.getBoolean() && (programs.size() > 0 || additional.size() > 0)) {
addMenuItems(subMenu, programs);
now = true;
if (subMenu instanceof JMenu) {
addNoProgramsItem((JMenu) subMenu);
}
}
if (Settings.propTrayNowProgramsInSubMenu.getBoolean() && Settings.propTrayNowProgramsEnabled.getBoolean()) {
mTrayMenu.add(subMenu);
}
if (Settings.propTraySoonProgramsEnabled.getBoolean()
&& (!nextPrograms.isEmpty() || !nextAdditionalPrograms.isEmpty())) {
final JMenu next = new ScrollableMenu(now ? mLocalizer.msg("menu.programsSoon", "Soon runs") : mLocalizer.msg(
"menu.programsSoonAlone", "Soon runs"));
addMenuItems(next, nextPrograms);
addMenuItems(next, nextAdditionalPrograms);
addNoProgramsItem(next);