Search in sources :

Example 1 with VisibilityRule

use of org.eclipse.smarthome.model.sitemap.VisibilityRule in project smarthome by eclipse.

the class SitemapResourceTest method initSitemapWidgets.

private EList<Widget> initSitemapWidgets() {
    // Initialize a sitemap containing 2 widgets linked to the same number item,
    // one slider and one switch
    Widget w1 = mock(Widget.class);
    EClass sliderEClass = mock(EClass.class);
    when(sliderEClass.getName()).thenReturn("slider");
    when(sliderEClass.getInstanceTypeName()).thenReturn("org.eclipse.smarthome.model.sitemap.Slider");
    when(w1.eClass()).thenReturn(sliderEClass);
    when(w1.getLabel()).thenReturn(WIDGET1_LABEL);
    when(w1.getItem()).thenReturn(ITEM_NAME);
    // add visibility rules to the mock widget:
    VisibilityRule visibilityRule = mock(VisibilityRule.class);
    when(visibilityRule.getItem()).thenReturn(VISIBILITY_RULE_ITEM_NAME);
    BasicEList<VisibilityRule> visibilityRules = new BasicEList<>(1);
    visibilityRules.add(visibilityRule);
    when(w1.getVisibility()).thenReturn(visibilityRules);
    // add label color conditions to the item:
    ColorArray labelColor = mock(ColorArray.class);
    when(labelColor.getItem()).thenReturn(LABEL_COLOR_ITEM_NAME);
    EList<ColorArray> labelColors = new BasicEList<>();
    labelColors.add(labelColor);
    when(w1.getLabelColor()).thenReturn(labelColors);
    // add value color conditions to the item:
    ColorArray valueColor = mock(ColorArray.class);
    when(valueColor.getItem()).thenReturn(VALUE_COLOR_ITEM_NAME);
    EList<ColorArray> valueColors = new BasicEList<>();
    valueColors.add(valueColor);
    when(w1.getValueColor()).thenReturn(valueColors);
    visibilityRules = new BasicEList<>();
    labelColors = new BasicEList<>();
    valueColors = new BasicEList<>();
    Widget w2 = mock(Widget.class);
    EClass switchEClass = mock(EClass.class);
    when(switchEClass.getName()).thenReturn("switch");
    when(switchEClass.getInstanceTypeName()).thenReturn("org.eclipse.smarthome.model.sitemap.Switch");
    when(w2.eClass()).thenReturn(switchEClass);
    when(w2.getLabel()).thenReturn(WIDGET2_LABEL);
    when(w2.getItem()).thenReturn(ITEM_NAME);
    when(w2.getVisibility()).thenReturn(visibilityRules);
    when(w2.getLabelColor()).thenReturn(labelColors);
    when(w2.getValueColor()).thenReturn(valueColors);
    BasicEList<Widget> widgets = new BasicEList<>(2);
    widgets.add(w1);
    widgets.add(w2);
    return widgets;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) VisibilityRule(org.eclipse.smarthome.model.sitemap.VisibilityRule) BasicEList(org.eclipse.emf.common.util.BasicEList) Widget(org.eclipse.smarthome.model.sitemap.Widget) ColorArray(org.eclipse.smarthome.model.sitemap.ColorArray)

Example 2 with VisibilityRule

use of org.eclipse.smarthome.model.sitemap.VisibilityRule in project smarthome by eclipse.

the class ItemUIRegistryImpl method getVisiblity.

@Override
public boolean getVisiblity(Widget w) {
    // Default to visible if parameters not set
    List<VisibilityRule> ruleList = w.getVisibility();
    if (ruleList == null) {
        return true;
    }
    if (ruleList.size() == 0) {
        return true;
    }
    logger.debug("Checking visiblity for widget '{}'.", w.getLabel());
    for (VisibilityRule rule : w.getVisibility()) {
        String itemName = rule.getItem();
        if (itemName == null) {
            continue;
        }
        if (rule.getState() == null) {
            continue;
        }
        // Try and find the item to test.
        // If it's not found, return visible
        Item item;
        try {
            item = itemRegistry.getItem(itemName);
        } catch (ItemNotFoundException e) {
            logger.error("Cannot retrieve visibility item {} for widget {}", rule.getItem(), w.eClass().getInstanceTypeName());
            // Default to visible!
            return true;
        }
        // Get the item state
        State state = item.getState();
        // Handle the sign
        String value;
        if (rule.getSign() != null) {
            value = rule.getSign() + rule.getState();
        } else {
            value = rule.getState();
        }
        if (matchStateToValue(state, value, rule.getCondition()) == true) {
            // We have the name for this value!
            return true;
        }
    }
    logger.debug("Widget {} is not visible.", w.getLabel());
    // The state wasn't in the list, so we don't display it
    return false;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) VisibilityRule(org.eclipse.smarthome.model.sitemap.VisibilityRule) State(org.eclipse.smarthome.core.types.State) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

VisibilityRule (org.eclipse.smarthome.model.sitemap.VisibilityRule)2 BasicEList (org.eclipse.emf.common.util.BasicEList)1 EClass (org.eclipse.emf.ecore.EClass)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 GroupItem (org.eclipse.smarthome.core.items.GroupItem)1 Item (org.eclipse.smarthome.core.items.Item)1 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)1 CallItem (org.eclipse.smarthome.core.library.items.CallItem)1 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)1 ContactItem (org.eclipse.smarthome.core.library.items.ContactItem)1 DateTimeItem (org.eclipse.smarthome.core.library.items.DateTimeItem)1 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)1 ImageItem (org.eclipse.smarthome.core.library.items.ImageItem)1 LocationItem (org.eclipse.smarthome.core.library.items.LocationItem)1 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)1 PlayerItem (org.eclipse.smarthome.core.library.items.PlayerItem)1 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)1 StringItem (org.eclipse.smarthome.core.library.items.StringItem)1 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)1 State (org.eclipse.smarthome.core.types.State)1