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.ArrayList; import java.util.List; import com.vaadin.annotations.AutoGenerated; import com.vaadin.data.Container; import com.vaadin.data.Property; import com.vaadin.data.Container.ItemSetChangeEvent; import com.vaadin.data.Container.ItemSetChangeListener; import com.vaadin.data.Property.ValueChangeEvent; 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 com.vaadin.ui.Window.Notification; import de.fzi.fhemapi.model.actuatorparameters.ActuatorParameters; import de.fzi.fhemapi.model.server.MessageResponse; import de.fzi.fhemapi.server.constants.FHEMParameters; import de.fzi.fhemapi.server.core.DeviceFactory; import de.fzi.fhemapi.server.core.ManufacturerManager; import de.fzi.fhemapi.view.vaadin.ui.core.ParameterExtractor; /** * A Panel which is shown when creating a new device. * @author Can Yumusak * */ public class NewDevicePanel extends CustomComponent { HWindow parent; VerticalLayout mainLayout; TextField nameField; ComboBox parameterType; ComboBox typeComboBox; List<HorizontalLayout> currentManufacturerDetails; List<TextField> currentManufacturerTextFields; VerticalLayout manufacturerLayout; /** * default constructor * @param parent a HWindow as it's parent */ public NewDevicePanel(HWindow parent) { this.parent = parent; initGUI(); } private void initGUI() { mainLayout = buildMainLayout(); setCompositionRoot(mainLayout); } @AutoGenerated private VerticalLayout buildMainLayout() { VerticalLayout layout = new VerticalLayout(); HorizontalLayout titleLabel = getTitlePanel(); layout.addComponent(titleLabel); layout.setComponentAlignment(titleLabel, Alignment.TOP_CENTER); nameField = new TextField(); HorizontalLayout nameLayout = UIHelper.buildAttributePanel("Name", nameField); layout.addComponent(nameLayout); layout.setComponentAlignment(nameLayout, Alignment.TOP_CENTER); typeComboBox = new ComboBox(); for (String name : DeviceFactory.getAvailableDevicetypes()) { typeComboBox.addItem(name); } HorizontalLayout typelayout = UIHelper.buildAttributePanel("Typ", typeComboBox); layout.addComponent(typelayout); layout.setComponentAlignment(typelayout, Alignment.TOP_CENTER); parameterType = new ComboBox(); String[] manufacturers = ManufacturerManager.getClassNames(); for (String name : manufacturers) { parameterType.addItem(name); } parameterType.addListener(new Property.ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { fillManufacturerDetails((String) event.getProperty().getValue()); } }); parameterType.setImmediate(true); HorizontalLayout parameterTypeLayout = UIHelper.buildAttributePanel("Hersteller", parameterType); layout.addComponent(parameterTypeLayout); layout.setComponentAlignment(parameterTypeLayout, Alignment.TOP_CENTER); manufacturerLayout = new VerticalLayout(); layout.addComponent(manufacturerLayout); layout.setComponentAlignment(manufacturerLayout, Alignment.TOP_CENTER); Button saveButton = new Button("Speichern"); layout.addComponent(saveButton); layout.setComponentAlignment(saveButton, Alignment.TOP_CENTER); saveButton.addListener(new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { if (allParametersMatches()) { String[] parameters = new String[currentManufacturerTextFields.size() + 1]; parameters[0] = (String) nameField.getValue(); for (int i = 1; i < parameters.length; i++) { parameters[i] = (String) currentManufacturerTextFields.get(i - 1).getValue(); } ActuatorParameters params = ParameterExtractor .getJavaParameters((String) parameterType.getValue(), parameters); MessageResponse response = parent.server.getDeviceManager().createNewActuatorAsMessage(params); getWindow().showNotification(response.toString(), Notification.TYPE_TRAY_NOTIFICATION); parent.server.getDeviceManager().update(); parent.server.getDeviceManager().getDevice(params.get(FHEMParameters.NAME)) .setDeviceType((String) typeComboBox.getValue()); parent.reloadTree(); } } }); return layout; } private static HorizontalLayout getTitlePanel() { HorizontalLayout titleLayout = new HorizontalLayout(); titleLayout.setImmediate(false); titleLayout.setWidth("-1"); titleLayout.setHeight("-1"); titleLayout.setMargin(false); // deviceTitle Label title = new Label(); title.setImmediate(true); title.setWidth("100%"); title.setHeight("50"); title.setValue("<h1>Neues Gert..</h1>"); title.setContentMode(Label.CONTENT_XHTML); titleLayout.addComponent(title); titleLayout.setComponentAlignment(title, Alignment.TOP_CENTER); return titleLayout; } private boolean allParametersMatches() { return true; } private void fillManufacturerDetails(String className) { // if(currentManufacturerDetails != null) // for(HorizontalLayout layout : currentManufacturerDetails) // mainLayout.removeComponent(layout); manufacturerLayout.removeAllComponents(); String[] parameterNames = ParameterExtractor.getParameters(className); // currentManufacturerDetails = new ArrayList<HorizontalLayout>(); currentManufacturerTextFields = new ArrayList<TextField>(); for (String name : parameterNames) { if (!name.toLowerCase().equals("name")) { TextField paramField = new TextField(); HorizontalLayout paramLayout = UIHelper.buildAttributePanel(name, paramField); manufacturerLayout.addComponent(paramLayout); manufacturerLayout.setComponentAlignment(paramLayout, Alignment.TOP_CENTER); // currentManufacturerDetails.add(paramLayout); currentManufacturerTextFields.add(paramField); } } } }