/**
   * Opens an index entry in the booklet.
   *
   * @param resetTextField will clear the text in the searchField and reset the search entry's data
   */
  @SuppressWarnings("unchecked")
  public static void openIndexEntry(
      GuiBooklet booklet, BookletEntry entry, int page, boolean resetTextField) {
    booklet.searchField.setVisible(entry instanceof BookletEntryAllSearch);
    booklet.searchField.setFocused(entry instanceof BookletEntryAllSearch);
    if (resetTextField) {
      booklet.searchField.setText("");
      if (entry instanceof BookletEntryAllSearch) {
        entry.chapters =
            (ArrayList<BookletChapter>) ((BookletEntryAllSearch) entry).allChapters.clone();
      }
    }

    booklet.currentPage = null;
    booklet.currentChapter = null;

    booklet.currentIndexEntry = entry;
    booklet.indexPageAmount =
        entry == null ? 1 : entry.chapters.size() / booklet.chapterButtons.length + 1;
    booklet.pageOpenInIndex =
        entry == null
            ? 1
            : (booklet.indexPageAmount <= page || page <= 0 ? booklet.indexPageAmount : page);

    booklet.buttonPreviousScreen.visible = entry != null;
    booklet.buttonForward.visible = booklet.pageOpenInIndex < booklet.indexPageAmount;
    booklet.buttonBackward.visible = booklet.pageOpenInIndex > 1;

    for (int i = 0; i < booklet.chapterButtons.length; i++) {
      IndexButton button = (IndexButton) booklet.chapterButtons[i];
      if (entry == null) {
        boolean entryExists = InitBooklet.entries.size() > i;
        button.visible = entryExists;
        if (entryExists) {
          button.displayString = InitBooklet.entries.get(i).getNameWithColor();
          button.chap = null;
        }
      } else {
        boolean entryExists =
            entry.chapters.size()
                > i
                    + (booklet.chapterButtons.length * booklet.pageOpenInIndex
                        - booklet.chapterButtons.length);
        button.visible = entryExists;
        if (entryExists) {
          BookletChapter chap =
              entry.chapters.get(
                  i
                      + (booklet.chapterButtons.length * booklet.pageOpenInIndex
                          - booklet.chapterButtons.length));
          button.displayString = chap.getNameWithColor();
          button.chap = chap;
        }
      }
    }
  }