com.github.peholmst.i18n4vaadin.simple.demo.DemoView.java Source code

Java tutorial

Introduction

Here is the source code for com.github.peholmst.i18n4vaadin.simple.demo.DemoView.java

Source

/*
 * Copyright (c) 2013 Petter Holmstrm
 *
 * 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 com.github.peholmst.i18n4vaadin.simple.demo;

import com.github.peholmst.i18n4vaadin.LocaleChangedEvent;
import com.github.peholmst.i18n4vaadin.LocaleChangedListener;
import com.github.peholmst.i18n4vaadin.annotations.Message;
import com.github.peholmst.i18n4vaadin.util.I18NHolder;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

/**
 * Example view
 *
 * @author Petter Holmstrm
 */
public class DemoView extends VerticalLayout implements View, LocaleChangedListener {

    @Message(key = "name.caption", value = "What is your name?")
    private TextField name;
    @Message(key = "sayHello.caption", value = "Say Hello!")
    private Button sayHello;
    private DemoViewBundle bundle = new DemoViewBundle();

    public DemoView() {
        setSizeUndefined();
        setSpacing(true);
        setMargin(true);
        name = new TextField();
        addComponent(name);

        sayHello = new Button();
        sayHello.addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                sayHello();
            }
        });
        addComponent(sayHello);

        updateStrings();
    }

    @Override
    public void attach() {
        super.attach();
        I18NHolder.get().addLocaleChangedListener(this);
    }

    @Override
    public void detach() {
        I18NHolder.get().removeLocaleChangedListener(this);
        super.detach();
    }

    @Message(key = "greeting", value = "Hello {0}! How is it going?")
    private void sayHello() {
        Notification.show(bundle.greeting(name.getValue()));
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
    }

    private void updateStrings() {
        name.setCaption(bundle.name_caption());
        sayHello.setCaption(bundle.sayHello_caption());
    }

    @Override
    public void localeChanged(LocaleChangedEvent event) {
        updateStrings();
    }
}