Java tutorial
/******************************************************************************* * 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.List; import com.vaadin.annotations.AutoGenerated; import com.vaadin.data.Property; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.Alignment; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; 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 de.fzi.fhemapi.model.devicetypes.Device; import de.fzi.fhemapi.model.devicetypes.OnOffActuator; import de.fzi.fhemapi.server.core.DeviceFactory; import de.fzi.fhemapi.view.vaadin.ui.core.DevicePanelManager; import de.fzi.fhemapi.view.vaadin.ui.model.DeviceDetailEntry; /** * This panel shows all the details of a given device. * @author Can Yumusak * */ public class DeviceDetailsPanel extends CustomComponent { private Device device; private VerticalLayout mainLayout; private HorizontalLayout deviceTypeHoriLayout; private HorizontalLayout ioDevHoriLayout; private TextField deviceTitle; private HWindow parent; /** * default constructor * @param dev the device to show * @param parent an HWindow as it's parent */ public DeviceDetailsPanel(Device dev, HWindow parent) { this.device = dev; this.parent = parent; initGUI(); } private void initGUI() { mainLayout = buildMainLayout(); setCompositionRoot(mainLayout); } @AutoGenerated private VerticalLayout buildMainLayout() { // common part: create layout VerticalLayout layout = new VerticalLayout(); layout.setImmediate(false); layout.setWidth("100.0%"); layout.setHeight("-1"); layout.setMargin(false); HorizontalLayout titleLayout = new HorizontalLayout(); titleLayout.setImmediate(false); titleLayout.setWidth("-1"); titleLayout.setHeight("50"); titleLayout.setMargin(false); layout.addComponent(titleLayout); layout.setComponentAlignment(titleLayout, Alignment.TOP_CENTER); // deviceTitle deviceTitle = new TextField(); deviceTitle.setImmediate(true); deviceTitle.setWidth("100%"); deviceTitle.setHeight("-1px"); deviceTitle.setValue(device.getName()); deviceTitle.addListener(new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { if (((String) event.getProperty().getValue()).length() > 1) { device.rename((String) event.getProperty().getValue()); parent.reloadTree(); } } }); titleLayout.addComponent(deviceTitle); titleLayout.setComponentAlignment(deviceTitle, Alignment.TOP_CENTER); ComboBox combobox = new ComboBox(); String[] classes = DeviceFactory.getAvailableDevicetypes(); for (String name : classes) { combobox.addItem(name); } combobox.select(device.getClass().getSimpleName()); combobox.addListener(new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { setDeviceType((String) event.getProperty().getValue()); } }); combobox.setImmediate(true); deviceTypeHoriLayout = UIHelper.buildAttributePanel("Device Type", combobox); layout.addComponent(deviceTypeHoriLayout); layout.setComponentAlignment(deviceTypeHoriLayout, Alignment.TOP_CENTER); layout.setExpandRatio(deviceTypeHoriLayout, 1); ioDevHoriLayout = buildAttributePanel("IO Device", device.getType()); layout.addComponent(ioDevHoriLayout); layout.setComponentAlignment(ioDevHoriLayout, Alignment.TOP_CENTER); layout.setExpandRatio(ioDevHoriLayout, 1); addDeviceDetails(DevicePanelManager.getDeviceDetails(device, this), layout); return layout; } private void setDeviceType(String type) { device.setDeviceType(type); device = device.getServer().getDeviceManager().reloadDevice(device.getName()); initGUI(); } private HorizontalLayout buildAttributePanel(String attributeName, String value) { Label label = UIHelper.buildRichTextField("<b>" + value + "</b>"); return UIHelper.buildAttributePanel(attributeName, label); } /** * Reloads this page. */ public void reloadPage() { removeAllComponents(); initGUI(); } private void addDeviceDetails(List<DeviceDetailEntry> entries, VerticalLayout mainLayout) { for (DeviceDetailEntry entry : entries) { HorizontalLayout layout = UIHelper.buildAttributePanel(entry.caption, entry.component); mainLayout.addComponent(layout); mainLayout.setComponentAlignment(layout, Alignment.TOP_CENTER); mainLayout.setExpandRatio(layout, 1); } } }