How to add n- custom cells between two rows

33 Views Asked by At

So I am trying to visualize a curricilum as a table. It should look like this: table

As you can see there are custom cells (+) which are not a lesson. They are buttons.

I have two classes:

public class Lesson {
  private Room schoolRoom;
  private Room teachingRoom;
  private TeacherSpecialization teachingInfo;
  private WeekDay weekDay;
  private int schoolHour;
}

and

public class ClassHour {

  Lesson[] dayLessons = new Lesson[18];
  private int hour;

  public ClassHour(int hour) {
    this.hour = hour;
  } 
}

Using this code I convert my Lesson Object to ClassHour objects, because I use the ClassHour Object to save the lessons in the table:

public ObservableList<ClassHour> convertToClassHour(List<Lesson> lessons) {
    ObservableList<ClassHour> classHours = FXCollections.observableArrayList();
    // Converting Lessons to ClassHour objects.
    lessons.forEach(lesson -> {
        ClassHour classHour = classHours.stream().filter(ch -> ch.getHour() == lesson.getSchoolHour()).findFirst().orElse(null);
        if (classHour == null) {
            classHour = new ClassHour(lesson.getSchoolHour());
            classHours.add(classHour);
        }
        classHour.getDayLessons()[lesson.getWeekDay().ordinal()] = lesson;
    });

    return classHours;
}

And the last step is to show the data in the table:

private void showLessons(String roomNr) throws Exception {
    try {
        //    lessons.addListener((ListChangeListener) e -> repopulate(lessons, classHours));

        ArrayList<Lesson> allLessonsByRoomNr = db.getAllLessonsByRoomNr(roomNr);
        ObservableList<ClassHour> classHours = db.convertToClassHour(allLessonsByRoomNr);
        for (int i = 0; i < 5; i++) {
            int day = i;
            TableColumn<ClassHour, Lesson> dayColumn = new TableColumn<>(WeekDay.values()[i].name());
            dayColumn.setSortable(false);
            dayColumn.setCellValueFactory(param -> new SimpleObjectProperty(param.getValue().getDayLessons()[day]));
            dayColumn.setCellFactory((TableColumn<ClassHour, Lesson> param) -> new TableCell<ClassHour, Lesson>() {
                @Override
                protected void updateItem(Lesson item, boolean empty) {

                    super.updateItem(item, empty);
                    setText(null);
                    setGraphic(null);
                    if (!empty) {
                        if (item != null) {
                            setText(item.toString());
                        } else {
                            Button btn = new Button("+ ADD");
                            btn.setOnAction(e -> {
                                tableLessons.getSelectionModel().select((ClassHour) getTableRow().getItem());
                                showAdd(day, ((ClassHour) getTableRow().getItem()).getHour(), btn);
                            });
                            setGraphic(new StackPane(btn));
                        }
                    }
                }
            });
            tableLessons.getColumns().addAll(dayColumn);
        }
        tableLessons.setItems(classHours);

    } catch (Exception ex) {
        showResultDialog("An error has occured:", ex.getMessage());
    }
}

The problem I am struggling are the custom cells (+ buttons). For some reason I can't draw them between two lessons. In the case below there should be 5 cells/rows between classhour 10 and 16 (monday). current table

1

There are 1 best solutions below

0
On BEST ANSWER

Note that your convertToClassHour creates a ClassHour instance if and only if there is a Lesson and if the Lessons are not ordered by hour, the order of ClassHours in the output is wrong.

Unless you've got a predetermined set of hours, you need to find the min and max hours to fix your issue:

public ObservableList<ClassHour> convertToClassHour(List<Lesson> lessons) {
    ObservableList<ClassHour> classHours = FXCollections.observableArrayList();

    if (!lessons.isEmpty()) {
        // find required hour range
        int minHour = lessons.mapToInt(Lesson::getSchoolHour).min().getAsInt();
        int maxHour = lessons.mapToInt(Lesson::getSchoolHour).max().getAsInt();

        // create ClassHours for range
        for (int i = minHour; i <= maxHour; i++) {
            classHours.add(new ClassHour(i));
        }
        // fill classHours with lessons
        for (Lesson lesson : lessons) {
            classHours.get(lesson.getSchoolHour() - minHour).getDayLessons()[lesson.getWeekDay().ordinal()] = lesson;
        }
    }

    return classHours;
}