Skip to content

Commit

Permalink
Split retrieving label source from retrieving label
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 committed Oct 17, 2023
1 parent b20d2f8 commit d93cebb
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,10 @@ private Set<SitemapEvent> constructSitemapEvents(Item item, State state, List<Wi

private SitemapWidgetEvent constructSitemapEventForWidget(Item item, State state, Widget widget) {
SitemapWidgetEvent event = new SitemapWidgetEvent();
ItemUIRegistry.WidgetLabelWithSource label = itemUIRegistry.getLabel(widget);

event.sitemapName = sitemapName;
event.pageId = pageId;
event.label = label.label;
event.labelSource = label.source.toString();
event.label = itemUIRegistry.getLabel(widget);
event.labelSource = itemUIRegistry.getLabelSource(widget).toString();
event.widgetId = itemUIRegistry.getWidgetId(widget);
event.visibility = itemUIRegistry.getVisiblity(widget);
event.descriptionChanged = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private PageDTO getPageBean(String sitemapName, String pageId, URI uri, Locale l
Widget pageWidget = itemUIRegistry.getWidget(sitemap, pageId);
if (pageWidget instanceof LinkableWidget widget) {
EList<Widget> children = itemUIRegistry.getChildren(widget);
PageDTO pageBean = createPageBean(sitemapName, itemUIRegistry.getLabel(pageWidget).label,
PageDTO pageBean = createPageBean(sitemapName, itemUIRegistry.getLabel(pageWidget),
itemUIRegistry.getCategory(pageWidget), pageId, children, false, isLeaf(children), uri,
locale, timeout, includeHidden);
EObject parentPage = pageWidget.eContainer();
Expand Down Expand Up @@ -521,17 +521,14 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null
logger.debug("{}", e.getMessage());
}
}

ItemUIRegistry.WidgetLabelWithSource label = itemUIRegistry.getLabel(widget);

bean.widgetId = widgetId;
bean.icon = itemUIRegistry.getCategory(widget);
bean.staticIcon = widget.getStaticIcon() != null;
bean.labelcolor = convertItemValueColor(itemUIRegistry.getLabelColor(widget), itemState);
bean.valuecolor = convertItemValueColor(itemUIRegistry.getValueColor(widget), itemState);
bean.iconcolor = convertItemValueColor(itemUIRegistry.getIconColor(widget), itemState);
bean.label = label.label;
bean.labelSource = label.source.toString();
bean.label = itemUIRegistry.getLabel(widget);
bean.labelSource = itemUIRegistry.getLabelSource(widget).toString();
bean.pattern = itemUIRegistry.getFormatPattern(widget);
bean.unit = itemUIRegistry.getUnitForWidget(widget);
bean.type = widget.eClass().getName();
Expand All @@ -549,7 +546,7 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null
}
} else if (!children.isEmpty()) {
String pageName = itemUIRegistry.getWidgetId(linkableWidget);
bean.linkedPage = createPageBean(sitemapName, itemUIRegistry.getLabel(widget).label,
bean.linkedPage = createPageBean(sitemapName, itemUIRegistry.getLabel(widget),
itemUIRegistry.getCategory(widget), pageName, drillDown ? children : null, drillDown,
isLeaf(children), uri, locale, false, evenIfHidden);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.openhab.core.types.State;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.openhab.core.ui.items.ItemUIRegistry.WidgetLabelSource;
import org.openhab.core.ui.items.ItemUIRegistry.WidgetLabelWithSource;

/**
* Test aspects of the {@link SitemapResource}.
Expand Down Expand Up @@ -306,8 +305,8 @@ private void configureItemUIRegistry(State state1, State state2) throws ItemNotF

when(itemUIRegistryMock.getWidgetId(widgets.get(0))).thenReturn(WIDGET1_ID);
when(itemUIRegistryMock.getCategory(widgets.get(0))).thenReturn("");
when(itemUIRegistryMock.getLabel(widgets.get(0)))
.thenReturn(new WidgetLabelWithSource(WIDGET1_LABEL, WidgetLabelSource.SITEMAP_WIDGET));
when(itemUIRegistryMock.getLabel(widgets.get(0))).thenReturn(WIDGET1_LABEL);
when(itemUIRegistryMock.getLabelSource(widgets.get(0))).thenReturn(WidgetLabelSource.SITEMAP_WIDGET);
when(itemUIRegistryMock.getVisiblity(widgets.get(0))).thenReturn(true);
when(itemUIRegistryMock.getLabelColor(widgets.get(0))).thenReturn("GREEN");
when(itemUIRegistryMock.getValueColor(widgets.get(0))).thenReturn("BLUE");
Expand All @@ -316,8 +315,8 @@ private void configureItemUIRegistry(State state1, State state2) throws ItemNotF

when(itemUIRegistryMock.getWidgetId(widgets.get(1))).thenReturn(WIDGET2_ID);
when(itemUIRegistryMock.getCategory(widgets.get(1))).thenReturn("");
when(itemUIRegistryMock.getLabel(widgets.get(1)))
.thenReturn(new WidgetLabelWithSource(ITEM_LABEL, WidgetLabelSource.ITEM_LABEL));
when(itemUIRegistryMock.getLabel(widgets.get(1))).thenReturn(ITEM_LABEL);
when(itemUIRegistryMock.getLabelSource(widgets.get(1))).thenReturn(WidgetLabelSource.ITEM_LABEL);
when(itemUIRegistryMock.getVisiblity(widgets.get(1))).thenReturn(true);
when(itemUIRegistryMock.getLabelColor(widgets.get(1))).thenReturn(null);
when(itemUIRegistryMock.getValueColor(widgets.get(1))).thenReturn(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ public class ItemUIRegistryImpl implements ItemUIRegistry {

private String groupMembersSorting = DEFAULT_SORTING;

private static class WidgetLabelWithSource {
public final String label;
public final WidgetLabelSource source;

public WidgetLabelWithSource(String l, WidgetLabelSource s) {
label = l;
source = s;
}
}

@Activate
public ItemUIRegistryImpl(@Reference ItemRegistry itemRegistry) {
this.itemRegistry = itemRegistry;
Expand Down Expand Up @@ -322,14 +332,12 @@ private Switch createPlayerButtons() {
}

@Override
public @Nullable WidgetLabelWithSource getLabel(Widget w) {
WidgetLabelWithSource labelWithSource = getLabelFromWidget(w);
String label = labelWithSource.label;
final WidgetLabelSource source = labelWithSource.source;
public @Nullable String getLabel(Widget w) {
String label = getLabelFromWidget(w).label;

String itemName = w.getItem();
if (itemName == null || itemName.isBlank()) {
return transform(label, source, true, null);
return transform(label, true, null);
}

String labelMappedOption = null;
Expand Down Expand Up @@ -465,7 +473,12 @@ private Switch createPlayerButtons() {
}
}

return transform(label, source, considerTransform, labelMappedOption);
return transform(label, considerTransform, labelMappedOption);
}

@Override
public WidgetLabelSource getLabelSource(Widget w) {
return getLabelFromWidget(w).source;
}

private QuantityType<?> convertStateToWidgetUnit(QuantityType<?> quantityState, Widget w) {
Expand Down Expand Up @@ -602,8 +615,7 @@ private String insertInLabel(String label, Object o) {
* If the value does not start with the call to a transformation service,
* we return the label with the mapped option value if provided (not null).
*/
private WidgetLabelWithSource transform(String label, WidgetLabelSource source, boolean matchTransform,
@Nullable String labelMappedOption) {
private String transform(String label, boolean matchTransform, @Nullable String labelMappedOption) {
String ret = label;
String formatPattern = getFormatPattern(label);
if (formatPattern != null) {
Expand Down Expand Up @@ -638,7 +650,7 @@ private WidgetLabelWithSource transform(String label, WidgetLabelSource source,
ret = labelMappedOption;
}
}
return new WidgetLabelWithSource(ret, source);
return ret;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ public enum WidgetLabelSource {
NONE
};

public static class WidgetLabelWithSource {
public final String label;
public final WidgetLabelSource source;

public WidgetLabelWithSource(String l, WidgetLabelSource s) {
label = l;
source = s;
}
}

/**
* Retrieves the label for a widget.
*
Expand All @@ -68,10 +58,18 @@ public WidgetLabelWithSource(String l, WidgetLabelSource s) {
* current value of the item and padded by a "<span>" element.
*
* @param w the widget to retrieve the label for
* @return the label to use for the widget, together with its source
* @return the label to use for the widget
*/
@Nullable
WidgetLabelWithSource getLabel(Widget w);
String getLabel(Widget w);

/**
* Retrieves the label source for a widget.
*
* @param w the widget to retrieve the label source for
* @return the source the widget label is taken from
*/
WidgetLabelSource getLabelSource(Widget w);

/**
* Retrieves the category for a widget.
Expand Down
Loading

0 comments on commit d93cebb

Please sign in to comment.