kn.uni.gis.ui.GameComposite.java Source code

Java tutorial

Introduction

Here is the source code for kn.uni.gis.ui.GameComposite.java

Source

/**
 * Copyright (C) 2013 Marcel Hanser & Martin Koelbl <nanohome.de@googlemail.com>
 *
 * 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 kn.uni.gis.ui;

import java.text.DateFormat;
import java.util.Date;

import kn.uni.gis.ui.Game.Player;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.ClassResource;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Slider;
import com.vaadin.ui.Slider.ValueOutOfBoundsException;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

public class GameComposite extends CustomComponent {
    @AutoGenerated
    private VerticalLayout mainLayout;

    @AutoGenerated
    private VerticalLayout verticalLayout_2;

    @AutoGenerated
    private Slider speedSlider;

    @AutoGenerated
    private Slider timeSlider;

    @AutoGenerated
    private Label label_1;

    @AutoGenerated
    private CheckBox annimated;

    @AutoGenerated
    private Table gamerTable;

    private static final Logger LOGGER = LoggerFactory.getLogger(GameComposite.class);

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final Game game;
    private final Player fox;

    private final GameVectorLayer layer;

    private final EventBus eventBus;

    /**
     * The constructor should first build the main layout, set the composition
     * root and then do any custom initialization.
     * 
     * The constructor will not be automatically regenerated by the visual
     * editor.
     */
    public GameComposite(final GameApplication app, final Game game, GameVectorLayer layer,
            final EventBus eventBus) {
        this.layer = layer;
        this.eventBus = eventBus;
        buildMainLayout();
        setCompositionRoot(mainLayout);

        this.game = game;

        this.fox = game.getFox();
        // timeSlider

        gamerTable.addContainerProperty("Player name", String.class, null);
        gamerTable.addContainerProperty("Show", CheckBox.class, null);

        addToTable(fox);

        gamerTable.setItemIcon(fox, new ClassResource("fox-icon.ico", app));
        gamerTable.setRowHeaderMode(Table.ROW_HEADER_MODE_ICON_ONLY);
        for (Player player : game.getStates().keySet()) {
            if (fox != player) {
                addToTable(player);
            }
        }
        speedSlider.addListener(new ValueChangeListener() {
            private static final long serialVersionUID = 3700972384216226371L;

            @Override
            public void valueChange(ValueChangeEvent event) {
                GameComposite.this.layer.setTimeMultiplayer((Double) event.getProperty().getValue());
            }
        });

        timeSlider.addListener(new ValueChangeListener() {
            private static final long serialVersionUID = 3700972384216226371L;
            Double oldTimestamp = 0d;

            @Override
            public void valueChange(ValueChangeEvent event) {
                Double eventValue = (Double) event.getProperty().getValue();

                if (eventValue < oldTimestamp - 2 || Math.abs(eventValue - oldTimestamp) > 10) {

                    long curr = game.getLast() - game.getFirst();
                    eventBus.post(new TimeChangedEvent(game.getId(),
                            (long) (game.getFirst() + curr * (eventValue / 100))));
                }
                oldTimestamp = eventValue;
            }
        });

        annimated.addListener(new ClickListener() {
            private static final long serialVersionUID = 3700972384216226371L;

            @Override
            public void buttonClick(ClickEvent event) {
                if ((Boolean) event.getButton().getValue()) {
                    GameComposite.this.layer.setTimeMultiplayer((Double) speedSlider.getValue());
                } else {
                    LOGGER.info("stop time");
                    GameComposite.this.layer.setTimeMultiplayer(0);
                }
            }
        });

        gamerTable.setImmediate(true);

        gamerTable.addListener(new ItemClickListener() {

            private static final long serialVersionUID = 1L;

            @Override
            public void itemClick(ItemClickEvent event) {
                GameComposite.this.eventBus
                        .post(new PlayerSelectionEvent(game.getId(), (Player) event.getItemId()));
            }
        });

        annimated.setValue(Boolean.TRUE);
        annimated.setImmediate(true);
        speedSlider.setMax(40);
        speedSlider.setMin(1);
        speedSlider.setImmediate(true);
        speedSlider.setResolution(2);

        timeSlider.setMax(100);
        timeSlider.setMin(0);
        timeSlider.setImmediate(true);
    }

    private void addToTable(final Player fox2) {

        LOGGER.info("adding player: {}", fox2.getName());
        CheckBox comboBox = new CheckBox();
        comboBox.setValue(true);
        comboBox.setImmediate(true);
        comboBox.addListener(new ClickListener() {

            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                boolean dings = (Boolean) event.getButton().getValue();
                LOGGER.info("setting player: {} to {}", fox2.getName(), dings);
                layer.setPlayerVisible(fox2, dings);
            }
        });
        gamerTable.addItem(new Object[] { fox2.getName(), comboBox }, fox2);
    }

    @Subscribe
    public void handleTimeEvent(TimeEvent event) {
        if (game.getId().equals(event.getGameId())) {
            synchronized (getApplication()) {
                label_1.setValue(DateFormat.getDateTimeInstance().format(new Date(event.getTimestamp())));

                double current = (event.getTimestamp() - game.getFirst())
                        / (double) (game.getLast() - game.getFirst());
                try {
                    timeSlider.setValue(current * 100 > 100 ? 100 : current * 100);
                } catch (ValueOutOfBoundsException e) {
                }
            }
        }
    }

    public Game getGame() {
        return game;
    }

    public Player getFox() {
        return fox;
    }

    @Override
    public void detach() {
        super.detach();
    }

    GameVectorLayer getLayer() {
        return layer;
    }

    @AutoGenerated
    private VerticalLayout buildMainLayout() {
        // common part: create layout
        mainLayout = new VerticalLayout();
        mainLayout.setImmediate(false);
        mainLayout.setWidth("100%");
        mainLayout.setHeight("100%");
        mainLayout.setMargin(false);

        // top-level component properties
        setWidth("100.0%");
        setHeight("100.0%");

        // gamerTable
        gamerTable = new Table();
        gamerTable.setImmediate(false);
        gamerTable.setWidth("-1px");
        gamerTable.setHeight("200px");
        mainLayout.addComponent(gamerTable);

        // verticalLayout_2
        verticalLayout_2 = buildVerticalLayout_2();
        mainLayout.addComponent(verticalLayout_2);

        return mainLayout;
    }

    @AutoGenerated
    private VerticalLayout buildVerticalLayout_2() {
        // common part: create layout
        verticalLayout_2 = new VerticalLayout();
        verticalLayout_2.setImmediate(false);
        verticalLayout_2.setWidth("-1px");
        verticalLayout_2.setHeight("-1px");
        verticalLayout_2.setMargin(false);

        // annimated
        annimated = new CheckBox();
        annimated.setCaption("Animated");
        annimated.setImmediate(false);
        annimated.setWidth("-1px");
        annimated.setHeight("-1px");
        verticalLayout_2.addComponent(annimated);

        // label_1
        label_1 = new Label();
        label_1.setCaption("Time");
        label_1.setImmediate(false);
        label_1.setWidth("-1px");
        label_1.setHeight("-1px");
        label_1.setValue("Label");
        verticalLayout_2.addComponent(label_1);

        // timeSlider
        timeSlider = new Slider();
        timeSlider.setCaption("TimeBar");
        timeSlider.setImmediate(false);
        timeSlider.setWidth("200px");
        timeSlider.setHeight("-1px");
        verticalLayout_2.addComponent(timeSlider);

        // speedSlider
        speedSlider = new Slider();
        speedSlider.setCaption("Animation speed");
        speedSlider.setImmediate(false);
        speedSlider.setWidth("200px");
        speedSlider.setHeight("-1px");
        verticalLayout_2.addComponent(speedSlider);

        return verticalLayout_2;
    }

}