blob: af700f82055fd35c241adff98d58e95734fa9704 [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 v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.vaadin.components.fields.search.filter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.eclipse.osbp.runtime.web.vaadin.common.data.filter.Filters;
import com.vaadin.data.Container.Filter;
// TODO: Auto-generated Javadoc
/**
* The Class BooleanFilterProperty.
*/
public class BooleanFilterProperty extends FilterProperty {
/** The selection. */
private OptionBean selection;
/** The options. */
private List<OptionBean> options;
/** The default option. */
private OptionBean defaultOption;
/** The filter provider. */
private Filters filterProvider = new Filters();
/**
* Instantiates a new boolean filter property.
*
* @param propertyId
* the property id
* @param locale
* the locale
*/
public BooleanFilterProperty(Object propertyId, Locale locale) {
super(propertyId, locale);
}
/**
* Gets the selection.
*
* @return the selection
*/
public OptionBean getSelection() {
return selection;
}
/**
* Sets the selection.
*
* @param selection
* the selection to set
*/
public void setSelection(OptionBean selection) {
this.selection = selection;
notifyFilterChanged();
}
/**
* Returns all available options.
*
* @return the options
*/
public List<OptionBean> getOptions() {
if (options == null) {
options = new ArrayList<BooleanFilterProperty.OptionBean>();
options.add(new OptionBean(Option.TRUE, getOptionsName(Option.TRUE,
getLocale())));
options.add(new OptionBean(Option.FALSE, getOptionsName(
Option.FALSE, getLocale())));
defaultOption = new OptionBean(Option.IGNORE, getOptionsName(
Option.IGNORE, getLocale()));
options.add(defaultOption);
}
return Collections.unmodifiableList(options);
}
/**
* Gets the options name.
*
* @param option
* the option
* @param locale
* the locale
* @return the options name
*/
private static String getOptionsName(Option option, Locale locale) {
switch (option) {
case TRUE:
return "true";
case FALSE:
return "false";
case IGNORE:
default:
return "ignore";
}
}
/**
* Returns the default selection.
*
* @return the default option
*/
public OptionBean getDefaultOption() {
return defaultOption;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.vaadin.components.fields.search.filter.IFilterProperty#getFilter()
*/
@Override
public Filter getFilter() {
if (filterProvider == null || selection == null) {
return null;
}
switch (selection.getOption()) {
case FALSE:
return filterProvider.eq(getPropertyId(), Boolean.FALSE);
case TRUE:
return filterProvider.eq(getPropertyId(), Boolean.TRUE);
case IGNORE:
return null;
}
throw new IllegalStateException("Not a valid state!");
}
/**
* The Enum Option.
*/
public static enum Option {
/** The true. */
TRUE,
/** The false. */
FALSE,
/** The ignore. */
IGNORE;
}
/**
* The Class OptionBean.
*/
public static class OptionBean {
/** The option. */
private final Option option;
/** The description. */
private final String description;
/**
* Instantiates a new option bean.
*
* @param option
* the option
* @param description
* the description
*/
public OptionBean(Option option, String description) {
super();
this.option = option;
this.description = description;
}
/**
* Gets the option.
*
* @return the option
*/
public Option getOption() {
return option;
}
/**
* Gets the description.
*
* @return the description
*/
public String getDescription() {
return description;
}
}
}