| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.xtext.datamart.common; |
| |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.List; |
| |
| import org.eclipse.osbp.runtime.common.event.IDualData; |
| //Temporarily not in use to avoid a loop due to the dependency from org.eclipse.osbp.runtime.web to this bundle |
| //import org.eclipse.osbp.runtime.web.vaadin.databinding.VaadinObservables; |
| import org.eclipse.osbp.ui.api.datamart.IDatamartSelectable; |
| import org.eclipse.osbp.ui.api.themes.EnumCssClass; |
| |
| import com.vaadin.data.Container; |
| import com.vaadin.ui.ListSelect; |
| import com.vaadin.ui.UI; |
| |
| public class DatamartMultiSelect extends ListSelect implements |
| IDatamartSelectable { |
| |
| private static final long serialVersionUID = 7284120542309904692L; |
| private String hierarchy = null; |
| private IDualData firstItem = null; |
| private boolean selectFirst; |
| |
| @SuppressWarnings("unchecked") |
| @Override |
| public boolean isMultiSelected() { |
| Collection<IDualData> obj = (Collection<IDualData>) getValue(); |
| return (obj.size() > 0); |
| } |
| |
| public DatamartMultiSelect(int rowsLimit, boolean selectFirst, Collection<IDualData> data) { |
| addStyleName(EnumCssClass.MULTI_SELECTION.styleName()); |
| this.selectFirst = selectFirst; |
| setMultiSelect(true); |
| update(data); |
| if (data.size() > rowsLimit) { |
| setRows(rowsLimit); |
| } else { |
| setRows(data.size()); |
| } |
| } |
| |
| public static long getSerialversionuid() { |
| return serialVersionUID; |
| } |
| |
| public DatamartMultiSelect() { |
| } |
| |
| /** |
| * (re)set all options visible in this box |
| * |
| * @param options |
| */ |
| public void update(Collection<IDualData> options) { |
| UI.getCurrent().access(new Runnable() { |
| @Override |
| public void run() { |
| removeAllItems(); |
| firstItem = null; |
| for (IDualData dd : options) { |
| setHierarchy(dd.getHierarchy()); |
| String item = dd.getSelectionValue(); |
| if (item != null) { |
| if (firstItem == null) { |
| firstItem = dd; |
| } |
| addItem(dd); |
| setItemCaption(dd, dd.getFormattedValue()); |
| } |
| } |
| // always select the first option |
| Collection<IDualData> selectedItems = new ArrayList<IDualData>(); |
| if(selectFirst) { |
| selectedItems.add(firstItem); |
| setNullSelectionAllowed(false); |
| } else { |
| setNullSelectionAllowed(true); |
| } |
| setValue(selectedItems); |
| setEnabled(true); |
| setImmediate(true); |
| } |
| }); |
| } |
| |
| public List<IDualData> getSelectedItems() { |
| List<IDualData> list = new ArrayList<IDualData>(); |
| for (Object item : (Collection<?>) getValue()) { |
| if (item != null){ |
| list.add((IDualData) item); |
| } |
| } |
| return (list); |
| } |
| |
| public IDualData getFirstItem() { |
| return firstItem; |
| } |
| |
| public String getHierarchy() { |
| return hierarchy; |
| } |
| |
| public void setHierarchy(String hierarchy) { |
| this.hierarchy = hierarchy; |
| } |
| |
| @Override |
| public Container getContainerDataSource() { |
| return super.getContainerDataSource(); |
| } |
| |
| @SuppressWarnings("unchecked") |
| @Override |
| public void select(Object obj) { |
| for(Object item:getItemIds()) { |
| for(IDualData sel:(List<IDualData>)obj) { |
| if(sel.equals((IDualData)item)) { |
| super.select(item); |
| } else { |
| super.unselect(item); |
| } |
| } |
| } |
| } |
| |
| @Override |
| public void addValueChangeListener(ValueChangeListener listener) { |
| super.addValueChangeListener(listener); |
| } |
| |
| @Override |
| public void removeValueChangeListener(ValueChangeListener listener) { |
| super.removeValueChangeListener(listener); |
| } |
| |
| @Override |
| public void setCaption(String caption) { |
| super.setCaption(caption); |
| } |
| |
| @Override |
| public void setDescription(String description) { |
| super.setDescription(description); |
| } |
| |
| @Override |
| public float getHeight() { |
| return super.getHeight(); |
| } |
| |
| @Override |
| public Unit getHeightUnits() { |
| return super.getHeightUnits(); |
| } |
| |
| @Override |
| public Object getValue() { |
| return super.getValue(); |
| } |
| } |