Example #1
0
  synchronized void openBookInternal(Book book, Bookmark bookmark) {
    if (book == null) {
      book = Library.Instance().getRecentBook();
      if (book == null || !book.File.exists()) {
        book = Book.getByFile(Library.getHelpFile());
      }
      if (book == null) {
        return;
      }
    }
    if (Model != null) {
      if (bookmark == null & book.File.getPath().equals(Model.Book.File.getPath())) {
        return;
      }
    }

    if (book != null) {
      onViewChanged();

      if (Model != null) {
        Model.Book.storePosition(BookTextView.getStartCursor());
      }
      BookTextView.setModel(null);
      FootnoteView.setModel(null);
      clearTextCaches();

      Model = null;
      System.gc();
      System.gc();
      try {
        Model = BookModel.createModel(book);
        ZLTextHyphenator.Instance().load(book.getLanguage());
        BookTextView.setModel(Model.getTextModel());
        BookTextView.gotoPosition(book.getStoredPosition());
        if (bookmark == null) {
          setView(BookTextView);
        } else {
          gotoBookmark(bookmark);
        }
        Library.Instance().addBookToRecentList(book);
        final StringBuilder title = new StringBuilder(book.getTitle());
        if (!book.authors().isEmpty()) {
          boolean first = true;
          for (Author a : book.authors()) {
            title.append(first ? " (" : ", ");
            title.append(a.DisplayName);
            first = false;
          }
          title.append(")");
        }
        setTitle(title.toString());
      } catch (BookReadingException e) {
        processException(e);
      }
    }
    getViewWidget().reset();
    getViewWidget().repaint();
  }
Example #2
0
  public Bookmark addBookmark(int maxLength, boolean visible) {
    final FBView view = getTextView();
    final ZLTextWordCursor cursor = view.getStartCursor();

    if (cursor.isNull()) {
      return null;
    }

    return new Bookmark(Model.Book, view.getModel().getId(), cursor, maxLength, visible);
  }
Example #3
0
  public void setModels(ArrayList /*<ZLTextModel>*/ models, String fileName) {
    myFileName = fileName;

    myPositionStack.clear();

    final int stackSize =
        new ZLIntegerRangeOption(
                ZLOption.STATE_CATEGORY, fileName, BUFFER_SIZE, 0, MAX_UNDO_STACK_SIZE, 0)
            .getValue();
    myCurrentPointInStack =
        new ZLIntegerRangeOption(
                ZLOption.STATE_CATEGORY,
                fileName,
                POSITION_IN_BUFFER,
                0,
                (stackSize == 0) ? 0 : (stackSize - 1),
                0)
            .getValue();

    if (models != null) {
      final ZLIntegerOption option = new ZLIntegerOption(ZLOption.STATE_CATEGORY, fileName, "", 0);
      final int size = models.size();
      for (int i = 0; i < stackSize; ++i) {
        //		option.changeName(MODEL_PREFIX + i);
        //		final int modelIndex = option.getValue();
        option.changeName(PARAGRAPH_PREFIX + i);
        int paragraphIndex = option.getValue();
        int modelIndex = -1;
        int paragraphsNumber = 0;
        while (paragraphIndex >= 0 && paragraphsNumber != 1) {
          modelIndex++;
          paragraphsNumber =
              modelIndex >= 0 && modelIndex < size
                  ? ((ZLTextModel) models.get(modelIndex)).getParagraphsNumber() + 1
                  : 1;
          paragraphIndex -= paragraphsNumber;
        }
        paragraphIndex += paragraphsNumber;
        option.changeName(WORD_PREFIX + i);
        final int wordIndex = option.getValue();
        option.changeName(CHAR_PREFIX + i);
        final int charIndex = option.getValue();
        myPositionStack.add(new Position(modelIndex, paragraphIndex, wordIndex, charIndex));
      }
    }
    if (!myPositionStack.isEmpty()) {
      super.setModels(models, ((Position) myPositionStack.get(myCurrentPointInStack)).ModelIndex);
    } else {
      super.setModels(models, 0);
    }
    if ((getModel() != null) && (!myPositionStack.isEmpty())) {
      gotoPosition((Position) myPositionStack.get(myCurrentPointInStack));
    }
  }
Example #4
0
 public void gotoBookmark(Bookmark bookmark) {
   final String modelId = bookmark.ModelId;
   if (modelId == null) {
     addInvisibleBookmark();
     BookTextView.gotoPosition(bookmark);
     setView(BookTextView);
   } else {
     FootnoteView.setModel(Model.getFootnoteModel(modelId));
     FootnoteView.gotoPosition(bookmark);
     setView(FootnoteView);
   }
   getViewWidget().repaint();
 }
Example #5
0
  public boolean jumpBack() {
    try {
      if (getTextView() != BookTextView) {
        showBookTextView();
        return true;
      }

      if (myJumpEndPosition == null || myJumpTimeStamp == null) {
        return false;
      }
      // more than 2 minutes ago
      if (myJumpTimeStamp.getTime() + 2 * 60 * 1000 < new Date().getTime()) {
        return false;
      }
      if (!myJumpEndPosition.equals(BookTextView.getStartCursor())) {
        return false;
      }

      final List<Bookmark> bookmarks = Library.Instance().invisibleBookmarks(Model.Book);
      if (bookmarks.isEmpty()) {
        return false;
      }
      final Bookmark b = bookmarks.get(0);
      b.delete();
      gotoBookmark(b);
      return true;
    } finally {
      myJumpEndPosition = null;
      myJumpTimeStamp = null;
    }
  }
Example #6
0
  protected synchronized void preparePaintInfo() {
    super.preparePaintInfo();
    if (myPositionStack.isEmpty()) {
      myPositionStack.add(new Position(myCurrentModelIndex, StartCursor));
    } else {
      ((Position) myPositionStack.get(myCurrentPointInStack)).set(StartCursor);
      ((Position) myPositionStack.get(myCurrentPointInStack)).ModelIndex = myCurrentModelIndex;

      //	Position position = (Position)myPositionStack.get(myCurrentPointInStack);
      //	System.out.println("current position " + position.ModelIndex + " , " +
      // position.ParagraphIndex);
    }
  }
Example #7
0
 public void tryOpenFootnote(String id) {
   if (Model != null) {
     myJumpEndPosition = null;
     myJumpTimeStamp = null;
     BookModel.Label label = Model.getLabel(id);
     if (label != null) {
       if (label.ModelId == null) {
         if (getTextView() == BookTextView) {
           addInvisibleBookmark();
           myJumpEndPosition = new ZLTextFixedPosition(label.ParagraphIndex, 0, 0);
           myJumpTimeStamp = new Date();
         }
         BookTextView.gotoPosition(label.ParagraphIndex, 0, 0);
         setView(BookTextView);
       } else {
         FootnoteView.setModel(Model.getFootnoteModel(label.ModelId));
         setView(FootnoteView);
         FootnoteView.gotoPosition(label.ParagraphIndex, 0, 0);
       }
       getViewWidget().repaint();
     }
   }
 }
Example #8
0
  public TOCTree getCurrentTOCElement() {
    final ZLTextWordCursor cursor = BookTextView.getStartCursor();
    if (Model == null || cursor == null) {
      return null;
    }

    int index = cursor.getParagraphIndex();
    if (cursor.isEndOfParagraph()) {
      ++index;
    }
    TOCTree treeToSelect = null;
    for (TOCTree tree : Model.TOCTree) {
      final TOCTree.Reference reference = tree.getReference();
      if (reference == null) {
        continue;
      }
      if (reference.ParagraphIndex > index) {
        break;
      }
      treeToSelect = tree;
    }
    return treeToSelect;
  }
Example #9
0
 public void onWindowClosing() {
   if (Model != null && BookTextView != null) {
     Model.Book.storePosition(BookTextView.getStartCursor());
   }
 }
Example #10
0
 public void clearTextCaches() {
   BookTextView.clearCaches();
   FootnoteView.clearCaches();
 }