de.fzi.fhemapi.view.vaadin.ui.NewRoomPanel.java Source code

Java tutorial

Introduction

Here is the source code for de.fzi.fhemapi.view.vaadin.ui.NewRoomPanel.java

Source

/*******************************************************************************
 * Copyright 2007-2014 FZI, http://www.fzi.de
 *                 Forschungszentrum Informatik - Information Process Engineering (IPE)
 *  
 *                 See the NOTICE file distributed with this work for additional
 *                 information regarding copyright ownership
 *                
 *                 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.
 * @author tzentek - <a href="mailto:zentek@fzi.de">Tom Zentek</a>
 * @author cyumusak - <a href="mailto:canyumusak@gmail.com">Can Yumusak</a>
 ******************************************************************************/
package de.fzi.fhemapi.view.vaadin.ui;

import java.util.LinkedList;
import java.util.List;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;

import de.fzi.fhemapi.model.Structure;
import de.fzi.fhemapi.model.devicetypes.Device;
import de.fzi.fhemapi.model.server.MessageResponse;

/**
 * The panel which is shown when a new Room is created.
 * @author Can Yumusak
 *
 */
public class NewRoomPanel extends CustomComponent {

    HWindow parent;
    VerticalLayout mainLayout;

    TextField nameTextField;
    ComboBox typeComboBox;
    CheckBox[] devicesCheckBox;

    /**
     * The existing types of structure.
     */
    public static final String[] roomTypes = { "room" };

    /**
     * default constructor
     * @param parent A HWindow instantiating this class.
     */
    public NewRoomPanel(HWindow parent) {
        this.parent = parent;
        initGUI();
    }

    private void initGUI() {
        mainLayout = buildMainLayout();
        setCompositionRoot(mainLayout);
    }

    private VerticalLayout buildMainLayout() {
        VerticalLayout layout = new VerticalLayout();

        nameTextField = new TextField();
        HorizontalLayout nameLay = UIHelper.buildAttributePanel("Name", nameTextField);
        layout.addComponent(nameLay);

        typeComboBox = new ComboBox();
        for (String type : roomTypes) {
            typeComboBox.addItem(type);
        }

        HorizontalLayout typeLay = UIHelper.buildAttributePanel("Typ", typeComboBox);
        layout.addComponent(typeLay);
        layout.setComponentAlignment(typeLay, Alignment.TOP_CENTER);

        Label devicesLabel = new Label();
        devicesLabel.setImmediate(false);
        devicesLabel.setWidth("100%");
        devicesLabel.setHeight("50");
        devicesLabel.setValue("<b>Gerte</b>");
        devicesLabel.setContentMode(Label.CONTENT_XHTML);
        layout.addComponent(devicesLabel);
        layout.setComponentAlignment(devicesLabel, Alignment.TOP_CENTER);

        List<Device> deviceList = parent.server.getDeviceManager().getDevices();
        devicesCheckBox = new CheckBox[deviceList.size()];
        for (int i = 0; i < deviceList.size(); i++) {
            devicesCheckBox[i] = new CheckBox(deviceList.get(i).getName());
            layout.addComponent(devicesCheckBox[i]);
            layout.setComponentAlignment(devicesCheckBox[i], Alignment.MIDDLE_CENTER);
        }

        Button saveButton = new Button("Speichern");
        saveButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                List<String> names = new LinkedList<String>();
                for (CheckBox checkbox : devicesCheckBox) {
                    if (checkbox.booleanValue())
                        names.add(checkbox.getCaption());
                }

                Structure struc = new Structure((String) nameTextField.getValue(), (String) typeComboBox.getValue(),
                        names);
                MessageResponse response = parent.server.getStructureManager().createNewStructure(struc);
                getWindow().showNotification(response.toString(), Notification.TYPE_TRAY_NOTIFICATION);
                parent.server.getStructureManager().rereadFromFHEM();
                parent.reloadTree();
            }
        });
        layout.addComponent(saveButton);
        layout.setComponentAlignment(saveButton, Alignment.MIDDLE_CENTER);

        return layout;
    }

    //   private static HorizontalLayout getTitlePanel(){
    //      HorizontalLayout titleLayout = new HorizontalLayout();
    //      titleLayout.setImmediate(false);
    //      titleLayout.setWidth("100%");
    //      titleLayout.setHeight("50");
    //      titleLayout.setMargin(false);
    //
    //      // deviceTitle
    //      Label title = new Label();
    //      title.setImmediate(true);
    //      title.setWidth("100%");
    //      title.setHeight("50");
    //      title.setValue("<h1>Neue Struktur..</h1>");
    //      title.setContentMode(Label.CONTENT_XHTML);
    //      
    //      titleLayout.addComponent(title);
    //      titleLayout.setComponentAlignment(title, Alignment.TOP_CENTER);
    //      return titleLayout;
    //   }
}