ItemUIRegistryImpl implements ItemUIRegistry { @Override public @Nullable Widget getDefaultWidget(@Nullable Class<? extends Item> targetItemType, String itemName) { for (ItemUIProvider provider : itemUIProviders) { Widget widget = provider.getDefaultWidget(targetItemType, itemName); if (widget != null) { return widget; } } Class<? extends Item> itemType = targetItemType; if (itemType == null) { itemType = getItemType(itemName); } if (itemType == null) { return null; } if (GroupItem.class.equals(itemType)) { return SitemapFactory.eINSTANCE.createGroup(); } else if (CallItem.class.equals(itemType) || ContactItem.class.equals(itemType) || DateTimeItem.class.equals(itemType)) { return SitemapFactory.eINSTANCE.createText(); } else if (ColorItem.class.equals(itemType)) { return SitemapFactory.eINSTANCE.createColorpicker(); } else if (DimmerItem.class.equals(itemType)) { Slider slider = SitemapFactory.eINSTANCE.createSlider(); slider.setSwitchEnabled(true); return slider; } else if (ImageItem.class.equals(itemType)) { return SitemapFactory.eINSTANCE.createImage(); } else if (LocationItem.class.equals(itemType)) { return SitemapFactory.eINSTANCE.createMapview(); } else if (NumberItem.class.isAssignableFrom(itemType) || StringItem.class.equals(itemType)) { boolean isReadOnly = isReadOnly(itemName); if (!isReadOnly && hasStateOptions(itemName)) { return SitemapFactory.eINSTANCE.createSelection(); } int commandOptionsSize = getCommandOptionsSize(itemName); if (!isReadOnly && commandOptionsSize > 0) { return commandOptionsSize <= MAX_BUTTONS ? SitemapFactory.eINSTANCE.createSwitch() : SitemapFactory.eINSTANCE.createSelection(); } else { return SitemapFactory.eINSTANCE.createText(); } } else if (PlayerItem.class.equals(itemType)) { return createPlayerButtons(); } else if (RollershutterItem.class.equals(itemType) || SwitchItem.class.equals(itemType)) { return SitemapFactory.eINSTANCE.createSwitch(); } return null; } @Activate  ItemUIRegistryImpl(@Reference ItemRegistry itemRegistry); @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) void addItemUIProvider(ItemUIProvider itemUIProvider); void removeItemUIProvider(ItemUIProvider itemUIProvider); @Override @Nullable String getCategory(String itemName); @Override @Nullable String getLabel(String itemName); @Override @Nullable Widget getWidget(String itemName); @Override @Nullable Widget getDefaultWidget(@Nullable Class<? extends Item> targetItemType, String itemName); @Override @Nullable String getLabel(Widget w); @Override @Nullable String getCategory(Widget w); @Override @Nullable State getState(Widget w); @Override @Nullable State convertState(Widget w, Item i, State state); @Override @Nullable Widget getWidget(Sitemap sitemap, String id); @Override EList<Widget> getChildren(Sitemap sitemap); @Override EList<Widget> getChildren(LinkableWidget w); @Override @Nullable EObject getParent(Widget w); @Override @Nullable State getItemState(String itemName); @Nullable String getItemCategory(String itemName); @Override Item getItem(String name); @Override Item getItemByPattern(String name); @Override Collection<Item> getItems(); @Override Collection<Item> getItemsOfType(String type); @Override Collection<Item> getItems(String pattern); @Override void addRegistryChangeListener(RegistryChangeListener<Item> listener); @Override void removeRegistryChangeListener(RegistryChangeListener<Item> listener); @Override Collection<Item> getAll(); @Override Stream<Item> stream(); @Override String getWidgetId(Widget widget); @Override @Nullable String getLabelColor(Widget w); @Override @Nullable String getValueColor(Widget w); @Override boolean getVisiblity(Widget w); @Override Collection<Item> getItemsByTag(String... tags); @Override Collection<Item> getItemsByTagAndType(String type, String... tags); @Override Collection<T> getItemsByTag(Class<T> typeFilter, String... tags); @Override Item add(Item element); @Override @Nullable Item update(Item element); @Override @Nullable Item remove(String key); @Override @Nullable Item get(String key); @Override @Nullable Item remove(String itemName, boolean recursive); @Override void addRegistryHook(RegistryHook<Item> hook); @Override void removeRegistryHook(RegistryHook<Item> hook); @Override @Nullable String getUnitForWidget(Widget w); @Override @Nullable State convertStateToLabelUnit(QuantityType<?> state, String label);  }
@Test public void getDefaultWidgets() { Widget defaultWidget = uiRegistry.getDefaultWidget(GroupItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Group.class))); defaultWidget = uiRegistry.getDefaultWidget(CallItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Text.class))); defaultWidget = uiRegistry.getDefaultWidget(ColorItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Colorpicker.class))); defaultWidget = uiRegistry.getDefaultWidget(ContactItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Text.class))); defaultWidget = uiRegistry.getDefaultWidget(DateTimeItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Text.class))); defaultWidget = uiRegistry.getDefaultWidget(DimmerItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Slider.class))); assertThat(((Slider) defaultWidget).isSwitchEnabled(), is(true)); defaultWidget = uiRegistry.getDefaultWidget(ImageItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Image.class))); defaultWidget = uiRegistry.getDefaultWidget(LocationItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Mapview.class))); defaultWidget = uiRegistry.getDefaultWidget(PlayerItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Switch.class))); assertThat(((Switch) defaultWidget).getMappings(), hasSize(4)); defaultWidget = uiRegistry.getDefaultWidget(RollershutterItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Switch.class))); defaultWidget = uiRegistry.getDefaultWidget(SwitchItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Switch.class))); }
@Test public void getDefaultWidgetsForNumberItem() { Widget defaultWidget = uiRegistry.getDefaultWidget(NumberItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Text.class))); final CommandDescriptionBuilder builder = CommandDescriptionBuilder.create() .withCommandOptions(Stream .of(new CommandOption("command1", "label1"), new CommandOption("command2", "label2"), new CommandOption("command3", "label3"), new CommandOption("command4", "label4")) .collect(Collectors.toList())); when(item.getCommandDescription()).thenReturn(builder.build()); defaultWidget = uiRegistry.getDefaultWidget(NumberItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Switch.class))); builder.withCommandOption(new CommandOption("command5", "label5")); when(item.getCommandDescription()).thenReturn(builder.build()); defaultWidget = uiRegistry.getDefaultWidget(NumberItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Selection.class))); when(item.getStateDescription()).thenReturn(StateDescriptionFragmentBuilder.create() .withOption(new StateOption("value", "label")).build().toStateDescription()); defaultWidget = uiRegistry.getDefaultWidget(NumberItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Selection.class))); }
@Test public void getDefaultWidgetsForStringItem() { Widget defaultWidget = uiRegistry.getDefaultWidget(StringItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Text.class))); final CommandDescriptionBuilder builder = CommandDescriptionBuilder.create() .withCommandOptions(Stream .of(new CommandOption("command1", "label1"), new CommandOption("command2", "label2"), new CommandOption("command3", "label3"), new CommandOption("command4", "label4")) .collect(Collectors.toList())); when(item.getCommandDescription()).thenReturn(builder.build()); defaultWidget = uiRegistry.getDefaultWidget(StringItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Switch.class))); builder.withCommandOption(new CommandOption("command5", "label5")); when(item.getCommandDescription()).thenReturn(builder.build()); defaultWidget = uiRegistry.getDefaultWidget(StringItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Selection.class))); when(item.getStateDescription()).thenReturn(StateDescriptionFragmentBuilder.create() .withOption(new StateOption("value", "label")).build().toStateDescription()); defaultWidget = uiRegistry.getDefaultWidget(StringItem.class, ITEM_NAME); assertThat(defaultWidget, is(instanceOf(Selection.class))); }