org.raccoon.hit.client.ui.player.skin.AviaVolumeControl.java Source code

Java tutorial

Introduction

Here is the source code for org.raccoon.hit.client.ui.player.skin.AviaVolumeControl.java

Source

/*
 * Copyright 2011 Fadeev Yegor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.raccoon.hit.client.ui.player.skin;

import com.bramosystems.oss.player.core.event.client.HasVolumeChangeHandlers;
import com.bramosystems.oss.player.core.event.client.VolumeChangeEvent;
import com.bramosystems.oss.player.core.event.client.VolumeChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Widget;

/**
 * Widget to control the volume of a player.  The control is placed on a player
 * as an icon which when clicked upon, opens a popup panel containing the
 * slider widget for the volume.
 *
 * <p>{@code VolumeChangeHandler}s are notified whenever the slider is adjusted.
 *
 * <h4>CSS Styles</h4>
 * <code><pre>
 * .player-VolumeControl { the slider widget }
 * .player-VolumeControl .volume { the volume level indicator }
 * .player-VolumeControl .track  { the sliders' track indicator }
 * </pre></code>
 *
 * @see VolumeChangeHandler
 * @author Sikirulai Braheem
 */
public class AviaVolumeControl extends Widget implements HasVolumeChangeHandlers, HasValue<Integer> {

    public final static int VOLUME_COUNT = 5;

    private Element volumeElement;
    private Element[] liElements;
    private Element[] aElements;
    private Integer value;

    private void initVolumeControl(int sliderHeight) {

        setVolume(0.0 / VOLUME_COUNT);
    }

    /**
     * Constructs <code>VolumeControl</code>.  This constructor is provided for complete CSS styling
     * support.  An horizontal slider of height {@code sliderHeight} is
     * displayed when this control is clicked on.
     *
     * <p>The slider popup panel has a fixed width of 50px.
     *
     * @param sliderHeight the height of the volume slider control.
     * @since 1.2
     */
    public @UiConstructor AviaVolumeControl(int sliderHeight) {

        liElements = new Element[VOLUME_COUNT];
        aElements = new Element[VOLUME_COUNT];

        final Element divElement = DOM.createDiv();
        divElement.setClassName("gwt-AviaVolume");

        final Element ulElement = DOM.createElement("ul");
        divElement.appendChild(ulElement);

        final Element spanTrick = DOM.createSpan();
        spanTrick.getStyle().setProperty("display", "block");
        spanTrick.getStyle().setProperty("clear", "both");
        spanTrick.setInnerHTML("<!-- -->");
        divElement.appendChild(spanTrick);

        value = 0;

        // Set volume element
        Element li = DOM.createElement("li");
        Element a = DOM.createAnchor();
        a.setAttribute("href", "javascript:;");
        li.appendChild(a);
        volumeElement = li;
        volumeElement.getStyle().setProperty("background", "url(\"img/music/volume.png\")");
        DOM.sinkEvents(a, Event.ONCLICK);
        ulElement.appendChild(li);

        setElement(divElement);

        // Set volume level
        for (int i = 0; i < VOLUME_COUNT; i++) {
            li = DOM.createElement("li");
            a = DOM.createAnchor();
            a.setAttribute("href", "javascript:;");
            li.appendChild(a);
            liElements[i] = li;
            aElements[i] = a;
            DOM.sinkEvents(a, Event.ONCLICK);
            ulElement.appendChild(li);
        }

        render();
        initVolumeControl(sliderHeight);
    }

    /**
     * Sets the level of the volume slider control.
     *
     * <p><b>Note:</b> {@code VolumeChangeListener}s are not notified by this
     * method.
     *
     * @param volume value between {@code 0} (silent) and {@code 1} (the maximum).
     * Any value outside the range will be ignored.
     */
    public final void setVolume(double volume) {
        if ((volume >= 0) && (volume <= 1.0)) {
            setValue((int) (volume * VOLUME_COUNT));
        }
    }

    /**
     * Adds the specified handler to the player.  The handler is called whenever the state
     * of the volume slider changes.
     *
     * @param handler the handler
     * @return the HandlerRegistration used to remove the handler
     * @see VolumeChangeHandler
     */
    @Override
    public HandlerRegistration addVolumeChangeHandler(VolumeChangeHandler handler) {
        return addHandler(handler, VolumeChangeEvent.TYPE);
    }

    @Override
    public void onBrowserEvent(Event event) {

        final com.google.gwt.dom.client.Element target = Element.as(event.getEventTarget());

        for (int i = 0; i < VOLUME_COUNT; i++) {
            if (aElements[i] == target) {
                onStarClicked(i);
                break;
            }
        }

    }

    protected void onStarClicked(int i) {

        setValue(i, true);
    }

    private void render() {

        for (int i = 0; i < VOLUME_COUNT; i++) {
            if (i < value) {
                liElements[i].getStyle().setProperty("background", "url(\"img/music/volumeFill.png\")");
            } else {
                liElements[i].getStyle().setProperty("background", "url(\"img/music/volumeEmpty.png\")");
            }
        }

    }

    @Override
    public Integer getValue() {
        return value;
    }

    @Override
    public void setValue(Integer value) {
        setValue(value, true);
    }

    @Override
    public void setValue(Integer value, boolean fireEvents) {
        if (this.value.compareTo(value) != 0) {
            final Integer oldValue = this.value;
            this.value = value;
            render();
            ValueChangeEvent.<Integer>fireIfNotEqual(this, oldValue, value);
        }
    }

    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Integer> handler) {
        return addHandler(handler, ValueChangeEvent.getType());
    }
}