Package ag.ion.bion.officelayer.internal.presentation

Source Code of ag.ion.bion.officelayer.internal.presentation.PageService

/****************************************************************************
* ubion.ORS - The Open Report Suite                                        *
*                                                                          *
* ------------------------------------------------------------------------ *
*                                                                          *
* Subproject: NOA (Nice Office Access)                                     *
*                                                                          *
*                                                                          *
* The Contents of this file are made available subject to                  *
* the terms of GNU Lesser General Public License Version 2.1.              *
*                                                                          *
* GNU Lesser General Public License Version 2.1                            *
* ======================================================================== *
* Copyright 2003-2008 by IOn AG                                            *
*                                                                          *
* This library is free software; you can redistribute it and/or            *
* modify it under the terms of the GNU Lesser General Public               *
* License version 2.1, as published by the Free Software Foundation.       *
*                                                                          *
* This library is distributed in the hope that it will be useful,          *
* but WITHOUT ANY WARRANTY; without even the implied warranty of           *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
* Lesser General Public License for more details.                          *
*                                                                          *
* You should have received a copy of the GNU Lesser General Public         *
* License along with this library; if not, write to the Free Software      *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,                    *
* MA  02111-1307  USA                                                      *
*                                                                          *
* Contact us:                                                              *
*  http://www.ion.ag                                                       *
*  info@ion.ag                                                             *
*                                                                          *
****************************************************************************/

/*
* Last changes made by $Author: markus $, $Date: 2007-06-19 15:50:33 +0200 (Di, 19 Jun 2007) $
*/
package ag.ion.bion.officelayer.internal.presentation;

import java.util.HashMap;
import java.util.Map;

import ag.ion.bion.officelayer.presentation.IPageService;
import ag.ion.bion.officelayer.presentation.IPresentationDocument;
import ag.ion.bion.officelayer.presentation.IPresentationPage;
import ag.ion.bion.officelayer.presentation.PresentationException;

import com.sun.star.drawing.XDrawPage;
import com.sun.star.drawing.XDrawPages;
import com.sun.star.drawing.XDrawPagesSupplier;
import com.sun.star.drawing.XDrawView;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.presentation.XPresentationPage;
import com.sun.star.uno.UnoRuntime;

/**
* Page service of a presentation document.
*
* @author Markus Kr�ger
* @version $Revision: 11494 $
*/
public class PageService implements IPageService {

  private IPresentationDocument presentationDocument = null;
  private Map<XPresentationPage, IPresentationPage> presentationPages = new HashMap<XPresentationPage, IPresentationPage>();

  private XDrawView xDrawView = null;

  // ----------------------------------------------------------------------------
  /**
   * Constructs new PageService.
   *
   * @param presentationDocument
   *            presentation document to be used
   *
   * @throws IllegalArgumentException
   *             if the submitted presentation document is not valid
   *
   * @author Markus Kr�ger
   * @date 07.01.2008
   */
  public PageService(IPresentationDocument presentationDocument)
      throws IllegalArgumentException {
    if (presentationDocument == null)
      throw new IllegalArgumentException(
          "Submitted presentation document is not valid.");
    this.presentationDocument = presentationDocument;
    this.xDrawView = getDrawView();
  }

  // ----------------------------------------------------------------------------
  /**
   * Returns number of available pages.
   *
   * @return number of available pages
   *
   * @author Markus Kr�ger
   * @date 07.01.2008
   */
  public int getPageCount() {
    try {
      XDrawPagesSupplier dSupplier = (XDrawPagesSupplier) UnoRuntime
          .queryInterface(XDrawPagesSupplier.class,
              presentationDocument.getXComponent());
      XDrawPages pages = dSupplier.getDrawPages();
      return pages.getCount();
    } catch (Exception exception) {
      return 0;
    }
  }

  // ----------------------------------------------------------------------------
  /**
   * Navigates to the page with the submitted index. The first page has the
   * index 0.
   *
   * @param index
   *            index of the page
   *
   * @throws PresentationException
   *             if the page is not available
   *
   * @author Markus Kr�ger
   * @author Sebastian Patschorke
   * @date 07.01.2008
   */
  public void goToPage(int index) throws PresentationException {
    try {
      IPresentationPage presentationPage = getPresentationPage(index);
      XPresentationPage page = presentationPage.getXPresentationPage();
      xDrawView.setCurrentPage(page);
    } catch (Throwable throwable) {
      PresentationException textException = new PresentationException(
          throwable.getMessage());
      textException.initCause(throwable);
      throw textException;
    }
  }

  // ----------------------------------------------------------------------------

  /**
   * @author Sebastian Patschorke
   */
  private XDrawView getDrawView() {
    XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class,
        presentationDocument.getPresentationSupplier());
    XController xController = xModel.getCurrentController();
    XDrawView drawView = (XDrawView) UnoRuntime.queryInterface(
        XDrawView.class, xController);
    return drawView;
  }

  /**
   * @author Sebastian Patschorke
   * @throws PresentationException
   */
  @Override
  public IPresentationPage getCurrentPresentationPage()
      throws PresentationException {
    XDrawPage dp = xDrawView.getCurrentPage();
    XPresentationPage xPresentationPage = (XPresentationPage) UnoRuntime
        .queryInterface(XPresentationPage.class, dp);

    return getPresentationPage(xPresentationPage);
  }

  /**
   * @author Sebastian Patschorke
   * @throws PresentationException
   */
  @Override
  public IPresentationPage getPresentationPage(int page)
      throws PresentationException {
    XPresentationPage xPresentationPage = getXPresentationPage(page);
    return getPresentationPage(xPresentationPage);
  }

  /**
   * @author Sebastian Patschorke
   * @throws PresentationException
   */
  private IPresentationPage getPresentationPage(
      XPresentationPage xPresentationPage) throws PresentationException {
    IPresentationPage presentationPage = presentationPages
        .get(xPresentationPage);
    if (presentationPage != null)
      return presentationPage;

    presentationPage = new PresentationPage(presentationDocument,
        xPresentationPage);
    presentationPages.put(xPresentationPage, presentationPage);
    return presentationPage;
  }

  /**
   * @author Sebastian Patschorke
   * @author Markus Kr�ger
   *
   * @throws PresentationException
   */
  private XPresentationPage getXPresentationPage(int page)
      throws PresentationException {
    try {
      XDrawPagesSupplier dSupplier = (XDrawPagesSupplier) UnoRuntime
          .queryInterface(XDrawPagesSupplier.class,
              presentationDocument.getXComponent());
      XDrawPages pages = dSupplier.getDrawPages();

      // TODO why no assignment?
      UnoRuntime.queryInterface(XDrawPage.class, pages.getByIndex(page));
      XPresentationPage xPresentationPage = (XPresentationPage) UnoRuntime
          .queryInterface(XPresentationPage.class, pages
              .getByIndex(page));
      return xPresentationPage;
    } catch (Throwable throwable) {
      PresentationException textException = new PresentationException(
          throwable.getMessage());
      textException.initCause(throwable);
      throw textException;
    }
  }
}
TOP

Related Classes of ag.ion.bion.officelayer.internal.presentation.PageService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.