blob: 88ebecbd52fc95ce13a61ec4bb799b5cf9661361 [file] [log] [blame]
/**
*
* 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.infogrid.vaadin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.osbp.infogrid.api.IGridSourceDescriptor;
import com.vaadin.data.Item;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.VerticalLayout;
/**
* This component needs configuration by IGridSourceDescriptor. They need to be
* added from outside logic.
*/
@SuppressWarnings("serial")
public class GridComponent extends CustomComponent {
private ComboBox sourceSelector;
private CssLayout gridArea;
private List<IGridSourceDescriptor> sources;
private IGridSourceDescriptor currentSource;
private VerticalLayout layout;
private Object currentInput;
public GridComponent() {
initUI();
}
/**
* Initializes the UI.
*/
protected void initUI() {
sourceSelector = new ComboBox();
sourceSelector.addContainerProperty("caption", String.class,
"undefined ID");
sourceSelector.addContainerProperty("gridSource",
IGridSourceDescriptor.class, null);
sourceSelector.setItemCaptionMode(ItemCaptionMode.PROPERTY);
sourceSelector.setItemCaptionPropertyId("caption");
sourceSelector.addValueChangeListener(e -> sourceChanged());
gridArea = new CssLayout();
gridArea.setSizeFull();
layout = new VerticalLayout(sourceSelector, gridArea);
layout.setExpandRatio(gridArea, 1.0f);
setCompositionRoot(layout);
}
/**
* Is called if the source became changed.
*/
protected void sourceChanged() {
Item item = sourceSelector.getItem(sourceSelector.getValue());
if (item == null) {
setSelectedSource(null);
} else {
setSelectedSource((IGridSourceDescriptor) item.getItemProperty(
"gridSource").getValue());
}
}
/**
* Sets the selected source descriptor.
*
* @param source
*/
public void setSelectedSource(IGridSourceDescriptor source) {
// TODO
// if (currentSource == source) {
// return;
// }
if (source != null && !sources.contains(source)) {
return;
}
currentSource = source;
gridArea.removeAllComponents();
if (currentSource != null) {
Component component = (Component) currentSource.getComponent();
component.setSizeFull();
gridArea.addComponent(component);
currentSource.setInput(currentInput);
}
}
/**
* Returns all descriptors.
*
* @return
*/
public List<IGridSourceDescriptor> getSourceDescriptors() {
return Collections.unmodifiableList(sources);
}
/**
* Sets all available descriptors to this component. The descriptors are
* responsible to create the UI content.
*
* @param sources
*/
@SuppressWarnings("unchecked")
public void setSourceDescriptors(List<IGridSourceDescriptor> sources) {
sourceSelector.removeAllItems();
for (IGridSourceDescriptor source : sources) {
Item item = sourceSelector.getItem(sourceSelector.addItem());
item.getItemProperty("caption").setValue(source.getId());
item.getItemProperty("gridSource").setValue(source);
}
this.sources = new ArrayList<>(sources);
}
/**
* Sets the value of the component.
*
* @param input
*/
public void setValue(Object input) {
if (currentInput == input) {
return;
}
currentInput = input;
if (currentSource != null) {
currentSource.setInput(currentInput);
}
}
public void dispose() {
}
}