### Slideshow mechanism.

The slideshow feature in the Photo Album Demo can be enabled by clicking on "Start Slideshow" link from two different places in the application: 1) from user's album preview (`/web/src/main/webapp/image/albumInfo.xhtml`) and 2) from a particular photo preview (`src/main/webapp/image/imageInfo.xhtml`). Both of two mentioned XHTML files include slideshow with the help of Facelets <ui:include tag (for more information about <ui:include see Facelets Reference Guide " http://www.jsftoolbox.com/documentation/facelets/01-Introduction/index.jsf).

The components that implement the slideshow functionality are:

- <rich:modalPanel> located in `web/src/main/webapp/includes/image/slideshow.xhtml` that is hidden by default as the attribute `showWhenRendered=" {slideshow.active}"` and the active property of SlideshowManager.java is set to "false" by default.

- <a4j:poll> located in `includes/misc/slideShowPooler.xhtml` which is also inactive due to the mentioned active property ( `active= {slideshow.active}`)

After activation, <a4j:poll> will send asynchronous requests to the server with some certain interval, as the result of these requests modal panel will display the next image in the row.

Now let's have a look at the details of the slideshow implementation.

The `startSlideshow()` method of `SlideshowManager.java` is invoked when no photo is selected in the current image list. The method iterates over all photos of a particular album starting from the first one in the list. Look at the `SlideshowManager.java` listing below:

    ...

      ...

    public void startSlideshow(){

          active = true;

          this.slideshowIndex = 0;

          if(model.getImages() == null || model.getImages().size() < 1){

                stopSlideshow();

                Events.instance().raiseEvent(Constants.ADD_ERROR_EVENT, "No images for slideshow!");

                return;

          }

          this.selectedImage = model.getImages().get(this.slideshowIndex);

          this.selectedImage.getAlbum().visitImage(selectedImage, true);

    }

    ...

The second variation of the `startSlideshow()` method is activated when a link to slide-show is clicked from a particular photo preview. This method iterates over the rest of photos starting from the current selected one:

    ...

     public void startSlideshow(Image selectedImage){

          active = true;

          if(model.getImages() == null || model.getImages().size() < 1){

                stopSlideshow();

                Events.instance().raiseEvent(Constants.ADD_ERROR_EVENT, "No images for slideshow!");

                return;

          }

          this.slideshowIndex = model.getImages().indexOf(selectedImage);

          this.selectedImage = selectedImage;

          this.selectedImage.getAlbum().visitImage(selectedImage, true);

    }

    ...

Both variants of `startSlideshow()` method set the `active` property to "true" as a result the poller is activated and modal panel becomes visible.

The slideshow modal panel is kept in the `web/src/main/webapp/includes/image/slideshow.xhtml` file and referred from the corresponding pages with the help of <ui:include> Facelets tag:

    ...

    <ui:include src="/includes/image/slideshow.xhtml"/>

    ...

Have a look at `web/src/main/webapp/includes/image/slideshow.xhtml` file:

    ...

    <ui:composition xmlns="http://www.w3.org/1999/xhtml"...>

          <rich:modalPanel showWhenRendered=" {slideshow.active}"

                      domElementAttachment="parent"

                      id="slideShowModalPanel"

                      width="650"

                      onshow="showPictureEffect();"

                      autosized="true">

                <f:facet name="controls">

                      <h:panelGroup>

                            <h:graphicImage value="/img/modal/close.png" style="cursor:pointer" id="hidelink">

                                  <a4j:support event="onclick" actionListener=" {slideshow.stopSlideshow}" reRender="slideShowForm, mainArea, tree" />

                            </h:graphicImage>

                      </h:panelGroup>

                </f:facet>

                ...

          </rich:modalPanel>

    </ui:composition>

    ...

This is the source code of `includes/misc/slideShowPooler.xhtml`:

    ...

    <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <ui:composition xmlns="http://www.w3.org/1999/xhtml"

        xmlns:s="http://jboss.com/products/seam/taglib"

        xmlns:ui="http://java.sun.com/jsf/facelets"

        xmlns:f="http://java.sun.com/jsf/core"

        xmlns:h="http://java.sun.com/jsf/html"

        xmlns:rich="http://richfaces.org/rich"

        xmlns:a4j="http://richfaces.org/a4j"

        xmlns:richx="http://richfaces.org/richx">

          <a4j:form id="slideShowForm">

                <a4j:poll reRender="slideshowImage"

                                  actionListener=" {slideshow.showNextImage()}"

                                  interval=" {slideshow.interval}"

                                  enabled=" {slideshow.active}"

                                  onsubmit="hidePictureEffect()"

                                  oncomplete="showPictureEffect();"/>

          </a4j:form>

    </ui:composition>

    ...

The slideshow poller sends the request for the next image (`showNextImage()` method) each four seconds. The interval is defined in the interval property of the `SlideshowManager.java` and refers to a `INITIAL.DELAY` constant (`constants.java`).

The described above example implements a modal panel with photos that rotate them in the order they are stored in an album.

Image size control

To stop the slide-show user clicks Close window button on the slide-show panel and `stopSlideshow()` method is invoked.

    ...

    @Observer("stopSlideshow")

          public void stopSlideshow(){

                active = false;

                this.selectedImage = null;

                this.slideshowIndex = 0;

          }

          ...

The `active` field is set to "false" again, consequently the poller becomes inactive and the modal panel becomes invisible too.

Vizit following pages at RichFaces Live Demo for more information, examples and sources on the components used in the application and described in this chapter:

- ModalPanel page for the <rich:modalPanel> component;

- Effect for the <rich:effect> component;

- MediaOutput for the <a4j:mediaOutput> component;

- AjaxSupport for the <a4j:suport> component;

- CommandLink for the <a4j:commandLink> component;

- AjaxForm for the <a4j:form> component;

- Poll for the <a4j:poll> component.