### Image Size Control with <r:inputNumberSlider>

The r:inputNumberSlider component in the Photo Album Demo is used as a control that helps a user to change photos size while previewing an album. A handler position on the slider track corresponds to a particular value of an image size. The component is included into the page with the help of ui:include:

                <ui:include src="/includes/image/inputNumberSlider.xhtml">
                    <ui:param name="size" value="#{'#{'}fn:length(model.images)}" />
                    <ui:param name="renderArea" value="userAlbumImages" />
                </ui:include>

Now let's have a look at `src/main/webapp/includes/image/inputNumberSlider.xhtml` file:

            <ui:composition … >
                <div>
                    …
                    <r:inputNumberSlider enableManualInput="false"
                        value="#{'#{'}imageSizeHelper.value}"
                        minValue="80"
                        maxValue="200"
                        showArrows="false"
                        showBoundaryValues="true"
                        showInput="false"
                        step="40"
                        width="100">
                        <r:ajax event="change" render="#{'#{'}renderArea}" />
                    </r:inputNumberSlider>
                    …
                </div>
            </ui:composition>

There is special Enumeration type that contains four predefined values for image size. Each type has a set of image related attributes such as CSS class for new photo size, postfix for a new file name, image background. The `setValue` method of the `ImageSizeHelper.java` class is triggered on each slider's position change. This method sets one of four predefined values for image size and due to slider's position.

            public void setValue(int value) {
                currentDimension = ImageDimension.getInstance(value);
                this.value = currentDimension.getX();
            }

And here is the `ImageDimension.java` class:

            public enum ImageDimension {

                SIZE_80(80), SIZE_120(120), SIZE_160(160), SIZE_200(200), SIZE_MEDIUM(600), ORIGINAL(0);

                final static String CSS_CLASS = "preview_box_photo_";
                final static String FILE_POSTFIX = "_small";
                final static String IMAGE_BG = "/img/shell/frame_photo_%1$d.png";
                final static String IMAGE_BG_STYLE = "width: %1$dpx; height: %1$dpx";

                int x;
                String bgStyle;
                String cssClass;
                String imageBgSrc;
                String filePostfix;

                private ImageDimension(int x) {
                    this.x = x;
                    this.bgStyle = String.format(IMAGE_BG_STYLE, x + 20);
                    cssClass = CSS_CLASS + x;
                    imageBgSrc = String.format(IMAGE_BG, (x == 160) ? 200 : x);
                    if (x == 600) {
                        filePostfix = "_medium";
                    } else if (x == 0) {
                        filePostfix = "";
                    } else {
                        filePostfix = FILE_POSTFIX + x;
                    }
                }
                …
            }

After the <r:ajax> is worked out user photos (more exactly, the h:panelGroup with `userAlbumImages` id that contains user photos) are rendered correspondingly to a new set value. Here is `web/src/main/webapp/includes/image/imageList.xhtml` :

            …
            <h:panelGroup id="userAlbumImages">
                <r:repeat id="imageList" value="#{'#{'}model.images}" var="image" rows="20">
                    <h:panelGroup layout="block" styleClass="#{'#{'}imageSizeHelper.currentDimension.cssClass}">
                        <h:graphicImage styleClass="pr_photo_bg" style="#{'#{'}imageSizeHelper.currentDimension.imageBgStyle}"
                            value="#{'#{'}imageSizeHelper.currentDimension.imageBg}" />

                        <h:panelGrid cellpadding="0">
                            <h:panelGroup>
                                <r:commandLink actionListener="#{'#{'}controller.showImage(image)}" render="mainArea, treePanel">
                                    <r:mediaOutput id="img" element="img" createContent="#{'#{'}imageLoader.paintImage}"
                                        style="border : 1px solid #FFFFFF;" mimeType="image/jpeg"
                                        value="#{'#{'}fileManager.transformPath(image.fullPath, imageSizeHelper.currentDimension.filePostfix)}">
                                        <f:param value="#{'#{'}imageSizeHelper.currentDimension.x}" name="x" />
                                        <r:dragSource rendered="#{'#{'}controller.isUserImage(image)}" render="mainArea, treePanel"
                                            id="dragSource" dragIndicator="iDragIndicator" type="image" dragValue="#{'#{'}image}" />
                                    </r:mediaOutput>
                                </r:commandLink>
                                <br />
                            </h:panelGroup>
                        </h:panelGrid>

                        <h:panelGroup layout="block" styleClass="photo_name">#{'#{'}image.name}</h:panelGroup>

                        <h:panelGroup layout="block" styleClass="photo_data">
                            <h:outputText value="#{'#{'}image.created}">
                                <f:convertDateTime />
                            </h:outputText>
                        </h:panelGroup>
                    </h:panelGroup>

                    <ui:include src="/includes/contextMenu/CMForImage.xhtml">
                        <ui:param name="image" value="#{'#{'}image}" />
                        <ui:param name="target" value="img" />
                    </ui:include>
                </r:repeat>
            </h:panelGroup>
            …

When the <r:inputNumberSlider< is rendered, at first its default value for image size is 120 px.

Image size control

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

- InputNumberSlider page for the <r:inputNumberSlider> component;

- Ajax for the <r:ajax> component.