Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ui] Switch widget as default widget for Number or String items with command options #1422

Merged
merged 5 commits into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ public class ItemUIRegistryImpl implements ItemUIRegistry {
protected static final String IDENTIFY_FORMAT_PATTERN_PATTERN = "%((unit%)|((\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z])))";

private static final Pattern LABEL_PATTERN = Pattern.compile(".*?\\[.*? (.*?)\\]");
private static final int MAX_BUTTONS = 4;

protected final Set<ItemUIProvider> itemUIProviders = new HashSet<>();

private @Nullable ItemRegistry itemRegistry;

private @Nullable ItemBuilderFactory itemBuilderFactory;

private final Map<Widget, Widget> defaultWidgets = Collections.synchronizedMap(new WeakHashMap<>());
Expand All @@ -140,15 +140,6 @@ public void unsetItemRegistry(ItemRegistry itemRegistry) {
this.itemRegistry = null;
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
public void addItemUIProvider(ItemUIProvider itemUIProvider) {
itemUIProviders.add(itemUIProvider);
}

public void removeItemUIProvider(ItemUIProvider itemUIProvider) {
itemUIProviders.remove(itemUIProvider);
}

@Reference(policy = ReferencePolicy.DYNAMIC)
protected void setItemBuilderFactory(ItemBuilderFactory itemBuilderFactory) {
this.itemBuilderFactory = itemBuilderFactory;
Expand All @@ -158,6 +149,15 @@ protected void unsetItemBuilderFactory(ItemBuilderFactory itemBuilderFactory) {
this.itemBuilderFactory = null;
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
public void addItemUIProvider(ItemUIProvider itemUIProvider) {
itemUIProviders.add(itemUIProvider);
}

public void removeItemUIProvider(ItemUIProvider itemUIProvider) {
itemUIProviders.remove(itemUIProvider);
}

@Override
public @Nullable String getCategory(String itemName) {
for (ItemUIProvider provider : itemUIProviders) {
Expand Down Expand Up @@ -239,60 +239,50 @@ protected void unsetItemBuilderFactory(ItemBuilderFactory itemBuilderFactory) {
if (itemType == null) {
return null;
}
boolean readOnly = isReadOnly(itemName);
int nbOptions = getNbOptions(itemName);

if (SwitchItem.class.equals(itemType)) {
return SitemapFactory.eINSTANCE.createSwitch();
}
if (GroupItem.class.equals(itemType)) {
return SitemapFactory.eINSTANCE.createGroup();
}
if (NumberItem.class.isAssignableFrom(itemType)) {
return (!readOnly && nbOptions > 0) ? SitemapFactory.eINSTANCE.createSelection()
: SitemapFactory.eINSTANCE.createText();
}
if (ContactItem.class.equals(itemType)) {
return SitemapFactory.eINSTANCE.createText();
}
if (DateTimeItem.class.equals(itemType)) {
return SitemapFactory.eINSTANCE.createText();
}
if (RollershutterItem.class.equals(itemType)) {
return SitemapFactory.eINSTANCE.createSwitch();
}
if (StringItem.class.equals(itemType)) {
return (!readOnly && nbOptions > 0) ? SitemapFactory.eINSTANCE.createSelection()
: SitemapFactory.eINSTANCE.createText();
}
if (LocationItem.class.equals(itemType)) {
return SitemapFactory.eINSTANCE.createMapview();
}
if (CallItem.class.equals(itemType)) {
} else if (CallItem.class.equals(itemType) //
|| ContactItem.class.equals(itemType) //
|| DateTimeItem.class.equals(itemType)) {
return SitemapFactory.eINSTANCE.createText();
}
if (DimmerItem.class.equals(itemType)) {
} 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;
}
if (ColorItem.class.equals(itemType)) {
return SitemapFactory.eINSTANCE.createColorpicker();
}
if (PlayerItem.class.equals(itemType)) {
return createPlayerButtons();
}
if (ImageItem.class.equals(itemType)) {
} 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;
}

private Switch createPlayerButtons() {
Switch playerItemSwitch = SitemapFactory.eINSTANCE.createSwitch();
List<Mapping> mappings = playerItemSwitch.getMappings();
Mapping commandMapping = null;
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("<<");
Expand Down Expand Up @@ -720,7 +710,7 @@ public EList<Widget> getChildren(LinkableWidget w) {
} else {
String itemName = widget.getItem();
if (itemName != null) {
Item item = itemRegistry.get(itemName);
Item item = get(itemName);
if (item != null) {
Widget defaultWidget = getDefaultWidget(item.getClass(), item.getName());
if (defaultWidget != null) {
Expand Down Expand Up @@ -781,34 +771,37 @@ private EList<Widget> getDynamicGroupChildren(Group group) {

private boolean isReadOnly(String itemName) {
try {
Item item = itemRegistry.getItem(itemName);
Item item = getItem(itemName);
StateDescription stateDescription = item.getStateDescription();
return stateDescription != null ? stateDescription.isReadOnly() : false;
} catch (ItemNotFoundException e) {
return false;
}
return false;
}

private int getNbOptions(String itemName) {
private boolean hasStateOptions(String itemName) {
try {
Item item = itemRegistry.getItem(itemName);
CommandDescription commandDescription = item.getCommandDescription();
int nbCommandOptions = (commandDescription != null && commandDescription.getCommandOptions() != null)
? commandDescription.getCommandOptions().size()
: 0;
Item item = getItem(itemName);
StateDescription stateDescription = item.getStateDescription();
int nbStateOptions = (stateDescription != null && stateDescription.getOptions() != null)
? stateDescription.getOptions().size()
: 0;
return nbCommandOptions > nbStateOptions ? nbCommandOptions : nbStateOptions;
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;
}
return 0;
}

private @Nullable Class<? extends Item> getItemType(String itemName) {
try {
Item item = itemRegistry.getItem(itemName);
Item item = getItem(itemName);
return item.getClass();
} catch (ItemNotFoundException e) {
return null;
Expand All @@ -818,7 +811,7 @@ private int getNbOptions(String itemName) {
@Override
public @Nullable State getItemState(String itemName) {
try {
Item item = itemRegistry.getItem(itemName);
Item item = getItem(itemName);
return item.getState();
} catch (ItemNotFoundException e) {
return null;
Expand All @@ -827,7 +820,7 @@ private int getNbOptions(String itemName) {

public @Nullable String getItemCategory(String itemName) {
try {
Item item = itemRegistry.getItem(itemName);
Item item = getItem(itemName);
return item.getCategory();
} catch (ItemNotFoundException e) {
return null;
Expand Down Expand Up @@ -1368,20 +1361,17 @@ public void removeRegistryHook(RegistryHook<Item> hook) {
if (labelUnit != null && !state.getUnit().toString().equals(labelUnit)) {
return state.toUnit(labelUnit);
}

return state;
}

private @Nullable String getUnitFromLabel(String label) {
if (label.isBlank()) {
return null;
}

Matcher m = LABEL_PATTERN.matcher(label);
if (m.matches()) {
return m.group(1);
}

return null;
}
}
Loading