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

Java tutorial

Introduction

Here is the source code for de.fzi.fhemapi.view.vaadin.ui.HWindow.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.annotations.AutoGenerated;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.Transferable;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.event.dd.acceptcriteria.ClientSideCriterion;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Tree.TargetInSubtree;
import com.vaadin.ui.Tree.TreeDragMode;
import com.vaadin.ui.Tree.TreeTargetDetails;
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.server.FHEMServer;

/**
 * This is the main window of all the UI. It extends a CustomComponent.
 * @author Can Yumusak
 *
 */
public class HWindow extends CustomComponent {

    public static final String PROPERTY_NAME = "name";

    private HorizontalLayout mainLayout;
    private HorizontalSplitPanel mainSplitPanel;
    private Tree devicesTree;
    private HierarchicalContainer devicesContainer;

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

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

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

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

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

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

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

    /**
     * Default constructor
     * @param server The FHEM Server
     */
    public HWindow(FHEMServer server) {
        buildMainLayout();
        setCompositionRoot(mainLayout);

        this.server = server;
        fillTree();
        //      server.getDeviceManager().printAllDevices();
        mainSplitPanel.setSplitPosition(300, Sizeable.UNITS_PIXELS);
    }

    private void fillTree() {

        initDevicesContainer();
        devicesTree.setContainerDataSource(devicesContainer);
        for (Object id : devicesTree.rootItemIds()) {
            devicesTree.expandItemsRecursively(id);
        }

        devicesTree.setImmediate(true);
        devicesTree.addListener(new ItemClickEvent.ItemClickListener() {
            @Override
            public void itemClick(ItemClickEvent event) {
                if (event.getButton() == ItemClickEvent.BUTTON_LEFT) {
                    if (!server.getStructureManager().structureExists((String) event.getItemId())) {
                        fillRightSideForDevice((String) event.getItemId());
                    } else {
                        mainSplitPanel.setSecondComponent(new Tree());
                    }
                }
            }
        });
        devicesTree.setDragMode(TreeDragMode.NODE);
        devicesTree.setDropHandler(new DropHandler() {

            @Override
            public AcceptCriterion getAcceptCriterion() {
                //            AcceptCriterion crit = new ClientSideCriterion() {
                //               
                //               @Override
                //               public boolean accept(DragAndDropEvent dragEvent) {
                //                    Transferable t = dragEvent.getTransferable();
                //                  TreeTargetDetails target = (TreeTargetDetails) dragEvent.getTargetDetails();
                //                  Object sourceItemId = t.getData("itemId");
                //                    Object targetItemId = target.getItemIdOver();
                //                    if(devicesContainer.getParent(sourceItemId) == null)
                //                       return false;
                //                    return true;
                //               }
                //            };
                //            return crit;
                return AcceptAll.get();
            }

            @Override
            public void drop(DragAndDropEvent event) {
                Transferable t = event.getTransferable();
                if (t.getSourceComponent() != devicesTree)
                    return;
                TreeTargetDetails target = (TreeTargetDetails) event.getTargetDetails();
                Object sourceItemId = t.getData("itemId");
                Object targetItemId = target.getItemIdOver();
                VerticalDropLocation location = target.getDropLocation();
                HierarchicalContainer container = (HierarchicalContainer) devicesTree.getContainerDataSource();

                // Drop right on an item -> make it a child
                if (location == VerticalDropLocation.MIDDLE) {
                    if (container.getParent(targetItemId) == null) {
                        server.getStructureManager().addDeviceToStructure((String) targetItemId,
                                (String) sourceItemId);
                    } else {
                        server.getStructureManager().addDeviceToStructure(
                                (String) container.getParent(targetItemId), (String) sourceItemId);
                    }
                    server.getStructureManager().rereadFromFHEM();
                    reloadTree();
                }
            }
        });

    }

    private void fillRightSideForDevice(String deviceName) {
        mainSplitPanel
                .setSecondComponent(new DeviceDetailsPanel(server.getDeviceManager().getDevice(deviceName), this));
    }

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

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

        // horizontalSplitPanel_2
        mainSplitPanel = buildHorizontalSplitPanel_2();
        mainLayout.addComponent(mainSplitPanel);

        return mainLayout;
    }

    @AutoGenerated
    private HorizontalSplitPanel buildHorizontalSplitPanel_2() {
        // common part: create layout
        mainSplitPanel = new HorizontalSplitPanel();
        mainSplitPanel.setImmediate(false);
        mainSplitPanel.setWidth("100.0%");
        mainSplitPanel.setHeight("100.0%");
        mainSplitPanel.setMargin(false);

        VerticalLayout leftPanelLayout = new VerticalLayout();
        leftPanelLayout.setWidth("100%");
        leftPanelLayout.setHeight("-1");
        leftPanelLayout.setImmediate(false);
        mainSplitPanel.addComponent(leftPanelLayout);

        leftPanelLayout.addComponent(buildMenuBar());

        // devicesTree
        devicesTree = new Tree();
        devicesTree.setImmediate(false);
        devicesTree.setWidth("-1px");
        devicesTree.setHeight("-1px");
        leftPanelLayout.addComponent(devicesTree);

        // verticalLayout_1
        mainSplitPanel.addComponent(new HorizontalLayout());

        return mainSplitPanel;
    }

    private MenuBar buildMenuBar() {
        MenuBar menubar = new MenuBar();
        menubar.setWidth("100%");
        MenuBar.MenuItem file = menubar.addItem("Neu..", null);
        file.addItem("Neue Struktur", menuCommand);
        file.addItem("Neues Gert", menuCommand);

        MenuBar.MenuItem data = menubar.addItem("Dateien", null);
        data.addItem("ffne Config", menuCommand);
        data.addItem("Autocreate an", menuCommand);
        data.addItem("Autocreate aus", menuCommand);

        MenuBar.MenuItem save = menubar.addItem("Speichern", null);

        return menubar;
    }

    /**
     * Reloads the contents of the tree taking the current local state of the API. All local updates should be
     * called before this.
     */
    public void reloadTree() {
        devicesTree.removeAllItems();

        fillTree();
    }

    private Command menuCommand = new Command() {
        @Override
        public void menuSelected(com.vaadin.ui.MenuBar.MenuItem selectedItem) {
            String label = selectedItem.getText();
            if (label.equals("Neuer Raum")) {
                getWindow().showNotification("Lege neuen Raum an!", Notification.TYPE_TRAY_NOTIFICATION);
            } else if (label.equals("ffne Config")) {
                openConfig();
            } else if (label.equals("Speichern")) {
                getWindow().showNotification(server.save().toString(), Notification.TYPE_TRAY_NOTIFICATION);
            } else if (label.equals("Neues Gert")) {
                openNewDevicePanel();
            } else if (label.equals("Neue Struktur")) {
                openNewStructurePanel();
            } else if (label.equals("Autocreate an")) {
                server.getConfigManager().setAutocreate(true);
            } else if (label.equals("Autocreate aus")) {
                server.getConfigManager().setAutocreate(false);
            }
        }
    };

    private void openConfig() {
        VerticalLayout layout = new VerticalLayout();

        final TextArea area = new TextArea(null, server.getConfigManager().getConfigFile());
        //      area.setHeight("100%");
        area.setRows(100);
        area.setWidth("100%");

        layout.addComponent(area);

        Button saveButton = new Button("Speichern");
        saveButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                server.setFHEMCfg(((String) area.getValue()));
                getWindow().showNotification(server.rereadConfiguration().toString(),
                        Notification.TYPE_TRAY_NOTIFICATION);
                server.getConfigManager().update();
            }
        });
        layout.addComponent(saveButton);
        layout.setComponentAlignment(saveButton, Alignment.TOP_CENTER);
        mainSplitPanel.setSecondComponent(layout);
    }

    private void openNewDevicePanel() {
        mainSplitPanel.setSecondComponent(new NewDevicePanel(this));
    }

    private void openNewStructurePanel() {
        mainSplitPanel.setSecondComponent(new NewRoomPanel(this));
    }

    private void initDevicesContainer() {
        List<Structure> structureList = server.getStructureManager().getStructures();
        List<String> devices = new LinkedList<String>();
        List<Device> deviceList = server.getDeviceManager().getDevices();
        for (Device dev : deviceList) {
            devices.add(dev.getName());
        }
        devicesContainer = new HierarchicalContainer();
        for (Structure struc : structureList) {
            devicesContainer.addItem(struc.name);
            for (String dev : struc.deviceNames) {
                devicesContainer.addItem(dev);
                devicesContainer.setParent(dev, struc.name);
                devicesContainer.setChildrenAllowed(dev, false);
                devices.remove(dev);
            }
        }

        if (devices.size() != 0)
            devicesContainer.addItem("without Room");
        for (String name : devices) {
            devicesContainer.addItem(name);
            devicesContainer.setParent(name, "without Room");
            devicesContainer.setChildrenAllowed(name, false);
        }
    }
}