event.setLevel(null);
}
int periodsSize = periods.size();
for (int periodIndex = 0; periodIndex < periodsSize; periodIndex++) {
Period period = periods.get(periodIndex);
UIEvent fromEvent = events.get(period.getFrom().getUid());
UIEvent toEvent = events.get(period.getTo().getUid());
int from = fromEvent.getFrom();
int to = toEvent.getTo();
for (int level = 0; level < MAX_LEVEL; level++) {
// try to put period on the "level" level
boolean canPlaceOnLevel = true;
for (int i = 0; i < periodIndex; i++) {
Period period1 = periods.get(i);
if (events.get(period1.getFrom().getUid()).getLevel() != level) {
continue;
}
int from1 = events.get(period1.getFrom().getUid()).getFrom();
int to1 = events.get(period1.getTo().getUid()).getTo();
if (to + DELTA > from1 &&
from - DELTA < to1) {
canPlaceOnLevel = false;
break;
}
}
if (canPlaceOnLevel) {
fromEvent.setLevel(level);
toEvent.setLevel(level);
break;
}
}
if (fromEvent.getLevel() == null) {
throw new RuntimeException("That's finally happened - we need to update algorithm");
}
}
List<UIEvent> eventList = new ArrayList<UIEvent>();
eventList.addAll(events.values());
int eventsSize = eventList.size();
for (int eventIndex = 0; eventIndex < eventsSize; eventIndex++) {
UIEvent event = eventList.get(eventIndex);
if (event.getLevel() != null) {
continue;
}
int from = event.getFrom();
int to = event.getTo();
for (int level = 0; level < MAX_LEVEL; level++) {
// try to place event on the level
boolean canPlaceOnLevel = true;
for (Period period1 : periods) {
if (events.get(period1.getFrom().getUid()).getLevel() != level) {
continue;
}
int from1 = events.get(period1.getFrom().getUid()).getFrom();
int to1 = events.get(period1.getTo().getUid()).getTo();
if (to + DELTA > from1 &&
from - DELTA < to1) {
canPlaceOnLevel = false;
break;
}
}
if (canPlaceOnLevel) {
for (int i = 0; i < eventIndex; i++) {
UIEvent event1 = eventList.get(i);
if (event1.getLevel() != level) {
continue;
}
int from1 = event1.getFrom();
int to1 = event1.getTo();
if (to + DELTA > from1 &&
from - DELTA < to1) {
canPlaceOnLevel = false;
break;
}