Skip to content

Commit

Permalink
[Sitemap] Introduce "Buttongrid" as new sitemap element
Browse files Browse the repository at this point in the history
Related to openhab#3441

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Sep 24, 2023
1 parent af4fce1 commit 584625b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

/**
* This is a data transfer object that is used to serialize command mappings.
*
*
* @author Kai Kreuzer - Initial contribution
* @author Laurent Garnier - New fields position and icon
*/
public class MappingDTO {

public Integer position;
public String command;
public String label;
public String icon;

public MappingDTO() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.model.sitemap.SitemapProvider;
import org.openhab.core.model.sitemap.sitemap.Button;
import org.openhab.core.model.sitemap.sitemap.Buttongrid;
import org.openhab.core.model.sitemap.sitemap.Chart;
import org.openhab.core.model.sitemap.sitemap.ColorArray;
import org.openhab.core.model.sitemap.sitemap.Frame;
Expand Down Expand Up @@ -131,6 +133,7 @@
* @author Wouter Born - Migrated to OpenAPI annotations
* @author Laurent Garnier - Added support for icon color
* @author Mark Herwege - Added pattern and unit fields
* @author Laurent Garnier - Added support for new sitemap element Buttongrid
*/
@Component(service = { RESTResource.class, EventSubscriber.class })
@JaxrsResource
Expand Down Expand Up @@ -614,6 +617,17 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null
bean.maxValue = setpointWidget.getMaxValue();
bean.step = setpointWidget.getStep();
}
if (widget instanceof Buttongrid buttonGridWidget) {
bean.columns = buttonGridWidget.getColumns();
for (Button button : buttonGridWidget.getButtons()) {
MappingDTO mappingBean = new MappingDTO();
mappingBean.position = button.getPosition();
mappingBean.command = button.getCmd();
mappingBean.label = button.getLabel();
mappingBean.icon = button.getIcon();
bean.mappings.add(mappingBean);
}
}
return bean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @author Chris Jackson - Initial contribution
* @author Laurent Garnier - New field iconcolor
* @author Mark herwege - New fields pattern, unit
* @author Laurent Garnier - New field columns
*/
public class WidgetDTO {

Expand Down Expand Up @@ -60,6 +61,7 @@ public class WidgetDTO {
public String yAxisDecimalPattern;
public Boolean legend;
public Boolean forceAsItem;
public Integer columns;
public String state;

public EnrichedItemDTO item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Widget:
(LinkableWidget | NonLinkableWidget);

NonLinkableWidget:
Switch | Selection | Slider | Setpoint | Video | Chart | Webview | Colorpicker | Mapview | Input | Default;
Switch | Selection | Slider | Setpoint | Video | Chart | Webview | Colorpicker | Mapview | Input | Buttongrid | Default;

LinkableWidget:
(Text | Group | Image | Frame)
Expand Down Expand Up @@ -149,6 +149,16 @@ Input:
('iconcolor=[' (IconColor+=ColorArray (',' IconColor+=ColorArray)* ']'))? &
('visibility=[' (Visibility+=VisibilityRule (',' Visibility+=VisibilityRule)* ']'))?);

Buttongrid:
'Buttongrid' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? &
(('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? &
('columns=' columns=INT) &
('buttons=[' buttons+=Button (',' buttons+=Button)* ']') &
('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? &
('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? &
('iconcolor=[' (IconColor+=ColorArray (',' IconColor+=ColorArray)* ']'))? &
('visibility=[' (Visibility+=VisibilityRule (',' Visibility+=VisibilityRule)* ']'))?);

Default:
'Default' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? &
(('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? &
Expand All @@ -158,6 +168,9 @@ Default:
('iconcolor=[' (IconColor+=ColorArray (',' IconColor+=ColorArray)* ']'))? &
('visibility=[' (Visibility+=VisibilityRule (',' Visibility+=VisibilityRule)* ']'))?);

Button:
position=INT ':' cmd=Command '=' label=(ID | STRING) ('=' icon=Icon)?;

Mapping:
cmd=Command '=' label=(ID | STRING);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openhab.core.model.core.EventType;
import org.openhab.core.model.core.ModelRepositoryChangeListener;
import org.openhab.core.model.sitemap.SitemapProvider;
import org.openhab.core.model.sitemap.sitemap.Button;
import org.openhab.core.model.sitemap.sitemap.ColorArray;
import org.openhab.core.model.sitemap.sitemap.LinkableWidget;
import org.openhab.core.model.sitemap.sitemap.Mapping;
Expand All @@ -38,6 +39,8 @@
import org.openhab.core.model.sitemap.sitemap.SitemapPackage;
import org.openhab.core.model.sitemap.sitemap.VisibilityRule;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.model.sitemap.sitemap.impl.ButtonImpl;
import org.openhab.core.model.sitemap.sitemap.impl.ButtongridImpl;
import org.openhab.core.model.sitemap.sitemap.impl.ChartImpl;
import org.openhab.core.model.sitemap.sitemap.impl.ColorArrayImpl;
import org.openhab.core.model.sitemap.sitemap.impl.ColorpickerImpl;
Expand Down Expand Up @@ -75,6 +78,7 @@
*
* @author Yannick Schaus - Initial contribution
* @author Laurent Garnier - icon color support for all widgets
* @author Laurent Garnier - Added support for new element Buttongrid
*/
@NonNullByDefault
@Component(service = SitemapProvider.class)
Expand Down Expand Up @@ -254,6 +258,12 @@ protected Sitemap buildSitemap(RootUIComponent rootComponent) {
setWidgetPropertyFromComponentConfig(widget, component, "frequency",
SitemapPackage.COLORPICKER__FREQUENCY);
break;
case "Buttongrid":
ButtongridImpl buttongridWidget = (ButtongridImpl) SitemapFactory.eINSTANCE.createButtongrid();
addWidgetButtons(buttongridWidget.getButtons(), component);
widget = buttongridWidget;
setWidgetPropertyFromComponentConfig(widget, component, "columns", SitemapPackage.BUTTONGRID__COLUMNS);
break;
case "Default":
DefaultImpl defaultWidget = (DefaultImpl) SitemapFactory.eINSTANCE.createDefault();
widget = defaultWidget;
Expand Down Expand Up @@ -333,6 +343,29 @@ private void addWidgetMappings(EList<Mapping> mappings, UIComponent component) {
}
}

private void addWidgetButtons(EList<Button> buttons, UIComponent component) {
if (component.getConfig() != null && component.getConfig().containsKey("buttons")) {
if (component.getConfig().get("buttons") instanceof Collection<?>) {
for (Object sourceButton : (Collection<?>) component.getConfig().get("buttons")) {
if (sourceButton instanceof String) {
String[] splitted1 = sourceButton.toString().split(":");
int idx = Integer.parseInt(splitted1[0].trim());
String[] splitted2 = splitted1[1].trim().split("=");
String cmd = splitted2[0].trim();
String label = splitted2[1].trim();
String icon = splitted2.length < 3 ? null : splitted2[2].trim();
ButtonImpl button = (ButtonImpl) SitemapFactory.eINSTANCE.createButton();
button.setPosition(idx);
button.setCmd(cmd);
button.setLabel(label);
button.setIcon(icon);
buttons.add(button);
}
}
}
}
}

private void addWidgetVisibility(EList<VisibilityRule> visibility, UIComponent component) {
if (component.getConfig() != null && component.getConfig().containsKey("visibility")) {
for (Object sourceVisibility : (Collection<?>) component.getConfig().get("visibility")) {
Expand Down

0 comments on commit 584625b

Please sign in to comment.