de.fatalix.lighty.web.ui.configuration.ConfigurationViewImpl.java Source code

Java tutorial

Introduction

Here is the source code for de.fatalix.lighty.web.ui.configuration.ConfigurationViewImpl.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 de.fatalix.lighty.web.ui.configuration;

import com.vaadin.cdi.CDIView;
import com.vaadin.data.Property;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.shared.ui.MarginInfo;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.VerticalLayout;
import de.fatalix.lighty.businesslogic.model.LightyConfiguration;
import de.fatalix.lighty.businesslogic.model.SettingKey;
import de.fatalix.lighty.web.component.LightyNotificationBar;
import de.fatalix.lighty.web.component.field.InlineEditField;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject;

/**
 *
 * @author Fatalix
 */
@CDIView("config")
public class ConfigurationViewImpl extends CustomComponent implements ConfigurationView, View {

    private Map<String, InlineEditField> configurationFields = new HashMap<>();
    @Inject
    private javax.enterprise.event.Event<ConfigurationPresenter.LoadConfiguration> loadEvent;
    @Inject
    private javax.enterprise.event.Event<ConfigurationPresenter.SaveConfiguration> saveEvent;
    @Inject
    private javax.enterprise.event.Event<ConfigurationPresenter.TestConfiguration> testConnectEvent;
    @Inject
    private javax.enterprise.event.Event<String> notificationEvent;

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        VerticalLayout mainLayout = new VerticalLayout();
        mainLayout.setSizeFull();
        mainLayout.setMargin(new MarginInfo(true, true, true, true));
        final InlineEditField lightBridgeURL = new InlineEditField("LightBridge URL", "", true);
        lightBridgeURL.setId(SettingKey.SVNURL.getKey());
        lightBridgeURL.addValueChangeListener(new Property.ValueChangeListener() {

            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                saveEvent.select(new AnnotationLiteral<ConfigurationPresenter.Save>() {
                }).fire(new ConfigurationPresenter.SaveConfiguration() {

                    @Override
                    public String getValue() {
                        return lightBridgeURL.getValue();
                    }

                    @Override
                    public String getKey() {
                        return SettingKey.SVNURL.getKey();
                    }

                    @Override
                    public void showStatus(String message) {
                        notificationEvent.select(new AnnotationLiteral<LightyNotificationBar.ShowNotification>() {
                        }).fire(message);
                    }
                });
            }
        });

        final InlineEditField lightBridgeUser = new InlineEditField("User", "", true);
        lightBridgeUser.setId(SettingKey.SVNUSER.getKey());
        lightBridgeUser.addValueChangeListener(new Property.ValueChangeListener() {

            @Override
            public void valueChange(Property.ValueChangeEvent event) {

                saveEvent.select(new AnnotationLiteral<ConfigurationPresenter.Save>() {
                }).fire(new ConfigurationPresenter.SaveConfiguration() {

                    @Override
                    public String getValue() {
                        return lightBridgeUser.getValue();
                    }

                    @Override
                    public String getKey() {
                        return SettingKey.SVNUSER.getKey();
                    }

                    @Override
                    public void showStatus(String message) {
                        notificationEvent.select(new AnnotationLiteral<LightyNotificationBar.ShowNotification>() {
                        }).fire(message);
                    }
                });
            }
        });

        Button checkConfiguration = new Button("check", new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                testConnectEvent.select(new AnnotationLiteral<ConfigurationPresenter.TestConfig>() {
                }).fire(new ConfigurationPresenter.TestConfiguration() {

                    @Override
                    public void showStatus(String message) {
                        notificationEvent.select(new AnnotationLiteral<LightyNotificationBar.ShowNotification>() {
                        }).fire(message);
                    }
                });
            }
        });

        configurationFields.put(SettingKey.SVNURL.getKey(), lightBridgeURL);
        configurationFields.put(SettingKey.SVNUSER.getKey(), lightBridgeUser);

        mainLayout.addComponents(lightBridgeURL, lightBridgeUser, checkConfiguration);
        setCompositionRoot(mainLayout);
        loadEvent.select(new AnnotationLiteral<ConfigurationPresenter.Load>() {
        }).fire(new ConfigurationPresenter.LoadConfiguration() {

            @Override
            public void loadConfiguration(List<LightyConfiguration> configuration) {
                for (LightyConfiguration configurationEntry : configuration) {
                    configurationFields.get(configurationEntry.getConfigurationKey())
                            .setFieldValue(configurationEntry.getConfigurationValue());
                }
            }
        });
    }

}