Java tutorial
/* * Copyright 2014 OpenNMS Group Inc., Entimoss ltd. * 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. */ package org.opennms.features.pluginmgr.vaadin.pluginmanager; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.opennms.karaf.licencemgr.metadata.jaxb.LicenceEntry; import org.opennms.karaf.licencemgr.metadata.jaxb.LicenceList; import com.vaadin.annotations.AutoGenerated; import com.vaadin.data.Property; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.ListSelect; import com.vaadin.ui.VerticalLayout; public class LicenceDescriptorTablePanel extends CustomComponent { /*- VaadinEditorProperties={"grid":"RegularGrid,5","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ @AutoGenerated private HorizontalLayout mainLayout; @AutoGenerated private VerticalLayout verticalLayout_1; @AutoGenerated private VerticalLayout licencePanels; @AutoGenerated private VerticalLayout verticalLayout_2; @AutoGenerated private VerticalLayout controlsVerticalLayout; @AutoGenerated private ListSelect licenceListSelect; private static final long serialVersionUID = 1L; private static final int LICENCE_LIST_SELECT_ROWS = 10; // default number of rows shown in licence list private Map<String, LicenceDescriptorPanel> panelIds = new HashMap<String, LicenceDescriptorPanel>(); private String selectedLicenceId = null; private Object selectedLicenceIdLock = new Object(); /** * The constructor should first build the main layout, set the * composition root and then do any custom initialization. * * The constructor will not be automatically regenerated by the * visual editor. */ public LicenceDescriptorTablePanel() { buildMainLayout(); setCompositionRoot(mainLayout); // manually add user code here licenceListSelect.setRows(LICENCE_LIST_SELECT_ROWS); // Show n items and a scrollbar if there are more licenceListSelect.setNullSelectionAllowed(false); // Feedback on value changes licenceListSelect.addValueChangeListener(new Property.ValueChangeListener() { private static final long serialVersionUID = 1L; @Override public void valueChange(ValueChangeEvent event) { // set all panels invisible for (LicenceDescriptorPanel pdp : panelIds.values()) { pdp.setVisible(false); } //set selected panel visible if (licenceListSelect.getValue() != null) { String selectedLicnceId = licenceListSelect.getValue().toString(); LicenceDescriptorPanel selectedLicenceDescriptorPanel = panelIds.get(selectedLicnceId); if (selectedLicenceDescriptorPanel != null) { selectedLicenceDescriptorPanel.setVisible(true); synchronized (selectedLicenceIdLock) { selectedLicenceId = selectedLicnceId; } } } mainLayout.markAsDirty(); } }); } public synchronized void addLicenceList(LicenceList licencelist) { // default if empty entry List<LicenceEntry> llist = new ArrayList<LicenceEntry>(); if (licencelist != null) llist = licencelist.getLicenceList(); Map<String, LicenceEntry> lmap = new TreeMap<String, LicenceEntry>(); for (LicenceEntry lentry : llist) { lmap.put(lentry.getProductId(), lentry); } licencePanels.removeAllComponents(); for (String productId : lmap.keySet()) { // update/add product ids to list select without throwing value change event if (!licenceListSelect.containsId(productId)) licenceListSelect.addItem(productId); // add a new licence descriptor panel and populate with LicenceMetadata( LicenceDescriptorPanel licenceDescriptorPanel = new LicenceDescriptorPanel(); licenceDescriptorPanel.setImmediate(true); licenceDescriptorPanel.setLicenceString(lmap.get(productId).getLicenceStr()); licenceDescriptorPanel.setVisible(false); licenceDescriptorPanel.setReadOnly(true); licenceDescriptorPanel.setNoUpdate(true); panelIds.put(productId, licenceDescriptorPanel); licencePanels.addComponent(licenceDescriptorPanel); } // update/remove product ids from list select without throwing value change event List<Object> itemIds = new ArrayList<Object>(licenceListSelect.getItemIds()); for (Object itemid : itemIds) { if (!lmap.keySet().contains(itemid)) licenceListSelect.removeItem(itemid); } // selects first value for display if (lmap.keySet().isEmpty()) { //if there are no panels to display display an empty panel LicenceDescriptorPanel licenceDescriptorPanel = new LicenceDescriptorPanel(); licenceDescriptorPanel.setReadOnly(true); licenceDescriptorPanel.setNoUpdate(true); licenceDescriptorPanel.setVisible(true); licencePanels.addComponent(licenceDescriptorPanel); } else { String selectedLicnceId = lmap.keySet().iterator().next(); LicenceDescriptorPanel selectedLicenceDescriptorPanel = panelIds.get(selectedLicnceId); if (selectedLicenceDescriptorPanel != null) { selectedLicenceDescriptorPanel.setVisible(true); synchronized (selectedLicenceIdLock) { selectedLicenceId = selectedLicnceId; } } } } public String getSelectedLicenceId() { String s = null; synchronized (selectedLicenceIdLock) { s = selectedLicenceId; } return s; } public VerticalLayout getControlsVerticalLayout() { return controlsVerticalLayout; } @AutoGenerated private HorizontalLayout buildMainLayout() { // common part: create layout mainLayout = new HorizontalLayout(); mainLayout.setCaption("Licence Details"); mainLayout.setImmediate(true); mainLayout.setWidth("-1px"); mainLayout.setHeight("-1px"); mainLayout.setMargin(true); // top-level component properties setWidth("-1px"); setHeight("-1px"); // verticalLayout_2 verticalLayout_2 = buildVerticalLayout_2(); mainLayout.addComponent(verticalLayout_2); mainLayout.setExpandRatio(verticalLayout_2, 1.0f); // verticalLayout_1 verticalLayout_1 = buildVerticalLayout_1(); mainLayout.addComponent(verticalLayout_1); mainLayout.setExpandRatio(verticalLayout_1, 1.0f); return mainLayout; } @AutoGenerated private VerticalLayout buildVerticalLayout_2() { // common part: create layout verticalLayout_2 = new VerticalLayout(); verticalLayout_2.setImmediate(true); verticalLayout_2.setWidth("-1px"); verticalLayout_2.setHeight("-1px"); verticalLayout_2.setMargin(true); // licenceListSelect licenceListSelect = new ListSelect(); licenceListSelect.setCaption("Licence Id"); licenceListSelect.setImmediate(true); licenceListSelect.setWidth("-1px"); licenceListSelect.setHeight("-1px"); verticalLayout_2.addComponent(licenceListSelect); verticalLayout_2.setExpandRatio(licenceListSelect, 1.0f); // controlsVerticalLayout controlsVerticalLayout = new VerticalLayout(); controlsVerticalLayout.setImmediate(true); controlsVerticalLayout.setWidth("-1px"); controlsVerticalLayout.setHeight("-1px"); controlsVerticalLayout.setMargin(false); verticalLayout_2.addComponent(controlsVerticalLayout); return verticalLayout_2; } @AutoGenerated private VerticalLayout buildVerticalLayout_1() { // common part: create layout verticalLayout_1 = new VerticalLayout(); verticalLayout_1.setImmediate(true); verticalLayout_1.setWidth("-1px"); verticalLayout_1.setHeight("-1px"); verticalLayout_1.setMargin(false); // licencePanels licencePanels = new VerticalLayout(); licencePanels.setImmediate(true); licencePanels.setWidth("-1px"); licencePanels.setHeight("-1px"); licencePanels.setMargin(false); verticalLayout_1.addComponent(licencePanels); verticalLayout_1.setExpandRatio(licencePanels, 1.0f); return verticalLayout_1; } }