com.cerebro.cable.xforce.view.GestioneRazze.java Source code

Java tutorial

Introduction

Here is the source code for com.cerebro.cable.xforce.view.GestioneRazze.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cerebro.cable.xforce.view;

import static com.cerebro.cable.xforce.MyUI.connPool;
import static com.cerebro.cable.xforce.MyUI.logger;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.sqlcontainer.SQLContainer;
import com.vaadin.data.util.sqlcontainer.query.TableQuery;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.server.FileResource;
import com.vaadin.server.Resource;
import com.vaadin.server.VaadinService;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.renderers.ImageRenderer;
import java.io.File;
import java.util.Locale;

/**
 *
 * @author matteo
 */
public class GestioneRazze extends HorizontalLayout implements View {

    String[][] racesTabCols = { { "name", "Nome" }, { "image", "Immagine" }, { "description", "Descrizione" },
            { "min_age", "Et minima" }, { "max_age", "Et massima" }, { "strenght", "Forza" },
            { "resistance", "Resistenza" }, { "agility", "Agilit" }, { "intelligence", "Intelligenza" },
            { "wisdom", "Saggezza" } };

    public GestioneRazze() {

        setMargin(true);

        TableQuery racesTQ = new TableQuery("races", connPool);
        SQLContainer racesContainer = null;
        try {
            racesContainer = new SQLContainer(racesTQ);
        } catch (Exception ex) {
            logger.error("Errore nella tabella delle razze: " + ex.getMessage());
        }
        Grid racesTable = new Grid(racesContainer);
        racesTable.removeAllColumns();
        for (int i = 0; i < racesTabCols.length; i++) {
            racesTable.addColumn(racesTabCols[i][0]);
            Grid.Column col = racesTable.getColumn(racesTabCols[i][0]);
            col.setHeaderCaption(racesTabCols[i][1]);
        }
        racesTable.getColumn("image").setRenderer(new ImageRenderer(), new Converter<Resource, String>() {
            @Override
            public String convertToModel(Resource value, Class<? extends String> targetType, Locale locale)
                    throws Converter.ConversionException {
                return "not needed";
            }

            @Override
            public Resource convertToPresentation(String value, Class<? extends Resource> targetType, Locale locale)
                    throws Converter.ConversionException {
                return new FileResource(
                        new File(VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + value));
            }

            @Override
            public Class<String> getModelType() {
                return String.class;
            }

            @Override
            public Class<Resource> getPresentationType() {
                return Resource.class;
            }

        });

        FormLayout raceEditor = new FormLayout();

        addComponents(racesTable);
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        logger.info("View Gestione delle Razze");
    }

}