/
ItemUIRegistryImpl.java
1463 lines (1308 loc) · 57.5 KB
/
ItemUIRegistryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.ui.internal.items;
import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.IllegalFormatException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.measure.Unit;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.registry.RegistryChangeListener;
import org.openhab.core.config.core.ConfigurableService;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemNotUniqueException;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.RegistryHook;
import org.openhab.core.library.items.CallItem;
import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.ContactItem;
import org.openhab.core.library.items.DateTimeItem;
import org.openhab.core.library.items.DimmerItem;
import org.openhab.core.library.items.ImageItem;
import org.openhab.core.library.items.LocationItem;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.items.PlayerItem;
import org.openhab.core.library.items.RollershutterItem;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.NextPreviousType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.PlayPauseType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.model.sitemap.sitemap.ColorArray;
import org.openhab.core.model.sitemap.sitemap.Default;
import org.openhab.core.model.sitemap.sitemap.Group;
import org.openhab.core.model.sitemap.sitemap.IconRule;
import org.openhab.core.model.sitemap.sitemap.LinkableWidget;
import org.openhab.core.model.sitemap.sitemap.Mapping;
import org.openhab.core.model.sitemap.sitemap.Sitemap;
import org.openhab.core.model.sitemap.sitemap.SitemapFactory;
import org.openhab.core.model.sitemap.sitemap.Slider;
import org.openhab.core.model.sitemap.sitemap.Switch;
import org.openhab.core.model.sitemap.sitemap.VisibilityRule;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationHelper;
import org.openhab.core.transform.TransformationService;
import org.openhab.core.types.CommandDescription;
import org.openhab.core.types.State;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateOption;
import org.openhab.core.types.UnDefType;
import org.openhab.core.types.util.UnitUtils;
import org.openhab.core.ui.internal.UIActivator;
import org.openhab.core.ui.items.ItemUIProvider;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class provides a simple way to ask different item providers by a
* single method call, i.e. the consumer does not need to iterate over all
* registered providers as this is done inside this class.
*
* @author Kai Kreuzer - Initial contribution
* @author Chris Jackson - Initial contribution
* @author Stefan Triller - Method to convert a state into something a sitemap entity can understand
* @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType
* @author Laurent Garnier - new method getIconColor
* @author Mark Herwege - new method getFormatPattern(widget), clean pattern
* @author Laurent Garnier - Support added for multiple AND conditions in labelcolor/valuecolor/visibility
* @author Laurent Garnier - new icon parameter based on conditional rules
* @author Danny Baumann - widget label source support
*/
@NonNullByDefault
@Component(immediate = true, configurationPid = "org.openhab.sitemap", //
property = Constants.SERVICE_PID + "=org.openhab.sitemap")
@ConfigurableService(category = "system", label = "Sitemap", description_uri = ItemUIRegistryImpl.CONFIG_URI)
public class ItemUIRegistryImpl implements ItemUIRegistry {
protected static final String CONFIG_URI = "system:sitemap";
/* RegEx to extract and parse a function String <code>'(.*?)\((.*)\):(.*)'</code> */
protected static final Pattern EXTRACT_TRANSFORM_FUNCTION_PATTERN = Pattern.compile("(.*?)\\((.*)\\):(.*)");
/* RegEx to identify format patterns. See java.util.Formatter#formatSpecifier (without the '%' at the very end). */
protected static final String IDENTIFY_FORMAT_PATTERN_PATTERN = "%(?:(unit%)|(?:(?:\\d+\\$)?(?:[-#+ 0,(<]*)?(?:\\d+)?(?:\\.\\d+)?(?:[tT])?(?:[a-zA-Z])))";
private static final Pattern FORMAT_PATTERN = Pattern.compile("(?:^|[^%])" + IDENTIFY_FORMAT_PATTERN_PATTERN);
private static final int MAX_BUTTONS = 4;
private static final String DEFAULT_SORTING = "NONE";
private final Logger logger = LoggerFactory.getLogger(ItemUIRegistryImpl.class);
protected final Set<ItemUIProvider> itemUIProviders = new HashSet<>();
private final ItemRegistry itemRegistry;
private final Map<Widget, Widget> defaultWidgets = Collections.synchronizedMap(new WeakHashMap<>());
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;
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
public void addItemUIProvider(ItemUIProvider itemUIProvider) {
itemUIProviders.add(itemUIProvider);
}
public void removeItemUIProvider(ItemUIProvider itemUIProvider) {
itemUIProviders.remove(itemUIProvider);
}
@Activate
protected void activate(Map<String, Object> config) {
applyConfig(config);
}
@Modified
protected void modified(@Nullable Map<String, Object> config) {
applyConfig(config);
}
/**
* Handle the initial or a changed configuration.
*
* @param config the configuration
*/
private void applyConfig(@Nullable Map<String, Object> config) {
if (config != null) {
final String groupMembersSortingString = Objects.toString(config.get("groupMembersSorting"), null);
if (groupMembersSortingString != null) {
groupMembersSorting = groupMembersSortingString;
}
}
}
@Override
public @Nullable String getCategory(String itemName) {
for (ItemUIProvider provider : itemUIProviders) {
String currentCategory = provider.getCategory(itemName);
if (currentCategory != null) {
return currentCategory;
}
}
// use the category, if defined
String category = getItemCategory(itemName);
if (category != null) {
return category.toLowerCase();
}
// do some reasonable default
// try to get the item type from the item name
Class<? extends Item> itemType = getItemType(itemName);
if (itemType == null) {
return null;
}
// we handle items here that have no specific widget,
// e.g. the default widget of a rollerblind is "Switch".
// We want to provide a dedicated default category for it
// like "rollerblind".
if (NumberItem.class.equals(itemType) || ContactItem.class.equals(itemType)
|| RollershutterItem.class.equals(itemType)) {
return itemType.getSimpleName().replace("Item", "").toLowerCase();
}
return null;
}
@Override
public @Nullable String getLabel(String itemName) {
for (ItemUIProvider provider : itemUIProviders) {
String currentLabel = provider.getLabel(itemName);
if (currentLabel != null) {
return currentLabel;
}
}
try {
Item item = getItem(itemName);
if (item.getLabel() != null) {
return item.getLabel();
}
} catch (ItemNotFoundException ignored) {
}
return null;
}
@Override
public @Nullable Widget getWidget(String itemName) {
for (ItemUIProvider provider : itemUIProviders) {
Widget currentWidget = provider.getWidget(itemName);
if (currentWidget != null) {
return resolveDefault(currentWidget);
}
}
return null;
}
@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;
}
}
// do some reasonable default, if no provider had an answer
// if the itemType is not defined, try to get it from the item name
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);
slider.setReleaseOnly(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);
int commandOptionsSize = getCommandOptionsSize(itemName);
if (!isReadOnly && commandOptionsSize > 0) {
return commandOptionsSize <= MAX_BUTTONS ? SitemapFactory.eINSTANCE.createSwitch()
: SitemapFactory.eINSTANCE.createSelection();
}
if (!isReadOnly && hasStateOptions(itemName)) {
return SitemapFactory.eINSTANCE.createSelection();
}
if (!isReadOnly && NumberItem.class.isAssignableFrom(itemType) && hasItemTag(itemName, "Setpoint")) {
return SitemapFactory.eINSTANCE.createSetpoint();
} 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;
}
private Switch createPlayerButtons() {
final Switch playerItemSwitch = SitemapFactory.eINSTANCE.createSwitch();
final List<Mapping> mappings = playerItemSwitch.getMappings();
Mapping commandMapping;
mappings.add(commandMapping = SitemapFactory.eINSTANCE.createMapping());
commandMapping.setCmd(NextPreviousType.PREVIOUS.name());
commandMapping.setLabel("<<");
mappings.add(commandMapping = SitemapFactory.eINSTANCE.createMapping());
commandMapping.setCmd(PlayPauseType.PAUSE.name());
commandMapping.setLabel("||");
mappings.add(commandMapping = SitemapFactory.eINSTANCE.createMapping());
commandMapping.setCmd(PlayPauseType.PLAY.name());
commandMapping.setLabel(">");
mappings.add(commandMapping = SitemapFactory.eINSTANCE.createMapping());
commandMapping.setCmd(NextPreviousType.NEXT.name());
commandMapping.setLabel(">>");
return playerItemSwitch;
}
@Override
public @Nullable String getLabel(Widget w) {
String label = getLabelFromWidget(w).label;
String itemName = w.getItem();
if (itemName == null || itemName.isBlank()) {
return transform(label, true, null);
}
String labelMappedOption = null;
State state = null;
StateDescription stateDescription = null;
String formatPattern = getFormatPattern(w);
if (formatPattern != null && label.indexOf("[") < 0) {
label = label + " [" + formatPattern + "]";
}
// now insert the value, if the state is a string or decimal value and there is some formatting pattern defined
// in the label or state description (i.e. it contains at least a %)
try {
final Item item = getItem(itemName);
// There is a known issue in the implementation of the method getStateDescription() of class Item
// in the following case:
// - the item provider returns as expected a state description without pattern but with for
// example a min value because a min value is set in the item definition but no label with
// pattern is set.
// - the channel state description provider returns as expected a state description with a pattern
// In this case, the result is no display of value by UIs because no pattern is set in the
// returned StateDescription. What is expected is the display of a value using the pattern
// provided by the channel state description provider.
stateDescription = item.getStateDescription();
if (formatPattern != null) {
state = item.getState();
if (formatPattern.contains("%d")) {
if (!(state instanceof Number)) {
// States which do not provide a Number will be converted to DecimalType.
// e.g.: GroupItem can provide a count of items matching the active state
// for some group functions.
state = item.getStateAs(DecimalType.class);
}
// for fraction digits in state we don't want to risk format exceptions,
// so treat everything as floats:
formatPattern = formatPattern.replace("%d", "%.0f");
}
}
} catch (ItemNotFoundException e) {
logger.warn("Cannot retrieve item '{}' for widget {}", itemName, w.eClass().getInstanceTypeName());
}
boolean considerTransform = false;
if (formatPattern != null) {
if (formatPattern.isEmpty()) {
label = label.substring(0, label.indexOf("[")).trim();
} else {
if (state == null || state instanceof UnDefType) {
formatPattern = formatUndefined(formatPattern);
considerTransform = true;
} else {
// if the channel contains options, we build a label with the mapped option value
if (stateDescription != null) {
for (StateOption option : stateDescription.getOptions()) {
String optionLabel = option.getLabel();
if (option.getValue().equals(state.toString()) && optionLabel != null) {
String formatPatternOption;
try {
formatPatternOption = String.format(formatPattern, optionLabel);
} catch (IllegalFormatException e) {
logger.debug(
"Mapping option value '{}' for item {} using format '{}' failed ({}); format is ignored and option label is used",
optionLabel, itemName, formatPattern, e.getMessage());
formatPatternOption = optionLabel;
}
labelMappedOption = label.trim();
labelMappedOption = labelMappedOption.substring(0, labelMappedOption.indexOf("[") + 1)
+ formatPatternOption + "]";
break;
}
}
}
if (state instanceof DecimalType) {
// for DecimalTypes we don't want to risk format exceptions, if pattern contains unit
// placeholder
if (formatPattern.contains(UnitUtils.UNIT_PLACEHOLDER)) {
formatPattern = formatPattern.replaceAll(UnitUtils.UNIT_PLACEHOLDER, "").stripTrailing();
}
} else if (state instanceof QuantityType quantityState) {
// sanity convert current state to the item state description unit in case it was updated in the
// meantime. The item state is still in the "original" unit while the state description will
// display the new unit:
Unit<?> patternUnit = UnitUtils.parseUnit(formatPattern);
if (patternUnit != null && !quantityState.getUnit().equals(patternUnit)) {
quantityState = quantityState.toInvertibleUnit(patternUnit);
}
// The widget may define its own unit in the widget label. Convert to this unit:
if (quantityState != null) {
quantityState = convertStateToWidgetUnit(quantityState, w);
state = quantityState;
}
} else if (state instanceof DateTimeType type) {
// Translate a DateTimeType state to the local time zone
try {
state = type.toLocaleZone();
} catch (DateTimeException ignored) {
}
}
// The following exception handling has been added to work around a Java bug with formatting
// numbers. See http://bugs.sun.com/view_bug.do?bug_id=6476425
// Without this catch, the whole sitemap, or page can not be displayed!
// This also handles IllegalFormatConversionException, which is a subclass of IllegalArgument.
try {
Matcher matcher = EXTRACT_TRANSFORM_FUNCTION_PATTERN.matcher(formatPattern);
if (matcher.find()) {
considerTransform = true;
String type = matcher.group(1);
String pattern = matcher.group(2);
String value = matcher.group(3);
formatPattern = type + "(" + pattern + "):" + state.format(value);
} else {
formatPattern = state.format(formatPattern);
}
} catch (IllegalArgumentException e) {
logger.warn("Exception while formatting value '{}' of item {} with format '{}': {}", state,
itemName, formatPattern, e.getMessage());
formatPattern = "Err";
}
}
label = label.trim();
int index = label.indexOf("[");
if (index >= 0) {
label = label.substring(0, index + 1) + formatPattern + "]";
}
}
}
return transform(label, considerTransform, labelMappedOption);
}
@Override
public WidgetLabelSource getLabelSource(Widget w) {
return getLabelFromWidget(w).source;
}
private QuantityType<?> convertStateToWidgetUnit(QuantityType<?> quantityState, Widget w) {
Unit<?> widgetUnit = UnitUtils.parseUnit(getFormatPattern(w));
if (widgetUnit != null && !widgetUnit.equals(quantityState.getUnit())) {
return Objects.requireNonNullElse(quantityState.toInvertibleUnit(widgetUnit), quantityState);
}
return quantityState;
}
@Override
public @Nullable String getFormatPattern(Widget w) {
String label = getLabelFromWidget(w).label;
String pattern = getFormatPattern(label);
String itemName = w.getItem();
try {
Item item = null;
if (itemName != null && !itemName.isBlank()) {
item = getItem(itemName);
}
if (item != null && pattern == null) {
StateDescription stateDescription = item.getStateDescription();
if (stateDescription != null) {
pattern = stateDescription.getPattern();
}
}
if (pattern == null) {
return null;
}
// remove last part of pattern, after unit, if it exists, as this is not valid and creates problems with
// updates
if (item instanceof NumberItem numberItem && numberItem.getDimension() != null) {
Matcher m = FORMAT_PATTERN.matcher(pattern);
int matcherEnd = 0;
if (m.find() && m.group(1) == null) {
matcherEnd = m.end();
}
String unit = pattern.substring(matcherEnd).trim();
String postfix = "";
int unitEnd = unit.indexOf(" ");
if (unitEnd > -1) {
postfix = unit.substring(unitEnd + 1).trim();
unit = unit.substring(0, unitEnd);
}
if (!postfix.isBlank()) {
logger.warn(
"Item '{}' with unit, nothing allowed after unit in label pattern '{}', dropping postfix",
itemName, pattern);
}
pattern = unit.isBlank() ? pattern.substring(0, matcherEnd)
: pattern.substring(0, pattern.indexOf(unit, matcherEnd) + unit.length());
}
} catch (ItemNotFoundException e) {
logger.warn("Cannot retrieve item '{}' for widget {}", itemName, w.eClass().getInstanceTypeName());
}
return pattern;
}
private @Nullable String getFormatPattern(@Nullable String label) {
if (label == null) {
return null;
}
String pattern = label.trim();
int indexOpenBracket = pattern.indexOf("[");
int indexCloseBracket = pattern.endsWith("]") ? pattern.length() - 1 : -1;
if ((indexOpenBracket >= 0) && (indexCloseBracket > indexOpenBracket)) {
return pattern.substring(indexOpenBracket + 1, indexCloseBracket);
} else {
return null;
}
}
private WidgetLabelWithSource getLabelFromWidget(Widget w) {
String label = null;
WidgetLabelSource source = WidgetLabelSource.NONE;
if (w.getLabel() != null) {
// if there is a label defined for the widget, use this
label = w.getLabel();
source = WidgetLabelSource.SITEMAP_WIDGET;
} else {
String itemName = w.getItem();
if (itemName != null) {
// check if any item ui provider provides a label for this item
label = getLabel(itemName);
// if there is no item ui provider saying anything, simply use the name as a label
if (label != null) {
source = WidgetLabelSource.ITEM_LABEL;
} else {
label = itemName;
source = WidgetLabelSource.ITEM_NAME;
}
}
}
// use an empty string, if no label could be found
return new WidgetLabelWithSource(label != null ? label : "", source);
}
/**
* Takes the given <code>formatPattern</code> and replaces it with an analog
* String-based pattern to replace all value Occurrences with a dash ("-")
*
* @param formatPattern the original pattern which will be replaces by a
* String pattern.
* @return a formatted String with dashes ("-") as value replacement
*/
protected String formatUndefined(String formatPattern) {
String undefinedFormatPattern = formatPattern.replaceAll(IDENTIFY_FORMAT_PATTERN_PATTERN, "%1\\$s");
try {
return String.format(undefinedFormatPattern, "-");
} catch (Exception e) {
logger.warn(
"Exception while formatting undefined value [sourcePattern={}, targetPattern={}, exceptionMessage={}]",
formatPattern, undefinedFormatPattern, e.getMessage());
return "Err";
}
}
private String insertInLabel(String label, Object o) {
return label.substring(0, label.indexOf("[") + 1) + o + "]";
}
/*
* check if there is a status value being displayed on the right side of the
* label (the right side is signified by being enclosed in square brackets [].
* If so, check if the value starts with the call to a transformation service
* (e.g. "[MAP(en.map):%s]") and execute the transformation in this case.
* 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 String transform(String label, boolean matchTransform, @Nullable String labelMappedOption) {
String ret = label;
String formatPattern = getFormatPattern(label);
if (formatPattern != null) {
Matcher matcher = EXTRACT_TRANSFORM_FUNCTION_PATTERN.matcher(formatPattern);
if (matchTransform && matcher.find()) {
String type = matcher.group(1);
String pattern = matcher.group(2);
String value = matcher.group(3);
TransformationService transformation = TransformationHelper
.getTransformationService(UIActivator.getContext(), type);
if (transformation != null) {
try {
String transformationResult = transformation.transform(pattern, value);
if (transformationResult != null) {
ret = insertInLabel(label, transformationResult);
} else {
logger.warn("transformation of type {} did not return a valid result", type);
ret = insertInLabel(label, UnDefType.NULL);
}
} catch (TransformationException e) {
logger.error("transformation throws exception [transformation={}, value={}]", transformation,
value, e);
ret = insertInLabel(label, value);
}
} else {
logger.warn(
"couldn't transform value in label because transformationService of type '{}' is unavailable",
type);
ret = insertInLabel(label, value);
}
} else if (labelMappedOption != null) {
ret = labelMappedOption;
}
}
return ret;
}
@Override
public @Nullable String getCategory(Widget w) {
String widgetTypeName = w.eClass().getInstanceTypeName()
.substring(w.eClass().getInstanceTypeName().lastIndexOf(".") + 1);
// the default is the widget type name, e.g. "switch"
String category = widgetTypeName.toLowerCase();
String conditionalIcon = getConditionalIcon(w);
// if an icon is defined for the widget, use it
if (w.getIcon() != null) {
category = w.getIcon();
} else if (w.getStaticIcon() != null) {
category = w.getStaticIcon();
} else if (conditionalIcon != null) {
category = conditionalIcon;
} else {
// otherwise check if any item ui provider provides an icon for this item
String itemName = w.getItem();
if (itemName != null) {
String result = getCategory(itemName);
if (result != null) {
category = result;
}
}
}
return category;
}
@Override
public @Nullable State getState(Widget w) {
String itemName = w.getItem();
if (itemName != null) {
try {
Item item = getItem(itemName);
return convertState(w, item, item.getState());
} catch (ItemNotFoundException e) {
logger.warn("Cannot retrieve item '{}' for widget {}", itemName, w.eClass().getInstanceTypeName());
}
}
return UnDefType.UNDEF;
}
/**
* Converts an item state to the type the widget supports (if possible)
*
* @param w Widget in sitemap that shows the state
* @param i item
* @param state the state
* @return the converted state or the original if conversion was not possible
*/
@Override
public @Nullable State convertState(Widget w, Item i, State state) {
State returnState = null;
State itemState = i.getState();
if (itemState instanceof QuantityType<?> quantityTypeState) {
itemState = convertStateToWidgetUnit(quantityTypeState, w);
}
if (w instanceof Switch && i instanceof RollershutterItem) {
// RollerShutter are represented as Switch in a Sitemap but need a PercentType state
returnState = itemState.as(PercentType.class);
} else if (w instanceof Slider) {
if (i.getAcceptedDataTypes().contains(PercentType.class)) {
returnState = itemState.as(PercentType.class);
} else if (!(itemState instanceof QuantityType<?>)) {
returnState = itemState.as(DecimalType.class);
}
} else if (w instanceof Switch sw) {
StateDescription stateDescr = i.getStateDescription();
if (sw.getMappings().isEmpty() && (stateDescr == null || stateDescr.getOptions().isEmpty())) {
returnState = itemState.as(OnOffType.class);
}
}
// we return the original state to not break anything
return Objects.requireNonNullElse(returnState, itemState);
}
@Override
public @Nullable Widget getWidget(Sitemap sitemap, String id) {
if (!id.isEmpty()) {
// see if the id is an itemName and try to get the widget for it
Widget w = getWidget(id);
if (w == null) {
// try to get the default widget instead
w = getDefaultWidget(null, id);
}
if (w != null) {
w.setItem(id);
} else {
try {
int widgetID = Integer.parseInt(id.substring(0, 2));
if (widgetID < sitemap.getChildren().size()) {
w = sitemap.getChildren().get(widgetID);
for (int i = 2; i < id.length(); i += 2) {
int childWidgetID = Integer.parseInt(id.substring(i, i + 2));
if (childWidgetID < ((LinkableWidget) w).getChildren().size()) {
w = ((LinkableWidget) w).getChildren().get(childWidgetID);
}
}
}
} catch (NumberFormatException e) {
// no valid number, so the requested page id does not exist
}
}
return resolveDefault(w);
}
logger.warn("Cannot find page for id '{}'.", id);
return null;
}
@Override
public EList<Widget> getChildren(Sitemap sitemap) {
EList<Widget> widgets = sitemap.getChildren();
EList<Widget> result = new BasicEList<>();
widgets.stream().map(this::resolveDefault).filter(Objects::nonNull).map(Objects::requireNonNull)
.forEach(result::add);
return result;
}
@Override
public EList<Widget> getChildren(LinkableWidget w) {
EList<Widget> widgets;
if (w instanceof Group group && w.getChildren().isEmpty()) {
widgets = getDynamicGroupChildren(group);
} else {
widgets = w.getChildren();
}
EList<Widget> result = new BasicEList<>();
widgets.stream().map(this::resolveDefault).filter(Objects::nonNull).map(Objects::requireNonNull)
.forEach(result::add);
return result;
}
@Override
public @Nullable EObject getParent(Widget w) {
Widget w2 = defaultWidgets.get(w);
return (w2 == null) ? w.eContainer() : w2.eContainer();
}
private @Nullable Widget resolveDefault(@Nullable Widget widget) {
if (!(widget instanceof Default)) {
return widget;
} else {
String itemName = widget.getItem();
if (itemName != null) {
Item item = get(itemName);
if (item != null) {
Widget defaultWidget = getDefaultWidget(item.getClass(), item.getName());
if (defaultWidget != null) {
copyProperties(widget, defaultWidget);
defaultWidgets.put(defaultWidget, widget);
return defaultWidget;
}
}
}
return null;
}
}
private void copyProperties(Widget source, Widget target) {
target.setItem(source.getItem());
target.setIcon(source.getIcon());
target.setStaticIcon(source.getStaticIcon());
target.setLabel(source.getLabel());
target.getVisibility().addAll(EcoreUtil.copyAll(source.getVisibility()));
target.getLabelColor().addAll(EcoreUtil.copyAll(source.getLabelColor()));
target.getValueColor().addAll(EcoreUtil.copyAll(source.getValueColor()));
target.getIconColor().addAll(EcoreUtil.copyAll(source.getIconColor()));
target.getIconRules().addAll(EcoreUtil.copyAll(source.getIconRules()));
}
/**
* This method creates a list of children for a group dynamically.
* If there are no explicit children defined in a sitemap, the children
* can thus be created on the fly by iterating over the members of the group item.
*
* @param group The group widget to get children for
* @return a list of default widgets provided for the member items
*/
private EList<Widget> getDynamicGroupChildren(Group group) {
EList<Widget> children = new BasicEList<>();
String itemName = group.getItem();
try {
if (itemName != null) {
Item item = getItem(itemName);
if (item instanceof GroupItem groupItem) {
List<Item> members = new ArrayList<>(groupItem.getMembers());
switch (groupMembersSorting) {
case "LABEL":
members.sort((u1, u2) -> {
String u1Label = u1.getLabel();
String u2Label = u2.getLabel();
if (u1Label != null && u2Label != null) {
return u1Label.compareTo(u2Label);
} else {
return u1.getName().compareTo(u2.getName());
}
});
break;
case "NAME":
members.sort(Comparator.comparing(Item::getName));
break;
default:
break;
}
for (Item member : members) {
Widget widget = getDefaultWidget(member.getClass(), member.getName());
if (widget != null) {
widget.setItem(member.getName());
children.add(widget);
}
}
} else {
logger.warn("Item '{}' is not a group.", item.getName());
}
} else {
logger.warn("Dynamic group with label '{}' does not specify an associated item - ignoring it.",
group.getLabel());
}
} catch (ItemNotFoundException e) {
logger.warn("Dynamic group with label '{}' will be ignored, because its item '{}' does not exist.",
group.getLabel(), itemName);
}
return children;
}
private boolean isReadOnly(String itemName) {
try {
Item item = getItem(itemName);
StateDescription stateDescription = item.getStateDescription();
return stateDescription != null && stateDescription.isReadOnly();
} catch (ItemNotFoundException e) {
return false;
}
}
private boolean hasStateOptions(String itemName) {
try {
Item item = getItem(itemName);
StateDescription stateDescription = item.getStateDescription();
return stateDescription != null && !stateDescription.getOptions().isEmpty();
} catch (ItemNotFoundException e) {
return false;
}
}
private int getCommandOptionsSize(String itemName) {
try {
Item item = getItem(itemName);
CommandDescription commandDescription = item.getCommandDescription();
return commandDescription != null ? commandDescription.getCommandOptions().size() : 0;
} catch (ItemNotFoundException e) {
return 0;
}
}
private @Nullable Class<? extends Item> getItemType(String itemName) {
try {
Item item = getItem(itemName);
return item.getClass();
} catch (ItemNotFoundException e) {
return null;
}
}
@Override
public @Nullable State getItemState(String itemName) {
try {
Item item = getItem(itemName);
return item.getState();
} catch (ItemNotFoundException e) {
return null;
}
}
public @Nullable String getItemCategory(String itemName) {
try {
Item item = getItem(itemName);
return item.getCategory();
} catch (ItemNotFoundException e) {
return null;
}
}
private boolean hasItemTag(String itemName, String tag) {
try {
Item item = getItem(itemName);
return item.hasTag(tag);
} catch (ItemNotFoundException e) {
return false;
}
}
@Override
public Item getItem(String name) throws ItemNotFoundException {
return itemRegistry.getItem(name);
}
@Override
public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
return itemRegistry.getItemByPattern(name);
}
@Override
public Collection<Item> getItems() {
return itemRegistry.getItems();
}
@Override
public Collection<Item> getItemsOfType(String type) {
return itemRegistry.getItemsOfType(type);
}
@Override
public Collection<Item> getItems(String pattern) {
return itemRegistry.getItems(pattern);
}
@Override
public void addRegistryChangeListener(RegistryChangeListener<Item> listener) {
itemRegistry.addRegistryChangeListener(listener);
}
@Override
public void removeRegistryChangeListener(RegistryChangeListener<Item> listener) {
itemRegistry.removeRegistryChangeListener(listener);
}
@Override
public Collection<Item> getAll() {
return itemRegistry.getAll();
}
@Override
public Stream<Item> stream() {