blob: 3482df9988d63874a8b751d4d8b2f1914d0dd176 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), 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:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.vaadin.components.fields;
import org.eclipse.core.databinding.Binding;
import org.eclipse.osbp.runtime.common.state.ISharedStateContext;
import org.eclipse.osbp.runtime.web.vaadin.common.data.BeanServiceLazyLoadingContainer;
import org.eclipse.osbp.runtime.web.vaadin.common.data.IBeanSearchService;
import org.eclipse.osbp.runtime.web.vaadin.components.widget.LazyLoadingComboBox;
import com.vaadin.data.Container.Filter;
import com.vaadin.data.Property;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.shared.ui.combobox.FilteringMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.TextField;
// TODO: Auto-generated Javadoc
/**
* The Class BeanReferenceField.
*
* @param <BEAN>
* the generic type
*/
@SuppressWarnings("serial")
public class BeanReferenceField<BEAN> extends CustomField<BEAN> {
/** The property. */
private ObjectProperty<BEAN> property;
/** The search service. */
private IBeanSearchService<BEAN> searchService;
/** The type. */
private final Class<BEAN> type;
/** The combo box. */
private CustomComboBox comboBox;
/** The value binding. */
private Binding valueBinding;
/** The item caption property id. */
private Object itemCaptionPropertyId;
/** The item icon property id. */
private Object itemIconPropertyId;
/** The filter. */
private Filter filter;
/** The shared state. */
private ISharedStateContext sharedState;
/** The search button. */
private NativeButton searchButton;
/** The search button icon. */
private Resource searchButtonIcon;
private DialogHandle dialogHandle;
/** The use search dialog. */
private boolean useSearchDialog = false;
private boolean nullSelectionAllowed = true;
/**
* Instantiates a new bean reference field.
*
* @param id
* the id
* @param propertyId
* the property id
* @param type
* the type
* @param searchService
* the search service
* @param filter
* the filter
* @param sharedState
* the shared state
*/
public BeanReferenceField(String id, Object propertyId, Class<BEAN> type, IBeanSearchService<BEAN> searchService,
Filter filter, ISharedStateContext sharedState) {
this.type = type;
this.searchService = searchService;
this.filter = filter;
this.sharedState = sharedState;
property = new ObjectProperty<BEAN>(null, type, false);
super.setPropertyDataSource(property);
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.CustomField#initContent()
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected Component initContent() {
HorizontalLayout root = new HorizontalLayout();
root.addStyleName("l-beansearchfield");
comboBox = new CustomComboBox();
comboBox.setImmediate(true);
comboBox.setNullSelectionAllowed(isNullSelectionAllowed());
comboBox.setInvalidAllowed(false);
BeanServiceLazyLoadingContainer container = new BeanServiceLazyLoadingContainer(searchService, type,
sharedState);
// add the passed container filter
if (filter != null) {
container.addContainerFilter(filter);
}
comboBox.setContainerDataSource(container);
comboBox.setFilteringMode(FilteringMode.CONTAINS);
if (itemCaptionPropertyId != null) {
comboBox.setItemCaptionPropertyId(itemCaptionPropertyId);
container.sort(new Object[] { itemCaptionPropertyId }, new boolean[] { true });
}
if (itemIconPropertyId != null) {
comboBox.setItemIconPropertyId(itemIconPropertyId);
}
searchButton = new NativeButton();
searchButton.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
openSearchDialog();
}
});
searchButton.setWidth("26px");
if (searchButtonIcon != null) {
searchButton.setIcon(searchButtonIcon);
} else {
searchButton.setIcon(new ThemeResource("icons/SearchButton.png"));
}
searchButton.setVisible(useSearchDialog);
root.addComponent(comboBox);
root.addComponent(searchButton);
// Create the property
comboBox.setPropertyDataSource(property);
// Create the bindings
// valueBinding =
// dbc.bindValue(VaadinObservables.observeValue(comboBox),
// PojoObservables.observeValue(property, "value"));
return root;
}
public DialogHandle getDialogHandle() {
return dialogHandle;
}
public void setDialogHandle(DialogHandle dialogHandle) {
this.dialogHandle = dialogHandle;
}
/**
* If true, then null selections are allowed. False otherwise.
*
* @return
*/
public boolean isNullSelectionAllowed() {
return nullSelectionAllowed;
}
/**
* True, if null selections should be allowed. False otherwise.
*
* @param nullSelectionAllowed
*/
public void setNullSelectionAllowed(boolean nullSelectionAllowed) {
this.nullSelectionAllowed = nullSelectionAllowed;
}
/**
* If true, then the search dialog button is visible. False otherwise.
*
* @param useSearchDialog
* the new use dialog
*/
public void setUseDialog(boolean useSearchDialog) {
this.useSearchDialog = useSearchDialog;
}
/**
* Gets the internal combo box.
*
* @return the comboBox
*/
public CustomComboBox getInternalComboBox() {
return comboBox;
}
/**
* Open search dialog.
*/
protected void openSearchDialog() {
if (dialogHandle != null) {
dialogHandle.open();
}
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractField#getType()
*/
@Override
public Class<? extends BEAN> getType() {
return type;
}
/**
* See {@link TextField#setNullRepresentation(String)}.
*
* @param value
* the new null representation
*/
public void setNullRepresentation(String value) {
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractField#getPropertyDataSource()
*/
@Override
public Property<BEAN> getPropertyDataSource() {
return property;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractField#setPropertyDataSource(com.vaadin.data.
* Property )
*/
@SuppressWarnings("rawtypes")
@Override
public void setPropertyDataSource(Property newDataSource) {
throw new UnsupportedOperationException();
}
/**
* Sets the item caption property id.
*
* @param propertyId
* the new item caption property id
* @see com.vaadin.ui.AbstractSelect#setItemCaptionPropertyId(java.lang.Object)
*/
public void setItemCaptionPropertyId(Object propertyId) {
this.itemCaptionPropertyId = propertyId;
if (comboBox != null) {
comboBox.setItemCaptionPropertyId(propertyId);
}
}
/**
* Sets the item icon property id.
*
* @param propertyId
* the new item icon property id
* @throws IllegalArgumentException
* the illegal argument exception
* @see com.vaadin.ui.AbstractSelect#setItemIconPropertyId(java.lang.Object)
*/
public void setItemIconPropertyId(Object propertyId) throws IllegalArgumentException {
this.itemIconPropertyId = propertyId;
if (comboBox != null) {
comboBox.setItemIconPropertyId(propertyId);
}
}
/**
* Dispose the field.
*/
public void dispose() {
if (valueBinding != null) {
valueBinding.dispose();
valueBinding = null;
}
}
/**
* Sets the search button icon.
*
* @param resource
* the new search button icon
*/
public void setSearchButtonIcon(Resource resource) {
if (searchButton != null) {
searchButton.setIcon(resource);
} else {
searchButtonIcon = resource;
}
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractField#focus()
*/
@Override
public void focus() {
if (comboBox != null) {
comboBox.focus();
}
}
/**
* The Class CustomComboBox.
*/
private static class CustomComboBox extends LazyLoadingComboBox {
/** The item icon property id. */
private Object itemIconPropertyId;
/*
* (non-Javadoc)
*
* @see
* com.vaadin.ui.AbstractSelect#setItemIconPropertyId(java.lang.Object)
*/
@Override
public void setItemIconPropertyId(Object propertyId) throws IllegalArgumentException {
if (propertyId == null) {
super.setItemIconPropertyId(propertyId);
} else if (!getContainerPropertyIds().contains(propertyId)) {
super.setItemIconPropertyId(propertyId);
} else if (String.class.isAssignableFrom(getType(propertyId))) {
itemIconPropertyId = propertyId;
} else {
super.setItemIconPropertyId(propertyId);
}
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractSelect#getItemIconPropertyId()
*/
public Object getItemIconPropertyId() {
return itemIconPropertyId != null ? itemIconPropertyId : super.getItemIconPropertyId();
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractSelect#getItemIcon(java.lang.Object)
*/
public Resource getItemIcon(Object itemId) {
if (itemIconPropertyId == null) {
return super.getItemIcon(itemId);
} else {
final Property<?> ip = getContainerProperty(itemId, getItemIconPropertyId());
if (ip == null) {
return null;
}
final Object icon = ip.getValue();
if (icon instanceof String) {
return new ThemeResource((String) icon);
}
}
return null;
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractSelect#setValue(java.lang.Object)
*/
@Override
public void setValue(Object newValue) throws com.vaadin.data.Property.ReadOnlyException {
super.setValue(newValue);
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractField#setValue(java.lang.Object, boolean)
*/
@Override
protected void setValue(Object newValue, boolean repaintIsNotNeeded)
throws com.vaadin.data.Property.ReadOnlyException {
super.setValue(newValue, repaintIsNotNeeded);
}
}
public interface DialogHandle {
void open();
void close();
}
}