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;
}
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;
}
Aggregations